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