File Coverage

blib/lib/Catalyst/ActionRole/OAuth2/ProtectedResource.pm
Criterion Covered Total %
statement 12 14 85.7
branch 3 4 75.0
condition 2 6 33.3
subroutine 3 3 100.0
pod 0 1 0.0
total 20 28 71.4


line stmt bran cond sub pod time code
1             package Catalyst::ActionRole::OAuth2::ProtectedResource;
2 8     8   156795 use Moose::Role;
  8         22  
  8         90  
3 8     8   53727 use CatalystX::OAuth2::Request::ProtectedResource;
  8         50  
  8         1572  
4              
5             # ABSTRACT: Resource endpoint for OAuth2 authentication flows
6              
7              
8             with 'CatalystX::OAuth2::ActionRole::RequestInjector';
9              
10             sub build_oauth2_request {
11 5     5 0 20 my ( $self, $controller, $c ) = @_;
12              
13 5 100       24 my $auth = $c->req->header('Authorization')
14             or $c->res->status(401), $c->detach;
15 3         746 my ( $type, $token ) = split ' ', $auth;
16              
17 3   33     223 my $is_valid = defined($token)
18             && length($token);
19              
20 3 50 33     151 if ( $is_valid
21             and my $token_obj = $controller->store->verify_client_token($token) )
22             {
23 3         61 return CatalystX::OAuth2::Request::ProtectedResource->new(
24             token => $token_obj );
25             }
26 0           $c->res->status(401);
27 0           $c->detach;
28             }
29              
30             1;
31              
32             __END__
33              
34             =pod
35              
36             =head1 NAME
37              
38             Catalyst::ActionRole::OAuth2::ProtectedResource - Resource endpoint for OAuth2 authentication flows
39              
40             =head1 VERSION
41              
42             version 0.001007
43              
44             =head1 SYNOPSIS
45              
46             package AuthServer::Controller::OAuth2::Resource;
47             use Moose;
48              
49             BEGIN { extends 'Catalyst::Controller::ActionRole' }
50              
51             with 'CatalystX::OAuth2::Controller::Role::WithStore';
52              
53             __PACKAGE__->config(
54             store => {
55             class => 'DBIC',
56             client_model => 'DB::Client'
57             }
58             );
59              
60             sub resource : Chained('/') Args(0) Does('OAuth2::ProtectedResource') {
61             my ( $self, $c ) = @_;
62             $c->res->body( 'my protected resource' );
63             }
64              
65             =head1 DESCRIPTION
66              
67             This action role implements an arbitrary resource endpoint to be protected by
68             the authorization flow. Clients will only be able to access this resource if
69             they provide a valid access token. The action body should be customized like a
70             regular action.
71              
72             =head1 AUTHOR
73              
74             Eden Cardim <edencardim@gmail.com>
75              
76             =head1 COPYRIGHT AND LICENSE
77              
78             This software is copyright (c) 2017 by Suretec Systems Ltd.
79              
80             This is free software; you can redistribute it and/or modify it under
81             the same terms as the Perl 5 programming language system itself.
82              
83             =cut