| 1 | package WWW::LetsEncrypt::JWS; | ||||||||||
| 2 | 9 | $WWW::LetsEncrypt::JWS::VERSION = '0.001'; | |||||||||
| 3 | 9 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 9 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 9 | ▶ | use Encode qw(encode_utf8); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 9 | ▶ | use MIME::Base64 qw(encode_base64url); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | 9 | ▶ | use JSON; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 9 | ▶ | use Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | |||||||||||
| 12 | has 'payload' => ( | ||||||||||
| 13 | is => 'rw', | ||||||||||
| 14 | isa => 'HashRef', | ||||||||||
| 15 | 0 | ▶ | default => sub { return {}; }, | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 16 | 9 | ); | |||||||||
| 17 | |||||||||||
| 18 | has 'headers' => ( | ||||||||||
| 19 | is => 'rw', | ||||||||||
| 20 | isa => 'HashRef', | ||||||||||
| 21 | 3 | ▶ | default => sub { return {}; }, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 22 | 9 | ); | |||||||||
| 23 | |||||||||||
| 24 | has 'protected_headers' => ( | ||||||||||
| 25 | is => 'rw', | ||||||||||
| 26 | isa => 'HashRef', | ||||||||||
| 27 | 2 | ▶ | default => sub { return {}; }, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 28 | 9 | ); | |||||||||
| 29 | |||||||||||
| 30 | 9 | has 'jwk' => ( | |||||||||
| 31 | is => 'rw', | ||||||||||
| 32 | isa => 'Object', | ||||||||||
| 33 | required => 1, | ||||||||||
| 34 | ); | ||||||||||
| 35 | |||||||||||
| 36 | sub serialize { | ||||||||||
| 37 | 3 | ▶ | my ($self) = @_; | ||||||||
|
Subroutine
serialize: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 38 | |||||||||||
| 39 | 3 | my $protected_headers_ref = $self->protected_headers; | |||||||||
| 40 | 3 | $protected_headers_ref->{alg} = $self->jwk->alg; | |||||||||
| 41 | 3 | $protected_headers_ref->{jwk} = $self->jwk->serialize_public_key(); | |||||||||
| 42 | |||||||||||
| 43 | 3 | my $serialized_p_header = _serialize_hash($protected_headers_ref); | |||||||||
| 44 | 3 | my $serialized_payload = _serialize_hash($self->payload); | |||||||||
| 45 | 3 | my $protected_data = "$serialized_p_header.$serialized_payload"; | |||||||||
| 46 | 3 | my $signed_data = encode_base64url($self->jwk->sign($protected_data)); | |||||||||
| 47 | |||||||||||
| 48 | 3 | my $output = { | |||||||||
| 49 | payload => $serialized_payload, | ||||||||||
| 50 | protected => $serialized_p_header, | ||||||||||
| 51 | signature => $signed_data, | ||||||||||
| 52 | }; | ||||||||||
| 53 | |||||||||||
| 54 | # Empty headers are not allowed as per the RFC, not sure why this is a thing but it is. | ||||||||||
| 55 | 3 | ▶ | $output->{header} = $self->headers if %{$self->headers}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 56 | |||||||||||
| 57 | 3 | return encode_json($output); | |||||||||
| 58 | } | ||||||||||
| 59 | |||||||||||
| 60 | sub _serialize_hash { | ||||||||||
| 61 | 6 | ▶ | my ($hash_ref) = @_; | ||||||||
|
Subroutine
_serialize_hash: called CC=1 SCAR=0.0
| |||||||||||
| 62 | 6 | return encode_base64url(encode_utf8(encode_json($hash_ref))); | |||||||||
| 63 | } | ||||||||||
| 64 | |||||||||||
| 65 | 9 | __PACKAGE__->meta->make_immutable; | |||||||||
| 66 | |||||||||||
| 67 | __END__ | ||||||||||
| 68 | |||||||||||
| 69 | =pod | ||||||||||
| 70 | |||||||||||
| 71 | =head1 NAME | ||||||||||
| 72 | |||||||||||
| 73 | WWW::LetsEncrypt::JWS | ||||||||||
| 74 | |||||||||||
| 75 | =head1 SYNOPSIS | ||||||||||
| 76 | |||||||||||
| 77 | use WWW::LetsEncrypt::JWS; | ||||||||||
| 78 | use WWW::LetsEncrypt::JWK; | ||||||||||
| 79 | |||||||||||
| 80 | my $JWK = WWW::LetsEncrypt::JWK->new(...); | ||||||||||
| 81 | |||||||||||
| 82 | my $JWS = WWW::LetsEncrypt::JWS->new({jwk => $JWK}); | ||||||||||
| 83 | |||||||||||
| 84 | $JWS->payload({ | ||||||||||
| 85 | item1 => 'a', | ||||||||||
| 86 | }); | ||||||||||
| 87 | |||||||||||
| 88 | IOFunction($JWS->serialize()); | ||||||||||
| 89 | |||||||||||
| 90 | =head1 DESCRIPTION | ||||||||||
| 91 | |||||||||||
| 92 | A (mostly) implemented JSON Web Signature object as per the RFC specification. | ||||||||||
| 93 | This object's implementation follows the JWS RFC enough to meet the minimum | ||||||||||
| 94 | requirements for ACME, and deviates as ACME may require. | ||||||||||
| 95 | |||||||||||
| 96 | =head2 Attributes | ||||||||||
| 97 | |||||||||||
| 98 | 'payload' a hashref attribute used to store the json payload that should be | ||||||||||
| 99 | communicated to the server. | ||||||||||
| 100 | |||||||||||
| 101 | 'headers' a hashref attribute used to store any headers that should be included | ||||||||||
| 102 | in the JWS. | ||||||||||
| 103 | |||||||||||
| 104 | 'protected_headers' a hashref attribute similar to headers, but are integrity | ||||||||||
| 105 | checked, should contain things like the nonce. | ||||||||||
| 106 | |||||||||||
| 107 | 'jwk' a JWK object that will used to create a signature of all integrity | ||||||||||
| 108 | protected items (payload and protected headers). The public key will also be | ||||||||||
| 109 | included in the protected headers of the JWS serialized output. | ||||||||||
| 110 | |||||||||||
| 111 | =head2 Public Functions | ||||||||||
| 112 | |||||||||||
| 113 | =over 4 | ||||||||||
| 114 | |||||||||||
| 115 | =item serialize | ||||||||||
| 116 | |||||||||||
| 117 | Object function that takes all of the set attributes and outputs a | ||||||||||
| 118 | flattened JSON Web Signature. | ||||||||||
| 119 | |||||||||||
| 120 | Input | ||||||||||
| 121 | |||||||||||
| 122 | $self - Object Reference | ||||||||||
| 123 | |||||||||||
| 124 | Output | ||||||||||
| 125 | |||||||||||
| 126 | scalar string that mostly conforms to the RFC | ||||||||||
| 127 | |||||||||||
| 128 | =back | ||||||||||
| 129 | |||||||||||
| 130 | =head2 Private Functions | ||||||||||
| 131 | |||||||||||
| 132 | =over 4 | ||||||||||
| 133 | |||||||||||
| 134 | =item _serialize_hash | ||||||||||
| 135 | |||||||||||
| 136 | Internal helper function that performs the necessary encoding for the payload and protected-headers. | ||||||||||
| 137 | |||||||||||
| 138 | Input | ||||||||||
| 139 | |||||||||||
| 140 | \%hash_ref | ||||||||||
| 141 | |||||||||||
| 142 | Output | ||||||||||
| 143 | |||||||||||
| 144 | scalar string representing the necessary encoding for various JWS elements | ||||||||||
| 145 | |||||||||||
| 146 | =back | ||||||||||
| 147 | |||||||||||
| 148 | =cut | ||||||||||
| 149 | |||||||||||