-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
134 lines (128 loc) · 5.13 KB
/
index.ts
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
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
/* eslint-disable @typescript-eslint/no-unsafe-call */
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
/* eslint-disable @typescript-eslint/no-unsafe-argument */
// Package imports
import osuApiV2, { OAuthAuthorizeScope } from "osu-api-v2"
import fs from "fs"
import http from "http"
import open from "open"
import path from "path"
const secretOAuthCredentialsPath = path.join(
__dirname,
"..",
"..",
"..",
"osu_api_v2_oauth_refresh_token.secret.json",
)
interface OAuthRefreshTokenSecret {
clientId: number
clientSecret: string
redirectUri: string
}
// MAKE SURE THAT YOU REGISTERED THE APPLICATION WITH THE URL
// "http://localhost:8888" because the server uses that one
const secretOAuthCredentials = JSON.parse(
fs.readFileSync(secretOAuthCredentialsPath).toString(),
) as OAuthRefreshTokenSecret
console.log(secretOAuthCredentials)
const REDIRECT_URL = "http://localhost"
const REDIRECT_PORT = 8888
if (secretOAuthCredentials.redirectUri !== `${REDIRECT_URL}:${REDIRECT_PORT}`) {
throw Error(
`The only supported redirect URI by this server in this example is '${REDIRECT_URL}:${REDIRECT_PORT}' - be sure that you also registered it on the osu! website with this URL`,
)
}
const OK_STATUS_CODE = 200
const FORBIDDEN_STATUS_CODE = 403
// Create server that will catch the browser redirect after a successful
// authorization and contains the code token
const server = http.createServer((req, res) => {
console.log(
`osu!api v2 redirect was detected ${JSON.stringify({
host: req.headers.host,
location: req.headers.location,
method: req.method,
referer: req.headers.referer,
url: req.url,
})}`,
)
if (req.url && req.headers.host) {
if (req.url.endsWith("/")) {
res.writeHead(OK_STATUS_CODE)
res.end(
"<html><body></body><script>window.location = window.location.href.replace('#', '?');</script></html>",
)
} else {
const url = new URL(req.headers.host + req.url)
const codeToken = url.searchParams.get("code")
if (codeToken != null) {
console.log(
"osu!api v2 redirect contained code token:",
codeToken,
)
osuApiV2.oauth
.authorizationCodeGrant(
secretOAuthCredentials.clientId,
secretOAuthCredentials.clientSecret,
secretOAuthCredentials.redirectUri,
codeToken,
)
.then((codeGrantAuthorization) => {
console.log(codeGrantAuthorization)
return codeGrantAuthorization
})
.then((codeGrantAuthorization) => {
// Tell user that the page can now be closed and clear the private tokens from the URL
const refreshToken =
codeGrantAuthorization.refresh_token
res.writeHead(OK_STATUS_CODE)
res.end(
`<html><style>.spoiler{
color: black;
background-color:black;
}
.spoiler:hover{
color: white;
}</style><body><p>osu!api v2 connection was successful. You can now close this window.</p><br><p>For a permanent authentication you can copy the following refresh token:</p><br><p>Refresh Token: <span class="spoiler">${
refreshToken ? refreshToken : "ERROR"
}</span></p></body><script>window.history.replaceState({}, document.title, "/");</script></html>`,
)
console.log("osu!api v2 connection was successful")
})
.catch((err) => {
res.writeHead(FORBIDDEN_STATUS_CODE)
res.end(
`<html><body>osu!api v2 connection was not successful: ${
(err as Error).message
}</body></html>`,
)
})
} else {
res.writeHead(FORBIDDEN_STATUS_CODE)
res.end(
"<html><body>osu!api v2 connection was not successful: Code was not found!</body></html>",
)
}
}
} else {
res.writeHead(FORBIDDEN_STATUS_CODE)
res.end("Error")
}
})
new Promise<void>((resolve) => {
server.listen(REDIRECT_PORT, undefined, () => {
console.log("Server started")
resolve()
})
})
.then(async () => {
// Request code grant
const authorizeUrl = osuApiV2.oauth.authorizeRedirectUrlGenerator(
secretOAuthCredentials.clientId,
secretOAuthCredentials.redirectUri,
[OAuthAuthorizeScope.PUBLIC, OAuthAuthorizeScope.IDENTIFY],
)
await open(authorizeUrl)
})
.catch(console.error)