| 1 | package Net::OAuth2::AuthorizationServer::ClientCredentialsGrant; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | =head1 NAME | ||||||||||||||
| 4 | |||||||||||||||
| 5 | Net::OAuth2::AuthorizationServer::ClientCredentialsGrant - OAuth2 Client Credentials Grant | ||||||||||||||
| 6 | |||||||||||||||
| 7 | =head1 SYNOPSIS | ||||||||||||||
| 8 | |||||||||||||||
| 9 | my $Grant = Net::OAuth2::AuthorizationServer::ClientCredentialsGrant->new( | ||||||||||||||
| 10 | clients => { | ||||||||||||||
| 11 | TrendyNewService => { | ||||||||||||||
| 12 | client_secret => 'TopSecretClientSecret', | ||||||||||||||
| 13 | # optional | ||||||||||||||
| 14 | scopes => { | ||||||||||||||
| 15 | post_images => 1, | ||||||||||||||
| 16 | annoy_friends => 1, | ||||||||||||||
| 17 | }, | ||||||||||||||
| 18 | }, | ||||||||||||||
| 19 | } | ||||||||||||||
| 20 | ); | ||||||||||||||
| 21 | |||||||||||||||
| 22 | # verify a client against known clients | ||||||||||||||
| 23 | my ( $is_valid,$error,$scopes ) = $Grant->verify_client( | ||||||||||||||
| 24 | client_id => $client_id, | ||||||||||||||
| 25 | client_secret => $client_secret, | ||||||||||||||
| 26 | scopes => [ qw/ list of scopes / ], # optional | ||||||||||||||
| 27 | ); | ||||||||||||||
| 28 | |||||||||||||||
| 29 | # generate a token | ||||||||||||||
| 30 | my $token = $Grant->token( | ||||||||||||||
| 31 | client_id => $client_id, | ||||||||||||||
| 32 | scopes => [ qw/ list of scopes / ], | ||||||||||||||
| 33 | user_id => $user_id, # optional | ||||||||||||||
| 34 | jwt_claims_cb => sub { ... }, # optional, see jwt_claims_cb in Manual | ||||||||||||||
| 35 | ); | ||||||||||||||
| 36 | |||||||||||||||
| 37 | # store access token | ||||||||||||||
| 38 | $Grant->store_access_token( | ||||||||||||||
| 39 | client_id => $client, | ||||||||||||||
| 40 | access_token => $access_token, | ||||||||||||||
| 41 | scopes => [ qw/ list of scopes / ], | ||||||||||||||
| 42 | ); | ||||||||||||||
| 43 | |||||||||||||||
| 44 | # verify an access token | ||||||||||||||
| 45 | my ( $is_valid,$error ) = $Grant->verify_access_token( | ||||||||||||||
| 46 | access_token => $access_token, | ||||||||||||||
| 47 | scopes => [ qw/ list of scopes / ], | ||||||||||||||
| 48 | ); | ||||||||||||||
| 49 | |||||||||||||||
| 50 | =head1 DESCRIPTION | ||||||||||||||
| 51 | |||||||||||||||
| 52 | This module implements the OAuth2 "Client Credentials Grant" flow as described | ||||||||||||||
| 53 | at L<http://tools.ietf.org/html/rfc6749#section-4.4>. | ||||||||||||||
| 54 | |||||||||||||||
| 55 | =head1 CONSTRUCTOR ARGUMENTS | ||||||||||||||
| 56 | |||||||||||||||
| 57 | Along with those detailed at L<Net::OAuth2::AuthorizationServer::Manual/"CONSTRUCTOR ARGUMENTS"> | ||||||||||||||
| 58 | the following are supported by this grant type: | ||||||||||||||
| 59 | |||||||||||||||
| 60 | =head1 CALLBACK FUNCTIONS | ||||||||||||||
| 61 | |||||||||||||||
| 62 | The following callbacks are supported by this grant type: | ||||||||||||||
| 63 | |||||||||||||||
| 64 | verify_client_cb | ||||||||||||||
| 65 | store_access_token_cb | ||||||||||||||
| 66 | verify_access_token_cb | ||||||||||||||
| 67 | |||||||||||||||
| 68 | Please see L<Net::OAuth2::AuthorizationServer::Manual/"CALLBACK FUNCTIONS"> for | ||||||||||||||
| 69 | documentation on each callback function. | ||||||||||||||
| 70 | |||||||||||||||
| 71 | =cut | ||||||||||||||
| 72 | |||||||||||||||
| 73 | 3 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 74 | 3 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 75 | |||||||||||||||
| 76 | 3 | ▶ | use Moo; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 77 | |||||||||||||||
| 78 | # prety much the same as implicit grant but even simpler | ||||||||||||||
| 79 | 3 | extends 'Net::OAuth2::AuthorizationServer::ImplicitGrant'; | |||||||||||||
| 80 | |||||||||||||||
| 81 | 3 | ▶ | use Carp qw/ croak /; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 82 | 3 | ▶ | use Types::Standard qw/ :all /; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 83 | |||||||||||||||
| 84 | 6 | ▶ | sub _uses_auth_codes { 0 }; | ||||||||||||
|
Subroutine
_uses_auth_codes: called CC=1 SCAR=0.0
| |||||||||||||||
| 85 | 0 | ▶ | sub _uses_user_passwords { 0 }; | ||||||||||||
|
Subroutine
_uses_user_passwords: not called CC=1 SCAR=6.9
| |||||||||||||||
| 86 | |||||||||||||||
| 87 | 24 | ▶ | sub _verify_client { | ||||||||||||
|
Subroutine
_verify_client: called CC=8 SCAR=20.8
| |||||||||||||||
| 88 | 24 | my ( $self, %args ) = @_; | |||||||||||||
| 89 | |||||||||||||||
| 90 | my ( $client_id, $scopes_ref, $client_secret ) | ||||||||||||||
| 91 | 24 | = @args{ qw/ client_id scopes client_secret / }; | |||||||||||||
| 92 | |||||||||||||||
| 93 | 24 | ▶ | if ( my $client = $self->clients->{ $client_id } ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 94 | 20 | my $client_scopes = []; | |||||||||||||
| 95 | |||||||||||||||
| 96 | 20 | ▶ | foreach my $scope ( @{ $scopes_ref // [] } ) { | ||||||||||||
|
Condition50%1/2
$scopes_ref // []
MC/DC0%0/1
$scopes_ref // []
$scopes_ref
Truth table50%1/2
A
A= $scopes_ref
| |||||||||||||||
| 97 | 48 | ▶ | if ( ! exists($self->clients->{ $client_id }{ scopes }{ $scope }) ) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 98 | 4 | return ( 0, 'invalid_scope' ); | |||||||||||||
| 99 | } | ||||||||||||||
| 100 | elsif ( $self->clients->{ $client_id }{ scopes }{ $scope } ) { | ||||||||||||||
| 101 | 40 | push @{$client_scopes}, $scope; | |||||||||||||
| 102 | } | ||||||||||||||
| 103 | } | ||||||||||||||
| 104 | |||||||||||||||
| 105 | 16 | ▶ | return ( 0, 'invalid_grant' ) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 106 | if ! defined $client_secret; | ||||||||||||||
| 107 | |||||||||||||||
| 108 | 12 | ▶ | if ( $client_secret ne $self->clients->{ $client_id }{ client_secret } ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 109 | 4 | return ( 0, 'invalid_grant' ); | |||||||||||||
| 110 | } | ||||||||||||||
| 111 | |||||||||||||||
| 112 | 8 | return ( 1, undef, $client_scopes ); | |||||||||||||
| 113 | } | ||||||||||||||
| 114 | |||||||||||||||
| 115 | 4 | return ( 0, 'unauthorized_client' ); | |||||||||||||
| 116 | } | ||||||||||||||
| 117 | |||||||||||||||
| 118 | 20 | ▶ | sub _verify_access_token { | ||||||||||||
|
Subroutine
_verify_access_token: called CC=1 SCAR=0.0
| |||||||||||||||
| 119 | 20 | my ( $self, %args ) = @_; | |||||||||||||
| 120 | 20 | return $self->SUPER::_verify_access_token( %args ); | |||||||||||||
| 121 | } | ||||||||||||||
| 122 | |||||||||||||||
| 123 | 4 | ▶ | sub _store_access_token { | ||||||||||||
|
Subroutine
_store_access_token: called CC=1 SCAR=0.0
| |||||||||||||||
| 124 | 4 | my ( $self, %args ) = @_; | |||||||||||||
| 125 | 4 | return $self->SUPER::_store_access_token( %args ); | |||||||||||||
| 126 | } | ||||||||||||||
| 127 | |||||||||||||||
| 128 | =head1 AUTHOR | ||||||||||||||
| 129 | |||||||||||||||
| 130 | Lee Johnson - C<leejo@cpan.org> | ||||||||||||||
| 131 | |||||||||||||||
| 132 | =head1 LICENSE | ||||||||||||||
| 133 | |||||||||||||||
| 134 | This library is free software; you can redistribute it and/or modify it under | ||||||||||||||
| 135 | the same terms as Perl itself. If you would like to contribute documentation | ||||||||||||||
| 136 | or file a bug report then please raise an issue / pull request: | ||||||||||||||
| 137 | |||||||||||||||
| 138 | https://github.com/Humanstate/net-oauth2-authorizationserver | ||||||||||||||
| 139 | |||||||||||||||
| 140 | =cut | ||||||||||||||
| 141 | |||||||||||||||
| 142 | 3 | __PACKAGE__->meta->make_immutable; | |||||||||||||