Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
76cd0649b1 | ||
|
|
e3c12df6fb | ||
|
|
a27f8804e4 | ||
|
|
2788275d25 | ||
|
|
2265fd39e0 | ||
|
|
83f3217565 | ||
|
|
a704849061 |
Vendored
-5
@@ -1,5 +0,0 @@
|
||||
{
|
||||
"files.associations": {
|
||||
"wrangler.json": "jsonc"
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,18 @@ import type { AccessTokenMaybeWithUserId, AccessTokenWithUserId, AuthProvider }
|
||||
import type { UserIdResolvable } from "@twurple/api"
|
||||
import * as TwitchAuth from "./TwitchAuth";
|
||||
|
||||
function getTwurpleProxy(token: TwitchAuth.TwitchToken): AccessTokenWithUserId {
|
||||
async function ensureUserId(token: TwitchAuth.TwitchToken | null): Promise<TwitchAuth.TwitchToken | null> {
|
||||
if (!token) return null
|
||||
if (!token.user_id) {
|
||||
const refresh_token = token.refresh_token
|
||||
token = await TwitchAuth.validateToken(token.access_token)
|
||||
token.refresh_token = refresh_token
|
||||
}
|
||||
return token
|
||||
}
|
||||
|
||||
async function getTwurpleProxy(token: TwitchAuth.TwitchToken): Promise<AccessTokenWithUserId> {
|
||||
token=await ensureUserId(token)
|
||||
return new Proxy(token, {
|
||||
get(target, name, receiver) {
|
||||
return target[name.toString().replace(/[A-Z]/g, letter => `_${letter.toLowerCase()}`) as keyof TwitchAuth.TwitchToken]
|
||||
@@ -11,12 +22,12 @@ function getTwurpleProxy(token: TwitchAuth.TwitchToken): AccessTokenWithUserId {
|
||||
}
|
||||
|
||||
export class SugoiAuthProvider implements AuthProvider {
|
||||
#token: TwitchAuth.TwitchToken | Promise<TwitchAuth.TwitchToken | null> | null = null;
|
||||
#token: TwitchAuth.TwitchToken | Promise<TwitchAuth.TwitchToken> | Promise<TwitchAuth.TwitchToken | null> | null = null;
|
||||
clientId: string;
|
||||
constructor(clientId:string,proxyOrigin?:string) {
|
||||
this.clientId = clientId;
|
||||
if (proxyOrigin) TwitchAuth.setProxyOrigin(proxyOrigin);
|
||||
this.#token=TwitchAuth.getUserTokenPassive(clientId).catch()
|
||||
this.#token = TwitchAuth.getUserTokenPassive(clientId).then(ensureUserId)
|
||||
}
|
||||
#authorize(scopes: string[]) {
|
||||
const promise = TwitchAuth.requestAuthCode(this.clientId, ...scopes)
|
||||
@@ -29,7 +40,7 @@ export class SugoiAuthProvider implements AuthProvider {
|
||||
*
|
||||
* https://dev.twitch.tv/docs/authentication/getting-tokens-oauth/#get-the-user-to-authorize-your-app
|
||||
*/
|
||||
async getAuthorization(...scopes: string[]): Promise<void> {
|
||||
async getAuthorization(...scopes: string[]): Promise<AccessTokenWithUserId> {
|
||||
const token = await this.#token
|
||||
if(!token) return this.#authorize(scopes)
|
||||
for (const scope of scopes) {
|
||||
@@ -37,6 +48,7 @@ export class SugoiAuthProvider implements AuthProvider {
|
||||
return this.#authorize(scopes)
|
||||
}
|
||||
}
|
||||
return getTwurpleProxy(token)
|
||||
}
|
||||
getCurrentScopesForUser(user: UserIdResolvable):string[] {
|
||||
if (!this.#token || this.#token instanceof Promise) throw new Error("unauthorized")
|
||||
@@ -56,8 +68,8 @@ export class SugoiAuthProvider implements AuthProvider {
|
||||
const token=await this.#token
|
||||
if (!token) throw new Error("unauthorized")
|
||||
if (!token.refresh_token) throw new Error("missing refresh token")
|
||||
this.#token = await TwitchAuth.refreshToken(this.clientId, token.refresh_token)
|
||||
return getTwurpleProxy(this.#token)
|
||||
this.#token = await TwitchAuth.refreshToken(this.clientId, token.refresh_token).then(ensureUserId)
|
||||
return getTwurpleProxy(this.#token!)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user