peertube-plugin-auth-yaknet 1.0.9 → 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.
Files changed (3) hide show
  1. package/client.js +3 -4
  2. package/main.js +41 -11
  3. package/package.json +1 -1
package/client.js CHANGED
@@ -1,4 +1,4 @@
1
- function registerClient({ registerHook, peertubeHelpers }) {
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
- if (typeof module !== 'undefined' && module.exports) {
57
- module.exports = { registerClient: registerClient };
58
- }
56
+ export { register, register as registerClient };
57
+ export default register;
package/main.js CHANGED
@@ -6,9 +6,20 @@ let clientId = '';
6
6
  let clientSecret = '';
7
7
  let authBaseUrl = 'https://developer-console.yakhub.com.tr';
8
8
 
9
- function postRequest(urlStr, data) {
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
- const url = new URL(urlStr);
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
- const url = new URL(urlStr);
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', () => {
@@ -184,7 +211,9 @@ async function register({ registerExternalAuth, registerSetting, settingsManager
184
211
  const autoRedir = await settingsManager.getSetting('auto-redirect-login');
185
212
  if (cid) clientId = cid;
186
213
  if (csec) clientSecret = csec;
187
- if (burl) authBaseUrl = burl.replace(/\/+$/, '');
214
+ if (burl) {
215
+ authBaseUrl = normalizeAuthUrl(burl.replace(/\/+$/, ''));
216
+ }
188
217
  if (autoRedir !== undefined && autoRedir !== null) {
189
218
  autoRedirectLogin = autoRedir === true || autoRedir === 'true';
190
219
  } else {
@@ -203,7 +232,7 @@ async function register({ registerExternalAuth, registerSetting, settingsManager
203
232
  authDisplayName: () => 'YakNet ile Giriş Yap',
204
233
  onAuthRequest: (req, res) => {
205
234
  const state = crypto.randomBytes(16).toString('hex');
206
- 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}`;
207
236
  return res.redirect(authUrl);
208
237
  }
209
238
  });
@@ -218,7 +247,7 @@ async function register({ registerExternalAuth, registerSetting, settingsManager
218
247
 
219
248
  router.get('/auth', (req, res) => {
220
249
  const state = crypto.randomBytes(16).toString('hex');
221
- 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}`;
222
251
  return res.redirect(authUrl);
223
252
  });
224
253
 
@@ -236,7 +265,8 @@ async function register({ registerExternalAuth, registerSetting, settingsManager
236
265
  }
237
266
 
238
267
  try {
239
- const tokenRes = await postRequest(`${authBaseUrl}/oauth/token`, {
268
+ const targetAuthUrl = normalizeAuthUrl(authBaseUrl);
269
+ const tokenRes = await postRequest(`${targetAuthUrl}/oauth/token`, {
240
270
  grant_type: 'authorization_code',
241
271
  client_id: clientId,
242
272
  client_secret: clientSecret,
@@ -245,14 +275,14 @@ async function register({ registerExternalAuth, registerSetting, settingsManager
245
275
  });
246
276
 
247
277
  if (!tokenRes.data || !tokenRes.data.access_token) {
248
- logger.error('Failed to get access token from YakNet:', tokenRes);
278
+ logger.error('Failed to get access token from YakNet: ' + JSON.stringify(tokenRes));
249
279
  return res.redirect('/login?externalAuthError=true');
250
280
  }
251
281
 
252
282
  const accessToken = tokenRes.data.access_token;
253
- const userRes = await getRequest(`${authBaseUrl}/api/user`, accessToken);
283
+ const userRes = await getRequest(`${targetAuthUrl}/api/user`, accessToken);
254
284
  if (!userRes.data || !userRes.data.email) {
255
- logger.error('Failed to get user profile from YakNet:', userRes);
285
+ logger.error('Failed to get user profile from YakNet: ' + JSON.stringify(userRes));
256
286
  return res.redirect('/login?externalAuthError=true');
257
287
  }
258
288
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "peertube-plugin-auth-yaknet",
3
- "version": "1.0.9",
3
+ "version": "1.0.10",
4
4
  "description": "YakNet SSO & OAuth2 Single Sign-On Authentication Plugin for PeerTube",
5
5
  "main": "main.js",
6
6
  "library": "./main.js",