-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsqlite.ts
210 lines (176 loc) · 4.88 KB
/
sqlite.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
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
import { Elysia } from 'elysia'
import oauth2, {
azure,
discord,
github,
spotify,
reddit,
google,
twitch,
validateToken
} from '../src'
import { randomBytes } from 'crypto'
import { Database } from 'bun:sqlite'
const db = new Database(':memory:')
db.run(
'CREATE TABLE IF NOT EXISTS storage (uuid TEXT, name TEXT, token TEXT, PRIMARY KEY(uuid, name))'
)
const uuid = '1f46b510-e674-4ae7-b6fc-d0872c9a4252'
const states = new Set()
const app = new Elysia()
const twitchProvider = twitch()
const auth = oauth2({
profiles: {
azure: {
provider: azure({ tenant: 'consumers' }),
scope: ['User.Read']
},
github: {
provider: github(),
scope: ['user']
},
discord: {
provider: discord({ prompt: 'none' }),
scope: ['identify']
},
spotify: {
provider: spotify(),
scope: ['user-read-private']
},
reddit: {
provider: reddit(),
scope: ['identity']
},
google: {
provider: google(),
scope: ['https://www.googleapis.com/auth/userinfo.profile']
},
twitch: {
provider: twitchProvider,
scope: ['user:read:follows']
}
},
state: {
check(ctx, name, state) {
if (states.has(state)) {
states.delete(state)
return true
}
return false
},
generate(ctx, name) {
const state = randomBytes(8).toString('hex')
states.add(state)
return state
}
},
storage: {
get(ctx, name) {
console.log(`get token: ${name}`)
const token = (
db
.query('SELECT token FROM storage WHERE uuid = ? AND name = ?')
.get(uuid, name) as { token: string }
)?.token
if (!token) {
return
}
return JSON.parse(token)
},
set(ctx, name, token) {
console.log(`new token: ${name}`)
db.run(
'INSERT OR REPLACE INTO storage (uuid, name, token) VALUES (?, ?, ?)',
[uuid, name, JSON.stringify(token)]
)
},
delete(ctx, name) {
db.run('DELETE FROM storage WHERE uuid = ? AND name = ?', [uuid, name])
}
}
})
function userPage(user: {}, logout: string) {
const html = `<!DOCTYPE html>
<html lang="en">
<body>
User:
<pre>${JSON.stringify(user, null, '\t')}</pre>
<a href="${logout}">Logout</a>
</body>
</html>`
return new Response(html, { headers: { 'Content-Type': 'text/html' } })
}
app
.use(auth)
.get('/', async (ctx) => {
const profiles = ctx.profiles()
if (await ctx.authorized('azure')) {
// https://docs.microsoft.com/en-us/graph/api/user-get
const user = await fetch('https://graph.microsoft.com/v1.0/me', {
headers: await ctx.tokenHeaders('azure')
})
return userPage(await user.json(), profiles.azure.logout)
}
if (await ctx.authorized('github')) {
// https://docs.github.com/en/rest/users/users#get-the-authenticated-user
const user = await fetch('https://api.github.com/user', {
headers: await ctx.tokenHeaders('github')
})
return userPage(await user.json(), profiles.github.logout)
}
if (await ctx.authorized('discord')) {
// https://discord.com/developers/docs/resources/user#get-current-user
const user = await fetch('https://discord.com/api/v10/users/@me', {
headers: await ctx.tokenHeaders('discord')
})
return userPage(await user.json(), profiles.discord.logout)
}
if (await ctx.authorized('spotify')) {
// https://developer.spotify.com/documentation/web-api/reference/#/operations/get-current-users-profile
const user = await fetch('https://api.spotify.com/v1/me', {
headers: await ctx.tokenHeaders('spotify')
})
return userPage(await user.json(), profiles.spotify.logout)
}
if (await ctx.authorized('reddit')) {
// https://www.reddit.com/dev/api/oauth/#GET_api_v1_me
const user = await fetch('https://oauth.reddit.com/api/v1/me', {
headers: await ctx.tokenHeaders('reddit')
})
return userPage(await user.json(), profiles.reddit.logout)
}
if (await ctx.authorized('google')) {
// https://cloud.google.com/identity-platform/docs/reference/rest/v1/UserInfo
const user = await fetch(
'https://www.googleapis.com/oauth2/v1/userinfo',
{
headers: await ctx.tokenHeaders('google')
}
)
return userPage(await user.json(), profiles.google.logout)
}
if (await ctx.authorized('twitch')) {
const tokenHeaders = await ctx.tokenHeaders('twitch')
if ((await validateToken(tokenHeaders)).status === 200) {
// https://dev.twitch.tv/docs/api/reference/#get-users
const user = await fetch('https://api.twitch.tv/helix/users', {
headers: { 'Client-Id': twitchProvider.clientId, ...tokenHeaders }
})
return userPage(await user.json(), profiles.twitch.logout)
}
}
const html = `<!DOCTYPE html>
<html lang="en">
<body>
Login:
<ul>
${Object.entries(profiles)
.map(([name, { login }]) => `<li><a href="${login}">${name}</a></li>`)
.join('\n')}
</ul>
</body>
</html>`
return new Response(html, { headers: { 'Content-Type': 'text/html' } })
})
.listen(3000)
console.log(`${app.server!.url}`)