File: WWWClient.pm

package info (click to toggle)
libgitlab-api-v4-perl 0.27-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,500 kB
  • sloc: perl: 5,960; sh: 838; python: 63; makefile: 12
file content (210 lines) | stat: -rw-r--r-- 4,388 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package GitLab::API::v4::WWWClient;
our $VERSION = '0.27';

=encoding utf8

=head1 NAME

GitLab::API::v4::WWWClient - A client that works against the GitLab web site.

=head1 SYNOPSIS

    use GitLab::API::v4::WWWClient;
    
    my $client = GitLab::API::v4::WWWClient->new(
        url => 'https://git.example.com/',
    );
    
    $client->sign_in( $username, $password );
    
    my $res = $client->get( $path );

=head1 DESCRIPTION

This class makes it possible to interact with the GitLab web site.

=cut

use Carp qw( croak );
use HTTP::Tiny;
use List::Util qw( first );
use Types::Common::String qw( NonEmptySimpleStr );

use Moo;
use strictures 2;
use namespace::clean;

has _session => (
    is       => 'rw',
    init_arg => undef,
);

sub _croak_res {
    my ($verb, $url, $res) = @_;
    
    return if $res->{status} !~ m{^5};

    local $Carp::Internal{ 'GitLab::API::v4::WWWClient' } = 1;

    croak sprintf(
        'Error %sing %s (HTTP %s): %s',
        uc($verb), $url,
        $res->{status}, ($res->{reason} || 'Unknown'),
    );
}

=head1 REQUIRED ARGUMENTS

=head2 url

This is the base URL to your GitLab web site.

=cut

has url => (
    is       => 'ro',
    isa      => NonEmptySimpleStr,
    required => 1,
);

=head1 METHODS

=head2 sign_in

    $client->sign_in( $username, $password );

Signs in the client given the username and password.

=cut

sub sign_in {
    my ($self, $username, $password) = @_;

    my $tiny = HTTP::Tiny->new(
        verify_SSL => 1,
        max_redirect => 0,
    );

    my $base_url = $self->url();
    $base_url =~ s{/$}{};
    my $sign_in_url = "$base_url/users/sign_in";

    my $load_res = $tiny->get( $sign_in_url );

    _croak_res( 'get', $sign_in_url, $load_res );

    my $token = (
        $load_res->{content} =~
        m{name="authenticity_token" value="(.+?)"}
    )[0];

    
    my ($first_session) = do{
        my $set_cookie_headers = $load_res->{headers}->{ 'set-cookie' };
        $set_cookie_headers = [ $set_cookie_headers ] if !ref $set_cookie_headers;
        my $value = first { $_ =~ m{^_gitlab_session=(.*)} } @$set_cookie_headers;
        $value =~ s{^_gitlab_session=}{}r;
    };

    my $submit_res = $tiny->post_form(
        $sign_in_url,
        {
            'utf8'               => '✓',
            'authenticity_token' => $token,
            'user[login]'        => $username,
            'user[password]'     => $password,
            'user[remember_me]'  => 0,
        },
        {
            headers => {
                'Referer' => $sign_in_url,
                'Cookie'  => "_gitlab_session=$first_session",
                'Cookie2' => '$Version="1"',
            },
        },
    );

    _croak_res( 'post', $sign_in_url, $submit_res );

    my ($second_session) = do{
        my $set_cookie_headers = $submit_res->{headers}->{ 'set-cookie' };
        $set_cookie_headers = [ $set_cookie_headers ] if !ref $set_cookie_headers;
        my $value = first { $_ =~ m{^_gitlab_session=(.*)} } @$set_cookie_headers;
        $value =~ s{^_gitlab_session=}{}r;
    };

    my $home_res = $tiny->get(
        $base_url,
        {
            headers => {
                'Referer' => $sign_in_url,
                'Cookie'  => "_gitlab_session=$second_session",
                'Cookie2' => '$Version="1"',
            },
        },
    );

    _croak_res( 'get', $base_url, $home_res );

    my $ok = ( $home_res->{content} =~ m{sign-out-link} ) ? 1 : 0;
    croak 'Failed to sign in' if !$ok;

    $self->_session( $second_session );

    return;
}

=head2 get

    my $res = $client->get( $path );

Gets the path and returns the L<HTTP::Tiny> response hash.

=cut

sub get {
    my ($self, $path) = @_;

    my $tiny = HTTP::Tiny->new(
        verify_SSL => 1,
        max_redirect => 0,
    );

    my $base_url = $self->url();
    $base_url =~ s{/$}{};
    $path =~ s{^/}{};
    my $url = "$base_url/$path";

    my $session = $self->_session();
    my $headers = $session ? {
        'Cookie'  => "_gitlab_session=$session",
        'Cookie2' => '$Version="1"',
    } : {};

    my $res = $tiny->get(
        $url,
        { headers=>$headers },
    );

    _croak_res( 'get', $url, $res );

    return $res;
}

1;
__END__

=head1 SUPPORT

See L<GitLab::API::v4/SUPPORT>.

=head1 AUTHORS

See L<GitLab::API::v4/AUTHORS>.

=head1 LICENSE

See L<GitLab::API::v4/LICENSE>.

=cut