File Coverage

blib/lib/Docker/Registry/Auth/Gitlab.pm
Criterion Covered Total %
statement 34 35 97.1
branch 1 2 50.0
condition n/a
subroutine 9 9 100.0
pod 2 2 100.0
total 46 48 95.8


line stmt bran cond sub pod time code
1             package Docker::Registry::Auth::Gitlab;
2 1     1   27772 use Moo;
  1         2  
  1         9  
3 1     1   421 use Types::Standard qw/Str/;
  1         3  
  1         10  
4 1     1   678 use namespace::autoclean;
  1         3  
  1         8  
5              
6             # ABSTRACT: Authentication module for gitlab registry
7              
8             with 'Docker::Registry::Auth';
9              
10 1     1   557 use Docker::Registry::Types qw(DockerRegistryURI);
  1         3  
  1         11  
11 1     1   1068 use HTTP::Tiny;
  1         53087  
  1         51  
12 1     1   504 use JSON::MaybeXS qw(decode_json);
  1         5863  
  1         397  
13              
14             has username => (
15             is => 'ro',
16             isa => Str,
17             required => 1,
18             );
19              
20             has access_token => (
21             is => 'ro',
22             isa => Str,
23             required => 1,
24             );
25              
26             has jwt => (
27             is => 'ro',
28             isa => DockerRegistryURI,
29             coerce => 1,
30             default => 'https://gitlab.com/jwt/auth',
31             );
32              
33             sub _build_token_uri {
34 3     3   9628 my ($self, $scope) = @_;
35              
36 3         52 my $uri = $self->jwt->clone;
37              
38 3         102 $uri->query_form({
39             service => 'container_registry',
40             scope => $scope,
41             client_id => 'docker',
42             offline_token => 'true',
43             });
44              
45 3         435 $uri->userinfo(join(':', $self->username, $self->access_token));
46 3         234 return $uri;
47             }
48              
49             sub get_bearer_token {
50 2     2 1 13405 my ($self, $scope) = @_;
51              
52 2         8 my $uri = $self->_build_token_uri($scope);
53              
54 2         12 my $ua = HTTP::Tiny->new();
55 2         192 my $res = $ua->get($uri);
56              
57 2 50       15 if ($res->{success}) {
58 2         35 return decode_json($res->{content})->{token};
59             }
60              
61 0         0 die "Unable to get token from gitlab!";
62             }
63              
64             sub authorize {
65 1     1 1 117 my ($self, $request, $scope) = @_;
66              
67 1         4 my $bearer_token = $self->get_bearer_token($scope);
68              
69 1         15 $request->header('Authorization', 'Bearer ' . $bearer_token);
70 1         112 $request->header('Accept',
71             'application/vnd.docker.distribution.manifest.v2+json');
72              
73 1         50 return $request;
74             }
75              
76             __PACKAGE__->meta->make_immutable;
77              
78             __END__