Browse Source

Add example of a custom integration

master
Adam Pippin 3 years ago
parent
commit
9cbb07a8ba
  1. 4
      .gitignore
  2. 30
      examples/custom/_common.php
  3. 7
      examples/custom/_config.php
  4. 34
      examples/custom/callback.php
  5. 21
      examples/custom/composer.json
  6. 614
      examples/custom/composer.lock
  7. 31
      examples/custom/index.php
  8. 13
      examples/custom/login.php
  9. 17
      examples/custom/logout.php

4
.gitignore

@ -1,2 +1,2 @@
/vendor
/.php_cs.cache
vendor/
.php_cs.cache

30
examples/custom/_common.php

@ -0,0 +1,30 @@
<?php
require(__DIR__.'/vendor/autoload.php');
require(__DIR__.'/_config.php');
session_start();
// Configure authkit
\authkit2\Authkit2::configure(OPENID_CLIENT_ID, OPENID_CLIENT_SECRET, OPENID_ENDPOINT);
function html_header()
{
return <<<EOT
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Authkit2 Example</title>
</head>
<body>
EOT;
}
function html_footer()
{
return <<<EOT
</body>
</html>
EOT;
}

7
examples/custom/_config.php

@ -0,0 +1,7 @@
<?php
define('OPENID_CLIENT_ID', 'test');
define('OPENID_CLIENT_SECRET', 'a0ee4936-ef77-4e4b-87f3-8b4a2706d53a');
define('OPENID_ENDPOINT', 'http://127.0.0.1:8080/auth/realms/Test');
define('OPENID_CALLBACK_URL', 'http://127.0.0.1:4444/callback.php');
define('OPENID_REDIRECT_URL', 'http://127.0.0.1:4444/');

34
examples/custom/callback.php

@ -0,0 +1,34 @@
<?php
require('_common.php');
use authkit2\Authkit2;
// Initialize the three-legged oauth flow
$flow = new \authkit2\Oidc\Flows\UserFlow(Authkit2::get_client());
// Step 2: Exchange the authentication code for a actual token
// First, validate state to ensure this request is a response to the one we
// triggered by validating the nonce
$state = $_GET['state'];
try
{
$flow->validateState($state);
}
catch (\Exception $ex)
{
die('Invalid state returned');
}
// If that passes, then we can exchange the authentication code for a token
$code = $_GET['code'];
$token = $flow->exchangeCodeForToken($code, OPENID_CALLBACK_URL);
// Set the token in the session
$_SESSION['access_token'] = $token->getAccessToken();
$_SESSION['refresh_token'] = $token->getRefreshToken();
// Now redirect them back to the home page!
header('Location: index.php');

21
examples/custom/composer.json

@ -0,0 +1,21 @@
{
"name": "cmg/authkit2-custom-example",
"description": "example of integrating authkit2 outside of laravel",
"type": "project",
"license": "proprietary",
"authors": [
{
"name": "Adam Pippin",
"email": "adam.pippin@createmusicgroup.com"
}
],
"repositories": [
{
"type": "path",
"url": "../../"
}
],
"require": {
"cmg/authkit2": "^2.0"
}
}

614
examples/custom/composer.lock

@ -0,0 +1,614 @@
{
"_readme": [
"This file locks the dependencies of your project to a known state",
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "faa046544b36f7c7c9e2e3888f380b53",
"packages": [
{
"name": "cmg/authkit2",
"version": "2.0.0",
"dist": {
"type": "path",
"url": "../..",
"reference": "aea8352e39283f5c1a8d4a3f9089ee101f3efcbe"
},
"require": {
"firebase/php-jwt": "^5.2",
"guzzlehttp/guzzle": "^7.2",
"psr/simple-cache": "^1.0"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^2.18",
"laravel/framework": "^8.31",
"phan/phan": "^4.0"
},
"type": "library",
"extra": {
"laravel": {
"providers": [
"authkit2\\Providers\\Authkit2ServiceProvider"
]
}
},
"autoload": {
"psr-4": {
"authkit2\\": "src"
}
},
"license": [
"proprietary"
],
"authors": [
{
"name": "Adam Pippin",
"email": "adam.pippin@createmusicgroup.com"
}
],
"description": "authn/authz toolkit",
"transport-options": {
"relative": true
}
},
{
"name": "firebase/php-jwt",
"version": "v5.4.0",
"source": {
"type": "git",
"url": "https://github.com/firebase/php-jwt.git",
"reference": "d2113d9b2e0e349796e72d2a63cf9319100382d2"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/firebase/php-jwt/zipball/d2113d9b2e0e349796e72d2a63cf9319100382d2",
"reference": "d2113d9b2e0e349796e72d2a63cf9319100382d2",
"shasum": ""
},
"require": {
"php": ">=5.3.0"
},
"require-dev": {
"phpunit/phpunit": ">=4.8 <=9"
},
"suggest": {
"paragonie/sodium_compat": "Support EdDSA (Ed25519) signatures when libsodium is not present"
},
"type": "library",
"autoload": {
"psr-4": {
"Firebase\\JWT\\": "src"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"BSD-3-Clause"
],
"authors": [
{
"name": "Neuman Vong",
"email": "neuman+pear@twilio.com",
"role": "Developer"
},
{
"name": "Anant Narayanan",
"email": "anant@php.net",
"role": "Developer"
}
],
"description": "A simple library to encode and decode JSON Web Tokens (JWT) in PHP. Should conform to the current spec.",
"homepage": "https://github.com/firebase/php-jwt",
"keywords": [
"jwt",
"php"
],
"support": {
"issues": "https://github.com/firebase/php-jwt/issues",
"source": "https://github.com/firebase/php-jwt/tree/v5.4.0"
},
"time": "2021-06-23T19:00:23+00:00"
},
{
"name": "guzzlehttp/guzzle",
"version": "7.3.0",
"source": {
"type": "git",
"url": "https://github.com/guzzle/guzzle.git",
"reference": "7008573787b430c1c1f650e3722d9bba59967628"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/guzzle/guzzle/zipball/7008573787b430c1c1f650e3722d9bba59967628",
"reference": "7008573787b430c1c1f650e3722d9bba59967628",
"shasum": ""
},
"require": {
"ext-json": "*",
"guzzlehttp/promises": "^1.4",
"guzzlehttp/psr7": "^1.7 || ^2.0",
"php": "^7.2.5 || ^8.0",
"psr/http-client": "^1.0"
},
"provide": {
"psr/http-client-implementation": "1.0"
},
"require-dev": {
"bamarni/composer-bin-plugin": "^1.4.1",
"ext-curl": "*",
"php-http/client-integration-tests": "^3.0",
"phpunit/phpunit": "^8.5.5 || ^9.3.5",
"psr/log": "^1.1"
},
"suggest": {
"ext-curl": "Required for CURL handler support",
"ext-intl": "Required for Internationalized Domain Name (IDN) support",
"psr/log": "Required for using the Log middleware"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "7.3-dev"
}
},
"autoload": {
"psr-4": {
"GuzzleHttp\\": "src/"
},
"files": [
"src/functions_include.php"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Michael Dowling",
"email": "mtdowling@gmail.com",
"homepage": "https://github.com/mtdowling"
},
{
"name": "Márk Sági-Kazár",
"email": "mark.sagikazar@gmail.com",
"homepage": "https://sagikazarmark.hu"
}
],
"description": "Guzzle is a PHP HTTP client library",
"homepage": "http://guzzlephp.org/",
"keywords": [
"client",
"curl",
"framework",
"http",
"http client",
"psr-18",
"psr-7",
"rest",
"web service"
],
"support": {
"issues": "https://github.com/guzzle/guzzle/issues",
"source": "https://github.com/guzzle/guzzle/tree/7.3.0"
},
"funding": [
{
"url": "https://github.com/GrahamCampbell",
"type": "github"
},
{
"url": "https://github.com/Nyholm",
"type": "github"
},
{
"url": "https://github.com/alexeyshockov",
"type": "github"
},
{
"url": "https://github.com/gmponos",
"type": "github"
}
],
"time": "2021-03-23T11:33:13+00:00"
},
{
"name": "guzzlehttp/promises",
"version": "1.4.1",
"source": {
"type": "git",
"url": "https://github.com/guzzle/promises.git",
"reference": "8e7d04f1f6450fef59366c399cfad4b9383aa30d"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/guzzle/promises/zipball/8e7d04f1f6450fef59366c399cfad4b9383aa30d",
"reference": "8e7d04f1f6450fef59366c399cfad4b9383aa30d",
"shasum": ""
},
"require": {
"php": ">=5.5"
},
"require-dev": {
"symfony/phpunit-bridge": "^4.4 || ^5.1"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "1.4-dev"
}
},
"autoload": {
"psr-4": {
"GuzzleHttp\\Promise\\": "src/"
},
"files": [
"src/functions_include.php"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Michael Dowling",
"email": "mtdowling@gmail.com",
"homepage": "https://github.com/mtdowling"
}
],
"description": "Guzzle promises library",
"keywords": [
"promise"
],
"support": {
"issues": "https://github.com/guzzle/promises/issues",
"source": "https://github.com/guzzle/promises/tree/1.4.1"
},
"time": "2021-03-07T09:25:29+00:00"
},
{
"name": "guzzlehttp/psr7",
"version": "2.0.0",
"source": {
"type": "git",
"url": "https://github.com/guzzle/psr7.git",
"reference": "1dc8d9cba3897165e16d12bb13d813afb1eb3fe7"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/guzzle/psr7/zipball/1dc8d9cba3897165e16d12bb13d813afb1eb3fe7",
"reference": "1dc8d9cba3897165e16d12bb13d813afb1eb3fe7",
"shasum": ""
},
"require": {
"php": "^7.2.5 || ^8.0",
"psr/http-factory": "^1.0",
"psr/http-message": "^1.0",
"ralouphie/getallheaders": "^3.0"
},
"provide": {
"psr/http-factory-implementation": "1.0",
"psr/http-message-implementation": "1.0"
},
"require-dev": {
"bamarni/composer-bin-plugin": "^1.4.1",
"http-interop/http-factory-tests": "^0.9",
"phpunit/phpunit": "^8.5.8 || ^9.3.10"
},
"suggest": {
"laminas/laminas-httphandlerrunner": "Emit PSR-7 responses"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "2.0-dev"
}
},
"autoload": {
"psr-4": {
"GuzzleHttp\\Psr7\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Michael Dowling",
"email": "mtdowling@gmail.com",
"homepage": "https://github.com/mtdowling"
},
{
"name": "Tobias Schultze",
"homepage": "https://github.com/Tobion"
},
{
"name": "Márk Sági-Kazár",
"email": "mark.sagikazar@gmail.com",
"homepage": "https://sagikazarmark.hu"
}
],
"description": "PSR-7 message implementation that also provides common utility methods",
"keywords": [
"http",
"message",
"psr-7",
"request",
"response",
"stream",
"uri",
"url"
],
"support": {
"issues": "https://github.com/guzzle/psr7/issues",
"source": "https://github.com/guzzle/psr7/tree/2.0.0"
},
"time": "2021-06-30T20:03:07+00:00"
},
{
"name": "psr/http-client",
"version": "1.0.1",
"source": {
"type": "git",
"url": "https://github.com/php-fig/http-client.git",
"reference": "2dfb5f6c5eff0e91e20e913f8c5452ed95b86621"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/php-fig/http-client/zipball/2dfb5f6c5eff0e91e20e913f8c5452ed95b86621",
"reference": "2dfb5f6c5eff0e91e20e913f8c5452ed95b86621",
"shasum": ""
},
"require": {
"php": "^7.0 || ^8.0",
"psr/http-message": "^1.0"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "1.0.x-dev"
}
},
"autoload": {
"psr-4": {
"Psr\\Http\\Client\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "PHP-FIG",
"homepage": "http://www.php-fig.org/"
}
],
"description": "Common interface for HTTP clients",
"homepage": "https://github.com/php-fig/http-client",
"keywords": [
"http",
"http-client",
"psr",
"psr-18"
],
"support": {
"source": "https://github.com/php-fig/http-client/tree/master"
},
"time": "2020-06-29T06:28:15+00:00"
},
{
"name": "psr/http-factory",
"version": "1.0.1",
"source": {
"type": "git",
"url": "https://github.com/php-fig/http-factory.git",
"reference": "12ac7fcd07e5b077433f5f2bee95b3a771bf61be"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/php-fig/http-factory/zipball/12ac7fcd07e5b077433f5f2bee95b3a771bf61be",
"reference": "12ac7fcd07e5b077433f5f2bee95b3a771bf61be",
"shasum": ""
},
"require": {
"php": ">=7.0.0",
"psr/http-message": "^1.0"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "1.0.x-dev"
}
},
"autoload": {
"psr-4": {
"Psr\\Http\\Message\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "PHP-FIG",
"homepage": "http://www.php-fig.org/"
}
],
"description": "Common interfaces for PSR-7 HTTP message factories",
"keywords": [
"factory",
"http",
"message",
"psr",
"psr-17",
"psr-7",
"request",
"response"
],
"support": {
"source": "https://github.com/php-fig/http-factory/tree/master"
},
"time": "2019-04-30T12:38:16+00:00"
},
{
"name": "psr/http-message",
"version": "1.0.1",
"source": {
"type": "git",
"url": "https://github.com/php-fig/http-message.git",
"reference": "f6561bf28d520154e4b0ec72be95418abe6d9363"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/php-fig/http-message/zipball/f6561bf28d520154e4b0ec72be95418abe6d9363",
"reference": "f6561bf28d520154e4b0ec72be95418abe6d9363",
"shasum": ""
},
"require": {
"php": ">=5.3.0"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "1.0.x-dev"
}
},
"autoload": {
"psr-4": {
"Psr\\Http\\Message\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "PHP-FIG",
"homepage": "http://www.php-fig.org/"
}
],
"description": "Common interface for HTTP messages",
"homepage": "https://github.com/php-fig/http-message",
"keywords": [
"http",
"http-message",
"psr",
"psr-7",
"request",
"response"
],
"support": {
"source": "https://github.com/php-fig/http-message/tree/master"
},
"time": "2016-08-06T14:39:51+00:00"
},
{
"name": "psr/simple-cache",
"version": "1.0.1",
"source": {
"type": "git",
"url": "https://github.com/php-fig/simple-cache.git",
"reference": "408d5eafb83c57f6365a3ca330ff23aa4a5fa39b"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/php-fig/simple-cache/zipball/408d5eafb83c57f6365a3ca330ff23aa4a5fa39b",
"reference": "408d5eafb83c57f6365a3ca330ff23aa4a5fa39b",
"shasum": ""
},
"require": {
"php": ">=5.3.0"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "1.0.x-dev"
}
},
"autoload": {
"psr-4": {
"Psr\\SimpleCache\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "PHP-FIG",
"homepage": "http://www.php-fig.org/"
}
],
"description": "Common interfaces for simple caching",
"keywords": [
"cache",
"caching",
"psr",
"psr-16",
"simple-cache"
],
"support": {
"source": "https://github.com/php-fig/simple-cache/tree/master"
},
"time": "2017-10-23T01:57:42+00:00"
},
{
"name": "ralouphie/getallheaders",
"version": "3.0.3",
"source": {
"type": "git",
"url": "https://github.com/ralouphie/getallheaders.git",
"reference": "120b605dfeb996808c31b6477290a714d356e822"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/ralouphie/getallheaders/zipball/120b605dfeb996808c31b6477290a714d356e822",
"reference": "120b605dfeb996808c31b6477290a714d356e822",
"shasum": ""
},
"require": {
"php": ">=5.6"
},
"require-dev": {
"php-coveralls/php-coveralls": "^2.1",
"phpunit/phpunit": "^5 || ^6.5"
},
"type": "library",
"autoload": {
"files": [
"src/getallheaders.php"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Ralph Khattar",
"email": "ralph.khattar@gmail.com"
}
],
"description": "A polyfill for getallheaders.",
"support": {
"issues": "https://github.com/ralouphie/getallheaders/issues",
"source": "https://github.com/ralouphie/getallheaders/tree/develop"
},
"time": "2019-03-08T08:55:37+00:00"
}
],
"packages-dev": [],
"aliases": [],
"minimum-stability": "stable",
"stability-flags": [],
"prefer-stable": false,
"prefer-lowest": false,
"platform": [],
"platform-dev": [],
"plugin-api-version": "2.0.0"
}

31
examples/custom/index.php

@ -0,0 +1,31 @@
<?php
require('_common.php');
use authkit2\Authkit2;
echo html_header();
if (isset($_SESSION['access_token']) && isset($_SESSION['refresh_token']))
{
// Fetch the token for the user so we can make requests on their behalf
$token = Authkit2::get_token($_SESSION['access_token'], $_SESSION['refresh_token']);
// If the token needs to be refreshed, the library will do that for us, but
// we need to set a callback so it can let us know to store the updated token.
$token->setRefreshCallback(function($token) {
echo "Refreshing token...<br>";
$_SESSION['access_token'] = $token->getAccessToken();
$_SESSION['refresh_token'] = $token->getRefreshToken();
});
// Fetch the user's information from the openid provider
$user_info = $token->getUserInfo();
echo "Hello, ".$user_info['name']."!<br>";
echo "Your id is: ".$token->getUserId()."<br>";
echo "<a href=\"logout.php\">Logout</a><br>";
}
else
{
echo "Not signed in.<br>";
echo "<a href=\"login.php\">Login</a><br>";
}
echo html_footer();

13
examples/custom/login.php

@ -0,0 +1,13 @@
<?php
require('_common.php');
use authkit2\Authkit2;
// Initialize the three-legged oauth flow
$flow = new \authkit2\Oidc\Flows\UserFlow(Authkit2::get_client());
// Step 1: Redirect the user to the OIDC provider for authentication
$redirect_url = $flow->getRedirectUrl(OPENID_CALLBACK_URL);
header('Location: '.$redirect_url);

17
examples/custom/logout.php

@ -0,0 +1,17 @@
<?php
require('_common.php');
use authkit2\Authkit2;
// Initialize the three-legged oauth flow
$flow = new \authkit2\Oidc\Flows\UserFlow(Authkit2::get_client());
// Log the user out locally
unset($_SESSION['access_token']);
unset($_SESSION['refresh_token']);
// Redirect them to the openid provider to log them out
$redirect_url = $flow->getLogoutUrl(OPENID_REDIRECT_URL);
header('Location: '.$redirect_url);
Loading…
Cancel
Save