| 1 | package Dancer2::Plugin::Auth::OAuth::Provider::Yandex; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use parent 'Dancer2::Plugin::Auth::OAuth::Provider'; | ||
| 5 | |||
| 6 | use HTTP::Request; | ||
| 7 | use JSON::MaybeXS; | ||
| 8 | use LWP::UserAgent; | ||
| 9 | |||
| 10 | sub config { { | ||
| 11 | version => 2, | ||
| 12 | urls => { | ||
| 13 | authorize_url => 'https://oauth.yandex.ru/authorize', | ||
| 14 | access_token_url => 'https://oauth.yandex.ru/token', | ||
| 15 | user_info => 'https://login.yandex.ru/info', | ||
| 16 | }, | ||
| 17 | query_params => { | ||
| 18 | authorize => { | ||
| 19 | response_type => 'code', | ||
| 20 | scope => 'login:birthday login:email login:info login:avatar', | ||
| 21 | display => 'popup', | ||
| 22 | } | ||
| 23 | } | ||
| 24 | } } | ||
| 25 | |||
| 26 | sub post_process { | ||
| 27 | my ($self, $session) = @_; | ||
| 28 | |||
| 29 | my $session_data = $session->read('oauth'); | ||
| 30 | |||
| 31 | my $ua = LWP::UserAgent->new; | ||
| 32 | my $req = HTTP::Request->new(GET => $self->provider_settings->{urls}{user_info}); | ||
| 33 | $req->content_type('application/x-www-form-urlencoded'); | ||
| 34 | $req->header(Authorization => 'OAuth '.$session_data->{yandex}{access_token}); | ||
| 35 | $req->content( | ||
| 36 | "format=".$self->provider_settings->{format}. | ||
| 37 | "&client_id=".$self->provider_settings->{tokens}{client_id} | ||
| 38 | ); | ||
| 39 | |||
| 40 | my $resp = $ua->request($req); | ||
| 41 | |||
| 42 | if( $resp->is_success ) { | ||
| 43 | my $user = $self->_stringify_json_booleans( | ||
| 44 | JSON::MaybeXS::decode_json( $resp->content ) | ||
| 45 | ); | ||
| 46 | $session_data->{yandex}{user_info} = $user; | ||
| 47 | $session->write('oauth', $session_data); | ||
| 48 | } | ||
| 49 | } | ||
| 50 | |||
| 51 | 1; |