Repository navigation
Expand file tree
/
Copy pathsc-api-auth.mjs
More file actions
1014 lines (902 loc) · 31.3 KB
/
Copy pathsc-api-auth.mjs
File metadata and controls
1014 lines (902 loc) · 31.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
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env node
/**
* SoundCloud OAuth 2.1 (PKCE) + local callback, or remote pairing code flow, then POST /me/apps.
* Node.js stdlib only (no npm dependencies).
* @see https://developers.soundcloud.com/docs/api/guide#authentication
*/
import crypto from "node:crypto";
import { spawn } from "node:child_process";
import http from "node:http";
import { hostname } from "node:os";
import { basename } from "node:path";
import { parseArgs } from "node:util";
import process from "node:process";
const SCRIPT_NAME = basename(process.argv[1] ?? "sc-api-auth.mjs");
const SOUNDCLOUD_AUTHORIZE = "https://secure.soundcloud.com/authorize";
const SOUNDCLOUD_TOKEN = "https://secure.soundcloud.com/oauth/token";
const PAIRING_ACTIVATE_BASE = "https://secure.soundcloud.com/activate";
const APP_REGISTRATION_API = "https://api-reg.soundcloud.com";
const ME_APPS_URL = new URL("/me/apps", APP_REGISTRATION_API).toString();
const PAIRING_SIGN_IN_SCOPE = "";
const PAIRING_DEVICE_LABEL = "SoundCloud API CLI";
function pairingDeviceName() {
const host = hostname();
if (/\.local$/i.test(host)) return host;
return PAIRING_DEVICE_LABEL;
}
const BUNDLED_REDIRECT_URI = "http://127.0.0.1:8765/callback";
const CALLBACK_HOST = "127.0.0.1";
const CALLBACK_PORT = 8765;
const CALLBACK_PATH = "/callback";
const CREATE_APP_ERROR_MESSAGES = {
user_already_has_application: "You already have a registered application.",
application_creation_not_available:
"Application registration is not available for your account. You may need an Artist Pro subscription.",
application_name_not_allowed: "This application name is not allowed.",
application_website_not_allowed: "This website URL is not allowed.",
};
const UPDATE_REDIRECT_ERROR_MESSAGES = {
application_redirect_not_allowed: "This redirect URI is not allowed.",
};
/** Public SoundCloud sign-in app for this CLI (OAuth + PKCE token exchange). Scoped to this tool only. */
const BUNDLED_CLIENT_ID = "nXIZT4VQQYkgHs75vpIYbnINQciCkV5Y";
const API_ERROR_DETAIL_MAX_LENGTH = 500;
const MAX_PAIRING_GATEWAY_TIMEOUTS = 5;
function truncateErrorDetail(text) {
if (text.length <= API_ERROR_DETAIL_MAX_LENGTH) return text;
return `${text.slice(0, API_ERROR_DETAIL_MAX_LENGTH)}…`;
}
function apiErrorDetail(text) {
if (!text) return "";
try {
const data = JSON.parse(text);
if (typeof data?.error === "string") return JSON.stringify({ error: data.error });
const first = data?.errors?.[0];
if (first) return JSON.stringify(first);
return truncateErrorDetail(text);
} catch {
return truncateErrorDetail(text);
}
}
function parseCliArgs(config) {
try {
return parseArgs(config);
} catch (e) {
console.error(e.message);
process.exit(1);
}
}
function parseStructuredApiError(text) {
try {
const first = JSON.parse(text).errors?.[0];
if (!first) return null;
return { code: first.code, error_message: first.error_message };
} catch {
return null;
}
}
function createAppUserMessage({ code, error_message: apiMessage }) {
if (apiMessage) return apiMessage;
if (code && CREATE_APP_ERROR_MESSAGES[code]) return CREATE_APP_ERROR_MESSAGES[code];
return "You are not allowed to create an application.";
}
function updateRedirectUserMessage({ code, error_message: apiMessage }) {
if (apiMessage) return apiMessage;
if (code && UPDATE_REDIRECT_ERROR_MESSAGES[code]) return UPDATE_REDIRECT_ERROR_MESSAGES[code];
return "Could not update redirect URI.";
}
function validateAppRedirectUri(redirectUri) {
const trimmed = redirectUri?.trim();
if (!trimmed) {
throw new Error("Missing required argument: --redirect-uri");
}
let url;
try {
url = new URL(trimmed);
} catch {
throw new Error("Invalid redirect URI: must be a valid absolute URL.");
}
if (url.protocol !== "http:" && url.protocol !== "https:") {
throw new Error("Invalid redirect URI: only http and https URLs are supported.");
}
return trimmed;
}
function base64url(buf) {
return buf
.toString("base64")
.replace(/\+/g, "-")
.replace(/\//g, "_")
.replace(/=+/g, "");
}
function newPkcePair() {
const codeVerifier = base64url(crypto.randomBytes(32));
const codeChallenge = base64url(crypto.createHash("sha256").update(codeVerifier).digest());
return { codeVerifier, codeChallenge };
}
function callbackPathVariants(callbackPath) {
const p = callbackPath;
if (p === "/") return [p];
const withSlash = p.endsWith("/") ? p : `${p}/`;
const noSlash = p.replace(/\/+$/, "") || "/";
if (p === withSlash) return [noSlash, p];
return [noSlash, withSlash];
}
function parseRequestQuery(req) {
const u = new URL(req.url ?? "/", `http://${req.headers.host ?? "127.0.0.1"}`);
return Object.fromEntries(u.searchParams.entries());
}
function parseRequestPathname(req) {
const u = new URL(req.url ?? "/", `http://${req.headers.host ?? "127.0.0.1"}`);
return u.pathname || "/";
}
async function readRequestBody(req) {
const chunks = [];
for await (const chunk of req) {
chunks.push(chunk);
}
return Buffer.concat(chunks).toString("utf8");
}
function parseUrlEncodedBody(body) {
if (!body) return {};
return Object.fromEntries(new URLSearchParams(body).entries());
}
function readOAuthCallbackParams({ query, body }) {
return {
code: query.code ?? body.code,
state: query.state ?? body.state,
error: query.error ?? body.error,
error_description: query.error_description ?? body.error_description,
};
}
function sendHtml(res, status, body) {
if (res.writableEnded) return;
res.writeHead(status, { "content-type": "text/html; charset=utf-8" });
res.end(body);
}
function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
function openBrowser(url) {
let command;
let args;
if (process.platform === "darwin") {
command = "open";
args = [url];
} else if (process.platform === "win32") {
command = "cmd";
args = ["/c", "start", "", url];
} else {
command = "xdg-open";
args = [url];
}
const child = spawn(command, args, { detached: true, stdio: "ignore" });
child.once("error", () => {
console.error("Could not open a browser automatically. Open the sign-in link printed above.");
});
child.unref();
}
async function tokenExchange({ clientId, code, codeVerifier, redirectUri }) {
const body = new URLSearchParams({
grant_type: "authorization_code",
client_id: clientId,
redirect_uri: redirectUri,
code_verifier: codeVerifier,
code,
});
const res = await fetch(SOUNDCLOUD_TOKEN, {
method: "POST",
headers: {
accept: "application/json; charset=utf-8",
"content-type": "application/x-www-form-urlencoded",
},
body: body.toString(),
});
const text = await res.text();
if (!res.ok) {
throw new Error(`Token exchange failed: ${res.status} ${apiErrorDetail(text)}`);
}
return JSON.parse(text);
}
/**
* App-registration API uses `Authorization: OAuth <access_token>`.
*/
async function soundcloudApiRequest({ accessToken, endpoint, method = "GET", body }) {
const headers = {
accept: "application/json; charset=utf-8",
authorization: `OAuth ${accessToken}`,
};
if (body !== undefined) {
headers["content-type"] = "application/json";
}
const res = await fetch(endpoint, {
method,
headers,
...(body !== undefined ? { body: JSON.stringify(body) } : {}),
});
return { res, text: await res.text() };
}
async function listUserApps({ accessToken }) {
const { res, text } = await soundcloudApiRequest({ accessToken, endpoint: ME_APPS_URL });
if (!res.ok) {
throw new Error(`List apps failed: ${res.status} ${apiErrorDetail(text)}`);
}
return JSON.parse(text);
}
function findUserApp(page, { notFoundMessage } = {}) {
const apps = appsFromPage(page);
if (apps.length === 0) {
throw new Error(notFoundMessage ?? "No application credentials were found for your account.");
}
return apps[0];
}
function selectAppForUpdate(page, { clientId } = {}) {
const apps = appsFromPage(page);
if (apps.length === 0) {
throw new Error("No registered application found. Run without update-redirect to register one.");
}
const normalizedId = clientId?.trim();
if (normalizedId) {
const app = apps.find((item) => String(item.client_id) === normalizedId);
if (!app) {
const ids = apps.map((a) => a.client_id).filter(Boolean);
throw new Error(
`No application with client_id ${JSON.stringify(normalizedId)}. Registered client_id values: ${ids.join(", ")}`
);
}
return app;
}
if (apps.length > 1) {
const ids = apps.map((a) => a.client_id).filter(Boolean);
throw new Error(
`Multiple applications found. Pass --client-id to choose one. Registered client_id values: ${ids.join(", ")}\n` +
`Tip: run \`${SCRIPT_NAME} list-apps\` to inspect apps.`
);
}
return apps[0];
}
/** PUT /me/apps/{urn} expects the credentials URN (e.g. soundcloud:credentials:456), not the application URN. */
function credentialsUrn(app) {
if (typeof app?.credentials === "string") return app.credentials;
return null;
}
function credentialsFromApp(app) {
return {
client_id: app.client_id,
client_secret: app.client_secret,
name: app.name,
description: app.description,
website: app.url,
redirect_uri: app.redirect_uri,
};
}
function credentialsFromAppsPage(page) {
return credentialsFromApp(findUserApp(page));
}
function appsFromPage(page) {
return (page?.collection ?? []).filter((item) => item?.client_id);
}
function listAppPublicFields(app) {
const fields = {};
for (const [key, value] of [
["client_id", app.client_id],
["name", app.name],
["description", app.description],
["website", app.url],
["redirect_uri", app.redirect_uri],
]) {
if (value !== undefined && value !== null && value !== "") {
fields[key] = value;
}
}
return fields;
}
function formatListAppsOutput(apps) {
return `${JSON.stringify(apps, null, 2)}\n`;
}
function printUpdateRedirectRequestDebug({ endpoint, body, urn, app, responseStatus, responseText }) {
console.error("");
console.error("Update redirect request (for debugging):");
console.error(` PUT ${endpoint}`);
console.error(` Body: ${JSON.stringify(body)}`);
if (app) {
console.error(` Selected app client_id: ${app.client_id ?? "(none)"}`);
console.error(` Selected credentials URN: ${urn ?? "(none)"}`);
}
if (responseStatus !== undefined) {
console.error(` Response status: ${responseStatus}`);
}
if (responseText !== undefined && responseText !== "") {
console.error(` Response body: ${responseText}`);
}
console.error("");
console.error("Equivalent curl (replace YOUR_ACCESS_TOKEN):");
const escapedUrl = endpoint.replace(/'/g, "'\\''");
const bodyJson = JSON.stringify(body);
console.error(` curl -sS -X PUT '${escapedUrl}' \\`);
console.error(` -H 'Authorization: OAuth YOUR_ACCESS_TOKEN' \\`);
console.error(` -H 'Content-Type: application/json' \\`);
console.error(` -d '${bodyJson}'`);
console.error("");
}
function credentialsWithRedirectUri(app, redirectUri) {
return { ...credentialsFromApp(app), redirect_uri: redirectUri };
}
function publicCredentialFields(credentials) {
const fields = {};
for (const key of ["client_id", "client_secret", "name", "description", "website", "redirect_uri"]) {
if (credentials[key] !== undefined && credentials[key] !== null) {
fields[key] = credentials[key];
}
}
return fields;
}
function formatCredentialOutput(credentials) {
const publicCredentials = publicCredentialFields(credentials);
const lines = [`client_id=${publicCredentials.client_id}`];
if (publicCredentials.client_secret) {
lines.push(`client_secret=${publicCredentials.client_secret}`);
}
lines.push("", JSON.stringify(publicCredentials, null, 2), "");
return lines.join("\n");
}
function pairingActivateUrl(code) {
return `${PAIRING_ACTIVATE_BASE}/${encodeURIComponent(code)}`;
}
function appRegUrl(path) {
return new URL(path, APP_REGISTRATION_API);
}
function appRegUrlWithParams(path, searchParams = {}) {
const url = appRegUrl(path);
for (const [key, value] of Object.entries(searchParams)) {
url.searchParams.set(key, value);
}
return url;
}
function accessTokenFromPairingSignIn(data) {
return data?.session?.access_token ?? data?.access_token ?? null;
}
async function createPairingCode({ clientId, device }) {
const res = await fetch(appRegUrlWithParams("/pairing/codes", { client_id: clientId }), {
method: "POST",
headers: {
accept: "application/json; charset=utf-8",
"content-type": "application/json",
},
body: JSON.stringify({ device }),
});
const text = await res.text();
if (!res.ok) {
throw new Error(`Create pairing code failed: ${res.status} ${apiErrorDetail(text)}`);
}
const data = JSON.parse(text);
if (!data.code || !data.poll_token) {
throw new Error("Pairing code response missing code or poll_token.");
}
return data;
}
async function checkPairingCodeStatus({ clientId, code, pollToken }) {
const res = await fetch(
appRegUrlWithParams(`/pairing/codes/${encodeURIComponent(code)}`, {
client_id: clientId,
poll_token: pollToken,
}),
{
method: "GET",
headers: {
accept: "application/json; charset=utf-8",
},
}
);
const text = await res.text();
let data;
try {
data = text ? JSON.parse(text) : null;
} catch {
data = null;
}
return { res, text, data };
}
async function signInWithPairingCode({ clientId, code, pollToken }) {
const res = await fetch(appRegUrlWithParams("/pairing/sign-in", { client_id: clientId }), {
method: "POST",
headers: {
accept: "application/json; charset=utf-8",
"content-type": "application/json",
},
body: JSON.stringify({
client_id: clientId,
pairing_code: code,
poll_token: pollToken,
scope: PAIRING_SIGN_IN_SCOPE,
}),
});
const text = await res.text();
let data;
try {
data = text ? JSON.parse(text) : null;
} catch {
data = null;
}
return { res, text, data };
}
async function waitForPairingActivation({ clientId, code, pollToken, pollIntervalSeconds, timeoutMs }) {
const deadline = Date.now() + timeoutMs;
const parsedInterval = Number.parseInt(String(pollIntervalSeconds ?? 3), 10);
let intervalMs = Math.max(1000, (Number.isFinite(parsedInterval) ? parsedInterval : 3) * 1000);
let gatewayTimeouts = 0;
while (Date.now() < deadline) {
const status = await checkPairingCodeStatus({ clientId, code, pollToken });
if (status.res.status === 504) {
gatewayTimeouts += 1;
if (gatewayTimeouts >= MAX_PAIRING_GATEWAY_TIMEOUTS) {
throw new Error("Pairing status repeatedly timed out. Try again later.");
}
await sleep(intervalMs);
continue;
}
if (status.res.status === 404) {
await sleep(intervalMs);
continue;
}
if (!status.res.ok) {
throw new Error(`Pairing status failed: ${status.res.status} ${apiErrorDetail(status.text)}`);
}
if (status.data?.status === "expired") {
throw new Error("Pairing code expired. Run the CLI again to get a new code.");
}
if (status.data?.status !== "activated") {
await sleep(intervalMs);
continue;
}
const { res, text, data } = await signInWithPairingCode({ clientId, code, pollToken });
if (res.status === 404) {
throw new Error("Pairing code is no longer valid. Run the CLI again to get a new code.");
}
if (!res.ok) {
throw new Error(`Pairing sign-in failed: ${res.status} ${apiErrorDetail(text)}`);
}
const accessToken = accessTokenFromPairingSignIn(data);
if (!accessToken) {
throw new Error("No access_token in pairing sign-in response.");
}
return { access_token: accessToken };
}
throw new Error("Timed out waiting for pairing code activation (10 minutes).");
}
async function authenticateRemote({ clientId }) {
const device = {
id: crypto.randomUUID(),
type: "cli",
name: pairingDeviceName(),
};
const pairing = await createPairingCode({ clientId, device });
const activateUrl = pairingActivateUrl(pairing.code);
console.log(`Sign in at:\n${activateUrl}\n`);
console.log("Waiting for authorization...\n");
try {
openBrowser(activateUrl);
} catch {
/* user can open the URL manually */
}
return waitForPairingActivation({
clientId,
code: pairing.code,
pollToken: pairing.poll_token,
pollIntervalSeconds: pairing.poll_interval_seconds,
timeoutMs: 10 * 60 * 1000,
});
}
async function runRemotePairingFlow({ clientId, appName, appDescription, appWebsite }) {
const tokens = await authenticateRemote({ clientId });
return createOrFetchUserAppCredentials({
accessToken: tokens.access_token,
name: appName,
description: appDescription,
website: appWebsite,
});
}
async function updateAppRedirectUri({ accessToken, urn, redirectUri, app }) {
const body = { redirect_uri: redirectUri };
const endpoint = appRegUrl(`/me/apps/${urn}`).toString();
const { res, text } = await soundcloudApiRequest({
accessToken,
endpoint,
method: "PUT",
body,
});
if (!res.ok) {
printUpdateRedirectRequestDebug({
endpoint,
body,
urn,
app,
responseStatus: res.status,
responseText: text,
});
const apiError = parseStructuredApiError(text);
const userMessage = apiError ? updateRedirectUserMessage(apiError) : null;
throw new Error(userMessage || `Update redirect failed: ${res.status} ${apiErrorDetail(text)}`);
}
const data = JSON.parse(text);
return data.redirect_uri;
}
async function runListAppsFlow({ clientId, remote }) {
const tokens = remote ? await authenticateRemote({ clientId }) : await authenticateLocal({ clientId });
const page = await listUserApps({ accessToken: tokens.access_token });
return appsFromPage(page).map(listAppPublicFields);
}
async function runUpdateRedirectFlow({ clientId, redirectUri, remote, targetClientId }) {
const tokens = remote ? await authenticateRemote({ clientId }) : await authenticateLocal({ clientId });
const page = await listUserApps({ accessToken: tokens.access_token });
const app = selectAppForUpdate(page, { clientId: targetClientId });
const urn = credentialsUrn(app);
if (!urn) {
throw new Error(
"Credentials URN missing from API response (expected app.credentials as a string, e.g. soundcloud:credentials:…)."
);
}
if (app.redirect_uri === redirectUri) {
return { credentials: credentialsFromApp(app), updated: false };
}
const updatedRedirectUri = await updateAppRedirectUri({
accessToken: tokens.access_token,
urn,
redirectUri,
app,
});
return {
credentials: credentialsWithRedirectUri(app, updatedRedirectUri ?? redirectUri),
updated: true,
notice: "Updated redirect URI.",
};
}
async function createOrFetchUserAppCredentials({ accessToken, name, description, website }) {
const { res, text } = await soundcloudApiRequest({
accessToken,
endpoint: ME_APPS_URL,
method: "POST",
body: { name, description, website },
});
if (res.status === 201) {
return { credentials: JSON.parse(text), existing: false };
}
if (res.status === 403) {
const apiError = parseStructuredApiError(text);
if (apiError?.code === "user_already_has_application") {
const page = await listUserApps({ accessToken });
return {
credentials: credentialsFromAppsPage(page),
existing: true,
notice: createAppUserMessage(apiError),
};
}
throw new Error(createAppUserMessage(apiError ?? {}));
}
throw new Error(`Create app failed: ${res.status} ${apiErrorDetail(text)}`);
}
function mainDoc() {
return `<!DOCTYPE html>
<html><head><meta charset="utf-8"><title>SoundCloud</title>
<style>body{font-family:system-ui,sans-serif;max-width:36em;margin:3em auto;padding:0 1em;}</style>
</head><body><h1>Done</h1><p>You can close this tab and return to the terminal.</p></body></html>`;
}
function errorDoc(msg) {
return `<!DOCTYPE html>
<html><head><meta charset="utf-8"><title>Error</title>
<style>body{font-family:system-ui,sans-serif;max-width:36em;margin:3em auto;padding:0 1em;}</style>
</head><body><h1>Authorization error</h1><p>${escapeHtml(msg)}</p></body></html>`;
}
function escapeHtml(s) {
return s.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """);
}
function printRegisterHelp() {
console.log(`Usage: ${SCRIPT_NAME} [options]
${SCRIPT_NAME} list-apps [options]
${SCRIPT_NAME} update-redirect [options]
Commands:
(default) Sign in and register or fetch your API application
list-apps Sign in and list your API applications (no client_secret)
update-redirect Sign in and update your application's OAuth redirect URI
Register options:
--name Required. App name for POST /me/apps
--description Required. App description for POST /me/apps
--website Required. App website URL for POST /me/apps
--remote Use pairing code sign-in instead of a local callback server
-h, --help Print usage and exit
List apps options:
--remote Use pairing code sign-in instead of a local callback server
-h, --help Print usage and exit
Update redirect options:
--redirect-uri Required. OAuth redirect URI for your API application
--client-id Required when you have multiple apps; use list-apps to find it
--remote Use pairing code sign-in instead of a local callback server
-h, --help Print usage and exit
Sign-in always uses a fixed local callback (${BUNDLED_REDIRECT_URI}) or, with
--remote, ${PAIRING_ACTIVATE_BASE}/<code>. That is separate from your app's
redirect URI, which you set with update-redirect.
With npm, pass a double dash before flags: npm start -- --name "…" …
`);
}
function printUpdateRedirectHelp() {
console.log(`Usage: ${SCRIPT_NAME} update-redirect [options]
Signs you in to SoundCloud, then calls PUT /me/apps/<credentials-urn> to update your
application's OAuth redirect URI. Only redirect_uri is changed; client_id and
client_secret are re-printed from your existing app.
Options:
--redirect-uri Required. OAuth redirect URI for your API application
--client-id Required when you have multiple apps; use list-apps to find it
--remote Use pairing code sign-in instead of a local callback server
-h, --help Print usage and exit
Sign-in uses a fixed local callback (${BUNDLED_REDIRECT_URI}) or, with
--remote, ${PAIRING_ACTIVATE_BASE}/<code>.
On failure, prints the PUT URL, body, and a curl template to stderr.
With npm: npm start -- update-redirect --redirect-uri "https://example.com/callback"
Multiple apps: npm start -- update-redirect --client-id "…" --redirect-uri "https://example.com/callback"
`);
}
function printListAppsHelp() {
console.log(`Usage: ${SCRIPT_NAME} list-apps [options]
Signs you in to SoundCloud, then calls GET /me/apps and prints your API
applications as JSON. client_secret is never included.
Options:
--remote Use pairing code sign-in instead of a local callback server
-h, --help Print usage and exit
Sign-in uses a fixed local callback (${BUNDLED_REDIRECT_URI}) or, with
--remote, ${PAIRING_ACTIVATE_BASE}/<code>.
With npm: npm start -- list-apps
`);
}
function authenticateLocal({ clientId }) {
const { codeVerifier, codeChallenge } = newPkcePair();
const state = base64url(crypto.randomBytes(24));
const callbackPaths = new Set(callbackPathVariants(CALLBACK_PATH));
let server;
let callbackHandled = false;
let idle;
return new Promise((resolve, reject) => {
const finish = (err, result) => {
if (callbackHandled) return;
callbackHandled = true;
clearTimeout(idle);
server?.close();
if (err) reject(err);
else resolve(result);
};
const handleOAuthCallback = async (req, res) => {
try {
const query = parseRequestQuery(req);
const rawBody = req.method === "POST" ? await readRequestBody(req) : "";
const body = parseUrlEncodedBody(rawBody);
const { code, state: st, error, error_description: ed } = readOAuthCallbackParams({ query, body });
if (error) {
sendHtml(res, 400, errorDoc(String(ed || error)));
finish(new Error(String(ed || error)));
return;
}
if (typeof code !== "string" || !code) {
sendHtml(res, 400, errorDoc("Missing code in callback (query or POST body)."));
finish(new Error("Missing authorization code."));
return;
}
if (st !== state) {
sendHtml(res, 400, errorDoc("Invalid state parameter (CSRF)."));
finish(new Error("State mismatch."));
return;
}
const tokens = await tokenExchange({
clientId,
code,
codeVerifier,
redirectUri: BUNDLED_REDIRECT_URI,
});
if (!tokens.access_token) {
finish(new Error("No access_token in token response"));
return;
}
sendHtml(res, 200, mainDoc());
finish(null, tokens);
} catch (e) {
sendHtml(res, 500, errorDoc(e.message));
finish(e);
}
};
server = http.createServer((req, res) => {
const pathname = parseRequestPathname(req);
if (!callbackPaths.has(pathname)) {
sendHtml(res, 404, errorDoc("Not found."));
return;
}
if (req.method !== "GET" && req.method !== "POST") {
sendHtml(res, 405, errorDoc("Method not allowed."));
return;
}
void handleOAuthCallback(req, res);
});
server.listen(CALLBACK_PORT, CALLBACK_HOST, () => {
const params = new URLSearchParams({
client_id: clientId,
redirect_uri: BUNDLED_REDIRECT_URI,
response_type: "code",
code_challenge: codeChallenge,
code_challenge_method: "S256",
state,
});
const authUrl = `${SOUNDCLOUD_AUTHORIZE}?${params.toString()}`;
console.log("Starting browser login. If it does not open, visit this URL:\n" + authUrl);
idle = setTimeout(
() => finish(new Error("Timed out waiting for SoundCloud callback (10 minutes).")),
10 * 60 * 1000
);
try {
openBrowser(authUrl);
} catch {
/* user can paste URL */
}
});
server.on("error", (e) => {
finish(new Error(`Could not start local server: ${e.message}`));
});
});
}
async function runLocalAuthFlow({ clientId, appName, appDescription, appWebsite }) {
const tokens = await authenticateLocal({ clientId });
return createOrFetchUserAppCredentials({
accessToken: tokens.access_token,
name: appName,
description: appDescription,
website: appWebsite,
});
}
function printCredentialResult(result) {
if (result.notice) {
console.error(result.notice);
}
process.stdout.write(formatCredentialOutput(result.credentials));
}
const CLI_SUBCOMMANDS = new Set(["list-apps", "update-redirect"]);
function runCli() {
const rawArgs = process.argv.slice(2);
const subcommand = CLI_SUBCOMMANDS.has(rawArgs[0]) ? rawArgs[0] : null;
const argsToParse = subcommand ? rawArgs.slice(1) : rawArgs;
if (!subcommand && rawArgs[0] && !rawArgs[0].startsWith("-")) {
console.error(`Unknown command: ${rawArgs[0]}`);
console.error(`Run ${SCRIPT_NAME} --help for usage.`);
process.exit(1);
}
if (subcommand === "list-apps") {
const {
values: { remote: remoteArg, help: helpArg },
positionals,
} = parseCliArgs({
args: argsToParse,
options: {
remote: { type: "boolean" },
help: { type: "boolean", short: "h" },
},
strict: true,
allowPositionals: true,
});
if (positionals.length > 0) {
console.error(`Unexpected extra argument(s): ${positionals.map((p) => JSON.stringify(p)).join(" ")}`);
process.exit(1);
}
if (helpArg) {
printListAppsHelp();
process.exit(0);
}
runListAppsFlow({ clientId: BUNDLED_CLIENT_ID, remote: remoteArg })
.then(
(apps) => {
process.stdout.write(formatListAppsOutput(apps));
},
(e) => {
console.error("Error:", e?.message || e);
process.exit(1);
}
)
.then(() => process.exit(0));
return;
}
if (subcommand === "update-redirect") {
const {
values: {
"redirect-uri": redirectArg,
"client-id": targetClientIdArg,
remote: remoteArg,
help: helpArg,
},
positionals,
} = parseCliArgs({
args: argsToParse,
options: {
"redirect-uri": { type: "string" },
"client-id": { type: "string" },
remote: { type: "boolean" },
help: { type: "boolean", short: "h" },
},
strict: true,
allowPositionals: true,
});
if (positionals.length > 0) {
console.error(`Unexpected extra argument(s): ${positionals.map((p) => JSON.stringify(p)).join(" ")}`);
process.exit(1);
}
if (helpArg) {
printUpdateRedirectHelp();
process.exit(0);
}
let redirectUri;
try {
redirectUri = validateAppRedirectUri(redirectArg);
} catch (e) {
console.error(e.message);
console.error(`Example: node ${SCRIPT_NAME} update-redirect --redirect-uri "https://example.com/callback"`);
process.exit(1);
}
runUpdateRedirectFlow({
clientId: BUNDLED_CLIENT_ID,
redirectUri,
remote: remoteArg,
targetClientId: targetClientIdArg,
})
.then(printCredentialResult, (e) => {
console.error("Error:", e?.message || e);
process.exit(1);
})
.then(() => process.exit(0));
return;
}
const {
values: { name: nameArg, description: descArg, website: siteArg, remote: remoteArg, help: helpArg },
positionals,
} = parseCliArgs({
args: argsToParse,
options: {
name: { type: "string" },
description: { type: "string" },
website: { type: "string" },
remote: { type: "boolean" },
help: { type: "boolean", short: "h" },
},
strict: true,
allowPositionals: true,
});
if (positionals.length > 0) {
console.error(`Unexpected extra argument(s): ${positionals.map((p) => JSON.stringify(p)).join(" ")}`);
console.error(
"If you used npm, put a double dash before the script options so npm does not swallow them, e.g.:\n" +
' npm start -- --name "My App" --description "…" --website "https://example.com"'
);
process.exit(1);
}
if (helpArg) {
printRegisterHelp();
process.exit(0);
}
const appName = nameArg;
const appDescription = descArg;
const appWebsite = siteArg;
if (!appName || !appDescription || !appWebsite) {
console.error("Missing required arguments: --name, --description, --website");
console.error(
`Example: node ${SCRIPT_NAME} --name "My First API App" --description "Internal integration for uploads" --website "https://example.com"`
);
process.exit(1);
}
if (remoteArg) {
runRemotePairingFlow({ clientId: BUNDLED_CLIENT_ID, appName, appDescription, appWebsite })
.then(printCredentialResult, (e) => {
console.error("Error:", e?.message || e);