| 1 | package Net::ACME2::JWTMaker; | ||
| 2 | |||
| 3 | #---------------------------------------------------------------------- | ||
| 4 | # This module exists because of a desire to do these computations | ||
| 5 | # in environments where a compiler may not be available. | ||
| 6 | # (Otherwise, CryptX would be ideal.) | ||
| 7 | #---------------------------------------------------------------------- | ||
| 8 | |||
| 9 | use strict; | ||
| 10 | use warnings; | ||
| 11 | |||
| 12 | use JSON (); | ||
| 13 | use MIME::Base64 (); | ||
| 14 | |||
| 15 | BEGIN { | ||
| 16 | *_encode_b64u = *MIME::Base64::encode_base64url; | ||
| 17 | } | ||
| 18 | |||
| 19 | sub new { | ||
| 20 | my ($class, %opts) = @_; | ||
| 21 | |||
| 22 | die 'need “key”' if !$opts{'key'}; | ||
| 23 | |||
| 24 | $opts{'format'} ||= q<>; | ||
| 25 | |||
| 26 | if ($opts{'format'} && $opts{'format'} ne 'compact') { | ||
| 27 | die "Unknown “format”: “$opts{'format'}”"; | ||
| 28 | } | ||
| 29 | |||
| 30 | return bless \%opts, $class; | ||
| 31 | } | ||
| 32 | |||
| 33 | sub create_full_jws { | ||
| 34 | my ($self, %args) = @_; | ||
| 35 | |||
| 36 | local $args{'extra_headers'}{'jwk'} = $self->{'key'}->get_struct_for_public_jwk(); | ||
| 37 | |||
| 38 | return $self->_create_jwt(%args); | ||
| 39 | } | ||
| 40 | |||
| 41 | sub create_key_id_jws { | ||
| 42 | my ($self, %args) = @_; | ||
| 43 | |||
| 44 | local $args{'extra_headers'}{'kid'} = $args{'key_id'}; | ||
| 45 | |||
| 46 | return $self->_create_jwt(%args); | ||
| 47 | } | ||
| 48 | |||
| 49 | #---------------------------------------------------------------------- | ||
| 50 | |||
| 51 | #expects: | ||
| 52 | # key - object | ||
| 53 | # payload | ||
| 54 | # extra_headers (optional, hashref) | ||
| 55 | sub _create_jwt { | ||
| 56 | my ( $self, %args ) = @_; | ||
| 57 | |||
| 58 | my $alg = $self->_ALG(); | ||
| 59 | my $signer_cr = $self->_get_signer(); | ||
| 60 | |||
| 61 | my $key = $self->{'key'}; | ||
| 62 | |||
| 63 | my $payload = $args{payload}; | ||
| 64 | |||
| 65 | my $header = $args{extra_headers} ? { %{$args{extra_headers}} } : {}; | ||
| 66 | |||
| 67 | # serialize payload | ||
| 68 | $payload = $self->_payload_enc($payload); | ||
| 69 | |||
| 70 | # encode payload | ||
| 71 | my $b64u_payload = _encode_b64u($payload); | ||
| 72 | |||
| 73 | # prepare header | ||
| 74 | $header->{alg} = $alg; | ||
| 75 | |||
| 76 | # encode header | ||
| 77 | my $json_header = $self->_encode_json($header); | ||
| 78 | my $b64u_header = _encode_b64u($json_header); | ||
| 79 | |||
| 80 | my $b64u_signature = _encode_b64u( $signer_cr->("$b64u_header.$b64u_payload", $key) ); | ||
| 81 | |||
| 82 | if ($self->{'format'} && $self->{'format'} eq 'compact') { | ||
| 83 | return join('.', $b64u_header, $b64u_payload, $b64u_signature); | ||
| 84 | } | ||
| 85 | |||
| 86 | return <<"END"; | ||
| 87 | { | ||
| 88 | "protected": "$b64u_header", | ||
| 89 | "payload": "$b64u_payload", | ||
| 90 | "signature": "$b64u_signature" | ||
| 91 | } | ||
| 92 | END | ||
| 93 | } | ||
| 94 | |||
| 95 | sub _encode_json { | ||
| 96 | my ($self, $payload) = @_; | ||
| 97 | |||
| 98 | #Always do a canonical encode so that we can test more easily. | ||
| 99 | #Note that JWS itself does NOT require this. | ||
| 100 | $self->{'_json'} ||= JSON->new()->canonical(1); | ||
| 101 | |||
| 102 | return $self->{'_json'}->encode($payload); | ||
| 103 | } | ||
| 104 | |||
| 105 | #Taken from Crypt::JWT | ||
| 106 | sub _payload_enc { | ||
| 107 | my ($self, $payload) = @_; | ||
| 108 | |||
| 109 | if (ref($payload) eq 'HASH' || ref($payload) eq 'ARRAY') { | ||
| 110 | $payload = $self->_encode_json($payload); | ||
| 111 | } | ||
| 112 | else { | ||
| 113 | utf8::downgrade($payload, 1) or die "JWT: payload cannot contain wide character"; | ||
| 114 | } | ||
| 115 | |||
| 116 | return $payload; | ||
| 117 | } | ||
| 118 | |||
| 119 | 1; |