File Coverage

blib/lib/Catmandu/Store/Datahub/OAuth.pm
Criterion Covered Total %
statement 12 26 46.1
branch 0 2 0.0
condition n/a
subroutine 4 7 57.1
pod 0 2 0.0
total 16 37 43.2


line stmt bran cond sub pod time code
1             package Catmandu::Store::Datahub::OAuth;
2              
3 1     1   5 use LWP::UserAgent;
  1         3  
  1         26  
4 1     1   5 use JSON;
  1         2  
  1         7  
5 1     1   114 use Moo;
  1         1  
  1         7  
6              
7 1     1   300 use Catmandu::Sane;
  1         3  
  1         8  
8              
9             has url => (is => 'ro', required => 1);
10              
11             has username => (is => 'ro', required => 1);
12             has password => (is => 'ro', required => 1);
13              
14             has client_id => (is => 'ro', required => 1);
15             has client_secret => (is => 'ro', required => 1);
16              
17             has ua => (is => 'lazy');
18              
19              
20             sub _build_ua {
21 0     0     my $self = shift;
22 0           return LWP::UserAgent->new();
23             }
24              
25             sub token {
26 0     0 0   my $self = shift;
27             # Replace this with something better as soon as we figure out how we can get rid of the password
28 0           return $self->get_token_u_p();
29             }
30              
31             sub get_token_u_p {
32 0     0 0   my ($self) = @_;
33 0           my $auth_url = '%s/oauth/v2/token?grant_type=password&username=%s&password=%s&client_id=%s&client_secret=%s';
34 0           my $req_url = sprintf($auth_url, $self->url, $self->username, $self->password, $self->client_id, $self->client_secret);
35 0           my $response = $self->ua->get($req_url);
36 0 0         if ($response->is_success) {
37 0           my $token_raw = $response->decoded_content;
38 0           my $token_parsed = decode_json($token_raw);
39 0           return $token_parsed->{'access_token'};
40             } else {
41 0           Catmandu::HTTPError->throw({
42             code => $response->code,
43             message => $response->status_line,
44             url => $response->request->uri,
45             method => $response->request->method,
46             request_headers => [],
47             request_body => $response->request->decoded_content,
48             response_headers => [],
49             response_body => $response->decoded_content,
50             });
51 0           return undef;
52             }
53             }
54              
55              
56              
57             1;