File Coverage

blib/lib/OAuth/Lite2/Server/GrantHandler/GroupingRefreshToken.pm
Criterion Covered Total %
statement 40 40 100.0
branch 19 26 73.0
condition 3 9 33.3
subroutine 7 7 100.0
pod 1 1 100.0
total 70 83 84.3


line stmt bran cond sub pod time code
1             package OAuth::Lite2::Server::GrantHandler::GroupingRefreshToken;
2 2     2   2058 use strict;
  2         3  
  2         84  
3 2     2   10 use warnings;
  2         3  
  2         45  
4 2     2   10 use parent 'OAuth::Lite2::Server::GrantHandler';
  2         2  
  2         14  
5              
6 2     2   87 use Carp ();
  2         4  
  2         33  
7 2     2   10 use OAuth::Lite2::Server::Error;
  2         3  
  2         45  
8 2     2   522 use OAuth::Lite2::ParamMethod::AuthHeader;
  2         5  
  2         960  
9              
10             sub handle_request {
11 7     7 1 217 my ($self, $dh) = @_;
12              
13 7         26 my $req = $dh->request;
14              
15 7         30 my $parser = OAuth::Lite2::ParamMethod::AuthHeader->new;
16 7         22 my $header_credentials = $parser->basic_credentials($req);
17 7 50       30 my $client_id = ($header_credentials->{client_id}) ? $header_credentials->{client_id} : $req->param("client_id");
18              
19             # validate grouping refresh_token
20 7 100       1538 my $refresh_token = $req->param("refresh_token")
21             or OAuth::Lite2::Server::Error::InvalidRequest->throw(
22             description => "'refresh_token' not found"
23             );
24              
25 6 100       71 my $grouping_auth_info = $dh->get_auth_info_by_refresh_token($refresh_token)
26             or OAuth::Lite2::Server::Error::InvalidGrant->throw(
27             description => "'refresh_token' is invalid"
28             );
29 5 50 33     87 Carp::croak "OAuth::Lite2::Server::DataHandler::get_auth_info_by_refresh_token doesn't return OAuth::Lite2::Model::AuthInfo"
30             unless ($grouping_auth_info
31             && $grouping_auth_info->isa("OAuth::Lite2::Model::AuthInfo"));
32              
33 5 100       15 my $group_id = $dh->get_group_id_by_client_id( $grouping_auth_info->client_id )
34             or OAuth::Lite2::Server::Error::InvalidGrant->throw(
35             description => "'refresh_token' does not have group id"
36             );
37              
38             # validate target client
39 4 100       41 my $target_group_id = $dh->get_group_id_by_client_id( $client_id )
40             or OAuth::Lite2::Server::Error::InvalidClient->throw(
41             description => "'client_id' does not have group id"
42             );
43              
44 3 100       25 OAuth::Lite2::Server::Error::InvalidRequest->throw(
45             description => "group id does not match"
46             ) unless ( $group_id eq $target_group_id );
47              
48 2         7 my $scope = $req->param("scope");
49 2 100       25 OAuth::Lite2::Server::Error::InvalidScope->throw
50             unless $dh->validate_grouping_scope( $client_id, $scope );
51              
52             # create response
53 1         15 my $auth_info = $dh->create_or_update_auth_info(
54             client_id => $client_id,
55             user_id => $grouping_auth_info->user_id,
56             scope => $scope,
57             );
58 1 50 33     16 Carp::croak "OAuth::Lite2::Server::DataHandler::create_or_update_auth_info doesn't return OAuth::Lite2::Model::AuthInfo"
59             unless ($auth_info
60             && $auth_info->isa("OAuth::Lite2::Model::AuthInfo"));
61              
62 1         5 my $access_token = $dh->create_or_update_access_token(
63             auth_info => $auth_info,
64             );
65 1 50 33     18 Carp::croak "OAuth::Lite2::Server::DataHandler::create_or_update_access_token doesn't return OAuth::Lite2::Model::AccessToken"
66             unless ($access_token
67             && $access_token->isa("OAuth::Lite2::Model::AccessToken"));
68              
69 1         6 my $res = {
70             token_type => 'Bearer',
71             access_token => $access_token->token,
72             };
73 1 50       8 $res->{expires_in} = $access_token->expires_in
74             if $access_token->expires_in;
75 1 50       12 $res->{refresh_token} = $auth_info->refresh_token
76             if $auth_info->refresh_token;
77 1 50       11 $res->{scope} = $auth_info->scope
78             if $auth_info->scope;
79              
80 1         57 return $res;
81             }
82              
83             =head1 NAME
84              
85             OAuth::Lite2::Server::GrantHandler::GroupingRefreshToken - handler for 'grouping-refresh-token' grant_type request
86              
87             =head1 SYNOPSIS
88              
89             my $handler = OAuth::Lite2::Server::GrantHandler::GroupingRefreshToken->new;
90             my $res = $handler->handle_request( $data_handler );
91              
92             =head1 DESCRIPTION
93              
94             handler for 'grouping-refresh-token' grant_type request.
95              
96             =head1 METHODS
97              
98             =head2 handle_request( $req )
99              
100             See L document.
101              
102             =head1 AUTHOR
103              
104             Lyo Kato, Elyo.kato@gmail.comE
105              
106             =head1 COPYRIGHT AND LICENSE
107              
108             Copyright (C) 2010 by Lyo Kato
109              
110             This library is free software; you can redistribute it and/or modify
111             it under the same terms as Perl itself, either Perl version 5.8.8 or,
112             at your option, any later version of Perl 5 you may have available.
113              
114             =cut
115              
116             1;