| 1 | package Mojo::UserAgent::Signature::Base; | ||||||||||||||||||||
| 2 | 2 | ▶ | use Mojo::Base -base; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 3 | |||||||||||||||||||||
| 4 | 10 | ▶ | sub apply_signature { | ||||||||||||||||||
|
Subroutine
apply_signature: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 5 | 10 | my ($self, $tx, $args) = @_; | |||||||||||||||||||
| 6 | 10 | ▶ | return $tx if _is_signed($tx); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 7 | 7 | $tx->req->headers->add('X-Mojo-Signature' => _pkg_name $self); | |||||||||||||||||||
| 8 | 7 | $self->sign_tx($tx, $args); | |||||||||||||||||||
| 9 | } | ||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | 2 | ▶ | sub init { | ||||||||||||||||||
|
Subroutine
init: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 12 | 2 | my ($self, $ua) = (shift, shift); | |||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | 2 | $self = $self->new(@_); | |||||||||||||||||||
| 15 | $ua->signature($self)->transactor->add_generator( | ||||||||||||||||||||
| 16 | 3 | ▶ | sign => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.4
| |||||||||||||||||||||
| 17 | 3 | my ($t, $tx) = (shift, shift); | |||||||||||||||||||
| 18 | 3 | ▶ | return $tx if _is_signed($tx); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | # Apply Signature | ||||||||||||||||||||
| 21 | 3 | ▶ | my $args = shift if ref $_[0]; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 22 | 3 | $self->apply_signature($tx, $args); | |||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | # Next Generator | ||||||||||||||||||||
| 25 | 3 | ▶ | if (@_ > 1) { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 26 | 2 | my $cb = $t->generators->{shift()}; | |||||||||||||||||||
| 27 | 2 | $t->$cb($tx, @_); | |||||||||||||||||||
| 28 | } | ||||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | # Body | ||||||||||||||||||||
| 31 | 0 | elsif (@_) { $tx->req->body(shift) } | |||||||||||||||||||
| 32 | |||||||||||||||||||||
| 33 | 3 | return $tx; | |||||||||||||||||||
| 34 | } | ||||||||||||||||||||
| 35 | 2 | ); | |||||||||||||||||||
| 36 | 2 | return $self; | |||||||||||||||||||
| 37 | } | ||||||||||||||||||||
| 38 | |||||||||||||||||||||
| 39 | 2 | ▶ | sub sign_tx { $_[1] } | ||||||||||||||||||
|
Subroutine
sign_tx: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 40 | |||||||||||||||||||||
| 41 | 13 | ▶ | sub _is_signed { shift->req->headers->header('X-Mojo-Signature') } | ||||||||||||||||||
|
Subroutine
_is_signed: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 42 | |||||||||||||||||||||
| 43 | 7 | ▶ | sub _pkg_name ($) { ((split /::/, ref $_[0] || $_[0])[-1]) } | ||||||||||||||||||
|
Subroutine
_pkg_name: called CC=2 SCAR=9.2
Condition33%1/3
ref $_[0] || $_[0]
MC/DC0%0/2
ref $_[0] || $_[0]
ref $_[0]
$_[0]
Truth table33%1/3
A || B
A= ref $_[0]B= $_[0]
| |||||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | package Mojo::UserAgent::Signature::None; | ||||||||||||||||||||
| 46 | 2 | ▶ | use Mojo::Base 'Mojo::UserAgent::Signature::Base'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | 2 | 1; | |||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | =encoding utf8 | ||||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | =head1 NAME | ||||||||||||||||||||
| 53 | |||||||||||||||||||||
| 54 | Mojo::UserAgent::Signature::Base - Signature base class | ||||||||||||||||||||
| 55 | |||||||||||||||||||||
| 56 | =head1 SYNOPSIS | ||||||||||||||||||||
| 57 | |||||||||||||||||||||
| 58 | package Mojo::UserAgent::Signature::SomeService; | ||||||||||||||||||||
| 59 | use Mojo::Base 'Mojo::UserAgent::Signature::Base; | ||||||||||||||||||||
| 60 | |||||||||||||||||||||
| 61 | sub sign_tx { | ||||||||||||||||||||
| 62 | my ($self, $tx, $args) = @_; | ||||||||||||||||||||
| 63 | |||||||||||||||||||||
| 64 | # Magic here! :) | ||||||||||||||||||||
| 65 | } | ||||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | =head1 DESCRIPTION | ||||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | L<Mojo::UserAgent::Signature::Base> is an abstract base class for | ||||||||||||||||||||
| 70 | L<Mojo::UserAgent> signatures. | ||||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | =head1 METHODS | ||||||||||||||||||||
| 73 | |||||||||||||||||||||
| 74 | L<Mojo::UserAgent::Signature::Base> inherits all methods from | ||||||||||||||||||||
| 75 | L<Mojo::UserAgent::Signature> and implements the following new ones. | ||||||||||||||||||||
| 76 | |||||||||||||||||||||
| 77 | =head2 apply_signature | ||||||||||||||||||||
| 78 | |||||||||||||||||||||
| 79 | $signed_tx = $signature->apply_signature($tx, $args); | ||||||||||||||||||||
| 80 | |||||||||||||||||||||
| 81 | Applies the signature produced by L</"sign_tx"> to L<Mojo::UserAgent> | ||||||||||||||||||||
| 82 | transaction. Also adds a header to the transaction, | ||||||||||||||||||||
| 83 | C<X-Mojo-Signature: SomeService>, to indicate that this transaction has been | ||||||||||||||||||||
| 84 | signed -- this prevents the automatic signature handling from applying the | ||||||||||||||||||||
| 85 | signature a second time, after the generator. | ||||||||||||||||||||
| 86 | |||||||||||||||||||||
| 87 | =head2 init | ||||||||||||||||||||
| 88 | |||||||||||||||||||||
| 89 | $signature = $signature->init($ua); | ||||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | Adds a transactor generator named C<sign> to the supplied C<$ua> instance for | ||||||||||||||||||||
| 92 | applying a signature to a transaction. Useful for overriding the signature | ||||||||||||||||||||
| 93 | details in the L</"signature"> instance. | ||||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | Another generator can follow the use of the C<sign> generator. | ||||||||||||||||||||
| 96 | |||||||||||||||||||||
| 97 | $ua->get($url => sign => {%args}); | ||||||||||||||||||||
| 98 | |||||||||||||||||||||
| 99 | =head2 sign_tx | ||||||||||||||||||||
| 100 | |||||||||||||||||||||
| 101 | $signed_tx = $signature->sign_tx($tx, {%args}); | ||||||||||||||||||||
| 102 | |||||||||||||||||||||
| 103 | This method will be called by L</"apply_signature">, either automatically when | ||||||||||||||||||||
| 104 | the L<transaction is built|Mojo::UserAgent/"build_tx">, or explicitly via the | ||||||||||||||||||||
| 105 | transaction L<generator|Mojo::UserAgent::Transactor/"GENERATORS">. | ||||||||||||||||||||
| 106 | Meant to be overloaded in a subclass. | ||||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||
| 109 | |||||||||||||||||||||
| 110 | Copyright (C) 2020, Stefan Adams. | ||||||||||||||||||||
| 111 | |||||||||||||||||||||
| 112 | This program is free software, you can redistribute it and/or modify it under | ||||||||||||||||||||
| 113 | the terms of the Artistic License version 2.0. | ||||||||||||||||||||
| 114 | |||||||||||||||||||||
| 115 | =head1 SEE ALSO | ||||||||||||||||||||
| 116 | |||||||||||||||||||||
| 117 | L<https://github.com/stefanadams/mojo-useragent-role-signature>, L<Mojo::UserAgent>. | ||||||||||||||||||||
| 118 | |||||||||||||||||||||
| 119 | =cut | ||||||||||||||||||||