File Coverage

blib/lib/OAuth2/Box.pm
Criterion Covered Total %
statement 34 45 75.5
branch 0 4 0.0
condition 0 3 0.0
subroutine 11 14 78.5
pod 3 3 100.0
total 48 69 69.5


line stmt bran cond sub pod time code
1             package OAuth2::Box;
2              
3             # ABSTRACT: Authorize with Box.com
4              
5 3     3   55057 use strict;
  3         6  
  3         109  
6 3     3   15 use warnings;
  3         4  
  3         88  
7              
8 3     3   2597 use Carp::Assert;
  3         4108  
  3         21  
9 3     3   4070 use HTTP::Tiny;
  3         199825  
  3         133  
10 3     3   2629 use JSON;
  3         37946  
  3         23  
11 3     3   3964 use Moo;
  3         65593  
  3         23  
12 3     3   10151 use Types::Standard qw(Str InstanceOf);
  3         237690  
  3         46  
13 3     3   12568 use URI;
  3         17216  
  3         167  
14              
15             our $VERSION = 0.03;
16              
17 3     3   29 use constant BOX_URL => 'https://www.box.com/api/oauth2/';
  3         30  
  3         2139  
18              
19             has url => ( is => 'ro', isa => Str, required => 1, default => sub { BOX_URL . 'authorize' } );
20             has token_url => ( is => 'ro', isa => Str, required => 1, default => sub { BOX_URL . 'token' } );
21             has client_id => ( is => 'ro', isa => Str, required => 1 );
22             has client_secret => ( is => 'ro', isa => Str, required => 1 );
23             has redirect_uri => ( is => 'ro', isa => Str, required => 1 );
24              
25             has agent => ( is => 'ro', isa => InstanceOf["HTTP::Tiny"], lazy => 1, default => sub { HTTP::Tiny->new } );
26             has jsonp => ( is => 'lazy', isa => InstanceOf["JSON"] );
27              
28 0     0   0 sub _build_jsonp { JSON->new->allow_nonref }
29              
30             sub authorization_uri {
31 3     3 1 1602 my ($self, %param) = @_;
32              
33 3         18 assert( $param{state}, 'need state' );
34              
35 2         32 my $uri = URI->new( $self->url );
36              
37 2         12703 $uri->query_form(
38             client_id => $self->client_id,
39             response_type => 'code',
40             redirect_uri => $self->redirect_uri,
41             state => $param{state},
42             );
43              
44 2         688 return $uri;
45             }
46              
47             sub authorize {
48 1     1 1 70 my ($self, %param) = @_;
49              
50 1         9 assert( $param{code}, 'need code' );
51              
52 0           return $self->_do_request(
53             code => $param{code},
54             grant_type => 'authorization_code',
55             );
56             }
57              
58             sub refresh_token {
59 0     0 1   my ($self, %param) = @_;
60              
61 0           assert( $param{refresh_token}, 'need refresh_token' );
62              
63 0           return $self->_do_request(
64             refresh_token => $param{refresh_token},
65             grant_type => 'refresh_token',
66             );
67             }
68              
69             sub _do_request {
70 0     0     my ($self, %param) = @_;
71              
72 0           my $result = $self->agent->post_form(
73             $self->token_url,
74             {
75             client_id => $self->client_id,
76             client_secret => $self->client_secret,
77             redirect_uri => $self->redirect_uri,
78             %param,
79             },
80             );
81              
82 0 0 0       if ( $result->{success} and $result->{content} ) {
83 0           my $data = $self->jsonp->decode( $result->{content} );
84              
85             return wantarray ?
86 0 0         ( $data->{access_token}, $data ) :
87             $data->{access_token};
88             }
89              
90 0           return;
91             }
92              
93             1;
94              
95             __END__