| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
|
|
2
|
|
|
|
|
|
|
use strict; |
|
3
|
2
|
|
|
2
|
|
921494
|
use 5.008_005; |
|
|
2
|
|
|
|
|
11
|
|
|
|
2
|
|
|
|
|
46
|
|
|
4
|
2
|
|
|
2
|
|
49
|
our $VERSION = '0.18'; |
|
|
2
|
|
|
|
|
7
|
|
|
5
|
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
use Dancer2::Plugin; |
|
7
|
2
|
|
|
2
|
|
945
|
use Module::Load; |
|
|
2
|
|
|
|
|
228166
|
|
|
|
2
|
|
|
|
|
12
|
|
|
8
|
2
|
|
|
2
|
|
39427
|
|
|
|
2
|
|
|
|
|
941
|
|
|
|
2
|
|
|
|
|
23
|
|
|
9
|
|
|
|
|
|
|
# setup the plugin |
|
10
|
|
|
|
|
|
|
on_plugin_import { |
|
11
|
|
|
|
|
|
|
my $dsl = shift; |
|
12
|
|
|
|
|
|
|
my $settings = plugin_setting; |
|
13
|
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
$settings->{prefix} ||= '/auth'; |
|
15
|
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
for my $provider ( keys %{$settings->{providers} || {}} ) { |
|
17
|
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
# load the provider plugin |
|
19
|
|
|
|
|
|
|
my $provider_class = __PACKAGE__."::Provider::".$provider; |
|
20
|
|
|
|
|
|
|
eval { load $provider_class; 1; } or do { |
|
21
|
|
|
|
|
|
|
$dsl->app->log(debug => "Couldn't load $provider_class"); |
|
22
|
|
|
|
|
|
|
next; |
|
23
|
|
|
|
|
|
|
}; |
|
24
|
|
|
|
|
|
|
$dsl->app->{_oauth}{$provider} ||= $provider_class->new($settings); |
|
25
|
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
# add the routes |
|
27
|
|
|
|
|
|
|
$dsl->app->add_route( |
|
28
|
|
|
|
|
|
|
method => 'get', |
|
29
|
|
|
|
|
|
|
regexp => sprintf( "%s/%s", $settings->{prefix}, lc($provider) ), |
|
30
|
|
|
|
|
|
|
code => sub { |
|
31
|
|
|
|
|
|
|
$dsl->app->redirect( |
|
32
|
|
|
|
|
|
|
$dsl->app->{_oauth}{$provider}->authentication_url( |
|
33
|
|
|
|
|
|
|
$dsl->app->request->uri_base |
|
34
|
|
|
|
|
|
|
) |
|
35
|
|
|
|
|
|
|
) |
|
36
|
|
|
|
|
|
|
}, |
|
37
|
|
|
|
|
|
|
); |
|
38
|
|
|
|
|
|
|
$dsl->app->add_route( |
|
39
|
|
|
|
|
|
|
method => 'get', |
|
40
|
|
|
|
|
|
|
regexp => sprintf( "%s/%s/callback", $settings->{prefix}, lc($provider) ), |
|
41
|
|
|
|
|
|
|
code => sub { |
|
42
|
|
|
|
|
|
|
my $redirect; |
|
43
|
|
|
|
|
|
|
if( $dsl->app->{_oauth}{$provider}->callback($dsl->app->request, $dsl->app->session) ) { |
|
44
|
|
|
|
|
|
|
$redirect = $settings->{success_url} || '/'; |
|
45
|
|
|
|
|
|
|
} else { |
|
46
|
|
|
|
|
|
|
$redirect = $settings->{error_url} || '/'; |
|
47
|
|
|
|
|
|
|
} |
|
48
|
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
$dsl->app->redirect( $redirect ); |
|
50
|
|
|
|
|
|
|
}, |
|
51
|
|
|
|
|
|
|
); |
|
52
|
|
|
|
|
|
|
} |
|
53
|
|
|
|
|
|
|
}; |
|
54
|
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
register_plugin; |
|
56
|
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
1; |
|
58
|
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
=encoding utf-8 |
|
60
|
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
=head1 NAME |
|
62
|
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
Dancer2::Plugin::Auth::OAuth - OAuth for your Dancer2 app |
|
64
|
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
66
|
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
# just 'use' the plugin, that's all. |
|
68
|
|
|
|
|
|
|
use Dancer2::Plugin::Auth::OAuth; |
|
69
|
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
71
|
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
Dancer2::Plugin::Auth::OAuth is a Dancer2 plugin which tries to make OAuth |
|
73
|
|
|
|
|
|
|
authentication easy. |
|
74
|
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
The module is highly influenced by L<Plack::Middleware::OAuth> and Dancer 1 |
|
76
|
|
|
|
|
|
|
OAuth modules, but unlike the Dancer 1 versions, this plugin only needs |
|
77
|
|
|
|
|
|
|
configuration (look mom, no code needed!). It automatically sets up the |
|
78
|
|
|
|
|
|
|
needed routes (defaults to C</auth/$provider> and C</auth/$provider/callback>). |
|
79
|
|
|
|
|
|
|
So if you define the Twitter provider in your config, you should automatically |
|
80
|
|
|
|
|
|
|
get C</auth/twitter> and C</auth/twitter/callback>. |
|
81
|
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
After a successful OAuth dance, the user info is stored in the session. What |
|
83
|
|
|
|
|
|
|
you do with it afterwards is up to you. |
|
84
|
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
=head1 CONFIGURATION |
|
86
|
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
The plugin comes with support for Facebook, Google, Twitter, GitHub, Stack |
|
88
|
|
|
|
|
|
|
Exchange and LinkedIn (other providers aren't hard to add, send me a pull |
|
89
|
|
|
|
|
|
|
request when you add more!) |
|
90
|
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
All it takes to use OAuth authentication for a given provider, is to add |
|
92
|
|
|
|
|
|
|
the configuration for it. |
|
93
|
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
The YAML below shows all available options. |
|
95
|
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
plugins: |
|
97
|
|
|
|
|
|
|
"Auth::OAuth": |
|
98
|
|
|
|
|
|
|
prefix: /auth [*] |
|
99
|
|
|
|
|
|
|
success_url: / [*] |
|
100
|
|
|
|
|
|
|
error_url: / [*] |
|
101
|
|
|
|
|
|
|
providers: |
|
102
|
|
|
|
|
|
|
Facebook: |
|
103
|
|
|
|
|
|
|
tokens: |
|
104
|
|
|
|
|
|
|
client_id: your_client_id |
|
105
|
|
|
|
|
|
|
client_secret: your_client_secret |
|
106
|
|
|
|
|
|
|
fields: id,email,name,gender,picture |
|
107
|
|
|
|
|
|
|
Google: |
|
108
|
|
|
|
|
|
|
tokens: |
|
109
|
|
|
|
|
|
|
client_id: your_client_id |
|
110
|
|
|
|
|
|
|
client_secret: your_client_secret |
|
111
|
|
|
|
|
|
|
Twitter: |
|
112
|
|
|
|
|
|
|
tokens: |
|
113
|
|
|
|
|
|
|
consumer_key: your_consumer_token |
|
114
|
|
|
|
|
|
|
consumer_secret: your_consumer_secret |
|
115
|
|
|
|
|
|
|
Github: |
|
116
|
|
|
|
|
|
|
tokens: |
|
117
|
|
|
|
|
|
|
client_id: your_client_id |
|
118
|
|
|
|
|
|
|
client_secret: your_client_secret |
|
119
|
|
|
|
|
|
|
Stackexchange: |
|
120
|
|
|
|
|
|
|
tokens: |
|
121
|
|
|
|
|
|
|
client_id: your_client_id |
|
122
|
|
|
|
|
|
|
client_secret: your_client_secret |
|
123
|
|
|
|
|
|
|
key: your_key |
|
124
|
|
|
|
|
|
|
site: stackoverflow |
|
125
|
|
|
|
|
|
|
Linkedin: |
|
126
|
|
|
|
|
|
|
tokens: |
|
127
|
|
|
|
|
|
|
client_id: your_client_id |
|
128
|
|
|
|
|
|
|
client_secret: your_client_secret |
|
129
|
|
|
|
|
|
|
fields: id,num-connections,picture-url,email-address |
|
130
|
|
|
|
|
|
|
VKontakte: # https://vk.com |
|
131
|
|
|
|
|
|
|
tokens: |
|
132
|
|
|
|
|
|
|
client_id: '...' |
|
133
|
|
|
|
|
|
|
client_secret: '...' |
|
134
|
|
|
|
|
|
|
fields: 'first_name,last_name,about,bdate,city,country,photo_max_orig,sex,site' |
|
135
|
|
|
|
|
|
|
api_version: '5.8' |
|
136
|
|
|
|
|
|
|
Odnoklassniki: # https://ok.ru |
|
137
|
|
|
|
|
|
|
tokens: |
|
138
|
|
|
|
|
|
|
client_id: your_client_id |
|
139
|
|
|
|
|
|
|
client_secret: your_client_secret |
|
140
|
|
|
|
|
|
|
application_key: your_application_key |
|
141
|
|
|
|
|
|
|
method: 'users.getCurrentUser' |
|
142
|
|
|
|
|
|
|
format: 'json' |
|
143
|
|
|
|
|
|
|
fields: 'email,name,gender,birthday,location,uid,pic_full' |
|
144
|
|
|
|
|
|
|
MailRU: |
|
145
|
|
|
|
|
|
|
tokens: |
|
146
|
|
|
|
|
|
|
client_id: your_client_id |
|
147
|
|
|
|
|
|
|
client_private: your_client_private |
|
148
|
|
|
|
|
|
|
client_secret: your_client_secret |
|
149
|
|
|
|
|
|
|
method: 'users.getInfo' |
|
150
|
|
|
|
|
|
|
format: 'json' |
|
151
|
|
|
|
|
|
|
secure: 1 |
|
152
|
|
|
|
|
|
|
Yandex: |
|
153
|
|
|
|
|
|
|
tokens: |
|
154
|
|
|
|
|
|
|
client_id: your_client_id |
|
155
|
|
|
|
|
|
|
client_secret: your_client_secret |
|
156
|
|
|
|
|
|
|
format: 'json' |
|
157
|
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
[*] default value, may be omitted. |
|
159
|
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
=head1 AUTHOR |
|
161
|
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
Menno Blom E<lt>blom@cpan.orgE<gt> |
|
163
|
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
=head1 COPYRIGHT |
|
165
|
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
Copyright 2014- Menno Blom |
|
167
|
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
=head1 LICENSE |
|
169
|
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or modify |
|
171
|
|
|
|
|
|
|
it under the same terms as Perl itself. |
|
172
|
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
=cut |