Akamai-Edgegrid

 view release on metacpan or  search on metacpan

lib/Akamai/Edgegrid.pm  view on Meta::CPAN

64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
    return _pad_digest(hmac_sha256_base64($data, $key));
}
 
sub _padded_sha256_base64 {
    my ($data) = @_;
    return _pad_digest(sha256_base64($data));
}
 
## methods
 
sub _debug {
    my ($self, $msg) = @_;
    if ($self->{debug}) {
        $msg =~ s/\n$//;
        warn "$msg\n";
    }
}
 
sub _make_signing_key {
    my ($self, $timestamp) = @_;
    my $signing_key = _padded_hmac_sha256_base64($timestamp, $self->{client_secret});
    $self->_debug("signing_key: $signing_key");
 
    return $signing_key;
}
 
sub _canonicalize_headers {
    my ($self, $r) = @_;
    return join("\t",
        map {
            my $header_name = lc($_);
            my $header_val = $r->header($_);

lib/Akamai/Edgegrid.pm  view on Meta::CPAN

103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
            defined $r->header($_)
        } @{$self->{headers_to_sign}}
    );
}
 
sub _make_content_hash {
    my ($self, $r) = @_;
    if ($r->method eq 'POST' and length($r->content) > 0) {
        my $body = $r->content;
        if (length($body) > $self->{max_body}) {
            $self->_debug(
                "data length " . length($body) . " is larger than maximum " . $self->{max_body}
            );
 
            $body = substr($body, 0, $self->{max_body});
 
            $self->_debug(
                "data truncated to " . length($body) . " for computing the hash"
            );
        }
        return _padded_sha256_base64($body);
    }
    return "";
}
 
sub _make_data_to_sign {
    my ($self, $r, $auth_header) = @_;

lib/Akamai/Edgegrid.pm  view on Meta::CPAN

132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
        $r->url->scheme,
        $r->url->host,
        $r->url->path_query,
        $self->_canonicalize_headers($r),
        $self->_make_content_hash($r),
        $auth_header
    ));
 
    my $display_to_sign = $data_to_sign;
    $display_to_sign =~ s/\t/\\t/g;
    $self->_debug("data to sign: $display_to_sign");
 
    return $data_to_sign;
}
 
sub _sign_request {
    my ($self, $r, $timestamp, $auth_header) = @_;
 
    return _padded_hmac_sha256_base64(
        $self->_make_data_to_sign($r, $auth_header),
        $self->_make_signing_key($timestamp)

lib/Akamai/Edgegrid.pm  view on Meta::CPAN

159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
        ['client_token' => $self->{client_token}],
        ['access_token' => $self->{access_token}],
        ['timestamp' => $timestamp],
        ['nonce' => $nonce]
    );
    my $auth_header = "EG1-HMAC-SHA256 " . join(';', map {
            my ($k,$v) = @$_;
            "$k=$v";
        } @kvps) . ';';
 
    $self->_debug("unsigned authorization header: $auth_header");
 
    my $signed_auth_header =
        $auth_header . 'signature=' $self->_sign_request($r, $timestamp, $auth_header);
 
    $self->_debug("signed authorization header: $signed_auth_header");
 
    return $signed_auth_header;
}
 
=head1 CONSTRUCTOR METHOD
 
=over 2
 
=item $ua = Akamai::Edgegrid->new( %options )

lib/Akamai/Edgegrid.pm  view on Meta::CPAN

190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
    KEY           SOURCE
    ------------- -----------------------------------------------
    client_token  from "Credentials" section of Manage APIs UI
    client_secret from "Credentials" section of Manage APIs UI
    access_token  from "Authorizations" section of Manage APIs UI
 
The following optional key/value pairs may be provided:
 
    KEY             DESCRIPTION
    --------------- -------------------------------------------------------
    debug           if true enables additional logging
    headers_to_sign listref of header names to sign (in order) (default [])
    max_body        maximum body size for POSTS (default 2048)
 
=cut
 
sub new {
    my $class = shift @_;
    my %args = @_;
 
    my @local_args = qw(config_file section client_token client_secret access_token headers_to_sign max_body debug);
    my @required_args = qw(client_token client_secret access_token);
    my @cred_args = qw(client_token client_secret access_token host);
    my %local = ();
 
    for my $arg (@local_args) {
        $local{$arg} = delete $args{$arg};
    }
 
    my $self = LWP::UserAgent::new($class, %args);



( run in 0.533 second using v1.01-cache-2.11-cpan-e5176c747c2 )