CatalystX/OAuth2/Provider/Controller/OAuth.pm

filter: Statementstmt 83.3% 10 / 12 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 87.5% 14 / 16 CC 1 SCAR 0.7
CC
1
Cov
83%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« CatalystX/OAuth2/Provider/TraitFor/Controller/OAuth/AuthorizationCode.pm ↑ Summary CatalystX/OAuth2/Provider.pm »
1 package CatalystX::OAuth2::Provider::Controller::OAuth;
2 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use Moose::Autobox;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use MooseX::Types::Moose qw/ HashRef ArrayRef ClassName Object Str /;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 0 ▶use MooseX::Types::Common::String qw/ NonEmptySimpleStr /;
Subroutine BEGIN: called CC=1 SCAR=2.6
6 use JSON::XS ();
7 use CatalystX::OAuth2::Provider::Error;
8 use namespace::autoclean;
9  
10 BEGIN { extends 'Catalyst::Controller::ActionRole'; }
11  
12 with 'CatalystX::Component::Traits';
13  
14 has '+_trait_merge' => ( default => 1 );
15  
16 __PACKAGE__->config( traits => [qw/AuthorizationCode/] );
17  
18 =head2 get_client
19 =cut
20 # TODO: Generic AUTH_INFO Model
21 sub _get_client : Private {
22 my ( $self, $ctx ) = @_;
23 foreach my $k ( values %{ $self->{auth_info} } ) {
24 return $k if ( $k->{client_id} eq $ctx->req->param('client_id') )
25 }
26 }
27  
28 =head2 base
29 Base for those chains
30 =cut
31 sub base :Chained('/') :PathPart('oauth') :CaptureArgs(0) {
32 my ( $self, $ctx ) = @_;
33  
34 CatalystX::OAuth2::Provider::Error::InvalidRequest->throw( description => 'Invalid request' )
35 if ! $ctx->req->param("client_id");
36 my $client = $ctx->forward('_get_client');
37 $ctx->stash( client => $client ); #use this client for testing
38 CatalystX::OAuth2::Provider::Error::InvalidClient->throw( description => "Invalid client id" )
39 if ! $ctx->stash->{client};
40 }
41  
42 =head2 logged_in_required
43 Let's chained this function to make require to log in
44 =cut
45 sub logged_in_required
46 :Chained('base')
47 :PathPart('')
48 :CaptureArgs(0)
49 {
50 my ( $self, $ctx ) = @_;
51 $ctx->forward( 'user_existed_or_authenticated' ); #CHECK USER
52 }
53  
54 =head2 user_existed_or_authenticated
55 See if user is logged in or not
56 If not try to authenticate, otherwise failed.
57 =cut
58 sub user_existed_or_authenticated
59 :Private
60 {
61 my ( $self, $ctx ) = @_;
62 return 1 if $ctx->user_exists();
63 return 1 if $ctx->authenticate(
64 { username => $ctx->req->param('username')
65 || $ctx->req->param($self->{login_form}->{field_names}->{username}),
66 password => $ctx->req->param('password')
67 || $ctx->req->param($self->{login_form}->{field_names}->{password}),
68 } );
69 $ctx->stash( template => $self->{login_form}->{template}
70 || 'user/login.tt' );
71 #$ctx->res->status( 403 ); #This doesn't work when running with fastcgi
72 $ctx->detach();
73 }
74  
75 =head2 logged_in_not_required
76 An empty function, just for those who wanna chain this. Doesn't matter.
77 =cut
78 sub logged_in_not_required
79 :Chained('base')
80 :PathPart('')
81 :CaptureArgs(0)
82 {}
83  
84 =head2 token
85 Token enpoint
86 =cut
87 sub token
88 :Chained('logged_in_not_required')
89 :PathPart('token') #Configurable?
90 :Args(0)
91 {
92 my ( $self, $ctx ) = @_;
93 my $grant_type = $ctx->req->param('grant_type');
94 $ctx->forward( 'handle_grant_type', [ $grant_type ] );
95 my %data = ( error => 'unsupported_grant_type',
96 error_description => 'Invalid grant type');
97 $ctx->res->body( JSON::XS->new->pretty(1)->encode( \%data ) );
98 }
99  
100 sub handle_grant_type : Private {
101 my ( $self, $ctx, $grant_type ) = @_;
102 }
103  
104  
105 =head2 authorize
106 Authorize endpoint
107 =cut
108 sub authorize
109 :Chained('logged_in_required')
110 :PathPart('authorize') #Configurable?
111 :Args(0)
112 {
113 my ( $self, $ctx ) = @_;
114  
115 if ( $ctx->req->method eq 'GET' ) {
116 $ctx->stash( authorize_endpoint => $ctx->uri_for_action($ctx->action) );
117 $ctx->stash( template => $self->{authorize_form}->{template}
118 || 'oauth/authorize.tt' );
119 }
120  
121 if ( $ctx->req->method eq 'POST' ) {
122  
123 my $uri = $ctx->uri_for( $ctx->req->param("redirect_uri"),
124 { code => $ctx->sessionid,
125 redirect_uri => $ctx->req->param("redirect_uri"),
126 } );
127 $uri =~ m,/(?<http>http://)(?<url>[\w\d:#@%/;$()~_?\+-=\\\.&]*),; #to external URI
128 $ctx->res->redirect( $+{http} . $+{url} );
129 }
130 $ctx->detach();
131 }
132  
133 =pod
134 =cut
« CatalystX/OAuth2/Provider/TraitFor/Controller/OAuth/AuthorizationCode.pm ↑ Summary CatalystX/OAuth2/Provider.pm »