revised auth provider
docker.yml / build (push) Failing after 47s

This commit is contained in:
2026-08-01 18:40:01 -07:00
parent f50403f381
commit 041bebe4c5
2 changed files with 65 additions and 0 deletions
+64
View File
@@ -0,0 +1,64 @@
import type { AccessTokenMaybeWithUserId, AccessTokenWithUserId, AuthProvider } from "@twurple/auth";
import type { UserIdResolvable } from "@twurple/api"
import * as TwitchAuth from "./TwitchAuth";
function getTwurpleProxy(token: TwitchAuth.TwitchToken): AccessTokenWithUserId {
return new Proxy(token, {
get(target, name, receiver) {
return target[name.toString().replace(/[A-Z]/g, letter => `_${letter.toLowerCase()}`) as keyof TwitchAuth.TwitchToken]
}
}) as unknown as AccessTokenWithUserId
}
export class SugoiAuthProvider implements AuthProvider {
#token: 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()
}
#authorize(scopes: string[]) {
const promise = TwitchAuth.requestAuthCode(this.clientId, ...scopes)
this.#token = promise
return promise
}
/**
* Checks cached token for given scopes, and redirects to authorization page on failure.
* You should only call this from your configured redirect uri.
*
* https://dev.twitch.tv/docs/authentication/getting-tokens-oauth/#get-the-user-to-authorize-your-app
*/
async getAuthorization(...scopes: string[]): Promise<void> {
const token = await this.#token
if(!token) return this.#authorize(scopes)
for (const scope of scopes) {
if (!token.scope!.includes(scope)) {
return this.#authorize(scopes)
}
}
}
getCurrentScopesForUser(user: UserIdResolvable):string[] {
if (!this.#token || this.#token instanceof Promise) throw new Error("unauthorized")
return this.#token.scope || []
}
async getAccessTokenForUser(user: UserIdResolvable, ...scopeSets: (string[] | undefined)[]): Promise<AccessTokenWithUserId | null> {
const token = await this.#token
if (!token) return null
return getTwurpleProxy(token)
}
async getAnyAccessToken(user?: UserIdResolvable): Promise<AccessTokenMaybeWithUserId> {
const token = await this.#token
if (!token) throw new Error("unauthorized")
return getTwurpleProxy(token)
}
async refreshAccessTokenForUser(user: UserIdResolvable): Promise<AccessTokenWithUserId> {
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)
}
}
export default SugoiAuthProvider
+1
View File
@@ -1,4 +1,5 @@
export { default as SugoiAuthProvider } from "./SugoiAuthProvider" export { default as SugoiAuthProvider } from "./SugoiAuthProvider"
export { default as SugoiAuthProvider2 } from "./SugoiAuthProvider2"
export { default as WebStorage } from "./WebStorage" export { default as WebStorage } from "./WebStorage"
export * as TwitchAuth from "./TwitchAuth" export * as TwitchAuth from "./TwitchAuth"
export * as tba from "./tba" export * as tba from "./tba"