Last active 1731861101

composer.json Raw
1{
2 "require": {
3 "jumbojett/openid-connect-php": "^1.0"
4 }
5}
6
config.php_example Raw
1<?php
2define('OIDC_URL', 'https://provider');
3define('OIDC_ID', 'appid');
4define('OIDC_SECRET', 'secret');
5
index_partial.php Raw
1<?php // -*-c++-*-
2
3# debug mode, don't do that on a live service
4ini_set('display_errors', 1);
5ini_set('display_startup_errors', 1);
6error_reporting(E_ALL);
7
8require __DIR__ . '/vendor/autoload.php';
9
10use Jumbojett\OpenIDConnectClient;
11
12require_once("config.php");
13
14if(isset($_COOKIE['access_token'])) {
15 $oidc = new OpenIDConnectClient(OIDC_URL, OIDC_ID, OIDC_SECRET);
16 $oidc->addScope(['email']);
17
18 $data = $oidc->introspectToken($_COOKIE['access_token']);
19 if(!$data->active) {
20 // assume we need to refresh
21 if(isset($_COOKIE['refresh_token'])) {
22 $oidc->refreshToken($_COOKIE['refresh_token']);
23 $data = $oidc->introspectToken($oidc->getAccessToken());
24 if(!$data->active) {
25 echo "refreshToken didn't work\n";
26 http_response_code(403);
27 exit;
28 }
29 } else {
30 echo "no refresh token not available\n";
31 http_response_code(403);
32 exit;
33 }
34 $tokenExpire=$data->exp;
35 setcookie('access_token', $oidc->getAccessToken(), $tokenExpire, "", "", true, true);
36 } else {
37 $id=$data->email;
38 }
39 } else {
40
41session_start();
42
43if(isset($_SESSION['oidc'])) {
44 $oidc=$_SESSION['oidc'];
45
46 try {
47 $id=$oidc->requestUserInfo('email');
48 } catch(Exception $e) {
49 try {
50 $oidc->authenticate();
51 $id=$oidc->requestUserInfo('email');
52 } catch(Exception $e) {
53 echo "reauthentication failed.";
54 exit(0);
55 }
56 }
57
58 if(!isset($id)) {
59 echo "could not get login name from oidc session\n";
60 exit(0);
61 }
62
63# if we are on the first request from the oidc provider, send a redirect
64 if(isset($_GET['code'])) {
65 header("Location: .");
66 exit(0);
67 }
68} else {
69
70 try {
71 $oidc = new OpenIDConnectClient(OIDC_URL, OIDC_ID, OIDC_SECRET);
72 $oidc->addScope(["email"]);
73 $oidc->authenticate();
74 #$name = $oidc->requestUserInfo('user_id');
75 } catch (Exception $e) {
76 echo '<pre>Caught exception: ', $e->getMessage(), "\n";
77 echo $e->getTraceAsString(), "</pre>\n";
78 exit(0);
79 }
80 $_SESSION['oidc']=$oidc;
81
82 if(isset($_GET['code'])) {
83 header("Location: .");
84 exit(0);
85 }
86}
87}
88
89if(isset($_GET['token']) && $_GET['token']) {
90 echo "<pre>\n";
91 echo "access_token=".$oidc->getAccessToken()."\n";
92 echo "refresh_token=".$oidc->getRefreshToken()."\n";
93 echo "</pre>\n";
94 exit;
95}
96
97