-
Notifications
You must be signed in to change notification settings - Fork 1
/
open-oauth-client.php
139 lines (114 loc) · 4.6 KB
/
open-oauth-client.php
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
<?php
/*
Plugin Name: Open OAuth Client
Plugin URI: https://github.com/wlalele/open-oauth-client
Description: Just a simple oauth client for WordPress
Author: Amine Dai
Version: 0.1.8
Author URI: https://github.com/wlalele
License: GPLv2
Text Domain: open-oauth-client
*/
require_once 'open-oauth-client.functions.php';
require_once 'classes/Controller.php';
require_once 'classes/Request.php';
require_once 'classes/Session.php';
if (!class_exists('open_oauth_client')) {
class open_oauth_client
{
private $controller;
public function __construct()
{
$this->controller = new Controller();
}
final public function init(): void
{
add_action('init', [$this, 'router']);
add_action('admin_menu', [$this, 'addMenuPage']);
}
final public function addMenuPage(): void
{
add_menu_page(
'Open OAuth Client',
'Open OAuth',
'manage_options',
'open_oauth_client',
'layout'
);
}
final private function startsWith(string $haystack, string $needle): bool
{
$length = strlen($needle);
return substr($haystack, 0, $length) === $needle;
}
final public function router(): void
{
$isLoginPage = $this->startsWith(Request::getUri(), '/wp-login');
// don't do anything on wp-login, that's a no-go
if ($isLoginPage) {
return;
}
if (Request::getMethod() === Request::METHOD_POST) {
$action = Request::getPostParameter('action');
$configurationNonce = Request::getPostParameter('configuration_nonce');
$attributesNonce = Request::getPostParameter('attributes_nonce');
// edit configuration
if (isset($action, $configurationNonce) && 'configuration' === $action && !empty($configurationNonce)) {
$this->controller->postConfiguration();
}
// edit attributes
if (isset($action, $attributesNonce) && 'attributes' === $action && !empty($attributesNonce)) {
$this->controller->postAttributes();
}
return;
}
$isLoggedIn = is_user_logged_in();
$debug = (bool) Request::getQueryParameter('debug');
$auth = Request::getQueryParameter('auth');
$code = Request::getQueryParameter('code');
$state = Request::getQueryParameter('state');
$error = Request::getQueryParameter('error');
$errorDescription = Request::getQueryParameter('error_description');
$isValidState = isset($state) && base64_decode($state) === get_option('open_oauth_app_name');
$forceAuth = get_option('open_oauth_force_auth', false);
if ($isLoggedIn && $debug === false && !isset($code) && !$isValidState) {
return;
}
// error returned from authentication server
if ($isValidState && isset($error)) {
var_dump($error, $errorDescription);die;
}
// normal process of callback after authorization
if ($isValidState && isset($code)) {
$accessToken = $this->controller->retrieveAccessToken($code);
if (isset($accessToken['error'])) {
//var_dump('accessToken', $accessToken); die;
header('Location: ' . home_url());
exit;
}
$userInfo = $this->controller->retrieveUserInfo($accessToken['access_token']);
if (isset($userInfo['error'])) {
var_dump('userInfo', $userInfo); die;
}
if ($userInfo && '1' === get_option('open_oauth_debug')) {
echo '<pre>' . print_r($userInfo, true) . '</pre>';
update_option('open_oauth_debug', false);
exit;
}
if ($userInfo) {
$this->controller->processLogin($userInfo);
}
}
if (!$forceAuth && $auth !== 'sso') {
return;
}
// save referrer in session and
// get authorization (fetch token from provider)
Session::start();
Session::set(Session::REFERRER_NAME, Request::getUri());
$this->controller->getAuthorization();
}
}
$plugin = new open_oauth_client();
$plugin->init();
}