PEF::Front::Oauth - This is an implementation of OAuth2 API for several popular services.
package MyApp::Local::Oauth; use PEF::Front::Config; use PEF::Front::Oauth; use PEF::Front::Session; use strict; use warnings; sub make_url { my ($req, $context) = @_; my $session = PEF::Front::Session->new($req); my $oauth = PEF::Front::Oauth->new($req->{service}, $session); my $expires = demo_login_expires(); $session->data->{oauth_return_url} = $context->{headers}->get_header('Referer') || '/'; return { result => "OK", url => $oauth->authorization_server($oauth->user_info_scope), auth => $session->key, expires => $expires, service => $req->{service}, }; } sub callback { my ($req, $context) = @_; my $session = PEF::Front::Session->new($req); my $back_url = $session->data->{oauth_return_url} || '/'; delete $session->data->{oauth_return_url}; unless ($req->{state} && $req->{code}) { delete $session->data->{oauth_state}; return { result => "OAUTHERR", answer => $req->{error_description} }; } my $service = $session->data->{oauth_state}{$req->{state}}; return { result => "OAUTHERR", answer => 'Unknoen oauth state' } unless $service; my $oauth = PEF::Front::Oauth->new($service, $session); $oauth->exchange_code_to_token($req); my $info = $oauth->get_user_info(); $session->data->{name} = $info->{name}; $session->data->{is_author} = 0; $session->data->{is_oauth} = 1; return { result => "OK", back_url => $back_url, %$info }; }
This module implements Oauth2 user authorization and gets some info about authorized user. It loads specific Oauth2 implementor class for given service. There're following supported services:
First, you has to register your application by required services and get your client id-s and client secret-s from them. Probably you have to register some patterns for return URLs also. Client id-s and client secret-s are configured with cfg_oauth_client_id($service) and cfg_oauth_client_secret($service).
client id
client secret
Client id
Second, your application has to make return url which will be used by Oauth2 service to pass authorization code to your application.
Third, your server exchanges this authorization code for an access token.
Fourth, using this access token your application access desired information or action.
PEF::Front::Oauth stores some information in user session data.
This function loads implementor class for given $auth_service and pass PEF::Front::Session object to it.
$auth_service
PEF::Front::Session
Returns full URL with required parameters for authorization server for given scope. Google, LinkedIn, Msn, Paypal and VKontakte services can work only when you pass them previously registered redirect uri.
This method stores in session following keys: oauth_state, oauth_redirect_uri.
oauth_state
oauth_redirect_uri
When Oauth2 service calls your site back, your application has to exchange code to access token. This method stores in session oauth_error key when token exchange was not successful.
oauth_error
This method returns some basic user information that is obtained from the service. It returns hash like this: { name => $username, email => $email, login => $login, avatar => [], }
avatar is array of user pictures when service returns it.
avatar
This method stores in session following keys: oauth_info_raw and oauth_info.
oauth_info_raw
oauth_info
This module was written and is maintained by Anton Petrusevich.
Copyright (c) 2016 Anton Petrusevich. Some Rights Reserved.
This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install PEF::Front, copy and paste the appropriate command in to your terminal.
cpanm
cpanm PEF::Front
CPAN shell
perl -MCPAN -e shell install PEF::Front
For more information on module installation, please visit the detailed CPAN module installation guide.