Revision 513

Date:
2015/09/22 12:02:55
Author:
ahitrov
Revision Log:
OAUTH Vkontakte: get user e-mail and authorize user by e-mail if applicable

Files:

Legend:

 
Added
 
Removed
 
Modified
  • utf8/plugins/session/lib/session/AUTH/VKontakte.pm

     
    20 20 app_secret: 32 hex digits
    21 21 authorize_url: http://api.vkontakte.ru/oauth/authorize
    22 22 access_token_url: https://api.vkontakte.ru/oauth/access_token
    23 user_info_url: https://api.vkontakte.ru/method/getProfiles
    23 user_info_url: https://api.vkontakte.ru/method/account.getProfileInfo
    24 24 user_post_url: ~
    25 25 =cut
    26 26
     
    122 122 return undef;
    123 123 }
    124 124 my $info = $JSON->decode($res->content);
    125 warn Dumper $info if $DEBUG;
    125 126 unless ( ref $info eq 'HASH' && ($access_token = $info->{access_token}) ) {
    126 127 warn "No access token in response: ".$res->content."\n";
    127 128 return undef;
    128 129 }
    129 130 $local_session->set( vk_access_token => $access_token );
    130 131 $local_session->set( vk_user_id => $info->{user_id} );
    132 if ( exists $info->{email} ) {
    133 $local_session->set( vk_email => $info->{email} );
    134 $local_session->set( email => $info->{email} ) unless exists $local_session->{email};
    135 }
    131 136 if ( my $expires = $info->{expires_in} ) {
    132 137 $local_session->set( vk_expires => time + $expires );
    133 138 } else {
     
    138 143 warn "VK: have access token" if $DEBUG;
    139 144 }
    140 145
    146 my @fields = qw( uid first_name last_name nickname domain sex bdate city country timezone photo photo_medium photo_big );
    141 147 my $req = URI->new( $self->{vk_user_info_url} );
    142 148 $req->query_param( uid => $local_session->{vk_user_id} );
    143 $req->query_param( fields => 'uid,first_name,last_name,nickname,domain,sex,bdate,city,country,timezone,photo,photo_medium,photo_big' );
    149 $req->query_param( fields => join ',', @fields );
    144 150 $req->query_param( access_token => $access_token );
    145 151
    146 152 warn "VK: Fetching user $req\n" if $DEBUG;
     
    174 180 $user = $keeper->{users}->get_profile( vkontakte => $user_info->{uid} );
    175 181 }
    176 182 }
    183 if ( !ref $user && (exists $local_session->{vk_email} || exists $local_session->{email}) ) {
    184 $user = $keeper->{users}->get_profile( email => exists $local_session->{vk_email} ? $local_session->{vk_email} : $local_session->{email} );
    185 }
    177 186 unless ( ref $user ) {
    178 187 $user = $keeper->{users}->get_profile( login => 'vkontakte:'.$user_info->{uid} );
    179 188 }
     
    181 190 my $user_class = $state->{users}->profile_document_class;
    182 191 $user = $user_class->new( $keeper );
    183 192 my %props = map { $_->{attr} => $_ } $user->structure;
    184 $user->login( 'vkontakte:'.$user_info->{uid} );
    193 $user->login( exists $local_session->{vk_email} ? $local_session->{vk_email} : 'vkontakte:'.$user_info->{uid} );
    185 194 $user->name( $user_info->{last_name}.', '.$user_info->{first_name} );
    186 195 $user->nickname( $user_info->{nickname} );
    187 196 $user->status( 1 );
     
    190 199 if ( exists $props{country} ) {
    191 200 $user->country( $user_info->{country} );
    192 201 }
    193 $user->email( $email );
    202 $user->email( $local_session->{vk_email} ? $local_session->{vk_email} : undef );
    194 203
    195 204 my ($prop_ava) = grep { $_->{attr} eq 'avatar' && $_->{type} eq 'image' } $user->structure;
    196 205 if ( ref $prop_ava ) {