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