| line | stmt | bran | cond | sub | pod | time | code | 
| 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. | 
| 54 |  |  |  |  |  |  |  | 
| 55 |  |  |  |  |  |  | =head1 CONSTRUCTOR ARGUMENTS | 
| 56 |  |  |  |  |  |  |  | 
| 57 |  |  |  |  |  |  | Along with those detailed at L | 
| 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 for | 
| 69 |  |  |  |  |  |  | documentation on each callback function. | 
| 70 |  |  |  |  |  |  |  | 
| 71 |  |  |  |  |  |  | =cut | 
| 72 |  |  |  |  |  |  |  | 
| 73 | 3 |  |  | 3 |  | 1570 | use strict; | 
|  | 3 |  |  |  |  | 7 |  | 
|  | 3 |  |  |  |  | 100 |  | 
| 74 | 3 |  |  | 3 |  | 16 | use warnings; | 
|  | 3 |  |  |  |  | 6 |  | 
|  | 3 |  |  |  |  | 77 |  | 
| 75 |  |  |  |  |  |  |  | 
| 76 | 3 |  |  | 3 |  | 1134 | use Moo; | 
|  | 3 |  |  |  |  | 23425 |  | 
|  | 3 |  |  |  |  | 17 |  | 
| 77 |  |  |  |  |  |  |  | 
| 78 |  |  |  |  |  |  | # prety much the same as implicit grant but even simpler | 
| 79 |  |  |  |  |  |  | extends 'Net::OAuth2::AuthorizationServer::ImplicitGrant'; | 
| 80 |  |  |  |  |  |  |  | 
| 81 | 3 |  |  | 3 |  | 3496 | use Carp qw/ croak /; | 
|  | 3 |  |  |  |  | 8 |  | 
|  | 3 |  |  |  |  | 165 |  | 
| 82 | 3 |  |  | 3 |  | 1170 | use Types::Standard qw/ :all /; | 
|  | 3 |  |  |  |  | 157484 |  | 
|  | 3 |  |  |  |  | 27 |  | 
| 83 |  |  |  |  |  |  |  | 
| 84 | 6 |  |  | 6 |  | 40 | sub _uses_auth_codes     { 0 }; | 
| 85 | 0 |  |  | 0 |  | 0 | sub _uses_user_passwords { 0 }; | 
| 86 |  |  |  |  |  |  |  | 
| 87 |  |  |  |  |  |  | sub _verify_client { | 
| 88 | 24 |  |  | 24 |  | 110 | my ( $self, %args ) = @_; | 
| 89 |  |  |  |  |  |  |  | 
| 90 |  |  |  |  |  |  | my ( $client_id, $scopes_ref, $client_secret ) | 
| 91 | 24 |  |  |  |  | 71 | = @args{ qw/ client_id scopes client_secret / }; | 
| 92 |  |  |  |  |  |  |  | 
| 93 | 24 | 100 |  |  |  | 106 | if ( my $client = $self->clients->{ $client_id } ) { | 
| 94 | 20 |  |  |  |  | 43 | my $client_scopes = []; | 
| 95 |  |  |  |  |  |  |  | 
| 96 | 20 |  | 50 |  |  | 36 | foreach my $scope ( @{ $scopes_ref // [] } ) { | 
|  | 20 |  |  |  |  | 66 |  | 
| 97 | 48 | 100 |  |  |  | 193 | if ( ! exists($self->clients->{ $client_id }{ scopes }{ $scope }) ) { | 
|  |  | 100 |  |  |  |  |  | 
| 98 | 4 |  |  |  |  | 29 | return ( 0, 'invalid_scope' ); | 
| 99 |  |  |  |  |  |  | } | 
| 100 |  |  |  |  |  |  | elsif ( $self->clients->{ $client_id }{ scopes }{ $scope } ) { | 
| 101 | 40 |  |  |  |  | 67 | push @{$client_scopes}, $scope; | 
|  | 40 |  |  |  |  | 107 |  | 
| 102 |  |  |  |  |  |  | } | 
| 103 |  |  |  |  |  |  | } | 
| 104 |  |  |  |  |  |  |  | 
| 105 | 16 | 100 |  |  |  | 66 | return ( 0, 'invalid_grant' ) | 
| 106 |  |  |  |  |  |  | if ! defined $client_secret; | 
| 107 |  |  |  |  |  |  |  | 
| 108 | 12 | 100 |  |  |  | 51 | if ( $client_secret ne $self->clients->{ $client_id }{ client_secret } ) { | 
| 109 | 4 |  |  |  |  | 30 | return ( 0, 'invalid_grant' ); | 
| 110 |  |  |  |  |  |  | } | 
| 111 |  |  |  |  |  |  |  | 
| 112 | 8 |  |  |  |  | 51 | return ( 1, undef, $client_scopes ); | 
| 113 |  |  |  |  |  |  | } | 
| 114 |  |  |  |  |  |  |  | 
| 115 | 4 |  |  |  |  | 29 | return ( 0, 'unauthorized_client' ); | 
| 116 |  |  |  |  |  |  | } | 
| 117 |  |  |  |  |  |  |  | 
| 118 |  |  |  |  |  |  | sub _verify_access_token { | 
| 119 | 20 |  |  | 20 |  | 105 | my ( $self, %args ) = @_; | 
| 120 | 20 |  |  |  |  | 85 | return $self->SUPER::_verify_access_token( %args ); | 
| 121 |  |  |  |  |  |  | } | 
| 122 |  |  |  |  |  |  |  | 
| 123 |  |  |  |  |  |  | sub _store_access_token { | 
| 124 | 4 |  |  | 4 |  | 24 | my ( $self, %args ) = @_; | 
| 125 | 4 |  |  |  |  | 24 | return $self->SUPER::_store_access_token( %args ); | 
| 126 |  |  |  |  |  |  | } | 
| 127 |  |  |  |  |  |  |  | 
| 128 |  |  |  |  |  |  | =head1 AUTHOR | 
| 129 |  |  |  |  |  |  |  | 
| 130 |  |  |  |  |  |  | Lee Johnson - C | 
| 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 |  |  |  |  |  |  | __PACKAGE__->meta->make_immutable; |