|
| 1 | +=pod |
| 2 | + |
| 3 | +=encoding UTF-8 |
| 4 | + |
| 5 | +=head1 NAME |
| 6 | + |
| 7 | +URI::Signature::Tiny - Mint and verify server-signed URIs |
| 8 | + |
| 9 | +=head1 SYNOPSIS |
| 10 | + |
| 11 | + use URI; |
| 12 | + use URI::Signature::Tiny; |
| 13 | + |
| 14 | + my $notary = URI::Signature::Tiny->new( |
| 15 | + secret => $secret, |
| 16 | + after_sign => sub { |
| 17 | + my ( $uri, $sig ) = @_; |
| 18 | + $uri->query_form({ $uri->query_form, s => $sig }); |
| 19 | + $uri; |
| 20 | + }, |
| 21 | + before_verify => sub { |
| 22 | + my ( $uri ) = @_; |
| 23 | + my %f = $uri->query_form; |
| 24 | + my $sig = delete $f{'s'}; |
| 25 | + $uri = $uri->clone; # important |
| 26 | + $uri->query_form( \%f ); |
| 27 | + ( $uri, ref $sig ? '' : $sig ); |
| 28 | + }, |
| 29 | + ); |
| 30 | + |
| 31 | + my $signed_uri = $notary->sign( URI->new( 'http://example.com/foo?bar=baz#pagetop' ) ); |
| 32 | + |
| 33 | + my $ok = $notary->verify( $signed_uri ); |
| 34 | + |
| 35 | +=head1 DESCRIPTION |
| 36 | + |
| 37 | +This is a minimal helper to generate URLs that you can later verify to not have |
| 38 | +been modified, so that you can trust security-relevant values such as user IDs. |
| 39 | +This is useful e.g. for a passwort reset link that the user should not be able |
| 40 | +to edit to log in as someone else. |
| 41 | + |
| 42 | +=head1 METHODS |
| 43 | + |
| 44 | +=over 2 |
| 45 | + |
| 46 | +=item C<new> |
| 47 | + |
| 48 | +Construct and return an instance of this class. |
| 49 | +Takes a list of key/value pairs specifying configuration options: |
| 50 | + |
| 51 | +=over 2 |
| 52 | + |
| 53 | +=item C<secret> |
| 54 | + |
| 55 | +A message authentication code (MAC) value, |
| 56 | +which needs to have cryptographically sufficient entropy. |
| 57 | + |
| 58 | +B<Required>. |
| 59 | + |
| 60 | +=item C<after_sign> |
| 61 | + |
| 62 | +A callback that defines how to incorporate the signature into a fresh URI. |
| 63 | +See L</C<sign>> for details. |
| 64 | + |
| 65 | +Defaults to a placeholder that croaks. |
| 66 | + |
| 67 | +=item C<before_verify> |
| 68 | + |
| 69 | +A callback that defines how to remove the signature from a signed URI. |
| 70 | +See L</C<verify>> for details. |
| 71 | + |
| 72 | +Defaults to a placeholder that croaks. |
| 73 | + |
| 74 | +=item C<sort_params> |
| 75 | + |
| 76 | +Whether to sort query parameters (if any) before computing the signature. |
| 77 | + |
| 78 | +Defaults to true. |
| 79 | + |
| 80 | +=item C<function> |
| 81 | + |
| 82 | +The function that will be called to compute the signature, |
| 83 | +which should have the same signature as the HMAC functions from L<Digest::SHA>: |
| 84 | +the (normalised) URI and the secret will be its first and second arguments. |
| 85 | + |
| 86 | +Defaults to |
| 87 | +L<C<\&Digest::SHA::hmac_sha256_base64>|Digest::SHA/hmac_sha256_base64>. |
| 88 | + |
| 89 | +You might also use this just to post-process the HMAC value, any way you wish: |
| 90 | + |
| 91 | + sub { substr &Digest::SHA::hmac_sha512224_base64, 0, 10 } |
| 92 | + |
| 93 | +=item C<recode_base64> |
| 94 | + |
| 95 | +Whether to apply substitutions to turn the return value of the L</C<function>> |
| 96 | +from regular C<base64> encoding into C<base64url>. |
| 97 | + |
| 98 | +Defaults to true. |
| 99 | + |
| 100 | +=back |
| 101 | + |
| 102 | +=item C<signature> |
| 103 | + |
| 104 | +Compute and return the signature for the URI |
| 105 | +which is passed as the only argument. |
| 106 | + |
| 107 | +The only way that the URI value might be modified here is |
| 108 | +to sort the query parameters if requested by L</C<sort_params>>. |
| 109 | + |
| 110 | +=item C<sign> |
| 111 | + |
| 112 | +Takes a fresh URI and returns the same URI with the signature added to it. |
| 113 | +Specifically it returns whatever the L</C<after_sign>> callback returns, |
| 114 | +which gets called with the fresh URI and its signature as arguments. |
| 115 | + |
| 116 | +=item C<verify> |
| 117 | + |
| 118 | +Takes a signed URI and checks whether it matches its signature. |
| 119 | +It passes its arguments to the L</C<after_sign>> callback, |
| 120 | +which must return two values: |
| 121 | +the bare URI with the signature stripped off, and the signature. |
| 122 | + |
| 123 | +=back |
| 124 | + |
| 125 | +=head1 SEE ALSO |
| 126 | + |
| 127 | +=over 2 |
| 128 | + |
| 129 | +=item * |
| 130 | + |
| 131 | +L<URL::Signature> |
| 132 | + |
| 133 | +=item * |
| 134 | + |
| 135 | +L<RFCE<nbsp>2104, I<HMAC: Keyed-Hashing for Message Authentication>|https://tools.ietf.org/html/rfc2104> |
| 136 | + |
| 137 | +=item * |
| 138 | + |
| 139 | +L<RFCE<nbsp>4648, I<The Base16, Base32, and Base64 Data Encodings>, section 5., I<Base 64 Encoding with URL and Filename Safe Alphabet>|https://tools.ietf.org/html/rfc4648#section-5> |
| 140 | + |
| 141 | +=back |
| 142 | + |
| 143 | +=cut |
| 144 | + |
| 145 | +vim: et sw=2 ts=2 sts=2 |
0 commit comments