Revision 239

Date:
2012/09/03 18:53:21
Author:
ahitrov
Revision Log:
New Facebook oAuth
Files:

Legend:

 
Added
 
Removed
 
Modified
  • utf8/plugins/session/comps/www/oauth/facebook.html

     
    1 <script type="text/javascript">
    2 <!--
    3 opener.AuthOnEnd();
    4 close();
    5 //-->
    6 </script>
    7
    8 <pre><% Dumper($fb_connect) %></pre>
    9 %#<a href="<% $auth_url->as_string %>"><% $auth_url->as_string %></a>
    10 <pre><% Dumper($session) %></pre>
    11
    12 <%doc>
    13
    14 Manual redirect:
    15
    16 use session::AUTH::FaceBook;
    17 my $site = $state->development ? 'www22.zvuki.ru' : 'www.zvuki.ru';
    18 my $fb_connect = session::AUTH::FaceBook->new(
    19 facebook_redirect_uri => 'http://'.$site.'/oauth/facebook.html',
    20 );
    21
    22 </%doc>
    23 <%args>
    24
    25 $code => undef
    26 $state => undef
    27
    28 </%args>
    29 <%init>
    30
    31 my $fb_connect = session::AUTH::FaceBook->new();
    32 my $auth_url = $fb_connect->fb_authorize_url( state => $state );
    33 if ( $code ) {
    34 my $local_session = $fb_connect->authenticate( code => $code, state => $state );
    35 if ( ref $local_session && exists $local_session->{id} ) {
    36 my $profile = $keeper->{users}->get_profile( id => $local_session->{id} );
    37 if ( ref $profile ) {
    38 unless ( exists $local_session->{avatar} ) {
    39 my $avatar = $profile->get_image('avatar');
    40 $session->{avatar} = ref $avatar && exists $avatar->{filename} ? $avatar->{mini}{'54x54'}{filename} : undef;
    41 $keeper->{session}->store_value (
    42 name => $profile->name_full,
    43 last_name => $profile->name_family,
    44 first_name => $profile->name_part,
    45 avatar => $session->{avatar},
    46 );
    47 } else {
    48 $keeper->{session}->store_value (
    49 name => $profile->name_full,
    50 last_name => $profile->name_family,
    51 first_name => $profile->name_part,
    52 );
    53 }
    54 }
    55 }
    56 } elsif ( $auth_url ) {
    57 $m->redirect($auth_url->as_string);
    58 } else {
    59 &abort404 unless $DEBUG;
    60 }
    61
    62 </%init>
  • utf8/plugins/session/config.proto

     
    50 50 ### AUTH::FaceBook
    51 51 ######################################
    52 52 FACEBOOK_APP_ID =
    53 FACEBOOK_APP_KEY =
    54 53 FACEBOOK_APP_SECRET =
    55 FACEBOOK_AUTHOTIZE_URL = https://graph.facebook.com/oauth/authorize
    54 FACEBOOK_AUTHOTIZE_URL = https://www.facebook.com/dialog/oauth
    56 55 FACEBOOK_ACCESS_TOKEN_URL = https://graph.facebook.com/oauth/access_token
    57 56 FACEBOOK_USER_INFO_URL = https://graph.facebook.com/me
    58 57 FACEBOOK_REDIRECT_URL =
    59 58 FACEBOOK_USER_POST_URL =
    60 59
    60 REWRITE += FACEBOOK_APP_ID FACEBOOK_APP_SECRET FACEBOOK_REDIRECT_URL
    61 61 REWRITE += FACEBOOK_AUTHOTIZE_URL FACEBOOK_ACCESS_TOKEN_URL FACEBOOK_USER_INFO_URL
    62 62
    63 63 CONNECTION_TIMEOUT = 3
  • utf8/plugins/session/lib/session/AUTH/FaceBook.pm

     
    16 16 =for rem
    17 17 facebook:
    18 18 auto_create_user: 1
    19 app_id: 122117614500563
    20 app_key: 3da06301715b0efc5c873535c56c2c33
    21 app_secret: 656bd1369486b902e9bf831a9a08132b
    22 authorize_url: https://graph.facebook.com/oauth/authorize
    19 app_id: 15 decimal digits
    20 app_secret: 32 hex digits
    21 authorize_url: https://www.facebook.com/dialog/oauth
    23 22 access_token_url: https://graph.facebook.com/oauth/access_token
    24 23 user_info_url: https://graph.facebook.com/me
    25 24 user_post_url: ~
    25 state: is passed back to your app as a parameter of the redirect_uri when the user completed the authentication
    26 26 store:
    27 27 class: "+Comments::Authentication::Store"
    28 28 type: facebook
     
    42 42 sub new {
    43 43 my ($class, %config) = @_;
    44 44 my $self = bless {}, $class;
    45 for (qw(facebook_app_id facebook_app_key facebook_app_secret facebook_authorize_url facebook_access_token_url facebook_user_info_url)) {
    46 $self->{$_} = $config{$_} || $state->{session}->{$_} || return undef;
    45 for (qw(facebook_app_id facebook_app_secret facebook_authorize_url facebook_access_token_url facebook_user_info_url)) {
    46 $self->{$_} = $config{$_} || $state->{session}{$_} || return undef;
    47 47 }
    48 $self->{timeout} = $state->{session}->{connection_timeout} || 3;
    48 $self->{timeout} = $state->{session}{connection_timeout} || 3;
    49 49 for (qw(facebook_user_post_url facebook_redirect_uri)) {
    50 $self->{$_} = $config{$_} || $state->{session}->{$_};
    50 $self->{$_} = $config{$_} || $state->{session}{$_};
    51 51 }
    52 52 return $self;
    53 53 }
     
    56 56 my $self = shift;
    57 57 my (%args) = @_;
    58 58 my $go = URI->new( $self->{facebook_authorize_url} );
    59 warn Dumper($go);
    60 $go->query_param( client_id => $self->{facebook_app_key} );
    59 $go->query_param( client_id => $self->{facebook_app_id} );
    60 $go->query_param( state => $args{state} ) if $args{state};
    61 61 $go->query_param( scope => "publish_stream" );
    62 62 $args{redirect_uri} ||= $self->{facebook_redirect_uri};
    63 63 for ( keys %args ) {
    64 64 $go->query_param( $_ => $args{$_} );
    65 65 }
    66 $keeper->{session}->store_value( facebook_redirect_url => $self->{facebook_redirect_uri} );
    66 warn Dumper($go) if $DEBUG;
    67 67 return $go;
    68 68 }
    69 69
     
    71 71 my ( $self, %authinfo ) = @_;
    72 72 warn "FB.authenticate" if $DEBUG;
    73 73 # TODO: we need callback url
    74 #warn "user_session=".dumper( $c->user_session )." ";
    75 74 my $local_session = $session || $keeper->{session}->get_session;
    76 my $redirect_uri = $local_session->{facebook_redirect_url};
    75 my $redirect_uri = $self->{facebook_redirect_uri};
    77 76
    78 77 my $access_token = $local_session->{facebook_access_token};
    79 78 my $expires = $local_session->{facebook_expires};
     
    95 94 $req->query_param( redirect_uri => $redirect_uri );
    96 95 $req->query_param( client_secret=> $self->{facebook_app_secret} );
    97 96 $req->query_param( code => $code);
    98 warn "Get $req";
    97 warn "Get $req" if $DEBUG;
    99 98 my $res = $ua->get($req);
    100 99 unless ($res->code == 200) {
    101 100 warn "access_token request failed: ".$res->status_line;
    102 101 return undef;
    103 102 }
    104 103 my %res = eval { URI->new("?".$res->content)->query_form };
    105 warn Dumper(\%res);
    104 warn Dumper(\%res) if $DEBUG;
    106 105 unless ($access_token = $res{access_token}) {
    107 106 warn "No access token in response: ".$res->content;
    108 107 return undef;
     
    115 114 } else {
    116 115 #$c->user_session->{'expires'} = time + 3600*24;
    117 116 }
    118 warn "FB: requested access token";
    117 warn "FB: requested access token" if $DEBUG;
    119 118 } else {
    120 warn "FB: have access token";
    119 warn "FB: have access token" if $DEBUG;
    121 120 }
    122 121
    123 122 my $req = URI->new( $self->{facebook_user_info_url} );
    124 123 $req->query_param( access_token => $access_token );
    125 124
    126 warn "Fetching user $req";
    125 warn "Fetching user $req" if $DEBUG;
    127 126 my $res = $ua->get($req);
    128 127 unless ($res->code == 200) {
    129 128 warn "user request failed: ".$res->status_line;
     
    134 133 warn "user '".$res->content."' decode failed: $@";
    135 134 return undef;
    136 135 }
    137 warn "Userhash = ".Dumper($info);
    136 warn "Userhash = ".Dumper($info) if $DEBUG;
    138 137 #warn "facebook: user=$info->{name} / $info->{id} / $info->{gender}";
    139 138
    140 139 $keeper->{session}->delete_key( 'facebook_redirect_url' );
     
    154 153 $user->login_method('facebook');
    155 154 $user->country( $info->{locale} );
    156 155 $user->email( undef );
    156
    157 my ($prop_ava) = grep { $_->{attr} eq 'avatar' && $_->{type} eq 'image' } $user->structure;
    158 if ( ref $prop_ava ) {
    159 my $avatar = $user->_store_image( 'https://graph.facebook.com/'.$info->{username}.'/picture?type=large', attr => 'avatar' );
    160 $user->avatar( $user->_serialize($avatar) );
    161 }
    162
    157 163 $user->store;
    164 } else {
    165 my ($prop_ava) = grep { $_->{attr} eq 'avatar' && $_->{type} eq 'image' } $user->structure;
    166 if ( ref $prop_ava ) {
    167 my $avatar = $user->get_image( 'avatar' );
    168 unless ( ref $avatar && exists $avatar->{filename} ) {
    169 my $avatar = $user->_store_image( 'https://graph.facebook.com/'.$info->{username}.'/picture?type=large', attr => 'avatar' );
    170 $user->avatar( $user->_serialize($avatar) );
    171 $user->store;
    172 }
    173 }
    158 174 }
    159 175 my %data = (
    160 176 id => $user->id,
     
    163 179 status => $user->status,
    164 180 type => $user->type,
    165 181 ltime => time,
    182 avatar => 'https://graph.facebook.com/'.$info->{username}.'/picture',
    166 183 );
    167 184 $keeper->{session}->store_value ( %data );
    168 185 while ( my ( $key, $value ) = each %data ) {