Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a704849061 | ||
|
|
2c89510400 | ||
|
|
041bebe4c5 | ||
|
|
f50403f381 | ||
|
|
6c138c93a4 | ||
|
|
6590dcdac7 | ||
|
|
ace36340a8 | ||
|
|
4918696c5a | ||
|
|
4bf079571b |
@@ -5,30 +5,28 @@ This is a server-side component providing cloud storage and [OAuth Code Grant Fl
|
|||||||
|
|
||||||
### Cloudflare Workers
|
### Cloudflare Workers
|
||||||
|
|
||||||
[](https://deploy.workers.cloudflare.com/?url=https%3A%2F%2Fgithub.com%2Fsugoidogo%2Ftwitch-cloud-ebs)
|
Previously, there was a "deploy to cloudflare" button here, but it seems to only work with GitHub repos, so you'll have to clone and deploy this yourself.
|
||||||
|
|
||||||
### Docker
|
### Docker
|
||||||
|
|
||||||
This worker requires that the KV store is pre-populated with client-id keys and client-secret values, but doesn't provide any mechanism for this to work with selflare. As such, the docker container may not be usable until this is resolved. PRs are welcome.
|
Previously, this repo produced oci images, but they were inclomlete and more work is needed before they are ready.
|
||||||
|
|
||||||
This worker is also availible as a docker container at [ghcr.io/sugoidogo/twitch-cloud-ebs](https://github.com/sugoidogo/twitch-clips-consent-api/pkgs/container/twitch-clips-consent-api).
|
|
||||||
It listens on HTTP port 8080 and requires mounts for `/worker/cache`, `/worker/kv`, `/worker/d1`, and `/worker/r2`.
|
|
||||||
The r2 and kv mounts are the only ones actually used, but the selflare runtime requires all of them regardless.
|
|
||||||
|
|
||||||
## Twurple Module
|
## Twurple Module
|
||||||
|
|
||||||
|
https://gitea.sugoidogo.com/sugoidogo/-/packages/npm/@sugoidogo%2Ftwitch-cloud-ebs/
|
||||||
|
|
||||||
This API provides two Twurple modules, one for authorization and one for storage, allowing for easy integration if you're already using the Twurple javascript library.
|
This API provides two Twurple modules, one for authorization and one for storage, allowing for easy integration if you're already using the Twurple javascript library.
|
||||||
the WebStorage module can be used to cache fetched resources from anywhere, but when used as demonstrated below, allows your client to maintain access to previously stored resources from cloud storage via the browser's CacheStorage API.
|
the WebStorage module can be used to cache fetched resources from anywhere, but when used as demonstrated below, allows your client to maintain access to previously stored resources from cloud storage via the browser's CacheStorage API.
|
||||||
|
|
||||||
<details><summary>javascript</summary>
|
<details><summary>javascript</summary>
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
import SugoiAuthProvider from 'https://ebs.domain.com/SugoiAuthProvider.js'
|
import { SugoiAuthProvider, WebStorage } from '@sugoidogo/twitch-cloud-ebs'
|
||||||
import WebStorage from 'https://ebs.domain.com/WebStorage.js'
|
import { ApiClient } from '@twurple/ebs';
|
||||||
import { ApiClient } from 'https://cdn.jsdelivr.net/npm/@twurple/api@7/+esm';
|
|
||||||
|
|
||||||
const authProvicder=new SugoiAuthProvider('your-client-id')
|
const ebs_host='https://your.ebs.hostname'
|
||||||
const webStorage=new WebStorage(authProvider)
|
const authProvicder=new SugoiAuthProvider('your-client-id',ebs_host)
|
||||||
|
const webStorage=new WebStorage(authProvider,undefined,ebs_host)
|
||||||
const apiClient = new ApiClient({ authProvider });
|
const apiClient = new ApiClient({ authProvider });
|
||||||
|
|
||||||
const config=await webStorage.fetch('config.json').then(response=>response.json())
|
const config=await webStorage.fetch('config.json').then(response=>response.json())
|
||||||
|
|||||||
Generated
+65
-2
@@ -1,14 +1,15 @@
|
|||||||
{
|
{
|
||||||
"name": "twitch-cloud-ebs",
|
"name": "@sugoidogo/twitch-cloud-ebs",
|
||||||
"version": "0.0.0",
|
"version": "0.0.0",
|
||||||
"lockfileVersion": 3,
|
"lockfileVersion": 3,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"packages": {
|
"packages": {
|
||||||
"": {
|
"": {
|
||||||
"name": "twitch-cloud-ebs",
|
"name": "@sugoidogo/twitch-cloud-ebs",
|
||||||
"version": "0.0.0",
|
"version": "0.0.0",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@sugoidogo/selflare": "^1.1.3",
|
"@sugoidogo/selflare": "^1.1.3",
|
||||||
|
"@twurple/api": "^7.4.0",
|
||||||
"@twurple/auth": "^7.4.0",
|
"@twurple/auth": "^7.4.0",
|
||||||
"fetch-retry": "^6.0.0",
|
"fetch-retry": "^6.0.0",
|
||||||
"typescript": "^5.9.3",
|
"typescript": "^5.9.3",
|
||||||
@@ -57,6 +58,17 @@
|
|||||||
"node": ">=12"
|
"node": ">=12"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/@d-fischer/cache-decorators": {
|
||||||
|
"version": "4.0.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/@d-fischer/cache-decorators/-/cache-decorators-4.0.1.tgz",
|
||||||
|
"integrity": "sha512-HNYLBLWs/t28GFZZeqdIBqq8f37mqDIFO6xNPof94VjpKvuP6ROqCZGafx88dk5zZUlBfViV9jD8iNNlXfc4CA==",
|
||||||
|
"dev": true,
|
||||||
|
"license": "MIT",
|
||||||
|
"dependencies": {
|
||||||
|
"@d-fischer/shared-utils": "^3.6.3",
|
||||||
|
"tslib": "^2.6.2"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/@d-fischer/cross-fetch": {
|
"node_modules/@d-fischer/cross-fetch": {
|
||||||
"version": "5.0.5",
|
"version": "5.0.5",
|
||||||
"resolved": "https://registry.npmjs.org/@d-fischer/cross-fetch/-/cross-fetch-5.0.5.tgz",
|
"resolved": "https://registry.npmjs.org/@d-fischer/cross-fetch/-/cross-fetch-5.0.5.tgz",
|
||||||
@@ -102,6 +114,21 @@
|
|||||||
"url": "https://github.com/sponsors/d-fischer"
|
"url": "https://github.com/sponsors/d-fischer"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/@d-fischer/rate-limiter": {
|
||||||
|
"version": "1.1.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/@d-fischer/rate-limiter/-/rate-limiter-1.1.0.tgz",
|
||||||
|
"integrity": "sha512-O5HgACwApyCZhp4JTEBEtbv/W3eAwEkrARFvgWnEsDmXgCMWjIHwohWoHre5BW6IYXFSHBGsuZB/EvNL3942kQ==",
|
||||||
|
"dev": true,
|
||||||
|
"license": "MIT",
|
||||||
|
"dependencies": {
|
||||||
|
"@d-fischer/logger": "^4.2.3",
|
||||||
|
"@d-fischer/shared-utils": "^3.6.3",
|
||||||
|
"tslib": "^2.6.2"
|
||||||
|
},
|
||||||
|
"funding": {
|
||||||
|
"url": "https://github.com/sponsors/d-fischer"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/@d-fischer/shared-utils": {
|
"node_modules/@d-fischer/shared-utils": {
|
||||||
"version": "3.6.4",
|
"version": "3.6.4",
|
||||||
"resolved": "https://registry.npmjs.org/@d-fischer/shared-utils/-/shared-utils-3.6.4.tgz",
|
"resolved": "https://registry.npmjs.org/@d-fischer/shared-utils/-/shared-utils-3.6.4.tgz",
|
||||||
@@ -1741,6 +1768,32 @@
|
|||||||
"stacktracey": "^2.1.8"
|
"stacktracey": "^2.1.8"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/@twurple/api": {
|
||||||
|
"version": "7.4.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/@twurple/api/-/api-7.4.0.tgz",
|
||||||
|
"integrity": "sha512-RlXLs4ZvS8n0+iIk7YyVDwrjhlwpn+N+h7fX5Q61HoxlmzoCShmnnFo03abYw9i8Cc3deGpbQATOSVmigXM4qg==",
|
||||||
|
"dev": true,
|
||||||
|
"license": "MIT",
|
||||||
|
"dependencies": {
|
||||||
|
"@d-fischer/cache-decorators": "^4.0.0",
|
||||||
|
"@d-fischer/cross-fetch": "^5.0.1",
|
||||||
|
"@d-fischer/detect-node": "^3.0.1",
|
||||||
|
"@d-fischer/logger": "^4.2.1",
|
||||||
|
"@d-fischer/rate-limiter": "^1.1.0",
|
||||||
|
"@d-fischer/shared-utils": "^3.6.1",
|
||||||
|
"@d-fischer/typed-event-emitter": "^3.3.1",
|
||||||
|
"@twurple/api-call": "7.4.0",
|
||||||
|
"@twurple/common": "7.4.0",
|
||||||
|
"retry": "^0.13.1",
|
||||||
|
"tslib": "^2.0.3"
|
||||||
|
},
|
||||||
|
"funding": {
|
||||||
|
"url": "https://github.com/sponsors/d-fischer"
|
||||||
|
},
|
||||||
|
"peerDependencies": {
|
||||||
|
"@twurple/auth": "7.4.0"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/@twurple/api-call": {
|
"node_modules/@twurple/api-call": {
|
||||||
"version": "7.4.0",
|
"version": "7.4.0",
|
||||||
"resolved": "https://registry.npmjs.org/@twurple/api-call/-/api-call-7.4.0.tgz",
|
"resolved": "https://registry.npmjs.org/@twurple/api-call/-/api-call-7.4.0.tgz",
|
||||||
@@ -2393,6 +2446,16 @@
|
|||||||
"node": ">=0.10.0"
|
"node": ">=0.10.0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/retry": {
|
||||||
|
"version": "0.13.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/retry/-/retry-0.13.1.tgz",
|
||||||
|
"integrity": "sha512-XQBQ3I8W1Cge0Seh+6gjj03LbmRFWuoszgK9ooCpwYIrhhoO80pfq4cUkU5DkknwfOfFteRwlZ56PYOGYyFWdg==",
|
||||||
|
"dev": true,
|
||||||
|
"license": "MIT",
|
||||||
|
"engines": {
|
||||||
|
"node": ">= 4"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/rollup-plugin-inject": {
|
"node_modules/rollup-plugin-inject": {
|
||||||
"version": "3.0.2",
|
"version": "3.0.2",
|
||||||
"resolved": "https://registry.npmjs.org/rollup-plugin-inject/-/rollup-plugin-inject-3.0.2.tgz",
|
"resolved": "https://registry.npmjs.org/rollup-plugin-inject/-/rollup-plugin-inject-3.0.2.tgz",
|
||||||
|
|||||||
@@ -13,6 +13,7 @@
|
|||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@sugoidogo/selflare": "^1.1.3",
|
"@sugoidogo/selflare": "^1.1.3",
|
||||||
|
"@twurple/api": "^7.4.0",
|
||||||
"@twurple/auth": "^7.4.0",
|
"@twurple/auth": "^7.4.0",
|
||||||
"fetch-retry": "^6.0.0",
|
"fetch-retry": "^6.0.0",
|
||||||
"typescript": "^5.9.3",
|
"typescript": "^5.9.3",
|
||||||
|
|||||||
@@ -0,0 +1,67 @@
|
|||||||
|
import type { AccessTokenMaybeWithUserId, AccessTokenWithUserId, AuthProvider } from "@twurple/auth";
|
||||||
|
import type { UserIdResolvable } from "@twurple/api"
|
||||||
|
import * as TwitchAuth from "./TwitchAuth";
|
||||||
|
|
||||||
|
async function getTwurpleProxy(token: TwitchAuth.TwitchToken): Promise<AccessTokenWithUserId> {
|
||||||
|
if (!token.user_id) {
|
||||||
|
token = await TwitchAuth.validateToken(token.access_token)
|
||||||
|
}
|
||||||
|
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
|
||||||
@@ -86,7 +86,7 @@ export function requestAccessToken(client_id: string, ...scopes: Array<string>)
|
|||||||
* @param {Array<string>|string} scopes
|
* @param {Array<string>|string} scopes
|
||||||
* @returns {Promise<AuthCode>}
|
* @returns {Promise<AuthCode>}
|
||||||
*/
|
*/
|
||||||
export function requestAuthCode(client_id: string, ...scopes: Array<string>): Promise<any> {
|
export function requestAuthCode(client_id: string, ...scopes: Array<string>): Promise<never> {
|
||||||
console.debug('requesting authorization code')
|
console.debug('requesting authorization code')
|
||||||
const url = new URL('https://id.twitch.tv/oauth2/authorize')
|
const url = new URL('https://id.twitch.tv/oauth2/authorize')
|
||||||
url.searchParams.append('response_type', 'code')
|
url.searchParams.append('response_type', 'code')
|
||||||
@@ -178,7 +178,7 @@ export function refreshToken(client_id: string, refresh_token: string): Promise<
|
|||||||
* @param {string} client_id
|
* @param {string} client_id
|
||||||
* @returns {TwitchToken}
|
* @returns {TwitchToken}
|
||||||
*/
|
*/
|
||||||
export async function getUserToken(client_id: string, ...scopes): Promise<TwitchToken> {
|
export async function getUserToken(client_id: string, ...scopes:string[]): Promise<TwitchToken> {
|
||||||
const token=await getUserTokenPassive(client_id, ...scopes)
|
const token=await getUserTokenPassive(client_id, ...scopes)
|
||||||
if(!token){
|
if(!token){
|
||||||
return requestAuthCode(client_id, ...scopes)
|
return requestAuthCode(client_id, ...scopes)
|
||||||
@@ -193,19 +193,19 @@ export async function getUserToken(client_id: string, ...scopes): Promise<Twitch
|
|||||||
* @param {string} client_id
|
* @param {string} client_id
|
||||||
* @returns {TwitchToken}
|
* @returns {TwitchToken}
|
||||||
*/
|
*/
|
||||||
export async function getUserTokenPassive(client_id: string, ...scopes): Promise<TwitchToken>{
|
export async function getUserTokenPassive(client_id: string, ...scopes:string[]): Promise<TwitchToken|null>{
|
||||||
console.debug('scopes requested:',...scopes)
|
console.debug('scopes requested:',...scopes)
|
||||||
const params = new URLSearchParams(location.search + '&' + location.hash.substring(1))
|
const params = new URLSearchParams(location.search + '&' + location.hash.substring(1))
|
||||||
if (params.has('code')) {
|
if (params.has('code')) {
|
||||||
const code = params.get('code')
|
const code = params.get('code')!
|
||||||
history.replaceState(null, '', redirect_uri)
|
history.replaceState(null, '', redirect_uri)
|
||||||
return exchangeCode(client_id, code)
|
return exchangeCode(client_id, code)
|
||||||
}
|
}
|
||||||
if (params.has('refresh_token')) {
|
if (params.has('refresh_token')) {
|
||||||
return refreshToken(client_id, params.get('refresh_token'))
|
return refreshToken(client_id, params.get('refresh_token')!)
|
||||||
}
|
}
|
||||||
if (params.has('access_token')) {
|
if (params.has('access_token')) {
|
||||||
return validateToken(params.get('access_token'))
|
return validateToken(params.get('access_token')!)
|
||||||
}
|
}
|
||||||
if (params.has('error')) {
|
if (params.has('error')) {
|
||||||
const error_message = params.get('error') + ': ' + params.get('error_description')
|
const error_message = params.get('error') + ': ' + params.get('error_description')
|
||||||
|
|||||||
@@ -1,2 +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 tba from "./tba"
|
||||||
@@ -69,4 +69,6 @@ export async function get_tokens(client_id: string,scope='',redirect_uri=locatio
|
|||||||
return token
|
return token
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function setProxyOrigin(proxy_origin:string) { TwitchAuth.setProxyOrigin(proxy_origin) }
|
||||||
|
|
||||||
export default get_tokens
|
export default get_tokens
|
||||||
@@ -3,6 +3,7 @@
|
|||||||
"target": "es2020",
|
"target": "es2020",
|
||||||
"module": "es2020",
|
"module": "es2020",
|
||||||
"moduleResolution": "node",
|
"moduleResolution": "node",
|
||||||
|
"skipLibCheck": true,
|
||||||
"allowImportingTsExtensions": true,
|
"allowImportingTsExtensions": true,
|
||||||
"emitDeclarationOnly": true,
|
"emitDeclarationOnly": true,
|
||||||
"declaration": true,
|
"declaration": true,
|
||||||
|
|||||||
Reference in New Issue
Block a user