| 1 | package Dancer2::Plugin::Auth::OAuth::Provider::VKontakte; | ||
| 2 | |||
| 3 | # It's a rewritten 'Facebook.pm' to work with 'https://vk.com'. | ||
| 4 | # | ||
| 5 | # To configure this add something like: | ||
| 6 | # | ||
| 7 | # VKontakte: | ||
| 8 | # tokens: | ||
| 9 | # client_id: '...' | ||
| 10 | # client_secret: '...' | ||
| 11 | # fields: '...' | ||
| 12 | # api_version: '5.8' | ||
| 13 | # | ||
| 14 | # to your providers configuration. | ||
| 15 | |||
| 16 | use strict; | ||
| 17 | use parent 'Dancer2::Plugin::Auth::OAuth::Provider'; | ||
| 18 | |||
| 19 | use HTTP::Request::Common; | ||
| 20 | |||
| 21 | sub config { { | ||
| 22 | version => 2, | ||
| 23 | urls => { | ||
| 24 | authorize_url => 'https://oauth.vk.com/authorize', | ||
| 25 | access_token_url => 'https://oauth.vk.com/access_token', | ||
| 26 | user_info => 'https://api.vk.com/method/users.get', | ||
| 27 | }, | ||
| 28 | query_params => { | ||
| 29 | authorize => { | ||
| 30 | response_type => 'code', | ||
| 31 | # VKontakte sends user's email if exists with access token reply. | ||
| 32 | # Because of this I had to modify 'Provider.pm' slightly. | ||
| 33 | # If you want the email to be sent you must use 'email' scope. | ||
| 34 | # Other user data should be requested with 'fields' configuration option. | ||
| 35 | scope => 'email', | ||
| 36 | display => 'popup', | ||
| 37 | } | ||
| 38 | } | ||
| 39 | } } | ||
| 40 | |||
| 41 | sub post_process { | ||
| 42 | my ($self, $session) = @_; | ||
| 43 | |||
| 44 | my $fields = ''; | ||
| 45 | if ( exists $self->provider_settings->{fields} ) { | ||
| 46 | $fields = "&fields=".$self->provider_settings->{fields}; | ||
| 47 | } | ||
| 48 | |||
| 49 | my $user_id = ''; | ||
| 50 | if ( exists $session->read('oauth')->{'user_id'} ) { | ||
| 51 | $user_id = "&user_ids=".$session->read('oauth')->{'user_id'}; | ||
| 52 | } | ||
| 53 | |||
| 54 | my $api_version = ''; | ||
| 55 | if ( exists $self->provider_settings->{api_version} ) { | ||
| 56 | $api_version = "&v=".$self->provider_settings->{api_version}; | ||
| 57 | } | ||
| 58 | |||
| 59 | my $session_data = $session->read('oauth'); | ||
| 60 | |||
| 61 | my $resp = $self->{ua}->request( | ||
| 62 | GET $self->provider_settings->{urls}{user_info}."?access_token=". | ||
| 63 | $session_data->{vkontakte}{access_token}. | ||
| 64 | $fields. | ||
| 65 | $user_id. | ||
| 66 | $api_version | ||
| 67 | ); | ||
| 68 | |||
| 69 | if( $resp->is_success ) { | ||
| 70 | my $user = $self->_stringify_json_booleans( | ||
| 71 | JSON::MaybeXS::decode_json( $resp->content ) | ||
| 72 | ); | ||
| 73 | $session_data->{vkontakte}{user_info} = $user; | ||
| 74 | $session->write('oauth', $session_data); | ||
| 75 | } | ||
| 76 | |||
| 77 | } | ||
| 78 | |||
| 79 | 1; |