File Coverage

blib/lib/CatalystX/OAuth2/Schema/Result/RefreshToken.pm
Criterion Covered Total %
statement 20 21 95.2
branch n/a
condition n/a
subroutine 8 9 88.8
pod 0 5 0.0
total 28 35 80.0


line stmt bran cond sub pod time code
1             package CatalystX::OAuth2::Schema::Result::RefreshToken;
2 8     8   5375 use warnings;
  8         29  
  8         320  
3 8     8   56 use strict;
  8         66  
  8         237  
4 8     8   54 use parent 'DBIx::Class';
  8         22  
  8         69  
5              
6             # ABSTRACT: A table for registering refresh tokens
7              
8             __PACKAGE__->load_components(qw(Core));
9             __PACKAGE__->table('refresh_token');
10             __PACKAGE__->add_columns(
11             id => { data_type => 'int', is_auto_increment => 1 },
12             code_id => { data_type => 'int', is_nullable => 0 },
13             );
14             __PACKAGE__->set_primary_key(qw(id));
15              
16             __PACKAGE__->add_unique_constraint( [qw(id code_id)] );
17              
18             __PACKAGE__->belongs_to( code => 'CatalystX::OAuth2::Schema::Result::Code' =>
19             { 'foreign.id' => 'self.code_id' } );
20              
21             # this is a has many but will only ever return a single record
22             # because of the constraint on the relationship table
23             __PACKAGE__->has_many(
24             from_access_token_map =>
25             'CatalystX::OAuth2::Schema::Result::AccessTokenToRefreshToken' => {
26             'foreign.refresh_token_id' => 'self.id',
27             'foreign.code_id' => 'self.code_id'
28             }
29             );
30             __PACKAGE__->many_to_many(
31             from_access_token_map_m2m => from_access_token_map => 'access_token' );
32              
33             # this is a has many but will only ever return a single record
34             # because of the constraint on the relationship table
35             __PACKAGE__->has_many(
36             to_access_token_map =>
37             'CatalystX::OAuth2::Schema::Result::RefreshTokenToAccessToken' => {
38             'foreign.refresh_token_id' => 'self.id',
39             'foreign.code_id' => 'self.code_id'
40             }
41             );
42             __PACKAGE__->many_to_many(
43             to_access_token_map_m2m => to_access_token_map => 'access_token' );
44              
45 0     0 0 0 sub from_access_token { shift->from_access_token_map_m2m->first }
46 2     2 0 8114 sub to_access_token { shift->to_access_token_map_m2m->first }
47              
48             sub create_access_token {
49 2     2 0 47 my ($self) = @_;
50 2         47 my $code = $self->code;
51 2         8232 my $token;
52             $self->result_source->storage->txn_do(
53             sub {
54             # create a new token from this refresh token
55 2     2   902 $token = $code->tokens->create(
56             { from_refresh_token_map => [ { refresh_token => $self } ] } );
57              
58             # create a new refresh token and add it to the new token
59 2         18365 my $refresh = $code->refresh_tokens->create( {} );
60 2         6111 $token->to_refresh_token_map->create(
61             { code => $code, refresh_token => $refresh } );
62             }
63 2         12 );
64 2         11953 return $token;
65             }
66              
67             # if we have already created a token from this refresh, de-activate it
68 1     1 0 10969 sub is_active { !shift->to_access_token_map->count }
69              
70 2     2 0 17821 sub as_string { shift->id }
71              
72             1;
73              
74             __END__
75              
76             =pod
77              
78             =head1 NAME
79              
80             CatalystX::OAuth2::Schema::Result::RefreshToken - A table for registering refresh tokens
81              
82             =head1 VERSION
83              
84             version 0.001006
85              
86             =head1 AUTHOR
87              
88             Eden Cardim <edencardim@gmail.com>
89              
90             =head1 COPYRIGHT AND LICENSE
91              
92             This software is copyright (c) 2017 by Suretec Systems Ltd.
93              
94             This is free software; you can redistribute it and/or modify it under
95             the same terms as the Perl 5 programming language system itself.
96              
97             =cut