Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add new package: @twurple/auth-ext for automatic authentication in ex…
…tensions
- Loading branch information
Showing
11 changed files
with
223 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2022 Daniel Fischer | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# Twurple - Electron auth provider | ||
|
||
[![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/twurple/twurple/blob/main/LICENSE) | ||
[![npm version](https://img.shields.io/npm/v/@twurple/auth-ext.svg?style=flat)](https://www.npmjs.com/package/@twurple/auth-ext) | ||
![PRs welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg) | ||
|
||
This is an {@AuthProvider} implementation for [the `twurple` package family](https://github.com/twurple/twurple) | ||
that will use the token provided to your Extension by Twitch. | ||
|
||
## Installation | ||
|
||
To add this library to your project, just execute: | ||
|
||
yarn add @twurple/auth-ext | ||
|
||
or using npm: | ||
|
||
npm install @twurple/auth-ext | ||
|
||
## Basic usage | ||
|
||
To instantiate an {@ApiClient} with this auth provider, just pass it to its constructor: | ||
|
||
```ts | ||
import { ApiClient } from '@twurple/api'; | ||
import { ExtensionAuthProvider } from '@twurple/auth-ext'; | ||
|
||
const clientId = 'abc123'; | ||
|
||
const authProvider = new ExtensionAuthProvider(clientId); | ||
|
||
const client = new ApiClient({ | ||
authProvider | ||
}); | ||
``` | ||
|
||
## If you're getting stuck... | ||
|
||
You can join the [Twitch API Libraries Discord Server](https://discord.gg/b9ZqMfz) and ask in `#twurple` for support. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
{ | ||
"name": "@twurple/auth-ext", | ||
"version": "5.1.0-pre.5", | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"description": "Obtain auth tokens for Twitch inside an Extension.", | ||
"keywords": [ | ||
"twitch", | ||
"authentication", | ||
"extension" | ||
], | ||
"sideEffects": false, | ||
"main": "lib", | ||
"types": "lib", | ||
"exports": { | ||
".": { | ||
"require": "./lib/index.js", | ||
"import": "./es/index.mjs" | ||
} | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/twurple/twurple.git", | ||
"directory": "packages/auth-ext" | ||
}, | ||
"homepage": "https://twurple.js.org", | ||
"author": "Daniel Fischer <daniel@d-fischer.dev>", | ||
"funding": "https://github.com/sponsors/d-fischer", | ||
"license": "MIT", | ||
"dependencies": { | ||
"tslib": "^2.0.3" | ||
}, | ||
"devDependencies": { | ||
"@twurple/auth": "^5.1.0-pre.5", | ||
"@types/twitch-ext": "^1.24.4" | ||
}, | ||
"peerDependencies": { | ||
"@twurple/auth": "^5.0.0", | ||
"@types/twitch-ext": "^1.24.4" | ||
}, | ||
"files": [ | ||
"LICENSE", | ||
"README.md", | ||
"lib", | ||
"!lib/**/*.d.ts.map", | ||
"es" | ||
], | ||
"scripts": { | ||
"build": "tsukuru", | ||
"rebuild": "tsukuru --clean" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import type { AccessToken, AuthProvider, AuthProviderTokenType } from '@twurple/auth'; | ||
|
||
export class ExtensionAuthProvider implements AuthProvider { | ||
authorizationType = 'Extension'; | ||
tokenType: AuthProviderTokenType = 'app'; | ||
currentScopes = []; | ||
|
||
constructor(public readonly clientId: string) { | ||
if (!('Twitch' in globalThis)) { | ||
throw new Error("This is not an extension, or you didn't load the Twitch Extension Helper properly."); | ||
} | ||
} | ||
|
||
async getAccessToken(scopes?: string[]): Promise<AccessToken> { | ||
if (scopes?.length) { | ||
throw new Error( | ||
`Scope ${scopes.join( | ||
', ' | ||
)} requested but direct extension calls do not support scopes. Please use an EBS instead.` | ||
); | ||
} | ||
const accessToken = Twitch.ext.viewer.helixToken as string | null; | ||
|
||
if (accessToken == null) { | ||
throw new Error(`Could not retrieve an access token from the Twitch extension environment. This could mean different things: | ||
- You're in a mobile extension or the Extension Developer Rig. This is a known issue that has to be resolved by Twitch. | ||
- You didn't wait for the onAuthorized callback before doing any API calls. Before this callback fires, a token is not available.`); | ||
} | ||
|
||
return { | ||
accessToken, | ||
refreshToken: null, | ||
expiresIn: null, | ||
obtainmentTimestamp: Date.now(), | ||
scope: [] | ||
}; | ||
} | ||
|
||
async refresh(): Promise<AccessToken> { | ||
// basically just retry | ||
return await this.getAccessToken(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { ExtensionAuthProvider } from './ExtensionAuthProvider'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"extends": "../../tsconfig", | ||
"compilerOptions": { | ||
"rootDir": "src", | ||
"outDir": "lib" | ||
}, | ||
"exclude": ["node_modules", "lib", "es"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters