antarctic 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +24 -0
- package/README.md +155 -0
- package/dist/auth.d.ts +63 -0
- package/dist/auth.js +191 -0
- package/dist/client.d.ts +16 -0
- package/dist/client.js +105 -0
- package/dist/index.d.ts +70 -0
- package/dist/index.js +69 -0
- package/dist/oauth2.d.ts +16 -0
- package/dist/oauth2.js +67 -0
- package/dist/oidc.d.ts +1 -0
- package/dist/oidc.js +11 -0
- package/dist/providers/42.d.ts +16 -0
- package/dist/providers/42.js +75 -0
- package/dist/providers/amazon-cognito.d.ts +21 -0
- package/dist/providers/amazon-cognito.js +76 -0
- package/dist/providers/anilist.d.ts +14 -0
- package/dist/providers/anilist.js +91 -0
- package/dist/providers/apple.d.ts +22 -0
- package/dist/providers/apple.js +106 -0
- package/dist/providers/atlassian.d.ts +17 -0
- package/dist/providers/atlassian.js +83 -0
- package/dist/providers/auth0.d.ts +21 -0
- package/dist/providers/auth0.js +80 -0
- package/dist/providers/authentik.d.ts +21 -0
- package/dist/providers/authentik.js +75 -0
- package/dist/providers/autodesk.d.ts +16 -0
- package/dist/providers/autodesk.js +60 -0
- package/dist/providers/battlenet.d.ts +16 -0
- package/dist/providers/battlenet.js +69 -0
- package/dist/providers/bitbucket.d.ts +15 -0
- package/dist/providers/bitbucket.js +79 -0
- package/dist/providers/box.d.ts +18 -0
- package/dist/providers/box.js +90 -0
- package/dist/providers/bungie.d.ts +17 -0
- package/dist/providers/bungie.js +72 -0
- package/dist/providers/coinbase.d.ts +18 -0
- package/dist/providers/coinbase.js +95 -0
- package/dist/providers/discord.d.ts +16 -0
- package/dist/providers/discord.js +68 -0
- package/dist/providers/donation-alerts.d.ts +17 -0
- package/dist/providers/donation-alerts.js +86 -0
- package/dist/providers/dribbble.d.ts +16 -0
- package/dist/providers/dribbble.js +73 -0
- package/dist/providers/dropbox.d.ts +16 -0
- package/dist/providers/dropbox.js +96 -0
- package/dist/providers/epicgames.d.ts +16 -0
- package/dist/providers/epicgames.js +60 -0
- package/dist/providers/etsy.d.ts +15 -0
- package/dist/providers/etsy.js +68 -0
- package/dist/providers/facebook.d.ts +16 -0
- package/dist/providers/facebook.js +80 -0
- package/dist/providers/figma.d.ts +15 -0
- package/dist/providers/figma.js +56 -0
- package/dist/providers/gitea.d.ts +19 -0
- package/dist/providers/gitea.js +62 -0
- package/dist/providers/github.d.ts +17 -0
- package/dist/providers/github.js +142 -0
- package/dist/providers/gitlab.d.ts +21 -0
- package/dist/providers/gitlab.js +64 -0
- package/dist/providers/google.d.ts +16 -0
- package/dist/providers/google.js +70 -0
- package/dist/providers/intuit.d.ts +16 -0
- package/dist/providers/intuit.js +66 -0
- package/dist/providers/kakao.d.ts +17 -0
- package/dist/providers/kakao.js +95 -0
- package/dist/providers/keycloak.d.ts +21 -0
- package/dist/providers/keycloak.js +74 -0
- package/dist/providers/kick.d.ts +18 -0
- package/dist/providers/kick.js +103 -0
- package/dist/providers/lichess.d.ts +14 -0
- package/dist/providers/lichess.js +63 -0
- package/dist/providers/line.d.ts +17 -0
- package/dist/providers/line.js +98 -0
- package/dist/providers/linear.d.ts +16 -0
- package/dist/providers/linear.js +112 -0
- package/dist/providers/linkedin.d.ts +17 -0
- package/dist/providers/linkedin.js +89 -0
- package/dist/providers/mastodon.d.ts +20 -0
- package/dist/providers/mastodon.js +67 -0
- package/dist/providers/mercadolibre.d.ts +17 -0
- package/dist/providers/mercadolibre.js +95 -0
- package/dist/providers/mercadopago.d.ts +17 -0
- package/dist/providers/mercadopago.js +95 -0
- package/dist/providers/microsoft-entra-id.d.ts +20 -0
- package/dist/providers/microsoft-entra-id.js +120 -0
- package/dist/providers/myanimelist.d.ts +15 -0
- package/dist/providers/myanimelist.js +56 -0
- package/dist/providers/naver.d.ts +17 -0
- package/dist/providers/naver.js +83 -0
- package/dist/providers/notion.d.ts +14 -0
- package/dist/providers/notion.js +65 -0
- package/dist/providers/okta.d.ts +22 -0
- package/dist/providers/okta.js +85 -0
- package/dist/providers/osu.d.ts +17 -0
- package/dist/providers/osu.js +82 -0
- package/dist/providers/patreon.d.ts +17 -0
- package/dist/providers/patreon.js +89 -0
- package/dist/providers/polar.d.ts +18 -0
- package/dist/providers/polar.js +100 -0
- package/dist/providers/reddit.d.ts +15 -0
- package/dist/providers/reddit.js +55 -0
- package/dist/providers/roblox.d.ts +16 -0
- package/dist/providers/roblox.js +67 -0
- package/dist/providers/salesforce.d.ts +21 -0
- package/dist/providers/salesforce.js +74 -0
- package/dist/providers/shikimori.d.ts +17 -0
- package/dist/providers/shikimori.js +81 -0
- package/dist/providers/slack.d.ts +14 -0
- package/dist/providers/slack.js +59 -0
- package/dist/providers/spotify.d.ts +15 -0
- package/dist/providers/spotify.js +67 -0
- package/dist/providers/startgg.d.ts +17 -0
- package/dist/providers/startgg.js +143 -0
- package/dist/providers/strava.d.ts +17 -0
- package/dist/providers/strava.js +85 -0
- package/dist/providers/synology.d.ts +9 -0
- package/dist/providers/synology.js +20 -0
- package/dist/providers/tiktok.d.ts +18 -0
- package/dist/providers/tiktok.js +109 -0
- package/dist/providers/tiltify.d.ts +17 -0
- package/dist/providers/tiltify.js +90 -0
- package/dist/providers/tumblr.d.ts +15 -0
- package/dist/providers/tumblr.js +63 -0
- package/dist/providers/twitch.d.ts +17 -0
- package/dist/providers/twitch.js +91 -0
- package/dist/providers/twitter.d.ts +16 -0
- package/dist/providers/twitter.js +67 -0
- package/dist/providers/vk.d.ts +16 -0
- package/dist/providers/vk.js +82 -0
- package/dist/providers/withings.d.ts +16 -0
- package/dist/providers/withings.js +120 -0
- package/dist/providers/workos.d.ts +16 -0
- package/dist/providers/workos.js +82 -0
- package/dist/providers/yahoo.d.ts +15 -0
- package/dist/providers/yahoo.js +65 -0
- package/dist/providers/yandex.d.ts +15 -0
- package/dist/providers/yandex.js +66 -0
- package/dist/providers/zoom.d.ts +16 -0
- package/dist/providers/zoom.js +66 -0
- package/dist/request.d.ts +26 -0
- package/dist/request.js +182 -0
- package/dist/utils.d.ts +2 -0
- package/dist/utils.js +20 -0
- package/package.json +42 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Antarctic is a fork of Arctic (https://github.com/pilcrowonpaper/arctic).
|
|
4
|
+
|
|
5
|
+
Copyright (c) 2023 pilcrowOnPaper
|
|
6
|
+
Copyright (c) 2026 Francisco Presencia
|
|
7
|
+
|
|
8
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
9
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
10
|
+
in the Software without restriction, including without limitation the rights
|
|
11
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
12
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
13
|
+
furnished to do so, subject to the following conditions:
|
|
14
|
+
|
|
15
|
+
The above copyright notice and this permission notice shall be included in all
|
|
16
|
+
copies or substantial portions of the Software.
|
|
17
|
+
|
|
18
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
19
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
20
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
21
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
22
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
23
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
24
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
# Antarctic
|
|
2
|
+
|
|
3
|
+
Antarctic is a fork of [Arctic](https://arcticjs.dev) by [pilcrowOnPaper](https://github.com/pilcrowOnPaper), adding a high level auth layer on top of its OAuth 2.0 clients. Only the authorization code flow is supported. Built on top of the Fetch API, it's light weight, fully-typed, and runtime-agnostic.
|
|
4
|
+
|
|
5
|
+
All of the OAuth 2.0 clients and provider coverage are Arctic's work. If you only need those, use [Arctic](https://arcticjs.dev) directly. See [credits](#credits).
|
|
6
|
+
|
|
7
|
+
```
|
|
8
|
+
npm install antarctic
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## High-level API
|
|
12
|
+
|
|
13
|
+
Construct a provider with an options object and get two methods that handle the whole flow: `getAuthorizationURL()` and `getUser()`. State and PKCE values are generated for you and kept in a [polystore](https://polystore.dev) compatible key-value store.
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
import * as auth from "antarctic";
|
|
17
|
+
import kv from "polystore";
|
|
18
|
+
|
|
19
|
+
const store = kv(new Map());
|
|
20
|
+
const scopes = ["read:user", "user:email"];
|
|
21
|
+
|
|
22
|
+
const github = new auth.GitHub({ store, scopes });
|
|
23
|
+
|
|
24
|
+
// Redirect the user here to sign in.
|
|
25
|
+
const url = await github.getAuthorizationURL();
|
|
26
|
+
|
|
27
|
+
// In the OAuth callback route:
|
|
28
|
+
const user = await github.getUser(request.url);
|
|
29
|
+
// { id: "1", name: "The Octocat", email: "octocat@github.com", image: "https://..." }
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
`getUser()` accepts the callback query as a full URL, a query string, a `URLSearchParams`, or a plain object. It validates the `state`, exchanges the code (with PKCE where the provider supports it), fetches the profile, deletes the consumed state, and returns a normalized user: `{ id, name, email, image }`.
|
|
33
|
+
|
|
34
|
+
Options resolve as `explicit > environment > provider default`. Every option except `store` can come from the environment, named after the provider:
|
|
35
|
+
|
|
36
|
+
```
|
|
37
|
+
GITHUB_CLIENT_ID
|
|
38
|
+
GITHUB_CLIENT_SECRET
|
|
39
|
+
GITHUB_REDIRECT_URI
|
|
40
|
+
GITHUB_SCOPES
|
|
41
|
+
|
|
42
|
+
GOOGLE_CLIENT_ID
|
|
43
|
+
...
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
`GITHUB_SCOPES` takes a list separated by commas, whitespace, or both. Scopes resolve as `argument > constructor > environment > provider default`, where the provider default is the minimal set that yields a full profile:
|
|
47
|
+
|
|
48
|
+
```ts
|
|
49
|
+
await github.getAuthorizationURL(["repo"]); // overrides the constructor and the environment
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
The environment is read when the provider is constructed, so load your `.env` file first. See the [documentation](https://documentation.page/github/franciscop/antarctic/) for the details.
|
|
53
|
+
|
|
54
|
+
Errors thrown by the high-level layer: `InvalidOAuthStateError`, `InvalidOAuthCallbackError`, `OAuthConfigurationError`, and `OAuthProviderError`.
|
|
55
|
+
|
|
56
|
+
Sessions, cookies, and your user database remain your responsibility: take the returned user and plug it into your framework of choice.
|
|
57
|
+
|
|
58
|
+
## Low-level API
|
|
59
|
+
|
|
60
|
+
The original Arctic API is unchanged and remains available on the same objects, including the positional constructors:
|
|
61
|
+
|
|
62
|
+
```ts
|
|
63
|
+
import * as arctic from "antarctic";
|
|
64
|
+
|
|
65
|
+
const github = new arctic.GitHub(clientId, clientSecret, redirectURI);
|
|
66
|
+
|
|
67
|
+
const state = arctic.generateState();
|
|
68
|
+
const scopes = ["user:email"];
|
|
69
|
+
const authorizationURL = github.createAuthorizationURL(state, scopes);
|
|
70
|
+
|
|
71
|
+
// ...
|
|
72
|
+
|
|
73
|
+
const tokens = await github.validateAuthorizationCode(code);
|
|
74
|
+
const accessToken = tokens.accessToken();
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
> Antarctic only supports providers that follow the OAuth 2.0 spec (including PKCE and token revocation).
|
|
78
|
+
|
|
79
|
+
## Credits
|
|
80
|
+
|
|
81
|
+
Antarctic is a fork of [Arctic](https://github.com/pilcrowonpaper/arctic), created and maintained by [pilcrowOnPaper](https://github.com/pilcrowOnPaper). The OAuth 2.0 clients, the provider implementations, the docs those pages grew from, and the design that makes all of it consistent are their work. Antarctic adds one layer on top: `getAuthorizationURL()`, `getUser()`, and the option resolution around them.
|
|
82
|
+
|
|
83
|
+
Arctic is MIT licensed. Antarctic keeps that license and the original copyright notice, and adds its own for the new work. See [LICENSE](./LICENSE).
|
|
84
|
+
|
|
85
|
+
If you only need the OAuth 2.0 clients without the high level layer, use [Arctic](https://arcticjs.dev) directly. Please report provider issues that are not specific to Antarctic's additions upstream, where they benefit everyone.
|
|
86
|
+
|
|
87
|
+
## Semver
|
|
88
|
+
|
|
89
|
+
Antarctic does not strictly follow semantic versioning. While we aim to only introduce breaking changes in major versions, we may introduce them in a minor update if a provider updates their API in a non-backward compatible way. However, they will never be introduced in a patch update.
|
|
90
|
+
|
|
91
|
+
## Supported providers
|
|
92
|
+
|
|
93
|
+
- 42 School
|
|
94
|
+
- Amazon Cognito
|
|
95
|
+
- AniList
|
|
96
|
+
- Apple
|
|
97
|
+
- Atlassian
|
|
98
|
+
- Auth0
|
|
99
|
+
- Authentik
|
|
100
|
+
- Autodesk Platform Services
|
|
101
|
+
- Battle.net
|
|
102
|
+
- Bitbucket
|
|
103
|
+
- Box
|
|
104
|
+
- Bungie
|
|
105
|
+
- Coinbase
|
|
106
|
+
- Discord
|
|
107
|
+
- DonationAlerts
|
|
108
|
+
- Dribbble
|
|
109
|
+
- Dropbox
|
|
110
|
+
- Etsy
|
|
111
|
+
- Epic Games
|
|
112
|
+
- Facebook
|
|
113
|
+
- Figma
|
|
114
|
+
- Gitea
|
|
115
|
+
- GitHub
|
|
116
|
+
- GitLab
|
|
117
|
+
- Google
|
|
118
|
+
- Intuit
|
|
119
|
+
- Kakao
|
|
120
|
+
- KeyCloak
|
|
121
|
+
- Kick
|
|
122
|
+
- Lichess
|
|
123
|
+
- Line
|
|
124
|
+
- Linear
|
|
125
|
+
- LinkedIn
|
|
126
|
+
- Mastodon
|
|
127
|
+
- MercadoLibre
|
|
128
|
+
- MercadoPago
|
|
129
|
+
- Microsoft Entra ID
|
|
130
|
+
- MyAnimeList
|
|
131
|
+
- Naver
|
|
132
|
+
- Notion
|
|
133
|
+
- Okta
|
|
134
|
+
- osu!
|
|
135
|
+
- Patreon
|
|
136
|
+
- Polar
|
|
137
|
+
- Reddit
|
|
138
|
+
- Roblox
|
|
139
|
+
- Salesforce
|
|
140
|
+
- Shikimori
|
|
141
|
+
- Slack
|
|
142
|
+
- Spotify
|
|
143
|
+
- Start.gg
|
|
144
|
+
- Strava
|
|
145
|
+
- Synology
|
|
146
|
+
- TikTok
|
|
147
|
+
- Tiltify
|
|
148
|
+
- Tumblr
|
|
149
|
+
- Twitch
|
|
150
|
+
- Twitter
|
|
151
|
+
- VK
|
|
152
|
+
- WorkOS
|
|
153
|
+
- Yahoo
|
|
154
|
+
- Yandex
|
|
155
|
+
- Zoom
|
package/dist/auth.d.ts
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
export interface OAuthUser {
|
|
2
|
+
id: string;
|
|
3
|
+
name?: string | null;
|
|
4
|
+
email?: string | null;
|
|
5
|
+
image?: string | null;
|
|
6
|
+
}
|
|
7
|
+
export interface OAuthStateStore {
|
|
8
|
+
get: (key: string) => Promise<any>;
|
|
9
|
+
set: (key: string, value: any, options?: {
|
|
10
|
+
expires?: number | string;
|
|
11
|
+
}) => Promise<any>;
|
|
12
|
+
del: (key: string) => Promise<any>;
|
|
13
|
+
}
|
|
14
|
+
export interface ProviderOptions {
|
|
15
|
+
clientId?: string;
|
|
16
|
+
clientSecret?: string;
|
|
17
|
+
redirectURI?: string;
|
|
18
|
+
scopes?: string[];
|
|
19
|
+
store: OAuthStateStore;
|
|
20
|
+
}
|
|
21
|
+
export declare class OAuthConfigurationError extends Error {
|
|
22
|
+
}
|
|
23
|
+
export declare class InvalidOAuthCallbackError extends Error {
|
|
24
|
+
}
|
|
25
|
+
export declare class InvalidOAuthStateError extends Error {
|
|
26
|
+
constructor();
|
|
27
|
+
}
|
|
28
|
+
export declare class OAuthProviderError extends Error {
|
|
29
|
+
code: string | null;
|
|
30
|
+
constructor(message: string, code?: string | null);
|
|
31
|
+
}
|
|
32
|
+
export interface AuthConfig {
|
|
33
|
+
clientId: string;
|
|
34
|
+
clientSecret: string | null;
|
|
35
|
+
redirectURI: string | null;
|
|
36
|
+
scopes: string[] | null;
|
|
37
|
+
store: OAuthStateStore;
|
|
38
|
+
}
|
|
39
|
+
export declare function resolveProviderOption(explicit: string | undefined, envPrefix: string, envSuffix: string): string | null;
|
|
40
|
+
export declare function requireProviderOption(explicit: string | undefined, envPrefix: string, envSuffix: string, optionName: string): string;
|
|
41
|
+
export declare function resolveAuthConfig(envPrefix: string, options: ProviderOptions, requirements: {
|
|
42
|
+
clientSecret?: boolean;
|
|
43
|
+
redirectURI?: boolean;
|
|
44
|
+
}): AuthConfig;
|
|
45
|
+
export declare function requireAuthConfig(auth: AuthConfig | null): AuthConfig;
|
|
46
|
+
export declare function resolveScopes(argument: string[] | undefined, auth: AuthConfig, defaultScopes: string[]): string[];
|
|
47
|
+
export interface StoredOAuthState {
|
|
48
|
+
codeVerifier?: string;
|
|
49
|
+
nonce?: string;
|
|
50
|
+
scopes?: string[];
|
|
51
|
+
}
|
|
52
|
+
export declare function saveOAuthState(store: OAuthStateStore, state: string, payload: StoredOAuthState): Promise<void>;
|
|
53
|
+
export declare function consumeOAuthState(store: OAuthStateStore, state: string): Promise<StoredOAuthState>;
|
|
54
|
+
export type OAuthCallbackQuery = string | URL | URLSearchParams | Record<string, string | string[] | undefined | null>;
|
|
55
|
+
export declare function parseCallbackQuery(query: OAuthCallbackQuery): {
|
|
56
|
+
code: string;
|
|
57
|
+
state: string;
|
|
58
|
+
};
|
|
59
|
+
export declare function generateOAuthState(): string;
|
|
60
|
+
export declare function generateOAuthCodeVerifier(): string;
|
|
61
|
+
export declare function fetchUserProfile(url: string | URL, accessToken: string, headers?: Record<string, string>): Promise<Record<string, unknown>>;
|
|
62
|
+
export declare function profileString(value: unknown): string | null;
|
|
63
|
+
export declare function profileId(value: unknown): string;
|
package/dist/auth.js
ADDED
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
import { generateCodeVerifier, generateState } from "./oauth2.js";
|
|
2
|
+
export class OAuthConfigurationError extends Error {
|
|
3
|
+
}
|
|
4
|
+
export class InvalidOAuthCallbackError extends Error {
|
|
5
|
+
}
|
|
6
|
+
export class InvalidOAuthStateError extends Error {
|
|
7
|
+
constructor() {
|
|
8
|
+
super("Unknown or expired OAuth state");
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
export class OAuthProviderError extends Error {
|
|
12
|
+
code;
|
|
13
|
+
constructor(message, code) {
|
|
14
|
+
super(message);
|
|
15
|
+
this.code = code ?? null;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
function readEnvironment(name) {
|
|
19
|
+
const env = globalThis.process
|
|
20
|
+
?.env;
|
|
21
|
+
const value = env?.[name];
|
|
22
|
+
if (value === undefined || value === "") {
|
|
23
|
+
return null;
|
|
24
|
+
}
|
|
25
|
+
return value;
|
|
26
|
+
}
|
|
27
|
+
export function resolveProviderOption(explicit, envPrefix, envSuffix) {
|
|
28
|
+
return explicit ?? readEnvironment(`${envPrefix}_${envSuffix}`);
|
|
29
|
+
}
|
|
30
|
+
export function requireProviderOption(explicit, envPrefix, envSuffix, optionName) {
|
|
31
|
+
const value = resolveProviderOption(explicit, envPrefix, envSuffix);
|
|
32
|
+
if (value === null) {
|
|
33
|
+
throw new OAuthConfigurationError(`Missing '${optionName}': pass it in the constructor options or set ${envPrefix}_${envSuffix}`);
|
|
34
|
+
}
|
|
35
|
+
return value;
|
|
36
|
+
}
|
|
37
|
+
export function resolveAuthConfig(envPrefix, options, requirements) {
|
|
38
|
+
const clientId = requireProviderOption(options.clientId, envPrefix, "CLIENT_ID", "clientId");
|
|
39
|
+
let clientSecret;
|
|
40
|
+
if (requirements.clientSecret === true) {
|
|
41
|
+
clientSecret = requireProviderOption(options.clientSecret, envPrefix, "CLIENT_SECRET", "clientSecret");
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
|
+
clientSecret = resolveProviderOption(options.clientSecret, envPrefix, "CLIENT_SECRET");
|
|
45
|
+
}
|
|
46
|
+
let redirectURI;
|
|
47
|
+
if (requirements.redirectURI === true) {
|
|
48
|
+
redirectURI = requireProviderOption(options.redirectURI, envPrefix, "REDIRECT_URI", "redirectURI");
|
|
49
|
+
}
|
|
50
|
+
else {
|
|
51
|
+
redirectURI = resolveProviderOption(options.redirectURI, envPrefix, "REDIRECT_URI");
|
|
52
|
+
}
|
|
53
|
+
let scopes = options.scopes ?? null;
|
|
54
|
+
if (scopes === null) {
|
|
55
|
+
const envScopes = readEnvironment(`${envPrefix}_SCOPES`);
|
|
56
|
+
if (envScopes !== null) {
|
|
57
|
+
scopes = envScopes.split(/[\s,]+/).filter((scope) => scope !== "");
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
if (options.store === undefined) {
|
|
61
|
+
throw new OAuthConfigurationError("Missing 'store': pass a polystore-compatible store");
|
|
62
|
+
}
|
|
63
|
+
return {
|
|
64
|
+
clientId,
|
|
65
|
+
clientSecret,
|
|
66
|
+
redirectURI,
|
|
67
|
+
scopes,
|
|
68
|
+
store: options.store
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
export function requireAuthConfig(auth) {
|
|
72
|
+
if (auth === null) {
|
|
73
|
+
throw new OAuthConfigurationError("getAuthorizationURL() and getUser() require the options constructor with a 'store'");
|
|
74
|
+
}
|
|
75
|
+
return auth;
|
|
76
|
+
}
|
|
77
|
+
export function resolveScopes(argument, auth, defaultScopes) {
|
|
78
|
+
return argument ?? auth.scopes ?? defaultScopes;
|
|
79
|
+
}
|
|
80
|
+
const stateKeyPrefix = "arctic:state:";
|
|
81
|
+
const stateExpiresSeconds = 60 * 10;
|
|
82
|
+
export async function saveOAuthState(store, state, payload) {
|
|
83
|
+
await store.set(stateKeyPrefix + state, payload, { expires: stateExpiresSeconds });
|
|
84
|
+
}
|
|
85
|
+
export async function consumeOAuthState(store, state) {
|
|
86
|
+
const key = stateKeyPrefix + state;
|
|
87
|
+
const payload = await store.get(key);
|
|
88
|
+
if (typeof payload !== "object" || payload === null) {
|
|
89
|
+
throw new InvalidOAuthStateError();
|
|
90
|
+
}
|
|
91
|
+
await store.del(key);
|
|
92
|
+
return payload;
|
|
93
|
+
}
|
|
94
|
+
export function parseCallbackQuery(query) {
|
|
95
|
+
let params;
|
|
96
|
+
if (typeof query === "string") {
|
|
97
|
+
if (query.startsWith("http://") || query.startsWith("https://")) {
|
|
98
|
+
params = new URL(query).searchParams;
|
|
99
|
+
}
|
|
100
|
+
else {
|
|
101
|
+
params = new URLSearchParams(query.startsWith("?") ? query.slice(1) : query);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
else if (query instanceof URL) {
|
|
105
|
+
params = query.searchParams;
|
|
106
|
+
}
|
|
107
|
+
else if (query instanceof URLSearchParams) {
|
|
108
|
+
params = query;
|
|
109
|
+
}
|
|
110
|
+
else if (typeof query === "object" && query !== null) {
|
|
111
|
+
params = new URLSearchParams();
|
|
112
|
+
for (const [key, value] of Object.entries(query)) {
|
|
113
|
+
if (typeof value === "string") {
|
|
114
|
+
params.set(key, value);
|
|
115
|
+
}
|
|
116
|
+
else if (Array.isArray(value) && typeof value[0] === "string") {
|
|
117
|
+
params.set(key, value[0]);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
else {
|
|
122
|
+
throw new InvalidOAuthCallbackError("Invalid callback query");
|
|
123
|
+
}
|
|
124
|
+
const error = params.get("error");
|
|
125
|
+
if (error !== null) {
|
|
126
|
+
const description = params.get("error_description");
|
|
127
|
+
throw new OAuthProviderError(`OAuth callback error: ${error}${description !== null ? `: ${description}` : ""}`, error);
|
|
128
|
+
}
|
|
129
|
+
const code = params.get("code");
|
|
130
|
+
const state = params.get("state");
|
|
131
|
+
if (code === null || code === "" || state === null || state === "") {
|
|
132
|
+
throw new InvalidOAuthCallbackError("Missing 'code' or 'state' in callback query");
|
|
133
|
+
}
|
|
134
|
+
return { code, state };
|
|
135
|
+
}
|
|
136
|
+
export function generateOAuthState() {
|
|
137
|
+
return generateState();
|
|
138
|
+
}
|
|
139
|
+
export function generateOAuthCodeVerifier() {
|
|
140
|
+
return generateCodeVerifier();
|
|
141
|
+
}
|
|
142
|
+
export async function fetchUserProfile(url, accessToken, headers) {
|
|
143
|
+
const request = new Request(url, { method: "GET" });
|
|
144
|
+
request.headers.set("Authorization", `Bearer ${accessToken}`);
|
|
145
|
+
request.headers.set("Accept", "application/json");
|
|
146
|
+
request.headers.set("User-Agent", "arctic");
|
|
147
|
+
if (headers !== undefined) {
|
|
148
|
+
for (const [name, value] of Object.entries(headers)) {
|
|
149
|
+
request.headers.set(name, value);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
let response;
|
|
153
|
+
try {
|
|
154
|
+
response = await fetch(request);
|
|
155
|
+
}
|
|
156
|
+
catch {
|
|
157
|
+
throw new OAuthProviderError("Failed to fetch user profile");
|
|
158
|
+
}
|
|
159
|
+
if (!response.ok) {
|
|
160
|
+
if (response.body !== null) {
|
|
161
|
+
await response.body.cancel();
|
|
162
|
+
}
|
|
163
|
+
throw new OAuthProviderError(`User profile request failed with status ${response.status}`);
|
|
164
|
+
}
|
|
165
|
+
let data;
|
|
166
|
+
try {
|
|
167
|
+
data = await response.json();
|
|
168
|
+
}
|
|
169
|
+
catch {
|
|
170
|
+
throw new OAuthProviderError("User profile response was not valid JSON");
|
|
171
|
+
}
|
|
172
|
+
if (typeof data !== "object" || data === null) {
|
|
173
|
+
throw new OAuthProviderError("Unexpected user profile response body");
|
|
174
|
+
}
|
|
175
|
+
return data;
|
|
176
|
+
}
|
|
177
|
+
export function profileString(value) {
|
|
178
|
+
if (typeof value === "string" && value !== "") {
|
|
179
|
+
return value;
|
|
180
|
+
}
|
|
181
|
+
return null;
|
|
182
|
+
}
|
|
183
|
+
export function profileId(value) {
|
|
184
|
+
if (typeof value === "string" && value !== "") {
|
|
185
|
+
return value;
|
|
186
|
+
}
|
|
187
|
+
if (typeof value === "number") {
|
|
188
|
+
return value.toString();
|
|
189
|
+
}
|
|
190
|
+
throw new OAuthProviderError("User profile is missing an id");
|
|
191
|
+
}
|
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { OAuth2Tokens } from "./oauth2.js";
|
|
2
|
+
export declare class OAuth2Client {
|
|
3
|
+
clientId: string;
|
|
4
|
+
private clientPassword;
|
|
5
|
+
private redirectURI;
|
|
6
|
+
constructor(clientId: string, clientPassword: string | null, redirectURI: string | null);
|
|
7
|
+
createAuthorizationURL(authorizationEndpoint: string, state: string, scopes: string[]): URL;
|
|
8
|
+
createAuthorizationURLWithPKCE(authorizationEndpoint: string, state: string, codeChallengeMethod: CodeChallengeMethod, codeVerifier: string, scopes: string[]): URL;
|
|
9
|
+
validateAuthorizationCode(tokenEndpoint: string, code: string, codeVerifier: string | null): Promise<OAuth2Tokens>;
|
|
10
|
+
refreshAccessToken(tokenEndpoint: string, refreshToken: string, scopes: string[]): Promise<OAuth2Tokens>;
|
|
11
|
+
revokeToken(tokenRevocationEndpoint: string, token: string): Promise<void>;
|
|
12
|
+
}
|
|
13
|
+
export declare enum CodeChallengeMethod {
|
|
14
|
+
S256 = 0,
|
|
15
|
+
Plain = 1
|
|
16
|
+
}
|
package/dist/client.js
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { encodeBasicCredentials, sendTokenRevocationRequest } from "./request.js";
|
|
2
|
+
import { createS256CodeChallenge } from "./oauth2.js";
|
|
3
|
+
import { createOAuth2Request, sendTokenRequest } from "./request.js";
|
|
4
|
+
export class OAuth2Client {
|
|
5
|
+
clientId;
|
|
6
|
+
clientPassword;
|
|
7
|
+
redirectURI;
|
|
8
|
+
constructor(clientId, clientPassword, redirectURI) {
|
|
9
|
+
this.clientId = clientId;
|
|
10
|
+
this.clientPassword = clientPassword;
|
|
11
|
+
this.redirectURI = redirectURI;
|
|
12
|
+
}
|
|
13
|
+
createAuthorizationURL(authorizationEndpoint, state, scopes) {
|
|
14
|
+
const url = new URL(authorizationEndpoint);
|
|
15
|
+
url.searchParams.set("response_type", "code");
|
|
16
|
+
url.searchParams.set("client_id", this.clientId);
|
|
17
|
+
if (this.redirectURI !== null) {
|
|
18
|
+
url.searchParams.set("redirect_uri", this.redirectURI);
|
|
19
|
+
}
|
|
20
|
+
url.searchParams.set("state", state);
|
|
21
|
+
if (scopes.length > 0) {
|
|
22
|
+
url.searchParams.set("scope", scopes.join(" "));
|
|
23
|
+
}
|
|
24
|
+
return url;
|
|
25
|
+
}
|
|
26
|
+
createAuthorizationURLWithPKCE(authorizationEndpoint, state, codeChallengeMethod, codeVerifier, scopes) {
|
|
27
|
+
const url = new URL(authorizationEndpoint);
|
|
28
|
+
url.searchParams.set("response_type", "code");
|
|
29
|
+
url.searchParams.set("client_id", this.clientId);
|
|
30
|
+
if (this.redirectURI !== null) {
|
|
31
|
+
url.searchParams.set("redirect_uri", this.redirectURI);
|
|
32
|
+
}
|
|
33
|
+
url.searchParams.set("state", state);
|
|
34
|
+
if (codeChallengeMethod === CodeChallengeMethod.S256) {
|
|
35
|
+
const codeChallenge = createS256CodeChallenge(codeVerifier);
|
|
36
|
+
url.searchParams.set("code_challenge_method", "S256");
|
|
37
|
+
url.searchParams.set("code_challenge", codeChallenge);
|
|
38
|
+
}
|
|
39
|
+
else if (codeChallengeMethod === CodeChallengeMethod.Plain) {
|
|
40
|
+
url.searchParams.set("code_challenge_method", "plain");
|
|
41
|
+
url.searchParams.set("code_challenge", codeVerifier);
|
|
42
|
+
}
|
|
43
|
+
if (scopes.length > 0) {
|
|
44
|
+
url.searchParams.set("scope", scopes.join(" "));
|
|
45
|
+
}
|
|
46
|
+
return url;
|
|
47
|
+
}
|
|
48
|
+
async validateAuthorizationCode(tokenEndpoint, code, codeVerifier) {
|
|
49
|
+
const body = new URLSearchParams();
|
|
50
|
+
body.set("grant_type", "authorization_code");
|
|
51
|
+
body.set("code", code);
|
|
52
|
+
if (this.redirectURI !== null) {
|
|
53
|
+
body.set("redirect_uri", this.redirectURI);
|
|
54
|
+
}
|
|
55
|
+
if (codeVerifier !== null) {
|
|
56
|
+
body.set("code_verifier", codeVerifier);
|
|
57
|
+
}
|
|
58
|
+
if (this.clientPassword === null) {
|
|
59
|
+
body.set("client_id", this.clientId);
|
|
60
|
+
}
|
|
61
|
+
const request = createOAuth2Request(tokenEndpoint, body);
|
|
62
|
+
if (this.clientPassword !== null) {
|
|
63
|
+
const encodedCredentials = encodeBasicCredentials(this.clientId, this.clientPassword);
|
|
64
|
+
request.headers.set("Authorization", `Basic ${encodedCredentials}`);
|
|
65
|
+
}
|
|
66
|
+
const tokens = await sendTokenRequest(request);
|
|
67
|
+
return tokens;
|
|
68
|
+
}
|
|
69
|
+
async refreshAccessToken(tokenEndpoint, refreshToken, scopes) {
|
|
70
|
+
const body = new URLSearchParams();
|
|
71
|
+
body.set("grant_type", "refresh_token");
|
|
72
|
+
body.set("refresh_token", refreshToken);
|
|
73
|
+
if (this.clientPassword === null) {
|
|
74
|
+
body.set("client_id", this.clientId);
|
|
75
|
+
}
|
|
76
|
+
if (scopes.length > 0) {
|
|
77
|
+
body.set("scope", scopes.join(" "));
|
|
78
|
+
}
|
|
79
|
+
const request = createOAuth2Request(tokenEndpoint, body);
|
|
80
|
+
if (this.clientPassword !== null) {
|
|
81
|
+
const encodedCredentials = encodeBasicCredentials(this.clientId, this.clientPassword);
|
|
82
|
+
request.headers.set("Authorization", `Basic ${encodedCredentials}`);
|
|
83
|
+
}
|
|
84
|
+
const tokens = await sendTokenRequest(request);
|
|
85
|
+
return tokens;
|
|
86
|
+
}
|
|
87
|
+
async revokeToken(tokenRevocationEndpoint, token) {
|
|
88
|
+
const body = new URLSearchParams();
|
|
89
|
+
body.set("token", token);
|
|
90
|
+
if (this.clientPassword === null) {
|
|
91
|
+
body.set("client_id", this.clientId);
|
|
92
|
+
}
|
|
93
|
+
const request = createOAuth2Request(tokenRevocationEndpoint, body);
|
|
94
|
+
if (this.clientPassword !== null) {
|
|
95
|
+
const encodedCredentials = encodeBasicCredentials(this.clientId, this.clientPassword);
|
|
96
|
+
request.headers.set("Authorization", `Basic ${encodedCredentials}`);
|
|
97
|
+
}
|
|
98
|
+
await sendTokenRevocationRequest(request);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
export var CodeChallengeMethod;
|
|
102
|
+
(function (CodeChallengeMethod) {
|
|
103
|
+
CodeChallengeMethod[CodeChallengeMethod["S256"] = 0] = "S256";
|
|
104
|
+
CodeChallengeMethod[CodeChallengeMethod["Plain"] = 1] = "Plain";
|
|
105
|
+
})(CodeChallengeMethod || (CodeChallengeMethod = {}));
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
export { AmazonCognito } from "./providers/amazon-cognito.js";
|
|
2
|
+
export { AniList } from "./providers/anilist.js";
|
|
3
|
+
export { Apple } from "./providers/apple.js";
|
|
4
|
+
export { Atlassian } from "./providers/atlassian.js";
|
|
5
|
+
export { Auth0 } from "./providers/auth0.js";
|
|
6
|
+
export { Authentik } from "./providers/authentik.js";
|
|
7
|
+
export { Autodesk } from "./providers/autodesk.js";
|
|
8
|
+
export { BattleNet } from "./providers/battlenet.js";
|
|
9
|
+
export { Bitbucket } from "./providers/bitbucket.js";
|
|
10
|
+
export { Box } from "./providers/box.js";
|
|
11
|
+
export { Bungie } from "./providers/bungie.js";
|
|
12
|
+
export { Coinbase } from "./providers/coinbase.js";
|
|
13
|
+
export { Discord } from "./providers/discord.js";
|
|
14
|
+
export { DonationAlerts } from "./providers/donation-alerts.js";
|
|
15
|
+
export { Dribbble } from "./providers/dribbble.js";
|
|
16
|
+
export { Dropbox } from "./providers/dropbox.js";
|
|
17
|
+
export { Etsy } from "./providers/etsy.js";
|
|
18
|
+
export { EpicGames } from "./providers/epicgames.js";
|
|
19
|
+
export { Facebook } from "./providers/facebook.js";
|
|
20
|
+
export { Figma } from "./providers/figma.js";
|
|
21
|
+
export { Gitea } from "./providers/gitea.js";
|
|
22
|
+
export { GitHub } from "./providers/github.js";
|
|
23
|
+
export { GitLab } from "./providers/gitlab.js";
|
|
24
|
+
export { Google } from "./providers/google.js";
|
|
25
|
+
export { Intuit } from "./providers/intuit.js";
|
|
26
|
+
export { Kakao } from "./providers/kakao.js";
|
|
27
|
+
export { Kick } from "./providers/kick.js";
|
|
28
|
+
export { KeyCloak } from "./providers/keycloak.js";
|
|
29
|
+
export { Lichess } from "./providers/lichess.js";
|
|
30
|
+
export { Line } from "./providers/line.js";
|
|
31
|
+
export { Linear } from "./providers/linear.js";
|
|
32
|
+
export { LinkedIn } from "./providers/linkedin.js";
|
|
33
|
+
export { Mastodon } from "./providers/mastodon.js";
|
|
34
|
+
export { MercadoLibre } from "./providers/mercadolibre.js";
|
|
35
|
+
export { MercadoPago } from "./providers/mercadopago.js";
|
|
36
|
+
export { MicrosoftEntraId } from "./providers/microsoft-entra-id.js";
|
|
37
|
+
export { MyAnimeList } from "./providers/myanimelist.js";
|
|
38
|
+
export { Naver } from "./providers/naver.js";
|
|
39
|
+
export { Notion } from "./providers/notion.js";
|
|
40
|
+
export { Okta } from "./providers/okta.js";
|
|
41
|
+
export { Osu } from "./providers/osu.js";
|
|
42
|
+
export { Patreon } from "./providers/patreon.js";
|
|
43
|
+
export { Polar } from "./providers/polar.js";
|
|
44
|
+
export { Reddit } from "./providers/reddit.js";
|
|
45
|
+
export { Roblox } from "./providers/roblox.js";
|
|
46
|
+
export { Salesforce } from "./providers/salesforce.js";
|
|
47
|
+
export { Shikimori } from "./providers/shikimori.js";
|
|
48
|
+
export { Slack } from "./providers/slack.js";
|
|
49
|
+
export { Spotify } from "./providers/spotify.js";
|
|
50
|
+
export { StartGG } from "./providers/startgg.js";
|
|
51
|
+
export { Strava } from "./providers/strava.js";
|
|
52
|
+
export { Synology } from "./providers/synology.js";
|
|
53
|
+
export { TikTok } from "./providers/tiktok.js";
|
|
54
|
+
export { Tiltify } from "./providers/tiltify.js";
|
|
55
|
+
export { Tumblr } from "./providers/tumblr.js";
|
|
56
|
+
export { Twitch } from "./providers/twitch.js";
|
|
57
|
+
export { Twitter } from "./providers/twitter.js";
|
|
58
|
+
export { VK } from "./providers/vk.js";
|
|
59
|
+
export { Withings } from "./providers/withings.js";
|
|
60
|
+
export { WorkOS } from "./providers/workos.js";
|
|
61
|
+
export { Yahoo } from "./providers/yahoo.js";
|
|
62
|
+
export { Yandex } from "./providers/yandex.js";
|
|
63
|
+
export { Zoom } from "./providers/zoom.js";
|
|
64
|
+
export { FortyTwo } from "./providers/42.js";
|
|
65
|
+
export { OAuth2Client, CodeChallengeMethod } from "./client.js";
|
|
66
|
+
export { OAuth2Tokens, generateCodeVerifier, generateState } from "./oauth2.js";
|
|
67
|
+
export { OAuth2RequestError, ArcticFetchError, UnexpectedErrorResponseBodyError, UnexpectedResponseError } from "./request.js";
|
|
68
|
+
export { decodeIdToken } from "./oidc.js";
|
|
69
|
+
export { InvalidOAuthCallbackError, InvalidOAuthStateError, OAuthConfigurationError, OAuthProviderError } from "./auth.js";
|
|
70
|
+
export type { OAuthCallbackQuery, OAuthStateStore, OAuthUser, ProviderOptions } from "./auth.js";
|