| 1 | package Net::HTTP::Spore::Middleware::Auth::ApiKey; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | { | ||||||||||||||||||||||||||||
| 6 | 1 | $Net::HTTP::Spore::Middleware::Auth::ApiKey::VERSION = '0.01'; | |||||||||||||||||||||||||||
| 7 | } | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | # ABSTRACT: middleware for authentication with apikey | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use MIME::Base64; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use Digest::SHA qw(hmac_sha512_hex); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 1 | extends 'Net::HTTP::Spore::Middleware::Auth'; | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 1 | has key_name => (isa => 'Str', is => 'rw', required => 0); | |||||||||||||||||||||||||||
| 17 | 1 | has api_key => (isa => 'Str', is => 'rw', required => 1); | |||||||||||||||||||||||||||
| 18 | 1 | has api_secret => (isa => 'Str', is => 'rw', required => 1); | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 0 | ▶ | sub call { | ||||||||||||||||||||||||||
|
Subroutine
call: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 21 | 0 | my ($self, $req) = @_; | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 0 | ▶ | return unless $self->should_authenticate($req); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 0 | my ($path, $query_string) = $req->_path; | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 0 | my $keyname = $self->key_name; | |||||||||||||||||||||||||||
| 28 | 0 | ▶ | if (!defined($keyname)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 29 | 0 | $keyname = 'Key'; | |||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 0 | my $nonce = time(); # XXX need $nonce++ and error handling to do more than | |||||||||||||||||||||||||||
| 33 | # one query per second | ||||||||||||||||||||||||||||
| 34 | 0 | my $content = $req->body; | |||||||||||||||||||||||||||
| 35 | 0 | ▶ | if (!defined($content) || length($content) == 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not defined $content or length $content == 0
MC/DC0%0/2
not defined $content or length $content == 0
not defined $content
length $content == 0
Truth table0%0/3
not(A) or B
A= not defined $contentB= length $content == 0
| |||||||||||||||||||||||||||||
| 36 | 0 | $content = ""; | |||||||||||||||||||||||||||
| 37 | } else { | ||||||||||||||||||||||||||||
| 38 | 0 | $content .= "&"; | |||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | 0 | $content .= "nonce=".$nonce; | |||||||||||||||||||||||||||
| 41 | 0 | ▶ | if (defined($query_string)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | 0 | $content .= "&".$query_string; | |||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | 0 | printf STDERR "ApiKey: content = '%s'\n", $content; | |||||||||||||||||||||||||||
| 45 | 0 | $req->body($content); | |||||||||||||||||||||||||||
| 46 | 0 | $req->header('Content-Type' => 'application/x-www-form-urlencoded'); | |||||||||||||||||||||||||||
| 47 | 0 | $req->header($keyname => $self->api_key); | |||||||||||||||||||||||||||
| 48 | 0 | $req->header('Sign' => hmac_sha512_hex($content, $self->api_secret)); | |||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 1 | 1; | |||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | __END__ | ||||||||||||||||||||||||||||