The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

PEF::Front::Oauth - This is an implementation of OAuth2 API for several popular services.

SYNOPSIS

  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
    };
  }

DESCRIPTION

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:

Facebook
GitHub
Google
LinkedIn
Msn
Paypal
VKontakte
Yandex

USAGE

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).

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.

new ($auth_service, $session)

This function loads implementor class for given $auth_service and pass PEF::Front::Session object to it.

authorization_server($scope, [$redirect_uri])

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.

exchange_code_to_token($req)

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.

get_user_info()

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.

This method stores in session following keys: oauth_info_raw and oauth_info.

AUTHOR

This module was written and is maintained by Anton Petrusevich.

Copyright and License

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.