forked from joshp23/YOURLS-OIDC
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplugin.php
More file actions
186 lines (149 loc) · 5.3 KB
/
plugin.php
File metadata and controls
186 lines (149 loc) · 5.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<?php
/*
* Plugin Name: OpenID Connect
* Plugin URI: https://github.com/gsu-library/YOURLS-OIDC/
* Description: Enables OpenID Connect user authentication.
* Version: 1.0.0
* Author: Georgia State University Library
* Author URI: https://library.gsu.edu/
*/
defined('YOURLS_ABSPATH') || exit;
if(!class_exists('Oidc_Auth')) {
/**
* Oidc_Auth class
*/
class Oidc_Auth {
private $oidc;
/**
* Setup YOURLS hooks.
*/
function __construct() {
yourls_add_filter('is_valid_user', [$this, 'is_valid_user']);
yourls_add_action('logout', [$this, 'logout']);
yourls_add_action('login_form_top', [$this, 'login_form_top']);
yourls_add_action('login_form_end', [$this, 'login_form_end']);
}
/**
* Checks and loads OpenID configuration. Returns true if configuration is successful.
*
* @return bool
*/
private function load_configuration() {
// Autoloader.
if(!file_exists(__DIR__ . '/vendor/autoload.php')) {
trigger_error('Autoloader not found for YOURLS-OIDC plugin, run \'composer install\' in ' . __DIR__, E_USER_WARNING);
return false;
}
// Check and load configuration.
if(!defined('OIDC_PROVIDER_URL') ||
empty(OIDC_PROVIDER_URL) ||
!defined('OIDC_CLIENT_ID') ||
empty(OIDC_CLIENT_ID)
) {
trigger_error('Required configuration not found for YOURLS-OIDC, please see the readme.', E_USER_WARNING);
return false;
}
$clientSecret = '';
if(defined('OIDC_CLIENT_SECRET')) {
$clientSecret = OIDC_CLIENT_SECRET;
}
require_once(__DIR__ . '/vendor/autoload.php');
$this->oidc = new Jumbojett\OpenIDConnectClient(OIDC_PROVIDER_URL, OIDC_CLIENT_ID, $clientSecret);
// Set authentication methods.
if(defined('OIDC_AUTH_METHODS') && !empty(OIDC_AUTH_METHODS)) {
$this->oidc->setTokenEndpointAuthMethodsSupported(OIDC_AUTH_METHODS);
}
// Set redirect URL.
if(defined('OIDC_REDIRECT_URL') && !empty(OIDC_REDIRECT_URL)) {
$this->oidc->setRedirectURL(OIDC_REDIRECT_URL);
}
// Set scopes.
if(defined('OIDC_SCOPES') && !empty(OIDC_SCOPES)) {
$this->oidc->addScope(OIDC_SCOPES);
}
// Setup endpoint configuration.
$configParams = [];
if(defined('OIDC_TOKEN_ENDPOINT') && !empty(OIDC_TOKEN_ENDPOINT)) {
$configParams['token_endpoint'] = OIDC_TOKEN_ENDPOINT;
}
if(defined('OIDC_USER_INFO_ENDPOINT') && !empty(OIDC_USER_INFO_ENDPOINT)) {
$configParams['userinfo_endpoint'] = OIDC_USER_INFO_ENDPOINT;
}
if(defined('OIDC_LOGOUT_ENDPOINT') && !empty(OIDC_LOGOUT_ENDPOINT)) {
$configParams['end_session_endpoint'] = OIDC_LOGOUT_ENDPOINT;
}
if(!empty($configParams)) {
$this->oidc->providerConfigParam($configParams);
}
return true;
}
/**
* Authorizes user through OIDC Connect client and validates the user. Returns a bool on
* whether the user was validated.
*
* @param bool $is_valid If the user is valid or not.
* @return boolean
*/
public function is_valid_user($is_valid) {
if(!$this->load_configuration()) {
return $is_valid;
}
// If already valid or is API return.
if($is_valid || yourls_is_API()) {
return $is_valid;
}
// Has all of the user/password combos at this point.
global $yourls_user_passwords;
// Authenticate and request user info
$this->oidc->authenticate();
$userInfo = $this->oidc->requestUserInfo();
if(defined('OIDC_USERNAME_FIELD') && !empty(OIDC_USERNAME_FIELD)) {
$user = $userInfo->{OIDC_USERNAME_FIELD};
} else {
$user = $userInfo->user_name;
}
if(array_key_exists($user, $yourls_user_passwords)) {
yourls_set_user($user);
yourls_redirect('.'); // clears URL parameters
return true;
}
return false;
}
/**
* Logs out the user from YOURLS and OpenID.
*
* @return void
*/
public function logout() {
if(!$this->load_configuration()) {
return;
}
yourls_store_cookie('');
// todo: save and pass token id?
$this->oidc->signOut(null, YOURLS_SITE);
}
/**
* Hides the login form and displays error message (only seen if YOURLS account does not exist).
*
* @return void
*/
public function login_form_top() {
$message = 'User account not found.';
if(defined('OIDC_ERROR_MESSAGE') && !empty(OIDC_ERROR_MESSAGE)) {
$message = OIDC_ERROR_MESSAGE;
}
echo "<style>.error{display:none;}</style>";
echo "<p class='error' style='display:inline !important;'>$message</p>";
echo "\n<!--\n";
}
/**
* Closing tag to hide the login form.
*
* @return void
*/
public function login_form_end() {
echo "\n-->\n";
}
}
$oidcAuth = new Oidc_Auth();
}