Repository navigation
Expand file tree
/
Copy pathOpenID.php
More file actions
232 lines (196 loc) · 8.4 KB
/
Copy pathOpenID.php
File metadata and controls
232 lines (196 loc) · 8.4 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
<?php
// Copyright (c) Tribufu. All Rights Reserved.
// SPDX-License-Identifier: MIT
namespace Paymenter\Extensions\Others\OpenID;
use App\Actions\Auth\Login;
use App\Attributes\ExtensionMeta;
use App\Classes\Extension\Extension;
use App\Helpers\ExtensionHelper;
use App\Models\User;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Contracts\View\View as ViewContract;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\View;
use Illuminate\Support\Str;
use Laravel\Socialite\Facades\Socialite;
use Laravel\Socialite\Two\InvalidStateException;
use Paymenter\Extensions\Others\OpenID\Models\Identity;
#[ExtensionMeta(
name: 'OpenID Connect',
description: 'Sign in with an OpenID Connect provider.',
version: '1.0.0',
author: 'Tribufu',
url: 'https://github.com/tribufu/paymenter-openid',
)]
class OpenID extends Extension
{
public function getConfig($values = [])
{
return [
[
'name' => 'issuer',
'label' => 'Issuer URL',
'type' => 'text',
'required' => true,
'description' => 'The discovery document is read from <issuer>/.well-known/openid-configuration. It may be an internal address when the provider gives the browser its public one, as Keycloak does.',
],
[
'name' => 'client_id',
'label' => 'Client ID',
'type' => 'text',
'required' => true,
],
[
'name' => 'client_secret',
'label' => 'Client Secret',
'type' => 'password',
'required' => true,
'encrypted' => true,
],
[
'name' => 'provider_name',
'label' => 'Provider Name',
'type' => 'text',
'required' => true,
'default' => 'OpenID',
'description' => 'Shown on the sign in button.',
],
[
'name' => 'link_existing_accounts',
'label' => 'Link Existing Accounts',
'type' => 'checkbox',
'required' => false,
'description' => 'On a first sign in, bind the account that already has the verified email of the user. Enable it only for a provider you trust to verify emails, since whoever controls an email there takes over its account here, administrators included.',
],
];
}
public function installed()
{
ExtensionHelper::runMigrations('extensions/Others/OpenID/database/migrations');
}
public function uninstalled()
{
ExtensionHelper::rollbackMigrations('extensions/Others/OpenID/database/migrations');
}
public function upgraded($oldVersion = null)
{
$this->installed();
}
public function boot()
{
require __DIR__ . '/routes/web.php';
View::addNamespace('openid', __DIR__ . '/resources/views');
app('translator')->addJsonPath(__DIR__ . '/lang');
Event::listen('auth.login', function () {
return ['view' => view('openid::button', ['name' => $this->config('provider_name')])];
});
}
public function redirect(): RedirectResponse
{
return $this->provider()->redirect();
}
// A failed sign in shows its own page instead of going back to the login page, which may lead to the provider
// again and loop while the provider keeps its session.
public function callback(Request $request): RedirectResponse|ViewContract
{
$name = $this->config('provider_name');
if ($request->has('error')) {
return $this->error(__('Sign in with :name was cancelled.', ['name' => $name]));
}
$provider = $this->provider();
try {
$oauthUser = $provider->user();
} catch (InvalidStateException) {
return $this->error(__('Your sign in expired. Please try again.'));
} catch (GuzzleException $exception) {
report($exception);
return $this->error(__(':name could not be reached. Please try again later.', ['name' => $name]));
}
// The account is the one bound to the subject the provider gives the user, which outlives a change of email.
$issuer = $this->endpoints()['issuer'];
$user = Identity::where('issuer', $issuer)->where('subject', $oauthUser->getId())->first()?->user;
if (!$user) {
$email = $oauthUser->getEmail();
// Some providers send the claim as a string.
$verified = in_array($oauthUser->user['email_verified'] ?? false, [true, 'true'], true);
if (!is_string($email) || $email === '' || !$verified) {
return $this->error(__('Verify the email address of your :name account, then try again.', ['name' => $name]));
}
$user = User::where('email', $email)->first();
if ($user) {
// Binding by email trusts the provider with the account of that email, so it is opt in.
if (!$this->config('link_existing_accounts')) {
return $this->error(__('This email address already has an account here.'));
}
// An account already bound to another user of the provider is not taken over by its email.
if (Identity::where('issuer', $issuer)->where('user_id', $user->id)->exists()) {
return $this->error(__('This email address belongs to another :name account.', ['name' => $name]));
}
} else {
if (config('settings.registration_disabled', false)) {
return $this->error(__('Registration is disabled.'));
}
$user = User::create([
'email' => $email,
// The account pages of Paymenter need both names as text, even when the provider has no last name.
'first_name' => $oauthUser->user['given_name'] ?? $oauthUser->getNickname() ?? '',
'last_name' => $oauthUser->user['family_name'] ?? '',
'password' => Hash::make(Str::random(64)),
'email_verified_at' => now(),
]);
}
Identity::create(['user_id' => $user->id, 'issuer' => $issuer, 'subject' => $oauthUser->getId()]);
}
// Kept for a logout that also ends the session of the provider, with endSessionUrl.
Session::put('openid.id_token', $provider->getIdToken());
if ($user->tfa_secret) {
Session::put('2fa', [
'user_id' => $user->id,
'remember' => true,
'expires' => now()->addMinutes(5),
]);
return redirect()->route('2fa');
}
(new Login)->execute($user, true);
return redirect()->intended(route('dashboard'));
}
private function error(string $message): ViewContract
{
return view('openid::error', ['message' => $message]);
}
/**
* Address that ends the session of the provider and sends the browser back to Paymenter.
*/
public function endSessionUrl(?string $idToken): string
{
$parameters = [
'client_id' => $this->config('client_id'),
'post_logout_redirect_uri' => url('/'),
];
if ($idToken) {
$parameters['id_token_hint'] = $idToken;
}
return $this->endpoints()['end_session_endpoint'] . '?' . http_build_query($parameters);
}
private function provider(): OpenIDProvider
{
return Socialite::buildProvider(OpenIDProvider::class, [
'client_id' => $this->config('client_id'),
'client_secret' => $this->config('client_secret'),
'redirect' => route('extensions.others.openid.callback'),
])->setEndpoints($this->endpoints())->enablePKCE();
}
private function endpoints(): array
{
$issuer = rtrim($this->config('issuer'), '/');
return Cache::remember('openid.discovery.' . sha1($issuer), now()->addHour(), function () use ($issuer) {
return Http::get($issuer . '/.well-known/openid-configuration')->throw()->json();
});
}
}