Skip to content

Instantly share code, notes, and snippets.

@vishva8kumara
Last active August 20, 2017 14:23
Show Gist options
  • Save vishva8kumara/be7349854beb5063bb5ee80dc10e3920 to your computer and use it in GitHub Desktop.
Save vishva8kumara/be7349854beb5063bb5ee80dc10e3920 to your computer and use it in GitHub Desktop.
info.lk minal OAuth Client Library
<?php
session_start();
//
// Consent denied by user
if (isset($_SESSION['info-lk']['ra']) && isset($_GET['error']))
$_SESSION['info-lk']['error'] = 'access denied';
//
// Process received single use token
else if (isset($_SESSION['info-lk']['ra']) && isset($_GET['code'])){
if (!isset($_GET['state']) || $_SESSION['info-lk']['nx'] != $_GET['state'])
$_SESSION['info-lk']['error'] = 'auth spoof detected';
else{
$context = stream_context_create(
array(
'http' => array(
'method' => 'POST',
'header' => 'Content-Type:application/x-www-form-urlencoded'."\r\n",
'content' => http_build_query(
array(
'code' => $_GET['code'],
'client_id' => $info_lk_settings['app'],
'client_secret' => $info_lk_settings['secret'],
'grant_type' => 'authorization_code',
'redirect_uri'=>$_SESSION['info-lk']['ra']
)
)
),
'ssl' => array(
'verify_peer' => $info_lk_settings['ssl_verify_peer']
)
)
);
$oauth = json_decode(file_get_contents('https://account.info.lk/oauth', false, $context), true);
if (isset($oauth['access_token'])){
$oauth = base64_decode($oauth['access_token']);
$_SESSION['info-lk']['user'] = json_decode(substr($oauth, 0, strrpos($oauth,'}')+1), true);
$redirect_after = $_SESSION['info-lk']['ra'];
unset($_SESSION['info-lk']['ra']);
unset($_SESSION['info-lk']['nx']);
header('location:'.$redirect_after);
}
else
$_SESSION['info-lk']['error'] = 'auth failed';
}
}
//
// Redirect to OAuth consent screen
else if (!isset($_SESSION['info-lk']) || !isset($_SESSION['info-lk']['user'])){
$redirect_after = (isset($_SERVER['REQUEST_SCHEME']) ?
$_SERVER['REQUEST_SCHEME'] :
(isset($_SERVER['HTTPS']) ? 'https' : 'http')
).'://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
$nonce = time().rand(100, 999);
$_SESSION['info-lk'] = array('ra' => $redirect_after, 'nx' => $nonce);
header('location:https://account.info.lk/#oauth/?response_type=code&'.
'client_id='.$info_lk_settings['app'].'&'.
'redirect_uri='.urlencode($redirect_after).'&'.
'scope='.$info_lk_settings['scope'].
'&state='.$nonce);
}
?>
@vishva8kumara
Copy link
Author

vishva8kumara commented Aug 17, 2017

Usage

<?php

 $info_lk_settings = array(
	'app' => '87345923489',
	'secret' => '98xd7s5h9df786sd09',
	'scope' => 'public_profile',
	'ssl_verify_peer' => true
 );

include 'info-lk.php';

print_r($_SESSION['info-lk']);

 ?>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment