peertube-plugin-auth-yaknet 1.0.8 → 1.0.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -2
- package/client.js +3 -4
- package/main.js +47 -16
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
[](https://opensource.org/licenses/MIT)
|
|
4
4
|
[](https://joinpeertube.org)
|
|
5
|
-
[](https://
|
|
5
|
+
[](https://developer-console.yakhub.com.tr)
|
|
6
6
|
|
|
7
7
|
**English** | [Türkçe](#türkçe)
|
|
8
8
|
|
|
@@ -43,7 +43,7 @@ In your PeerTube admin panel (**Administration > Plugins / Themes > Settings** f
|
|
|
43
43
|
|
|
44
44
|
- **Client ID:** Your registered YakNet OAuth2 Application Client ID.
|
|
45
45
|
- **Client Secret:** Your YakNet OAuth2 Client Secret.
|
|
46
|
-
- **Auth Base URL:** Default is `https://
|
|
46
|
+
- **Auth Base URL:** Default is `https://developer-console.yakhub.com.tr`.
|
|
47
47
|
- **Auto-Redirect on Login:** Automatically bypass the standard login form and redirect users directly to YakNet SSO (also synchronizes `redirect_on_single_external_auth` in PeerTube configuration).
|
|
48
48
|
|
|
49
49
|
---
|
package/client.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
function
|
|
1
|
+
async function register({ registerHook, peertubeHelpers }) {
|
|
2
2
|
try {
|
|
3
3
|
fetch('/plugins/auth-yaknet/router/status')
|
|
4
4
|
.then(function (r) {
|
|
@@ -53,6 +53,5 @@ function registerClient({ registerHook, peertubeHelpers }) {
|
|
|
53
53
|
}
|
|
54
54
|
}
|
|
55
55
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
}
|
|
56
|
+
export { register, register as registerClient };
|
|
57
|
+
export default register;
|
package/main.js
CHANGED
|
@@ -4,11 +4,22 @@ const http = require('http');
|
|
|
4
4
|
|
|
5
5
|
let clientId = '';
|
|
6
6
|
let clientSecret = '';
|
|
7
|
-
let authBaseUrl = 'https://
|
|
7
|
+
let authBaseUrl = 'https://developer-console.yakhub.com.tr';
|
|
8
8
|
|
|
9
|
-
function
|
|
9
|
+
function normalizeAuthUrl(urlStr) {
|
|
10
|
+
if (typeof urlStr === 'string' && urlStr.includes('auth.yakhub.com.tr')) {
|
|
11
|
+
return urlStr.replace('auth.yakhub.com.tr', 'developer-console.yakhub.com.tr');
|
|
12
|
+
}
|
|
13
|
+
return urlStr;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function postRequest(urlStr, data, maxRedirects = 5) {
|
|
10
17
|
return new Promise((resolve, reject) => {
|
|
11
|
-
|
|
18
|
+
if (maxRedirects <= 0) {
|
|
19
|
+
return reject(new Error('Too many redirects while calling YakNet auth server'));
|
|
20
|
+
}
|
|
21
|
+
const safeUrlStr = normalizeAuthUrl(urlStr);
|
|
22
|
+
const url = new URL(safeUrlStr);
|
|
12
23
|
const postData = typeof data === 'string' ? data : new URLSearchParams(data).toString();
|
|
13
24
|
const lib = url.protocol === 'https:' ? https : http;
|
|
14
25
|
|
|
@@ -26,6 +37,12 @@ function postRequest(urlStr, data) {
|
|
|
26
37
|
}
|
|
27
38
|
},
|
|
28
39
|
res => {
|
|
40
|
+
// Follow 301, 302, 307, 308 redirects automatically
|
|
41
|
+
if ([301, 302, 307, 308].includes(res.statusCode) && res.headers.location) {
|
|
42
|
+
const nextUrl = new URL(res.headers.location, safeUrlStr).toString();
|
|
43
|
+
return resolve(postRequest(nextUrl, data, maxRedirects - 1));
|
|
44
|
+
}
|
|
45
|
+
|
|
29
46
|
let body = '';
|
|
30
47
|
res.on('data', chunk => (body += chunk));
|
|
31
48
|
res.on('end', () => {
|
|
@@ -44,9 +61,13 @@ function postRequest(urlStr, data) {
|
|
|
44
61
|
});
|
|
45
62
|
}
|
|
46
63
|
|
|
47
|
-
function getRequest(urlStr, token) {
|
|
64
|
+
function getRequest(urlStr, token, maxRedirects = 5) {
|
|
48
65
|
return new Promise((resolve, reject) => {
|
|
49
|
-
|
|
66
|
+
if (maxRedirects <= 0) {
|
|
67
|
+
return reject(new Error('Too many redirects while calling YakNet auth server'));
|
|
68
|
+
}
|
|
69
|
+
const safeUrlStr = normalizeAuthUrl(urlStr);
|
|
70
|
+
const url = new URL(safeUrlStr);
|
|
50
71
|
const lib = url.protocol === 'https:' ? https : http;
|
|
51
72
|
|
|
52
73
|
const req = lib.request(
|
|
@@ -62,6 +83,12 @@ function getRequest(urlStr, token) {
|
|
|
62
83
|
}
|
|
63
84
|
},
|
|
64
85
|
res => {
|
|
86
|
+
// Follow 301, 302, 307, 308 redirects automatically
|
|
87
|
+
if ([301, 302, 307, 308].includes(res.statusCode) && res.headers.location) {
|
|
88
|
+
const nextUrl = new URL(res.headers.location, safeUrlStr).toString();
|
|
89
|
+
return resolve(getRequest(nextUrl, token, maxRedirects - 1));
|
|
90
|
+
}
|
|
91
|
+
|
|
65
92
|
let body = '';
|
|
66
93
|
res.on('data', chunk => (body += chunk));
|
|
67
94
|
res.on('end', () => {
|
|
@@ -104,16 +131,17 @@ async function register({ registerExternalAuth, registerSetting, settingsManager
|
|
|
104
131
|
name: 'auth-base-url',
|
|
105
132
|
label: 'YakNet Auth URL',
|
|
106
133
|
type: 'input',
|
|
107
|
-
description: 'YakNet SSO Server URL (Default: https://
|
|
134
|
+
description: 'YakNet SSO Server URL (Default: https://developer-console.yakhub.com.tr)',
|
|
108
135
|
private: false,
|
|
109
|
-
default: 'https://
|
|
136
|
+
default: 'https://developer-console.yakhub.com.tr'
|
|
110
137
|
});
|
|
111
138
|
|
|
112
139
|
registerSetting({
|
|
113
140
|
name: 'auto-redirect-login',
|
|
114
|
-
label:
|
|
141
|
+
label: "Giriş Sayfasında Doğrudan YakNet SSO'ya Yönlendir",
|
|
115
142
|
type: 'input-checkbox',
|
|
116
|
-
description:
|
|
143
|
+
description:
|
|
144
|
+
'Aktif olduğunda, kullanıcılar giriş butonuna veya /login sayfasına gittiğinde standart PeerTube şifre formu yerine doğrudan YakNet SSO sunucusuna yönlendirilir.',
|
|
117
145
|
private: false,
|
|
118
146
|
default: true
|
|
119
147
|
});
|
|
@@ -183,7 +211,9 @@ async function register({ registerExternalAuth, registerSetting, settingsManager
|
|
|
183
211
|
const autoRedir = await settingsManager.getSetting('auto-redirect-login');
|
|
184
212
|
if (cid) clientId = cid;
|
|
185
213
|
if (csec) clientSecret = csec;
|
|
186
|
-
if (burl)
|
|
214
|
+
if (burl) {
|
|
215
|
+
authBaseUrl = normalizeAuthUrl(burl.replace(/\/+$/, ''));
|
|
216
|
+
}
|
|
187
217
|
if (autoRedir !== undefined && autoRedir !== null) {
|
|
188
218
|
autoRedirectLogin = autoRedir === true || autoRedir === 'true';
|
|
189
219
|
} else {
|
|
@@ -202,7 +232,7 @@ async function register({ registerExternalAuth, registerSetting, settingsManager
|
|
|
202
232
|
authDisplayName: () => 'YakNet ile Giriş Yap',
|
|
203
233
|
onAuthRequest: (req, res) => {
|
|
204
234
|
const state = crypto.randomBytes(16).toString('hex');
|
|
205
|
-
const authUrl = `${authBaseUrl}/oauth/authorize?client_id=${encodeURIComponent(clientId)}&redirect_uri=${encodeURIComponent(callbackUrl)}&response_type=code&scope=&state=${state}`;
|
|
235
|
+
const authUrl = `${normalizeAuthUrl(authBaseUrl)}/oauth/authorize?client_id=${encodeURIComponent(clientId)}&redirect_uri=${encodeURIComponent(callbackUrl)}&response_type=code&scope=&state=${state}`;
|
|
206
236
|
return res.redirect(authUrl);
|
|
207
237
|
}
|
|
208
238
|
});
|
|
@@ -217,7 +247,7 @@ async function register({ registerExternalAuth, registerSetting, settingsManager
|
|
|
217
247
|
|
|
218
248
|
router.get('/auth', (req, res) => {
|
|
219
249
|
const state = crypto.randomBytes(16).toString('hex');
|
|
220
|
-
const authUrl = `${authBaseUrl}/oauth/authorize?client_id=${encodeURIComponent(clientId)}&redirect_uri=${encodeURIComponent(callbackUrl)}&response_type=code&scope=&state=${state}`;
|
|
250
|
+
const authUrl = `${normalizeAuthUrl(authBaseUrl)}/oauth/authorize?client_id=${encodeURIComponent(clientId)}&redirect_uri=${encodeURIComponent(callbackUrl)}&response_type=code&scope=&state=${state}`;
|
|
221
251
|
return res.redirect(authUrl);
|
|
222
252
|
});
|
|
223
253
|
|
|
@@ -235,7 +265,8 @@ async function register({ registerExternalAuth, registerSetting, settingsManager
|
|
|
235
265
|
}
|
|
236
266
|
|
|
237
267
|
try {
|
|
238
|
-
const
|
|
268
|
+
const targetAuthUrl = normalizeAuthUrl(authBaseUrl);
|
|
269
|
+
const tokenRes = await postRequest(`${targetAuthUrl}/oauth/token`, {
|
|
239
270
|
grant_type: 'authorization_code',
|
|
240
271
|
client_id: clientId,
|
|
241
272
|
client_secret: clientSecret,
|
|
@@ -244,14 +275,14 @@ async function register({ registerExternalAuth, registerSetting, settingsManager
|
|
|
244
275
|
});
|
|
245
276
|
|
|
246
277
|
if (!tokenRes.data || !tokenRes.data.access_token) {
|
|
247
|
-
logger.error('Failed to get access token from YakNet:'
|
|
278
|
+
logger.error('Failed to get access token from YakNet: ' + JSON.stringify(tokenRes));
|
|
248
279
|
return res.redirect('/login?externalAuthError=true');
|
|
249
280
|
}
|
|
250
281
|
|
|
251
282
|
const accessToken = tokenRes.data.access_token;
|
|
252
|
-
const userRes = await getRequest(`${
|
|
283
|
+
const userRes = await getRequest(`${targetAuthUrl}/api/user`, accessToken);
|
|
253
284
|
if (!userRes.data || !userRes.data.email) {
|
|
254
|
-
logger.error('Failed to get user profile from YakNet:'
|
|
285
|
+
logger.error('Failed to get user profile from YakNet: ' + JSON.stringify(userRes));
|
|
255
286
|
return res.redirect('/login?externalAuthError=true');
|
|
256
287
|
}
|
|
257
288
|
|