File Coverage

blib/lib/CatalystX/OAuth2/Store/DBIC.pm
Criterion Covered Total %
statement 38 54 70.3
branch 12 26 46.1
condition n/a
subroutine 12 17 70.5
pod 0 12 0.0
total 62 109 56.8


line stmt bran cond sub pod time code
1             package CatalystX::OAuth2::Store::DBIC;
2 8     8   5975 use Moose;
  8         22  
  8         70  
3 8     8   63305 use Moose::Util::TypeConstraints;
  8         21  
  8         84  
4              
5             # ABSTRACT: An interface to a DBIC-based OAuth2 store
6              
7             with 'CatalystX::OAuth2::Store';
8              
9             has app => ( is => 'ro', required => 1 );
10             has client_model => (
11             isa => 'Str',
12             is => 'ro',
13             required => 1
14             );
15             has _client_model => (
16             isa => 'DBIx::Class::ResultSet',
17             is => 'ro',
18             lazy_build => 1
19             );
20             has endpoint_field => ( isa => 'Str', is => 'ro', default => 'endpoint' );
21             has refresh_relation =>
22             ( isa => 'Str', is => 'ro', default => 'refresh_tokens' );
23             has token_relation => ( isa => 'Str', is => 'ro', default => 'tokens' );
24             has code_relation => ( isa => 'Str', is => 'ro', default => 'codes' );
25             has code_activation_field =>
26             ( isa => 'Str', is => 'ro', default => 'is_active' );
27              
28             sub _build__client_model {
29 9     9   33 my ($self) = @_;
30 9         328 return $self->app->model( $self->client_model );
31             }
32              
33             sub find_client {
34 18     18 0 76 my ( $self, $id ) = @_;
35 18         691 $self->_client_model->find($id);
36             }
37              
38             sub client_endpoint {
39 0     0 0 0 my ( $self, $id ) = @_;
40 0 0       0 my $client = $self->find_client($id)
41             or return;
42 0         0 return $client->get_column( $self->endpoint_field );
43             }
44              
45             sub _code_rs {
46 14     14   47 my ( $self, $id ) = @_;
47 14 100       400 return $self->_client_model->related_resultset( $self->code_relation )
48             unless defined($id);
49 3 50       12 my $client = $self->find_client($id)
50             or return;
51 3         8495 return $client->related_resultset( $self->code_relation );
52             }
53              
54             sub create_client_code {
55 3     3 0 12 my ( $self, $id ) = @_;
56 3         13 $self->_code_rs($id)->create( {} );
57             }
58              
59             sub find_client_code {
60 11     11 0 47 my ( $self, $code, $id ) = @_;
61 11 100       78 return $id
62             ? $self->_code_rs->find($code)
63             : $self->_code_rs($id)->find($code);
64             }
65              
66             sub activate_client_code {
67 0     0 0 0 my ( $self, $id, $code ) = @_;
68 0 0       0 my $code_row = $self->find_client_code( $id, $code )
69             or return;
70 0         0 $code_row->update( { $self->code_activation_field => 1 } );
71             }
72              
73             sub deactivate_client_code {
74 0     0 0 0 my ( $self, $id, $code ) = @_;
75 0 0       0 my $code_row = $self->find_client_code( $id, $code )
76             or return;
77 0         0 $code_row->update( { $self->code_activation_field => 0 } );
78             }
79              
80             sub client_code_is_active {
81 0     0 0 0 my ( $self, $id ) = @_;
82 0 0       0 my $client = $self->find_client($id)
83             or return;
84 0         0 return $client->get_column( $self->code_activation_field );
85             }
86              
87             sub create_access_token {
88 3     3 0 13 my ( $self, $code, $with_refresh ) = @_;
89 3 50       14 my $code_row = $self->find_client_code($code)
90             or return;
91 3 100       16475 return $code_row->related_resultset( $self->token_relation )->create( {} )
92             unless $with_refresh;
93 1         4 my $token;
94             $code_row->result_source->storage->dbh_do(
95             sub {
96 1     1   472 my $refresh = $code_row->related_resultset( $self->refresh_relation )->create( {} );
97 1         5511 $token = $refresh->create_access_token;
98             }
99 1         9 );
100 1         147 return $token;
101             }
102              
103             sub create_access_token_from_refresh {
104 1     1 0 6 my ( $self, $refresh ) = @_;
105 1 50       47 my $refresh_row = $self->_client_model->find_refresh($refresh)
106             or return;
107 1         14370 return $refresh_row->create_access_token;
108             }
109              
110             sub find_code_from_refresh {
111 1     1 0 4 my ( $self, $refresh ) = @_;
112 1 50       31 my $refresh_row = $self->_client_model->find_refresh($refresh)
113             or return;
114 1         12821 return $refresh_row->code;
115             }
116              
117             sub verify_client_secret {
118 0     0 0 0 my ( $self, $client_id, $access_secret ) = @_;
119 0         0 my $client = $self->find_client($client_id);
120 0         0 return $client->client_secret eq $access_secret;
121             }
122              
123             sub verify_client_token {
124 3     3 0 15 my ( $self, $access_token ) = @_;
125 3         131 my $rs = $self->_client_model;
126 3 50       16 return 0 unless defined $access_token;
127 3         115 my $token_rs = $rs->related_resultset( $self->code_relation )
128             ->related_resultset( $self->token_relation );
129 3 50       3739 if(my $token = $token_rs->find($access_token)) {
130 3         29013 return $token;
131             }
132 0           return 0;
133             }
134              
135             1;
136              
137             __END__
138              
139             =pod
140              
141             =head1 NAME
142              
143             CatalystX::OAuth2::Store::DBIC - An interface to a DBIC-based OAuth2 store
144              
145             =head1 VERSION
146              
147             version 0.001007
148              
149             =head1 AUTHOR
150              
151             Eden Cardim <edencardim@gmail.com>
152              
153             =head1 COPYRIGHT AND LICENSE
154              
155             This software is copyright (c) 2017 by Suretec Systems Ltd.
156              
157             This is free software; you can redistribute it and/or modify it under
158             the same terms as the Perl 5 programming language system itself.
159              
160             =cut