« blib/lib/Lemonldap/NG/Common/Conf/AccessLib.pm
↑ Summary
blib/lib/Lemonldap/NG/Common/Conf/Backends/RDBI.pm »
| 1 | package Lemonldap::NG::Common::JWT; | ||
| 2 | require Exporter; | ||
| 3 | our @ISA = qw(Exporter); | ||
| 4 | our @EXPORT_OK = | ||
| 5 | qw(getAccessTokenSessionId getJWTHeader getJWTPayload getJWTSignature getJWTSignedData) | ||
| 6 | ; # symbols to export on request | ||
| 7 | |||
| 8 | use JSON; | ||
| 9 | use MIME::Base64 qw/encode_base64 decode_base64/; | ||
| 10 | |||
| 11 | our $VERSION = '2.21.0'; | ||
| 12 | |||
| 13 | # Gets the Access Token session ID embedded in a LLNG-emitted JWT | ||
| 14 | sub getAccessTokenSessionId { | ||
| 15 | my ($access_token) = @_; | ||
| 16 | |||
| 17 | # Access Token is a JWT, extract the JTI field | ||
| 18 | # and use it as session ID | ||
| 19 | if ( index( $access_token, '.' ) > 0 ) { | ||
| 20 | my $data = getJWTPayload($access_token); | ||
| 21 | if ( $data and $data->{jti} ) { | ||
| 22 | return $data->{jti}; | ||
| 23 | } | ||
| 24 | else { | ||
| 25 | return; | ||
| 26 | } | ||
| 27 | } | ||
| 28 | |||
| 29 | # Access Token is the session ID directly | ||
| 30 | else { | ||
| 31 | return $access_token; | ||
| 32 | } | ||
| 33 | } | ||
| 34 | |||
| 35 | # Isolate and decode parts of a JWT | ||
| 36 | # @param jwt in serialized form | ||
| 37 | # @param part 0 for header, 1 for payload, 2 for signature | ||
| 38 | # @param json whether or not to decode as JSON | ||
| 39 | # @return JSON string | ||
| 40 | sub getJWTPart { | ||
| 41 | my ( $jwt, $part ) = @_; | ||
| 42 | my @jwt_parts = split( /\./, $jwt ); | ||
| 43 | return undef unless @jwt_parts > 1; | ||
| 44 | my $json_hash; | ||
| 45 | eval { $json_hash = from_json( decode_base64url( $jwt_parts[$part] ) ); }; | ||
| 46 | return undef if ($@); | ||
| 47 | return $json_hash; | ||
| 48 | } | ||
| 49 | |||
| 50 | # Return the JWT data that has to be signed by the OP | ||
| 51 | # @param jwt serialized JWT | ||
| 52 | # @return the string data to pass to the signature method | ||
| 53 | sub getJWTSignedData { | ||
| 54 | my ($jwt) = @_; | ||
| 55 | my @jwt_parts = split( /\./, $jwt ); | ||
| 56 | return ( $jwt_parts[0] . "." . $jwt_parts[1] ); | ||
| 57 | } | ||
| 58 | |||
| 59 | # Convenience method to get the decoded JWT header | ||
| 60 | # @param jwt JWT in serialized form | ||
| 61 | # @return Perl hash | ||
| 62 | sub getJWTHeader { | ||
| 63 | my ($jwt) = @_; | ||
| 64 | return getJWTPart( $jwt, 0 ); | ||
| 65 | } | ||
| 66 | |||
| 67 | # Convenience method to get the decoded JWT payload | ||
| 68 | # @param jwt JWT in serialized form | ||
| 69 | # @return Perl hash | ||
| 70 | sub getJWTPayload { | ||
| 71 | my ($jwt) = @_; | ||
| 72 | return getJWTPart( $jwt, 1 ); | ||
| 73 | } | ||
| 74 | |||
| 75 | sub getJWTSignature { | ||
| 76 | my ($jwt) = @_; | ||
| 77 | my @jwt_parts = split( /\./, $jwt ); | ||
| 78 | return $jwt_parts[2]; | ||
| 79 | } | ||
| 80 | |||
| 81 | sub decode_base64url { | ||
| 82 | my $s = shift; | ||
| 83 | $s =~ tr[-_][+/]; | ||
| 84 | $s .= '=' while length($s) % 4; | ||
| 85 | return decode_base64($s); | ||
| 86 | } | ||
| 87 | |||
| 88 | 1; |