| 1 | package Catalyst::Authentication::Credential::OAuth2; | ||||||||||||||
| 2 | 3 | ▶ | use Moose; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 3 | 3 | ▶ | use MooseX::Types::Common::String qw(NonEmptySimpleStr); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 3 | ▶ | use LWP::UserAgent; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | 3 | ▶ | use HTTP::Request::Common; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | 3 | ▶ | use JSON::Any; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | 3 | ▶ | use Moose::Util; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | |||||||||||||||
| 9 | # ABSTRACT: Authenticate against OAuth2 servers | ||||||||||||||
| 10 | |||||||||||||||
| 11 | |||||||||||||||
| 12 | 3 | has [qw(grant_uri token_uri client_id)] => ( | |||||||||||||
| 13 | is => 'ro', | ||||||||||||||
| 14 | isa => NonEmptySimpleStr, | ||||||||||||||
| 15 | required => 1, | ||||||||||||||
| 16 | ); | ||||||||||||||
| 17 | |||||||||||||||
| 18 | 3 | has token_uri_method => (is=>'ro', required=>1, default=>'GET'); | |||||||||||||
| 19 | 3 | has token_uri_post_content_type => (is=>'ro', required=>1, default=>'application/x-www-form-urlencoded'); | |||||||||||||
| 20 | 3 | has extra_find_user_token_fields => (is=>'ro', required=>0, predicate=>'has_extra_find_user_token_fields'); | |||||||||||||
| 21 | 3 | has scope => (is=>'ro', required=>0, predicate=>'has_scope'); | |||||||||||||
| 22 | |||||||||||||||
| 23 | 3 | has client_secret => ( | |||||||||||||
| 24 | is => 'ro', | ||||||||||||||
| 25 | isa => NonEmptySimpleStr, | ||||||||||||||
| 26 | required => 0, | ||||||||||||||
| 27 | predicate => 'has_client_secret' | ||||||||||||||
| 28 | ); | ||||||||||||||
| 29 | |||||||||||||||
| 30 | 1 | ▶ | has ua => ( is => 'ro', default => sub { LWP::UserAgent->new } ); | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 31 | |||||||||||||||
| 32 | sub BUILDARGS { | ||||||||||||||
| 33 | 4 | ▶ | my ( $class, $config, $app, $realm ) = @_; | ||||||||||||
|
Subroutine
BUILDARGS: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 34 | 4 | Moose::Util::ensure_all_roles( $realm, 'CatalystX::OAuth2::ClientInjector' ); | |||||||||||||
| 35 | 4 | Moose::Util::ensure_all_roles( $realm->store, 'CatalystX::OAuth2::ClientPersistor'); | |||||||||||||
| 36 | 4 | return $config; | |||||||||||||
| 37 | } | ||||||||||||||
| 38 | |||||||||||||||
| 39 | sub authenticate { | ||||||||||||||
| 40 | 4 | ▶ | my ( $self, $ctx, $realm, $auth_info ) = @_; | ||||||||||||
|
Subroutine
authenticate: called CC=5 SCAR=16.6
Pod
undocumented
| |||||||||||||||
| 41 | 4 | my $callback_uri = $self->_build_callback_uri($ctx); | |||||||||||||
| 42 | |||||||||||||||
| 43 | 4 | ▶ | unless ( defined( my $code = $ctx->request->params->{code} ) ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 44 | 2 | my $auth_url = $self->extend_permissions( $callback_uri, $auth_info ); | |||||||||||||
| 45 | 2 | $ctx->response->redirect($auth_url); | |||||||||||||
| 46 | |||||||||||||||
| 47 | 2 | return; | |||||||||||||
| 48 | } else { | ||||||||||||||
| 49 | 2 | my $token = | |||||||||||||
| 50 | $self->request_access_token( $callback_uri, $code, $auth_info ); | ||||||||||||||
| 51 | 2 | ▶ | die 'Error validating verification code' unless $token; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 52 | |||||||||||||||
| 53 | 2 | my %find_user_fields = (token => $token->{access_token}); | |||||||||||||
| 54 | 2 | ▶ | if($self->has_extra_find_user_token_fields) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 55 | 0 | $find_user_fields{$_} = $token->{$_} for @{$self->extra_find_user_token_fields}; | |||||||||||||
| 56 | } | ||||||||||||||
| 57 | 2 | return $realm->find_user( \%find_user_fields, $ctx ); | |||||||||||||
| 58 | } | ||||||||||||||
| 59 | } | ||||||||||||||
| 60 | |||||||||||||||
| 61 | sub _build_callback_uri { | ||||||||||||||
| 62 | 5 | ▶ | my ( $self, $ctx ) = @_; | ||||||||||||
|
Subroutine
_build_callback_uri: called CC=1 SCAR=0.0
| |||||||||||||||
| 63 | 5 | my $uri = $ctx->request->uri->clone; | |||||||||||||
| 64 | 5 | $uri->query(undef); | |||||||||||||
| 65 | 5 | return $uri; | |||||||||||||
| 66 | } | ||||||||||||||
| 67 | |||||||||||||||
| 68 | sub extend_permissions { | ||||||||||||||
| 69 | 3 | ▶ | my ( $self, $callback_uri, $auth_info ) = @_; | ||||||||||||
|
Subroutine
extend_permissions: called CC=4 SCAR=14.2
Pod
undocumented
| |||||||||||||||
| 70 | 3 | my $uri = URI->new( $self->grant_uri ); | |||||||||||||
| 71 | 3 | my $query = { | |||||||||||||
| 72 | response_type => 'code', | ||||||||||||||
| 73 | client_id => $self->client_id, | ||||||||||||||
| 74 | redirect_uri => $callback_uri, | ||||||||||||||
| 75 | }; | ||||||||||||||
| 76 | 3 | ▶ | $query->{state} = $auth_info->{state} if exists $auth_info->{state}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 77 | 3 | ▶ | $query->{scope} = $self->scope if $self->has_scope; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 78 | 3 | ▶ | $query->{scope} = $auth_info->{scope} if exists $auth_info->{scope}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 79 | |||||||||||||||
| 80 | 3 | $uri->query_form($query); | |||||||||||||
| 81 | 3 | return $uri; | |||||||||||||
| 82 | } | ||||||||||||||
| 83 | |||||||||||||||
| 84 | 3 | my $j = JSON::Any->new; | |||||||||||||
| 85 | |||||||||||||||
| 86 | sub request_access_token { | ||||||||||||||
| 87 | 2 | ▶ | my ( $self, $callback_uri, $code, $auth_info ) = @_; | ||||||||||||
|
Subroutine
request_access_token: called CC=8 SCAR=27.3
Pod
undocumented
| |||||||||||||||
| 88 | 2 | my $uri = URI->new( $self->token_uri ); | |||||||||||||
| 89 | 2 | my @data = ( | |||||||||||||
| 90 | client_id => $self->client_id, | ||||||||||||||
| 91 | redirect_uri => "$callback_uri", #stringify for JSON | ||||||||||||||
| 92 | code => $code, | ||||||||||||||
| 93 | grant_type => 'authorization_code'); | ||||||||||||||
| 94 | 2 | ▶ | push(@data, (state=>$auth_info->{state})) if exists $auth_info->{state}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 95 | 2 | ▶ | push(@data, (client_secret=>$self->client_secret)) if $self->has_client_secret; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 96 | |||||||||||||||
| 97 | 2 | my $req; | |||||||||||||
| 98 | 2 | ▶ | if($self->token_uri_method eq 'GET') { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 99 | 2 | $uri->query_form(+{@data}); | |||||||||||||
| 100 | 2 | $req = GET $uri; | |||||||||||||
| 101 | } elsif($self->token_uri_method eq 'POST') { | ||||||||||||||
| 102 | 0 | ▶ | if($self->token_uri_post_content_type eq 'application/json') { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 103 | 0 | $req = POST $uri, 'Content_Type' => 'application/json', Content => $j->to_json(+{@data}); | |||||||||||||
| 104 | } elsif($self->token_uri_post_content_type eq 'application/x-www-form-urlencoded') { | ||||||||||||||
| 105 | 0 | $req = POST $uri, 'Content_Type' => 'application/x-www-form-urlencoded', Content => \@data; | |||||||||||||
| 106 | } else { | ||||||||||||||
| 107 | 0 | die "Unrecognized 'token_uri_post_content_type' of '${\$self->token_uri_post_content_type}'"; | |||||||||||||
| 108 | } | ||||||||||||||
| 109 | } else { | ||||||||||||||
| 110 | 0 | die "Unrecognized 'token_uri_method' of '${\$self->token_uri_method}'"; | |||||||||||||
| 111 | } | ||||||||||||||
| 112 | |||||||||||||||
| 113 | 2 | my $response = $self->ua->request($req); | |||||||||||||
| 114 | 2 | ▶ | if($response->is_success) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 115 | 2 | my $data = $j->jsonToObj( $response->decoded_content ); # Eval wrap | |||||||||||||
| 116 | 2 | return $data; | |||||||||||||
| 117 | } else { | ||||||||||||||
| 118 | 0 | return; | |||||||||||||
| 119 | } | ||||||||||||||
| 120 | } | ||||||||||||||
| 121 | |||||||||||||||
| 122 | 3 | 1; | |||||||||||||
| 123 | |||||||||||||||
| 124 | __END__ | ||||||||||||||
| 125 | |||||||||||||||
| 126 | =pod | ||||||||||||||
| 127 | |||||||||||||||
| 128 | =head1 NAME | ||||||||||||||
| 129 | |||||||||||||||
| 130 | Catalyst::Authentication::Credential::OAuth2 - Authenticate against OAuth2 servers | ||||||||||||||
| 131 | |||||||||||||||
| 132 | =head1 VERSION | ||||||||||||||
| 133 | |||||||||||||||
| 134 | version 0.001006 | ||||||||||||||
| 135 | |||||||||||||||
| 136 | =head1 SYNOPSIS | ||||||||||||||
| 137 | |||||||||||||||
| 138 | __PACKAGE__->config( | ||||||||||||||
| 139 | 'Plugin::Authentication' => { | ||||||||||||||
| 140 | default => { | ||||||||||||||
| 141 | credential => { | ||||||||||||||
| 142 | class => 'OAuth2', | ||||||||||||||
| 143 | grant_uri => 'http://authserver/request', | ||||||||||||||
| 144 | token_uri => 'http://authserver/token', | ||||||||||||||
| 145 | client_id => 'dead69beef' | ||||||||||||||
| 146 | }, | ||||||||||||||
| 147 | store => { class => 'Null' } | ||||||||||||||
| 148 | } | ||||||||||||||
| 149 | } | ||||||||||||||
| 150 | ); | ||||||||||||||
| 151 | |||||||||||||||
| 152 | =head1 DESCRIPTION | ||||||||||||||
| 153 | |||||||||||||||
| 154 | This module implements authentication via OAuth2 credentials, giving you a | ||||||||||||||
| 155 | user object which stores tokens for accessing protected resources. | ||||||||||||||
| 156 | |||||||||||||||
| 157 | =head1 ATTRIBUTES | ||||||||||||||
| 158 | |||||||||||||||
| 159 | =head2 grant_uri | ||||||||||||||
| 160 | |||||||||||||||
| 161 | =head2 token_uri | ||||||||||||||
| 162 | |||||||||||||||
| 163 | =head2 client_id | ||||||||||||||
| 164 | |||||||||||||||
| 165 | Required attributes that you get from your Oauth2 provider | ||||||||||||||
| 166 | |||||||||||||||
| 167 | =head2 client_secret | ||||||||||||||
| 168 | |||||||||||||||
| 169 | optional secret code from your Oauth2 provider (you need to review the docs from | ||||||||||||||
| 170 | your provider). | ||||||||||||||
| 171 | |||||||||||||||
| 172 | =head2 scope | ||||||||||||||
| 173 | |||||||||||||||
| 174 | Value of 'scope' field submitted to the grant_uri | ||||||||||||||
| 175 | |||||||||||||||
| 176 | =head2 token_uri_method | ||||||||||||||
| 177 | |||||||||||||||
| 178 | Default is GET; some providers require POST | ||||||||||||||
| 179 | |||||||||||||||
| 180 | =head2 token_uri_post_content_type | ||||||||||||||
| 181 | |||||||||||||||
| 182 | Default is 'application/x-www-form-urlencoded', some providers support 'application/json'. | ||||||||||||||
| 183 | |||||||||||||||
| 184 | =head2 has_extra_find_user_token_fields | ||||||||||||||
| 185 | |||||||||||||||
| 186 | By default we call ->find_user on the store with a hashref that contains key 'token' and the | ||||||||||||||
| 187 | value of the access_token (which we get from calling the 'token_uri'). The results of calling | ||||||||||||||
| 188 | the token_uri is usually a JSON named array structure which can contain other fields such as | ||||||||||||||
| 189 | id_token (typically a JWT). You can set this to an arrayref of extra fields you want to pass. | ||||||||||||||
| 190 | |||||||||||||||
| 191 | =head1 AUTHOR | ||||||||||||||
| 192 | |||||||||||||||
| 193 | Eden Cardim <edencardim@gmail.com> | ||||||||||||||
| 194 | |||||||||||||||
| 195 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||
| 196 | |||||||||||||||
| 197 | This software is copyright (c) 2017 by Suretec Systems Ltd. | ||||||||||||||
| 198 | |||||||||||||||
| 199 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||
| 200 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||
| 201 | |||||||||||||||
| 202 | =cut | ||||||||||||||