File Coverage

blib/lib/Mojo/UserAgent/Signature/Base.pm
Criterion Covered Total %
statement 25 26 96.1
branch 8 10 80.0
condition 1 3 33.3
subroutine 8 8 100.0
pod 3 3 100.0
total 45 50 90.0


line stmt bran cond sub pod time code
1             package Mojo::UserAgent::Signature::Base;
2 2     2   521437 use Mojo::Base -base;
  2         15  
  2         13  
3              
4             sub apply_signature {
5 10     10 1 1311 my ($self, $tx, $args) = @_;
6 10 100       28 return $tx if _is_signed($tx);
7 7         129 $tx->req->headers->add('X-Mojo-Signature' => _pkg_name $self);
8 7         143 $self->sign_tx($tx, $args);
9             }
10              
11             sub init {
12 2     2 1 70 my ($self, $ua) = (shift, shift);
13              
14 2         14 $self = $self->new(@_);
15             $ua->signature($self)->transactor->add_generator(
16             sign => sub {
17 3     3   643 my ($t, $tx) = (shift, shift);
18 3 50       9 return $tx if _is_signed($tx);
19              
20             # Apply Signature
21 3 100       56 my $args = shift if ref $_[0];
22 3         11 $self->apply_signature($tx, $args);
23              
24             # Next Generator
25 3 100       124 if (@_ > 1) {
    50          
26 2         9 my $cb = $t->generators->{shift()};
27 2         14 $t->$cb($tx, @_);
28             }
29              
30             # Body
31 0         0 elsif (@_) { $tx->req->body(shift) }
32              
33 3         270 return $tx;
34             }
35 2         29 );
36 2         106 return $self;
37             }
38              
39 2     2 1 31 sub sign_tx { $_[1] }
40              
41 13     13   34 sub _is_signed { shift->req->headers->header('X-Mojo-Signature') }
42              
43 7   33 7   127 sub _pkg_name ($) { ((split /::/, ref $_[0] || $_[0])[-1]) }
44              
45             package Mojo::UserAgent::Signature::None;
46 2     2   1151 use Mojo::Base 'Mojo::UserAgent::Signature::Base';
  2         5  
  2         10  
47              
48             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 is an abstract base class for
70             L signatures.
71              
72             =head1 METHODS
73              
74             L inherits all methods from
75             L 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 to L
82             transaction. Also adds a header to the transaction,
83             C, 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 to the supplied C<$ua> instance for
92             applying a signature to a transaction. Useful for overriding the signature
93             details in the L instance.
94              
95             Another generator can follow the use of the C 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, either automatically when
104             the L, or explicitly via the
105             transaction L.
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, L.
118              
119             =cut