| 1 | package OIDC::Client::AccessTokenBuilder; | ||||||||||||||||||
| 2 | |||||||||||||||||||
| 3 | 6 | ▶ | use utf8; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 4 | 6 | ▶ | use Moose; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 5 | 6 | ▶ | use Moose::Exporter; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 6 | 6 | ▶ | use MooseX::Params::Validate; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 7 | 6 | ▶ | use List::Util qw(first); | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 8 | 6 | ▶ | use OIDC::Client::AccessToken; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 9 | 6 | ▶ | use OIDC::Client::Utils qw(get_values_from_space_delimited_string); | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 10 | |||||||||||||||||||
| 11 | =encoding utf8 | ||||||||||||||||||
| 12 | |||||||||||||||||||
| 13 | =head1 NAME | ||||||||||||||||||
| 14 | |||||||||||||||||||
| 15 | OIDC::Client::AccessTokenBuilder - AccessToken object builder | ||||||||||||||||||
| 16 | |||||||||||||||||||
| 17 | =head1 DESCRIPTION | ||||||||||||||||||
| 18 | |||||||||||||||||||
| 19 | Exports functions that help create an L<OIDC::Client::AccessToken> object. | ||||||||||||||||||
| 20 | |||||||||||||||||||
| 21 | =cut | ||||||||||||||||||
| 22 | |||||||||||||||||||
| 23 | 6 | Moose::Exporter->setup_import_methods(as_is => [qw/build_access_token_from_token_response | |||||||||||||||||
| 24 | build_access_token_from_claims/]); | ||||||||||||||||||
| 25 | |||||||||||||||||||
| 26 | |||||||||||||||||||
| 27 | =head1 FUNCTIONS | ||||||||||||||||||
| 28 | |||||||||||||||||||
| 29 | =head2 build_access_token_from_token_response( $token_response ) | ||||||||||||||||||
| 30 | |||||||||||||||||||
| 31 | Builds an L<OIDC::Client::AccessToken> object from an L<OIDC::Client::TokenResponse> object. | ||||||||||||||||||
| 32 | |||||||||||||||||||
| 33 | =cut | ||||||||||||||||||
| 34 | |||||||||||||||||||
| 35 | 29 | ▶ | sub build_access_token_from_token_response { | ||||||||||||||||
|
Subroutine
build_access_token_from_token_response: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||
| 36 | 29 | my ($token_response) = pos_validated_list(\@_, { isa => 'OIDC::Client::TokenResponse', optional => 0 }); | |||||||||||||||||
| 37 | |||||||||||||||||||
| 38 | 29 | my $token_type = $token_response->token_type; | |||||||||||||||||
| 39 | 29 | ▶ | my $expires_at = defined $token_response->expires_in ? _get_time() + $token_response->expires_in | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 40 | : undef; | ||||||||||||||||||
| 41 | 29 | ▶ | my $scopes = defined $token_response->scope ? get_values_from_space_delimited_string($token_response->scope) | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 42 | : undef; | ||||||||||||||||||
| 43 | |||||||||||||||||||
| 44 | 29 | ▶ | return OIDC::Client::AccessToken->new( | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 45 | token => $token_response->access_token, | ||||||||||||||||||
| 46 | defined $token_type ? (token_type => $token_type) : (), | ||||||||||||||||||
| 47 | defined $expires_at ? (expires_at => $expires_at) : (), | ||||||||||||||||||
| 48 | defined $scopes ? (scopes => $scopes ) : (), | ||||||||||||||||||
| 49 | ); | ||||||||||||||||||
| 50 | } | ||||||||||||||||||
| 51 | |||||||||||||||||||
| 52 | |||||||||||||||||||
| 53 | =head2 build_access_token_from_claims( $claims, $token ) | ||||||||||||||||||
| 54 | |||||||||||||||||||
| 55 | Builds an L<OIDC::Client::AccessToken> object from claims (hashref) and token (string). | ||||||||||||||||||
| 56 | |||||||||||||||||||
| 57 | =cut | ||||||||||||||||||
| 58 | |||||||||||||||||||
| 59 | 12 | ▶ | sub build_access_token_from_claims { | ||||||||||||||||
|
Subroutine
build_access_token_from_claims: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||
| 60 | 12 | my ($claims, $token) = pos_validated_list(\@_, { isa => 'HashRef', optional => 0 }, | |||||||||||||||||
| 61 | { isa => 'Str', optional => 0 }); | ||||||||||||||||||
| 62 | |||||||||||||||||||
| 63 | 12 | my $token_type = $claims->{token_type}; | |||||||||||||||||
| 64 | 12 | my $expires_at = $claims->{exp}; | |||||||||||||||||
| 65 | 12 | my $scopes = _get_scopes_from_claims($claims); | |||||||||||||||||
| 66 | |||||||||||||||||||
| 67 | 12 | ▶ | return OIDC::Client::AccessToken->new( | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 68 | token => $token, | ||||||||||||||||||
| 69 | claims => $claims, | ||||||||||||||||||
| 70 | defined $token_type ? (token_type => $token_type) : (), | ||||||||||||||||||
| 71 | defined $expires_at ? (expires_at => $expires_at) : (), | ||||||||||||||||||
| 72 | defined $scopes ? (scopes => $scopes ) : (), | ||||||||||||||||||
| 73 | ); | ||||||||||||||||||
| 74 | } | ||||||||||||||||||
| 75 | |||||||||||||||||||
| 76 | 12 | ▶ | sub _get_scopes_from_claims { | ||||||||||||||||
|
Subroutine
_get_scopes_from_claims: called CC=4 SCAR=13.9
| |||||||||||||||||||
| 77 | 12 | my ($claims) = pos_validated_list(\@_, { isa => 'HashRef', optional => 0 }); | |||||||||||||||||
| 78 | |||||||||||||||||||
| 79 | 12 | ▶ | my $claim_name = first { defined $claims->{$_} } qw( scp scope ) | ||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||||||||
| 80 | or return; | ||||||||||||||||||
| 81 | |||||||||||||||||||
| 82 | 5 | my $scopes = $claims->{$claim_name}; | |||||||||||||||||
| 83 | |||||||||||||||||||
| 84 | 5 | ▶ | unless (ref $scopes) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 85 | 1 | return get_values_from_space_delimited_string($scopes); | |||||||||||||||||
| 86 | } | ||||||||||||||||||
| 87 | |||||||||||||||||||
| 88 | 4 | ▶ | return ref $scopes eq 'ARRAY' ? $scopes : []; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 89 | } | ||||||||||||||||||
| 90 | |||||||||||||||||||
| 91 | # to be mocked in tests | ||||||||||||||||||
| 92 | 19 | ▶ | sub _get_time { | ||||||||||||||||
|
Subroutine
_get_time: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 93 | 19 | return time(); | |||||||||||||||||
| 94 | } | ||||||||||||||||||
| 95 | |||||||||||||||||||
| 96 | 6 | 1; | |||||||||||||||||