| 1 | package OIDC::Lite::Model::IDToken; | ||||||||||||||||||||||||||||
| 2 | 6 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 6 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 6 | ▶ | use base 'Class::Accessor::Fast'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 6 | ▶ | use MIME::Base64 qw/encode_base64url decode_base64url/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 6 | ▶ | use JSON::WebToken qw/encode_jwt decode_jwt/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 6 | ▶ | use Params::Validate; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 6 | ▶ | use Digest::SHA qw/sha256 sha384 sha512/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 6 | ▶ | use OIDC::Lite::Util::JWT; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 6 | ▶ | use constant HALF_BITS_DENOMINATOR => 2 * 8; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 6 | ▶ | use constant ALG_LEN => 2; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 6 | ▶ | use constant BITS_LEN => 3; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | =head1 NAME | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | OIDC::Lite::Model::IDToken - model class that represents ID token | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | =head1 ACCESSORS | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | =head2 header | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | JWT Header | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | =head2 payload | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | JWT Payload | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | =head2 key | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | Key for JWT Signature | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | =cut | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 6 | __PACKAGE__->mk_accessors(qw( | |||||||||||||||||||||||||||
| 38 | header | ||||||||||||||||||||||||||||
| 39 | payload | ||||||||||||||||||||||||||||
| 40 | key | ||||||||||||||||||||||||||||
| 41 | token_string | ||||||||||||||||||||||||||||
| 42 | )); | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | =head1 METHODS | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | =head2 new( \%header, \%payload, $key ) | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | Constructor | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | my $id_token = OIDC::Lite::Model::IDToken->new(); | ||||||||||||||||||||||||||||
| 51 | ... | ||||||||||||||||||||||||||||
| 52 | my $id_token = OIDC::Lite::Model::IDToken->new( | ||||||||||||||||||||||||||||
| 53 | header => \%header, | ||||||||||||||||||||||||||||
| 54 | payload => \%payload, | ||||||||||||||||||||||||||||
| 55 | key => $key, | ||||||||||||||||||||||||||||
| 56 | ); | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | =cut | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | sub new { | ||||||||||||||||||||||||||||
| 61 | 28 | ▶ | my $class = shift; | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 62 | 28 | ▶ | my @args = @_ == 1 ? %{$_[0]} : @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | 28 | my %params = Params::Validate::validate_with( | |||||||||||||||||||||||||||
| 64 | params => \@args, | ||||||||||||||||||||||||||||
| 65 | spec => { | ||||||||||||||||||||||||||||
| 66 | header => { optional => 1 }, | ||||||||||||||||||||||||||||
| 67 | payload => { optional => 1 }, | ||||||||||||||||||||||||||||
| 68 | key => { optional => 1 }, | ||||||||||||||||||||||||||||
| 69 | }, | ||||||||||||||||||||||||||||
| 70 | allow_extra => 0, | ||||||||||||||||||||||||||||
| 71 | ); | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 28 | my $self = bless \%params, $class; | |||||||||||||||||||||||||||
| 74 | 28 | ▶ | unless($self->header){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | 2 | my %header=(); | |||||||||||||||||||||||||||
| 76 | 2 | $self->header(\%header); | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | 28 | ▶ | unless($self->payload){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | 2 | my %payload=(); | |||||||||||||||||||||||||||
| 80 | 2 | $self->payload(\%payload); | |||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 28 | return $self; | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | =head2 get_token_string() | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | generate signature and return ID Token string. | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | my $id_token_string = $id_token->get_token_string(); | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | =cut | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | sub get_token_string { | ||||||||||||||||||||||||||||
| 95 | 19 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
get_token_string: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | $self->header->{typ} = q{JWT} | ||||||||||||||||||||||||||||
| 98 | 19 | ▶ | unless($self->header->{typ}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | $self->header->{alg} = q{none} | ||||||||||||||||||||||||||||
| 100 | 19 | ▶ | unless($self->header->{alg}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | # generate token string | ||||||||||||||||||||||||||||
| 103 | 19 | my $jwt = encode_jwt($self->payload, $self->key, $self->header->{alg}, $self->header); | |||||||||||||||||||||||||||
| 104 | 19 | $self->token_string($jwt); | |||||||||||||||||||||||||||
| 105 | 19 | return $jwt; | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | =head2 access_token_hash() | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | generate signature and return ID Token string. | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | $id_token->code_hash($access_token); | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | =cut | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | sub access_token_hash { | ||||||||||||||||||||||||||||
| 117 | 8 | ▶ | my ($self, $access_token_string) = @_; | ||||||||||||||||||||||||||
|
Subroutine
access_token_hash: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 8 | ▶ | if($self->header->{alg} && $self->header->{alg} ne 'none') | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->header->{"alg"} and $self->header->{"alg"} ne "none"
MC/DC100%2/2
$self->header->{"alg"} and $self->header->{"alg"} ne "none"
$self->header->{"alg"}
$self->header->{"alg"} ne "none"
Truth table100%3/3
A and B
A= $self->header->{"alg"}B= $self->header->{"alg"} ne "none"
| |||||||||||||||||||||||||||||
| 120 | { | ||||||||||||||||||||||||||||
| 121 | 6 | my $bit = substr($self->header->{alg}, ALG_LEN, BITS_LEN); | |||||||||||||||||||||||||||
| 122 | 6 | my $len = $bit/HALF_BITS_DENOMINATOR; | |||||||||||||||||||||||||||
| 123 | 6 | my $sha = Digest::SHA->new($bit); | |||||||||||||||||||||||||||
| 124 | 6 | $sha->add($access_token_string); | |||||||||||||||||||||||||||
| 125 | 6 | $self->payload->{at_hash} = encode_base64url(substr($sha->digest, 0, $len)); | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | =head2 code_hash() | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | Set Authorization Code Hash to ID Token. | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | $id_token->code_hash($authorization_code); | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | =cut | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | sub code_hash { | ||||||||||||||||||||||||||||
| 138 | 8 | ▶ | my ($self, $authorization_code) = @_; | ||||||||||||||||||||||||||
|
Subroutine
code_hash: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 8 | ▶ | if($self->header->{alg} && $self->header->{alg} ne 'none') | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->header->{"alg"} and $self->header->{"alg"} ne "none"
MC/DC100%2/2
$self->header->{"alg"} and $self->header->{"alg"} ne "none"
$self->header->{"alg"}
$self->header->{"alg"} ne "none"
Truth table100%3/3
A and B
A= $self->header->{"alg"}B= $self->header->{"alg"} ne "none"
| |||||||||||||||||||||||||||||
| 141 | { | ||||||||||||||||||||||||||||
| 142 | 6 | my $bit = substr($self->header->{alg}, ALG_LEN, BITS_LEN); | |||||||||||||||||||||||||||
| 143 | 6 | my $len = $bit/HALF_BITS_DENOMINATOR; | |||||||||||||||||||||||||||
| 144 | 6 | my $sha = Digest::SHA->new($bit); | |||||||||||||||||||||||||||
| 145 | 6 | $sha->add($authorization_code); | |||||||||||||||||||||||||||
| 146 | 6 | $self->payload->{c_hash} = encode_base64url(substr($sha->digest, 0, $len)); | |||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | =head2 load($token_string) | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | load ID Token object from token string | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | my $token_string = 'eyJhbGciOiJub25lIiwidHlwIjoiSldTIn0.eyJmb28iOiJiYXIifQ.'; | ||||||||||||||||||||||||||||
| 155 | my $id_token = OIDC::Lite::Model::IDToken->load($token_string); | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | =cut | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | sub load { | ||||||||||||||||||||||||||||
| 160 | 12 | ▶ | my ($self, $token_string) = @_; | ||||||||||||||||||||||||||
|
Subroutine
load: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 161 | 12 | ▶ | return unless($token_string); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | 11 | my $header = OIDC::Lite::Util::JWT::header($token_string); | |||||||||||||||||||||||||||
| 164 | 11 | my $payload = OIDC::Lite::Util::JWT::payload($token_string); | |||||||||||||||||||||||||||
| 165 | 11 | ▶ | return unless ( $header and $payload ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$header and $payload
MC/DC100%2/2
$header and $payload
$header
$payload
Truth table100%3/3
A and B
A= $headerB= $payload
| |||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | 8 | my $id_token = OIDC::Lite::Model::IDToken->new( | |||||||||||||||||||||||||||
| 168 | header => $header, | ||||||||||||||||||||||||||||
| 169 | payload => $payload, | ||||||||||||||||||||||||||||
| 170 | ); | ||||||||||||||||||||||||||||
| 171 | 8 | $id_token->token_string($token_string); | |||||||||||||||||||||||||||
| 172 | 8 | return $id_token; | |||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | =head2 verify() | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | verify token signature. | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | my $token_string = '...'; | ||||||||||||||||||||||||||||
| 180 | my $id_token = OIDC::Lite::Model::IDToken->load($token_string); | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | my $key = 'shared_secret_key'; | ||||||||||||||||||||||||||||
| 183 | $id_token->key($key); | ||||||||||||||||||||||||||||
| 184 | unless($id_token->verify()){ | ||||||||||||||||||||||||||||
| 185 | # validation failed | ||||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | =cut | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | sub verify { | ||||||||||||||||||||||||||||
| 191 | 8 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
verify: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 192 | 8 | ▶ | return 0 | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 193 | unless($self->token_string); | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | 7 | ▶ | $self->key('') | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 196 | unless($self->key); | ||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | 7 | my $payload = undef; | |||||||||||||||||||||||||||
| 199 | 7 | eval{ | |||||||||||||||||||||||||||
| 200 | 7 | $payload = decode_jwt($self->token_string, $self->key, 1, 1); | |||||||||||||||||||||||||||
| 201 | }; | ||||||||||||||||||||||||||||
| 202 | 7 | ▶ | if($@){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 203 | 5 | return 0; | |||||||||||||||||||||||||||
| 204 | } | ||||||||||||||||||||||||||||
| 205 | 2 | return (defined($payload)); | |||||||||||||||||||||||||||
| 206 | } | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | Ryo Ito, E<lt>ritou.06@gmail.comE<gt> | ||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | Copyright (C) 2012 by Ryo Ito | ||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | This library is free software; you can redistribute it and/or modify | ||||||||||||||||||||||||||||
| 217 | it under the same terms as Perl itself, either Perl version 5.8.8 or, | ||||||||||||||||||||||||||||
| 218 | at your option, any later version of Perl 5 you may have available. | ||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | =cut | ||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | 6 | 1; | |||||||||||||||||||||||||||