Skip to content

PHP OAUTH2 authentication API - Azure, Facebook, Github, Google, Microsoft, Naver

Notifications You must be signed in to change notification settings

OOPS-ORG-PHP/OAUTH2

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

59 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

OAUTH2 pear package

GitHub license

License

Copyright (c) 2020 JoungKyun.Kim <http://oops.org> All rights reserved

This program is under BSD license

Description

This is OAUTH2 login tool and support follow vendors:

  • Google
  • Facebook
  • Github
  • Naver
  • Kakao
  • Microsoft

Installation

We recommand to install with pear command cause of dependency pear packages.

1. use pear command

[root@host ~]$ # add pear channel 'pear.oops.org'
[root@host ~]$ pear channel-discover pear.oops.org
Adding Channel "pear.oops.org" succeeded
Discovery of channel "pear.oops.org" succeeded
[root@host ~]$ # add OAUTH2 pear package
[root@host ~]$ pear install oops/OAUTH2
downloading OAUTH2-1.0.9.tgz ...
Starting to download OAUTH2-1.0.9.tgz (10,893 bytes)
....done: 10,893 bytes
downloading HTTPRelay-1.0.5.tgz ...
Starting to download HTTPRelay-1.0.5.tgz (5,783 bytes)
...done: 5,783 bytes
downloading myException-1.0.2.tgz ...
Starting to download myException-1.0.2.tgz (3,048 bytes)
...done: 3,048 bytes
install ok: channel://pear.oops.org/myException-1.0.2
install ok: channel://pear.oops.org/HTTPRelay-1.0.5
install ok: channel://pear.oops.org/OAUTH2-1.0.9
[root@host ~]$

If you wnat to upgarde version:

[root@host ~]$ pear upgrade oops/OAUT2

2. install by hand

Get last release at https://github.com/OOPS-ORG-PHP/OAUTH2/releases and uncompress pakcage within PHP include_path.

You must need follow dependency pear packages:

Usages

Refence siste: http://pear.oops.org/docs/oops-OAUTH2/OAUth2.html

reference is written by Korean. If you can't read korean, use google translator.

<?php
session_start ();

require_once 'OAUTH2.php';

set_error_handler ('myException::myErrorHandler');

// Callback URL is this page.
$callback = sprintf (
    '%s://%s%s',
    $_SERVER['HTTPS'] ? 'https' : 'http',
    $_SERVER['HTTP_HOST'],
    $_SERVER['REQUEST_URI']
);

$appId = (object) array (
    'vendor'   => 'google',
    'id'       => 'APPLICATION_ID',
    'secret'   => 'APPLICATION_SECRET_KEY',
    'callback' => $callback,
);

try {
    $oauth2 = new oops\OAUTH2 ($appId);

    // If you want to logout, give logout parameter at callback url.
    // If you need redirect after logout, give redrect parameter.
    // For example:
    //  http://callback_url?logout&redirect=http%3A%2F%2Fredirect_url
    if ( isset ($_GET['logout']) ) {
        unset ($_SESSION['oauth2']);

        if ( $_GET['redirect'] )
            Header ('Location: ' . $redirect);

        printf ('%s Complete logout', strtoupper ($appId->vendor));
        exit;
    }

    $user = $oauth2->Profile ();
    $uid = sprintf ('%s:%s', $appId->vendor, $user->id);
    $_SESSION['oauth2'] = (object) array (
        'uid' => $uid,
        'name' => $user->name,
        'email' => $user->email,
        'img' => $user->img,
        'logout' => $callback . '?logout'
    );

    print_r ($_SESS['oauth2']);
} catch ( myException $e ) {
    echo $e->Message () . "\n";
    print_r ($e->TraceAsArray);
    $e->finalize ();
}
?>