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   1438 use strict;
  2         2  
  2         42  
3 2     2   6 use warnings;
  2         2  
  2         38  
4 2     2   4 use parent 'OAuth::Lite2::Server::GrantHandler';
  2         2  
  2         6  
5              
6 2     2   64 use Carp ();
  2         2  
  2         18  
7 2     2   6 use OAuth::Lite2::Server::Error;
  2         2  
  2         28  
8 2     2   362 use OAuth::Lite2::ParamMethod::AuthHeader;
  2         2  
  2         476  
9              
10             sub handle_request {
11 7     7 1 189 my ($self, $dh) = @_;
12              
13 7         14 my $req = $dh->request;
14              
15 7         21 my $parser = OAuth::Lite2::ParamMethod::AuthHeader->new;
16 7         13 my $header_credentials = $parser->basic_credentials($req);
17 7 50       19 my $client_id = ($header_credentials->{client_id}) ? $header_credentials->{client_id} : $req->param("client_id");
18              
19             # validate grouping refresh_token
20 7 100       996 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       51 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     60 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       11 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       26 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       15 OAuth::Lite2::Server::Error::InvalidRequest->throw(
45             description => "group id does not match"
46             ) unless ( $group_id eq $target_group_id );
47              
48 2         4 my $scope = $req->param("scope");
49 2 100       15 OAuth::Lite2::Server::Error::InvalidScope->throw
50             unless $dh->validate_grouping_scope( $client_id, $scope );
51              
52             # create response
53 1         14 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     15 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         3 my $access_token = $dh->create_or_update_access_token(
63             auth_info => $auth_info,
64             );
65 1 50 33     17 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         4 my $res = {
70             token_type => 'Bearer',
71             access_token => $access_token->token,
72             };
73 1 50       6 $res->{expires_in} = int($access_token->expires_in)
74             if $access_token->expires_in;
75 1 50       9 $res->{refresh_token} = $auth_info->refresh_token
76             if $auth_info->refresh_token;
77 1 50       7 $res->{scope} = $auth_info->scope
78             if $auth_info->scope;
79              
80 1         15 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;