File: Auth.pm

package info (click to toggle)
libwebservice-dropbox-perl 2.10-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 360 kB
  • sloc: perl: 885; makefile: 2
file content (85 lines) | stat: -rw-r--r-- 2,230 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
package WebService::Dropbox::Auth;
use strict;
use warnings;
use parent qw(Exporter);

our @EXPORT = do {
    no strict 'refs';
    grep { $_ !~ qr{ \A [A-Z]+ \z }xms } keys %{ __PACKAGE__ . '::' };
};

# https://www.dropbox.com/developers/documentation/http/documentation#oauth2-authorize
sub authorize {
    my ($self, $params) = @_;

    $params ||= {};
    $params->{response_type} ||= 'code';

    my $url = URI->new('https://www.dropbox.com/oauth2/authorize');
    $url->query_form(
        client_id => $self->key,
        %$params,
    );
    $url->as_string;
}

# https://www.dropbox.com/developers/documentation/http/documentation#oauth2-token
sub token {
    my ($self, $code, $redirect_uri) = @_;

    my $data = $self->api({
        url => 'https://api.dropboxapi.com/oauth2/token',
        params => {
            client_id     => $self->key,
            client_secret => $self->secret,
            grant_type    => 'authorization_code',
            code          => $code,
            ( $redirect_uri ? ( redirect_uri => $redirect_uri ) : () ),
        },
    });

    if ($data && $data->{access_token}) {
        $self->access_token($data->{access_token});
    }

    $data;
}

# https://www.dropbox.com/developers/documentation/http/documentation#oauth2-token
# This uses the token method with a refresh token to get an updated access token.
sub refresh_access_token {
    my ($self, $refresh_token) = @_;

    my $data = $self->api({
        url => 'https://api.dropboxapi.com/oauth2/token',
        params => {
            client_id     => $self->key,
            client_secret => $self->secret,
            grant_type    => 'refresh_token',
            refresh_token => $refresh_token,
        },
    });

    # at this point the access token should be in $data
    # so set it like WebService::Dropbox::Auth::token does
    if ($data && $data->{access_token}) {
        $self->access_token($data->{access_token});
    }

    $data;
}

# https://www.dropbox.com/developers/documentation/http/documentation#auth-token-revoke
sub revoke {
    my ($self) = @_;

    my $res = $self->api({
        url => 'https://api.dropboxapi.com/2/auth/token/revoke',
    });

    $self->access_token(undef);

    $res;
}

1;