Difference between revisions of "CMU OAUTH PHP CLASS"

From CMU ITSC Network
 
(9 intermediate revisions by the same user not shown)
Line 1: Line 1:
 +
== Introduction ==
 +
Authorization Code Flow<br>
 +
[[File:CMUOAuth-authozizationcode flow.jpg|link=]]<br>
 +
from [https://oauth.cmu.ac.th CMU OAuth]
 +
== Requirement ==
 +
[http://php.net/manual/en/book.curl.php php_curl]
 
== Methods ==
 
== Methods ==
 
=== Constructor ===
 
=== Constructor ===
Line 96: Line 102:
 
| no return value
 
| no return value
 
|}
 
|}
 +
 +
 +
=== setScope===
 +
Set scope
 +
{| class="wikitable"
 +
|+ style="text-align:left;"|Description
 +
|-
 +
|setScope(string $scope)
 +
|}
 +
 +
{| class="wikitable"
 +
|+ style="text-align:left;"|Parameters
 +
|-
 +
|name || description
 +
|-
 +
|scope
 +
|access token scope name comma separate value
 +
|}
 +
 +
{| class="wikitable"
 +
|+ style="text-align:left;"|Return Values
 +
|-
 +
| no return value
 +
|}
 +
 +
=== setState===
 +
Set state
 +
{| class="wikitable"
 +
|+ style="text-align:left;"|Description
 +
|-
 +
|setState()
 +
|}
 +
 +
{| class="wikitable"
 +
|+ style="text-align:left;"|Parameters
 +
|-
 +
|no parameters
 +
|}
 +
 +
{| class="wikitable"
 +
|+ style="text-align:left;"|Return Values
 +
|-
 +
| String
 +
|-
 +
| Random String
 +
|}
 +
 
=== initOauth ===
 
=== initOauth ===
 
Initial redirect to CMU Oauth for authorization.
 
Initial redirect to CMU Oauth for authorization.
Line 116: Line 169:
 
|}
 
|}
  
=== getAccessToken ===
+
=== getAccessTokenAuthCode ===
Get user's authorized access token.
+
Get user's authorized access token for authorization code flow.
 
{| class="wikitable"
 
{| class="wikitable"
 
|+ style="text-align:left;"|Description
 
|+ style="text-align:left;"|Description
 
|-
 
|-
|object getAccessToken(string $code)
+
|object getAccessTokenAuthCode(string $code)
 
|}
 
|}
  
Line 145: Line 198:
 
|}
 
|}
  
=== getUserInfo ===
+
=== getAccessTokenClientCred ===
Get user's information by user's authorized access token.
+
Get access token for client credential flow.
 
{| class="wikitable"
 
{| class="wikitable"
 
|+ style="text-align:left;"|Description
 
|+ style="text-align:left;"|Description
 
|-
 
|-
|object getUserInfo(string $accessToken)
+
|object getAccessTokenClientCred()
 
|}
 
|}
  
Line 156: Line 209:
 
|+ style="text-align:left;"|Parameters
 
|+ style="text-align:left;"|Parameters
 
|-
 
|-
|name || description
+
|no parameter
|-
 
|accessToken
 
|user's authorized access token
 
 
|}
 
|}
  
Line 167: Line 217:
 
| object
 
| object
 
|-
 
|-
|<pre>{
+
|<syntaxhighlight lang=json>{
   "status": true,
+
   "access_token": "66822448858031556636",  
  "data": {
+
  "expires_in": 3600,  
    "timestamp": "2017-03-31T17:30:55.7933253+07:00",
+
  "refresh_token": null
    "itaccount_name": "jon_s",
+
}</syntaxhighlight>
    "citizen_id": "1111111111111",
 
    "student_id": "520510999",
 
    "prefix": {
 
      "en_US": "Mr.",
 
      "th_TH": "นาย"
 
    },
 
    "first_name": {
 
      "en_US": "JON",
 
      "th_TH": "จอน"
 
    },
 
    "last_name": {
 
      "en_US": "SNOW",
 
      "th_TH": "สโนว์"
 
    },
 
    "organization": {
 
      "code": "05",
 
      "name": {
 
        "en_US": "Faculty of Science",
 
        "th_TH": "คณะวิทยาศาสตร์"
 
      }
 
    },
 
    "itaccount_type": {
 
      "id": "AlumAcc",
 
      "en_US": "Alumni Account",
 
      "th_TH": "นักศึกษาเก่า"
 
    }
 
  }
 
}</pre>
 
 
|}
 
|}
 +
 +
  
 
== Examples ==
 
== Examples ==
Line 206: Line 230:
 
<syntaxhighlight lang=php>
 
<syntaxhighlight lang=php>
 
<?php
 
<?php
 +
session_start();
 
// provide your application id,secret and redirect uri
 
// provide your application id,secret and redirect uri
$appId = 'your cmu ouath client ID';
+
$appId = '';
$appSecret = 'your cmu oauth client secret';
+
$appSecret = '';
$callbackUri = 'your cmu oauth Redirect URI';
+
$callbackUri[5] = 'http://localhost/php5/callback.php';
 +
$callbackUri[7] = 'http://localhost/php7/callback.php';
 +
$scope = 'cmuitaccount.basicinfo';
  
 
require('cmu.oauth.class.php');
 
require('cmu.oauth.class.php');
Line 217: Line 244:
 
$cmuOauth->setAppId($appId);
 
$cmuOauth->setAppId($appId);
 
$cmuOauth->setAppSecret($appSecret);
 
$cmuOauth->setAppSecret($appSecret);
$cmuOauth->setCallbackUri($callbackUri);
+
$cmuOauth->setCallbackUri($callbackUri[PHP_MAJOR_VERSION]);
 +
$cmuOauth->setScope($scope);
  
if(isset($_GET['code'])){
+
if (isset($_GET['error'])) {
 +
session_destroy();
 +
  exit($_GET['error']);
 +
} elseif(!isset($_GET['code'])){
 +
//set state
 +
$_SESSION['oauth2state'] = $cmuOauth->setState();
 +
 
 +
// initial redirect to CMU Oauth login page.
 +
$cmuOauth->initOauth();
 +
 
 +
// Check given state against previously stored one to mitigate CSRF attack
 +
} elseif(empty($_GET['state']) || (isset($_SESSION['oauth2state']) && $_GET['state'] !== $_SESSION['oauth2state'])){
 +
if (isset($_SESSION['oauth2state'])) {
 +
unset($_SESSION['oauth2state']);
 +
}
 +
exit('Invalid state');
 +
} else {
 
// code parse from CMU Oauth to your redirect uri.
 
// code parse from CMU Oauth to your redirect uri.
 
$code = $_GET['code'];
 
$code = $_GET['code'];
 
// get access token from code.
 
// get access token from code.
$accessToken = $cmuOauth->getAccessToken($code);
+
$accessToken = $cmuOauth->getAccessTokenAuthCode($code);
// get user information from access token.
+
  $_SESSION['accessToken']=$accessToken->access_token;
$userInfo = $cmuOauth->getUserInfo($accessToken->access_token);
+
  echo "<pre>";
 
+
var_dump($accessToken);
+
  echo "</pre>";
// do login process
+
  echo "<a href=\"userInfo.php\">View User Info</a>";
// create session if status == true, refer to return values of cmuOauth::getUserInfo
+
   echo "<br>";
// else destroy session
+
  echo "<a href=\"index.php\">Home</a>";
if($userInfo->status===true){
 
  session_start();
 
  $sid = session_id();
 
  $_SESSION["user_$sid"]=$userInfo->data->itaccount_name."@cmu.ac.th";    
 
  header("location: https://example.com/main.html");
 
  exit();
 
}else {
 
  session_start();
 
  unset($_SESSION["user_$sid"]);  
 
  session_destroy();
 
  header("location: https://example.com/403.html");
 
  exit();
 
}
 
}else{
 
// initial redirect to CMU Oauth login page.
 
$cmuOauth->initOauth();
 
 
}
 
}
 
?>
 
?>
Line 252: Line 280:
  
 
== Download ==
 
== Download ==
[http://network.cmu.ac.th/cmu.oauth.class.php.zip cmu.oauth.class.php.zip]
+
[https://myweb.cmu.ac.th/supawit.w/cmu.oauth.class.php.zip cmu.oauth.class.php.zip]
 +
 
 
== Reference ==
 
== Reference ==
 
[https://oauth.cmu.ac.th https://oauth.cmu.ac.th]<br>
 
[https://oauth.cmu.ac.th https://oauth.cmu.ac.th]<br>
 
[https://tools.ietf.org/html/rfc6749 https://tools.ietf.org/html/rfc6749]
 
[https://tools.ietf.org/html/rfc6749 https://tools.ietf.org/html/rfc6749]

Latest revision as of 09:27, 2 February 2024

Introduction

Authorization Code Flow
CMUOAuth-authozizationcode flow.jpg
from CMU OAuth

Requirement

php_curl

Methods

Constructor

Set Client ID, Client Secret, Redirect URI

Description
__construct([string $appId, string $clientSecret, string $redirectURI])
Parameters
name description
appId cmu oauth Client ID
clientSecret cmu oauth Client Secret
redirectURI cmu oauth Redirect URI
Return Values
no return value

setAppId

set Client ID

Description
setAppId(string $appid)
Parameters
name description
appid cmu oauth Client ID
Return Values
no return value

setAppSecret

Set Client Secret

Description
setAppSecret(string $appSecret)
Parameters
name description
appSecret cmu oauth Client Secret
Return Values
no return value

setCallbackUri

Set Redirect URI

Description
setCallbackUri(string $uri)
Parameters
name description
uri Application Callback / Redirect URI
Return Values
no return value


setScope

Set scope

Description
setScope(string $scope)
Parameters
name description
scope access token scope name comma separate value
Return Values
no return value

setState

Set state

Description
setState()
Parameters
no parameters
Return Values
String
Random String

initOauth

Initial redirect to CMU Oauth for authorization.

Description
initOauth()
Parameters
no parameter
Return Values
no return value

getAccessTokenAuthCode

Get user's authorized access token for authorization code flow.

Description
object getAccessTokenAuthCode(string $code)
Parameters
name description
code code that parse by CMU Oauth to redirect URI.
Return Values
object
{
  "access_token": "66822448858031556636", 
  "expires_in": 3600, 
  "refresh_token": "23178027621214615262"
}

getAccessTokenClientCred

Get access token for client credential flow.

Description
object getAccessTokenClientCred()
Parameters
no parameter
Return Values
object
{
  "access_token": "66822448858031556636", 
  "expires_in": 3600, 
  "refresh_token": null
}


Examples

callback.php

<?php
session_start();
// provide your application id,secret and redirect uri
$appId = '';
$appSecret = '';
$callbackUri[5] = 'http://localhost/php5/callback.php';
$callbackUri[7] = 'http://localhost/php7/callback.php';
$scope = 'cmuitaccount.basicinfo';

require('cmu.oauth.class.php');
// new CMU Oauth Instance.
$cmuOauth = new cmuOauth();
// set your application id,secret and redirect uri
$cmuOauth->setAppId($appId);
$cmuOauth->setAppSecret($appSecret);
$cmuOauth->setCallbackUri($callbackUri[PHP_MAJOR_VERSION]);
$cmuOauth->setScope($scope);

if (isset($_GET['error'])) {
	session_destroy();
  exit($_GET['error']);
} elseif(!isset($_GET['code'])){
	//set state
	$_SESSION['oauth2state'] = $cmuOauth->setState();

	// initial redirect to CMU Oauth login page.
	$cmuOauth->initOauth();

// Check given state against previously stored one to mitigate CSRF attack
} elseif(empty($_GET['state']) || (isset($_SESSION['oauth2state']) && $_GET['state'] !== $_SESSION['oauth2state'])){
	if (isset($_SESSION['oauth2state'])) {
		unset($_SESSION['oauth2state']);
	}
	exit('Invalid state');
} else {
	// code parse from CMU Oauth to your redirect uri.
	$code = $_GET['code'];
	// get access token from code.
	$accessToken = $cmuOauth->getAccessTokenAuthCode($code);
  $_SESSION['accessToken']=$accessToken->access_token;
  echo "<pre>";
	var_dump($accessToken);
  echo "</pre>";
  echo "<a href=\"userInfo.php\">View User Info</a>";
  echo "<br>";
  echo "<a href=\"index.php\">Home</a>";
}
?>

Download

cmu.oauth.class.php.zip

Reference

https://oauth.cmu.ac.th
https://tools.ietf.org/html/rfc6749