@atlasauth/chrome-extension 0.3.1 → 0.5.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/CHANGELOG.md +8 -0
- package/README.md +17 -0
- package/dist/background.d.ts +1 -0
- package/dist/background.js +1 -0
- package/dist/background.js.map +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/posture.d.ts +46 -0
- package/dist/posture.js +52 -0
- package/dist/posture.js.map +1 -0
- package/dist/web-auth-flow.d.ts +77 -0
- package/dist/web-auth-flow.js +149 -0
- package/dist/web-auth-flow.js.map +1 -0
- package/package.json +5 -4
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,13 @@
|
|
|
1
1
|
# @atlasauth/chrome-extension changelog
|
|
2
2
|
|
|
3
|
+
## 0.5.0 — 2026-10-30
|
|
4
|
+
|
|
5
|
+
- `signInWithWebAuthFlow()`: sign in through `chrome.identity.launchWebAuthFlow` (or Firefox `browser.identity`) against the instance, with PKCE and `state`, and get back a refreshing session in `chrome.storage` whose JWT your backend verifies with the instance JWKS.
|
|
6
|
+
|
|
7
|
+
## 0.4.0 — 2026-10-28
|
|
8
|
+
|
|
9
|
+
- Reports device posture to Atlas (self-reported; never marks a device trusted)
|
|
10
|
+
|
|
3
11
|
## 0.3.1
|
|
4
12
|
- **Fixed:** depends on `@atlasauth/react` `^0.9.0` instead of one exact version. With an exact pin, an app that also
|
|
5
13
|
installed a newer `@atlasauth/react` got two copies, and hooks from one couldn't see the other's
|
package/README.md
CHANGED
|
@@ -18,6 +18,23 @@ to every request by hand, as a bearer.
|
|
|
18
18
|
| `@atlasauth/chrome-extension/background` | no | the service worker |
|
|
19
19
|
| `@atlasauth/chrome-extension/react` | yes (peer) | popup / options / side panel |
|
|
20
20
|
|
|
21
|
+
## Sign in
|
|
22
|
+
|
|
23
|
+
```ts
|
|
24
|
+
import { signInWithWebAuthFlow } from '@atlasauth/chrome-extension/background';
|
|
25
|
+
|
|
26
|
+
const session = await signInWithWebAuthFlow({
|
|
27
|
+
frontendApi: 'https://fapi.your-instance.atlasauth.net',
|
|
28
|
+
publishableKey: 'pk_...',
|
|
29
|
+
clientId: 'your-extension-oauth-client-id',
|
|
30
|
+
});
|
|
31
|
+
const jwt = await session.getToken(); // Authorization: Bearer for your backend
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
Needs the `"identity"` permission. Register `chrome.identity.getRedirectURL()`
|
|
35
|
+
(`https://<extension-id>.chromiumapp.org/`) as the redirect URI of a public,
|
|
36
|
+
first-party OAuth client. See the "Sign in from a browser extension" guide.
|
|
37
|
+
|
|
21
38
|
## Background service worker
|
|
22
39
|
|
|
23
40
|
```ts
|
package/dist/background.d.ts
CHANGED
package/dist/background.js
CHANGED
|
@@ -37,4 +37,5 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
37
37
|
__exportStar(require("./storage"), exports);
|
|
38
38
|
__exportStar(require("./client"), exports);
|
|
39
39
|
__exportStar(require("./native-session"), exports);
|
|
40
|
+
__exportStar(require("./web-auth-flow"), exports);
|
|
40
41
|
//# sourceMappingURL=background.js.map
|
package/dist/background.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"background.js","sourceRoot":"","sources":["../src/background.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AACH,4CAA0B;AAC1B,2CAAyB;AACzB,mDAAiC"}
|
|
1
|
+
{"version":3,"file":"background.js","sourceRoot":"","sources":["../src/background.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AACH,4CAA0B;AAC1B,2CAAyB;AACzB,mDAAiC;AACjC,kDAAgC"}
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -30,4 +30,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
30
30
|
__exportStar(require("./storage"), exports);
|
|
31
31
|
__exportStar(require("./client"), exports);
|
|
32
32
|
__exportStar(require("./native-session"), exports);
|
|
33
|
+
__exportStar(require("./web-auth-flow"), exports);
|
|
34
|
+
__exportStar(require("./posture"), exports);
|
|
33
35
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA;;;;;;;;;;;;GAYG;AACH,4CAA0B;AAC1B,2CAAyB;AACzB,mDAAiC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA;;;;;;;;;;;;GAYG;AACH,4CAA0B;AAC1B,2CAAyB;AACzB,mDAAiC;AACjC,kDAAgC;AAChC,4CAA0B"}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import type { FapiClient } from '@atlasauth/js';
|
|
2
|
+
import type { AtlasClient } from './client';
|
|
3
|
+
/** The slice of `navigator` read for posture, so it can be passed in and tested. */
|
|
4
|
+
export interface PostureNavigator {
|
|
5
|
+
userAgent?: string;
|
|
6
|
+
platform?: string;
|
|
7
|
+
userAgentData?: {
|
|
8
|
+
platform?: string;
|
|
9
|
+
brands?: Array<{
|
|
10
|
+
brand: string;
|
|
11
|
+
version: string;
|
|
12
|
+
}>;
|
|
13
|
+
getHighEntropyValues?(hints: string[]): Promise<{
|
|
14
|
+
platformVersion?: string;
|
|
15
|
+
uaFullVersion?: string;
|
|
16
|
+
}>;
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
export interface BrowserPosture {
|
|
20
|
+
platform: string;
|
|
21
|
+
os_version?: string;
|
|
22
|
+
browser?: string;
|
|
23
|
+
browser_version?: string;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* What a browser can honestly say about itself: the OS family, its version (high-entropy client hints; for
|
|
27
|
+
* Windows the Windows-11 style `platformVersion` is the NT build and is passed through as reported), and the
|
|
28
|
+
* browser brand and version. No hardware, encryption or screen-lock facts: a browser cannot attest those,
|
|
29
|
+
* so Atlas never accepts them from here.
|
|
30
|
+
*/
|
|
31
|
+
export declare function collectBrowserPosture(nav?: PostureNavigator): Promise<BrowserPosture>;
|
|
32
|
+
/** A stable per-install id kept in extension storage (generated on first use). */
|
|
33
|
+
export declare function installId(storage: {
|
|
34
|
+
get(key: string): Promise<string | undefined>;
|
|
35
|
+
set(key: string, value: string): Promise<void>;
|
|
36
|
+
}, random?: () => string): Promise<string>;
|
|
37
|
+
/**
|
|
38
|
+
* Report this browser's posture for the signed-in user (`POST /v1/client/me/device_posture`). Self-reported
|
|
39
|
+
* and unmanaged: it feeds platform and OS-version conditions, never device trust.
|
|
40
|
+
*/
|
|
41
|
+
export declare function reportDevicePosture(client: Pick<AtlasClient, 'fapi'> & {
|
|
42
|
+
fapi: Pick<FapiClient, 'post'>;
|
|
43
|
+
}, opts: {
|
|
44
|
+
installId: string;
|
|
45
|
+
navigator?: PostureNavigator;
|
|
46
|
+
}): Promise<void>;
|
package/dist/posture.js
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.collectBrowserPosture = collectBrowserPosture;
|
|
4
|
+
exports.installId = installId;
|
|
5
|
+
exports.reportDevicePosture = reportDevicePosture;
|
|
6
|
+
const BROWSER_BRANDS = ['Microsoft Edge', 'Google Chrome', 'Chromium', 'Brave', 'Opera'];
|
|
7
|
+
/**
|
|
8
|
+
* What a browser can honestly say about itself: the OS family, its version (high-entropy client hints; for
|
|
9
|
+
* Windows the Windows-11 style `platformVersion` is the NT build and is passed through as reported), and the
|
|
10
|
+
* browser brand and version. No hardware, encryption or screen-lock facts: a browser cannot attest those,
|
|
11
|
+
* so Atlas never accepts them from here.
|
|
12
|
+
*/
|
|
13
|
+
async function collectBrowserPosture(nav = globalThis.navigator ?? {}) {
|
|
14
|
+
const platform = nav.userAgentData?.platform || nav.platform || nav.userAgent || 'unknown';
|
|
15
|
+
const out = { platform };
|
|
16
|
+
const brand = nav.userAgentData?.brands?.find((b) => BROWSER_BRANDS.includes(b.brand));
|
|
17
|
+
if (brand) {
|
|
18
|
+
out.browser = brand.brand;
|
|
19
|
+
out.browser_version = brand.version;
|
|
20
|
+
}
|
|
21
|
+
try {
|
|
22
|
+
const high = await nav.userAgentData?.getHighEntropyValues?.(['platformVersion', 'uaFullVersion']);
|
|
23
|
+
if (high?.platformVersion)
|
|
24
|
+
out.os_version = high.platformVersion;
|
|
25
|
+
if (high?.uaFullVersion)
|
|
26
|
+
out.browser_version = high.uaFullVersion;
|
|
27
|
+
}
|
|
28
|
+
catch {
|
|
29
|
+
// Client hints can be blocked; the report still carries the platform.
|
|
30
|
+
}
|
|
31
|
+
return out;
|
|
32
|
+
}
|
|
33
|
+
/** A stable per-install id kept in extension storage (generated on first use). */
|
|
34
|
+
async function installId(storage, random = () => crypto.randomUUID()) {
|
|
35
|
+
const existing = await storage.get('atlas_install_id');
|
|
36
|
+
if (existing)
|
|
37
|
+
return existing;
|
|
38
|
+
const fresh = random();
|
|
39
|
+
await storage.set('atlas_install_id', fresh);
|
|
40
|
+
return fresh;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Report this browser's posture for the signed-in user (`POST /v1/client/me/device_posture`). Self-reported
|
|
44
|
+
* and unmanaged: it feeds platform and OS-version conditions, never device trust.
|
|
45
|
+
*/
|
|
46
|
+
async function reportDevicePosture(client, opts) {
|
|
47
|
+
const posture = await collectBrowserPosture(opts.navigator);
|
|
48
|
+
const res = await client.fapi.post('/v1/client/me/device_posture', { install_id: opts.installId, ...posture });
|
|
49
|
+
if (!res.ok)
|
|
50
|
+
throw new Error(`Posture report failed (${res.status}).`);
|
|
51
|
+
}
|
|
52
|
+
//# sourceMappingURL=posture.js.map
|
|
@@ -0,0 +1 @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,OAA0G,EAAE,SAAuB,GAAG,EAAE,CAAC,MAAM,CAAC,UAAU,EAAE;IAC1L,MAAM,QAAQ,GAAG,MAAM,OAAO,CAAC,GAAG,CAAC,kBAAkB,CAAC,CAAC;IACvD,IAAI,QAAQ;QAAE,OAAO,QAAQ,CAAC;IAC9B,MAAM,KAAK,GAAG,MAAM,EAAE,CAAC;IACvB,MAAM,OAAO,CAAC,GAAG,CAAC,kBAAkB,EAAE,KAAK,CAAC,CAAC;IAC7C,OAAO,KAAK,CAAC;AACf,CAAC;AAED;;;GAGG;AACI,KAAK,UAAU,mBAAmB,CACvC,MAAsE,EACtE,IAAyD;IAEzD,MAAM,OAAO,GAAG,MAAM,qBAAqB,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;IAC5D,MAAM,GAAG,GAAG,MAAM,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,8BAA8B,EAAE,EAAE,UAAU,EAAE,IAAI,CAAC,SAAS,EAAE,GAAG,OAAO,EAAE,CAAC,CAAC;IAC/G,IAAI,CAAC,GAAG,CAAC,EAAE;QAAE,MAAM,IAAI,KAAK,CAAC,0BAA0B,GAAG,CAAC,MAAM,IAAI,CAAC,CAAC;AACzE,CAAC"}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sign in from a browser extension with `chrome.identity.launchWebAuthFlow`.
|
|
3
|
+
*
|
|
4
|
+
* The extension is a PUBLIC, first-party OAuth client of the Atlas instance.
|
|
5
|
+
* The whole sign-in runs on the instance's own pages (every method the instance
|
|
6
|
+
* offers: password, passkeys, Google and the other social connections, MFA), in
|
|
7
|
+
* the browser's auth window, and comes back to the extension's redirect URL:
|
|
8
|
+
*
|
|
9
|
+
* 1. PKCE S256 + `state`, then `launchWebAuthFlow` on `{frontendApi}/oauth2/authorize`
|
|
10
|
+
* with `redirect_uri = chrome.identity.getRedirectURL()`
|
|
11
|
+
* (`https://<extension-id>.chromiumapp.org/`, registered EXACTLY on the client).
|
|
12
|
+
* 2. The returned URL must carry our `state`; its `code` is redeemed at
|
|
13
|
+
* `/oauth2/token` with the verifier.
|
|
14
|
+
* 3. The OAuth access token is traded for a real Atlas SESSION (RFC 8693
|
|
15
|
+
* token exchange, first-party clients only): a session JWT your backend
|
|
16
|
+
* verifies with the instance JWKS, plus a rotating refresh token.
|
|
17
|
+
* 4. The session goes into a {@link NativeSessionManager} persisted in
|
|
18
|
+
* `chrome.storage`, which refreshes it on demand.
|
|
19
|
+
*
|
|
20
|
+
* Firefox's `browser.identity` has the same two calls; its redirect URL is
|
|
21
|
+
* `https://<hash>.extensions.allizom.org/`.
|
|
22
|
+
*/
|
|
23
|
+
import { type NativeSessionManager } from '@atlasauth/js';
|
|
24
|
+
import type { TokenStore } from './storage';
|
|
25
|
+
/** The slice of `chrome.identity` (or Firefox's `browser.identity`) this flow uses. */
|
|
26
|
+
export interface ChromeIdentity {
|
|
27
|
+
getRedirectURL(path?: string): string;
|
|
28
|
+
launchWebAuthFlow(details: {
|
|
29
|
+
url: string;
|
|
30
|
+
interactive?: boolean;
|
|
31
|
+
}): Promise<string | undefined>;
|
|
32
|
+
}
|
|
33
|
+
/** Why {@link signInWithWebAuthFlow} failed. */
|
|
34
|
+
export type WebAuthFlowErrorCode =
|
|
35
|
+
/** No `chrome.identity` (missing `"identity"` permission, or not an extension). */
|
|
36
|
+
'identity_unavailable'
|
|
37
|
+
/** The user closed the window, or `interactive: false` needed a prompt. */
|
|
38
|
+
| 'cancelled'
|
|
39
|
+
/** The returned URL was not the answer to this request (wrong redirect or `state`). */
|
|
40
|
+
| 'state_mismatch'
|
|
41
|
+
/** The instance answered with an OAuth error (`error` from the redirect, e.g. `access_denied`). */
|
|
42
|
+
| 'authorization_error'
|
|
43
|
+
/** The code could not be redeemed at `/oauth2/token`. */
|
|
44
|
+
| 'token_error'
|
|
45
|
+
/** The access token could not be exchanged for a session (is the client first-party?). */
|
|
46
|
+
| 'session_exchange_failed';
|
|
47
|
+
export declare class AtlasWebAuthFlowError extends Error {
|
|
48
|
+
readonly code: WebAuthFlowErrorCode;
|
|
49
|
+
constructor(code: WebAuthFlowErrorCode, message: string);
|
|
50
|
+
}
|
|
51
|
+
export interface WebAuthFlowOptions {
|
|
52
|
+
/** The instance's FAPI origin, e.g. `https://fapi.acme.atlasauth.net`. */
|
|
53
|
+
frontendApi: string;
|
|
54
|
+
publishableKey: string;
|
|
55
|
+
/** The extension's OAuth client id: public (`token_endpoint_auth_method: none`) and first-party. */
|
|
56
|
+
clientId: string;
|
|
57
|
+
/** Space-delimited scopes. @default `openid profile email` */
|
|
58
|
+
scope?: string;
|
|
59
|
+
/** @default `identity.getRedirectURL()` — register exactly this on the client. */
|
|
60
|
+
redirectUri?: string;
|
|
61
|
+
/** `false` signs in silently when the instance session allows it, else fails with `cancelled`. @default true */
|
|
62
|
+
interactive?: boolean;
|
|
63
|
+
/** Extra authorize parameters, e.g. `{ prompt: 'login', login_hint: 'a@example.com' }`. */
|
|
64
|
+
authorizeParams?: Record<string, string>;
|
|
65
|
+
/** Where the session is persisted (see {@link createNativeSessionManager}). */
|
|
66
|
+
store?: TokenStore;
|
|
67
|
+
/** @default `chrome.identity`, else `browser.identity` */
|
|
68
|
+
identity?: ChromeIdentity;
|
|
69
|
+
fetchImpl?: typeof fetch;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Run the sign-in and return a {@link NativeSessionManager} holding the new,
|
|
73
|
+
* persisted session: `await manager.getToken()` is the session JWT for your
|
|
74
|
+
* backend (`Authorization: Bearer …`), refreshed when it is about to expire.
|
|
75
|
+
* Throws {@link AtlasWebAuthFlowError} on any failure; nothing is stored then.
|
|
76
|
+
*/
|
|
77
|
+
export declare function signInWithWebAuthFlow(options: WebAuthFlowOptions): Promise<NativeSessionManager>;
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Sign in from a browser extension with `chrome.identity.launchWebAuthFlow`.
|
|
4
|
+
*
|
|
5
|
+
* The extension is a PUBLIC, first-party OAuth client of the Atlas instance.
|
|
6
|
+
* The whole sign-in runs on the instance's own pages (every method the instance
|
|
7
|
+
* offers: password, passkeys, Google and the other social connections, MFA), in
|
|
8
|
+
* the browser's auth window, and comes back to the extension's redirect URL:
|
|
9
|
+
*
|
|
10
|
+
* 1. PKCE S256 + `state`, then `launchWebAuthFlow` on `{frontendApi}/oauth2/authorize`
|
|
11
|
+
* with `redirect_uri = chrome.identity.getRedirectURL()`
|
|
12
|
+
* (`https://<extension-id>.chromiumapp.org/`, registered EXACTLY on the client).
|
|
13
|
+
* 2. The returned URL must carry our `state`; its `code` is redeemed at
|
|
14
|
+
* `/oauth2/token` with the verifier.
|
|
15
|
+
* 3. The OAuth access token is traded for a real Atlas SESSION (RFC 8693
|
|
16
|
+
* token exchange, first-party clients only): a session JWT your backend
|
|
17
|
+
* verifies with the instance JWKS, plus a rotating refresh token.
|
|
18
|
+
* 4. The session goes into a {@link NativeSessionManager} persisted in
|
|
19
|
+
* `chrome.storage`, which refreshes it on demand.
|
|
20
|
+
*
|
|
21
|
+
* Firefox's `browser.identity` has the same two calls; its redirect URL is
|
|
22
|
+
* `https://<hash>.extensions.allizom.org/`.
|
|
23
|
+
*/
|
|
24
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
25
|
+
exports.AtlasWebAuthFlowError = void 0;
|
|
26
|
+
exports.signInWithWebAuthFlow = signInWithWebAuthFlow;
|
|
27
|
+
const js_1 = require("@atlasauth/js");
|
|
28
|
+
const native_session_1 = require("./native-session");
|
|
29
|
+
class AtlasWebAuthFlowError extends Error {
|
|
30
|
+
code;
|
|
31
|
+
constructor(code, message) {
|
|
32
|
+
super(message);
|
|
33
|
+
this.code = code;
|
|
34
|
+
this.name = 'AtlasWebAuthFlowError';
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
exports.AtlasWebAuthFlowError = AtlasWebAuthFlowError;
|
|
38
|
+
const base64url = (bytes) => {
|
|
39
|
+
let bin = '';
|
|
40
|
+
for (const b of bytes)
|
|
41
|
+
bin += String.fromCharCode(b);
|
|
42
|
+
return btoa(bin).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
|
|
43
|
+
};
|
|
44
|
+
const random = (n) => {
|
|
45
|
+
const raw = new Uint8Array(n);
|
|
46
|
+
crypto.getRandomValues(raw);
|
|
47
|
+
return base64url(raw);
|
|
48
|
+
};
|
|
49
|
+
function defaultIdentity() {
|
|
50
|
+
const g = globalThis;
|
|
51
|
+
return g.chrome?.identity ?? g.browser?.identity;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Run the sign-in and return a {@link NativeSessionManager} holding the new,
|
|
55
|
+
* persisted session: `await manager.getToken()` is the session JWT for your
|
|
56
|
+
* backend (`Authorization: Bearer …`), refreshed when it is about to expire.
|
|
57
|
+
* Throws {@link AtlasWebAuthFlowError} on any failure; nothing is stored then.
|
|
58
|
+
*/
|
|
59
|
+
async function signInWithWebAuthFlow(options) {
|
|
60
|
+
const identity = options.identity ?? defaultIdentity();
|
|
61
|
+
if (!identity) {
|
|
62
|
+
throw new AtlasWebAuthFlowError('identity_unavailable', 'chrome.identity is unavailable: add "identity" to the manifest permissions.');
|
|
63
|
+
}
|
|
64
|
+
const baseUrl = (0, js_1.resolveFrontendApi)(options.frontendApi, { component: 'signInWithWebAuthFlow()', allowSameOrigin: false });
|
|
65
|
+
const redirectUri = options.redirectUri ?? identity.getRedirectURL();
|
|
66
|
+
const fetchImpl = options.fetchImpl ?? fetch;
|
|
67
|
+
const verifier = random(32);
|
|
68
|
+
const challenge = base64url(new Uint8Array(await crypto.subtle.digest('SHA-256', new TextEncoder().encode(verifier))));
|
|
69
|
+
const state = random(16);
|
|
70
|
+
const authorize = new URL(`${baseUrl}/oauth2/authorize`);
|
|
71
|
+
for (const [k, v] of Object.entries(options.authorizeParams ?? {}))
|
|
72
|
+
authorize.searchParams.set(k, v);
|
|
73
|
+
authorize.searchParams.set('response_type', 'code');
|
|
74
|
+
authorize.searchParams.set('client_id', options.clientId);
|
|
75
|
+
authorize.searchParams.set('redirect_uri', redirectUri);
|
|
76
|
+
authorize.searchParams.set('scope', options.scope ?? 'openid profile email');
|
|
77
|
+
authorize.searchParams.set('state', state);
|
|
78
|
+
authorize.searchParams.set('code_challenge', challenge);
|
|
79
|
+
authorize.searchParams.set('code_challenge_method', 'S256');
|
|
80
|
+
let returned;
|
|
81
|
+
try {
|
|
82
|
+
returned = await identity.launchWebAuthFlow({ url: authorize.toString(), interactive: options.interactive ?? true });
|
|
83
|
+
}
|
|
84
|
+
catch (error) {
|
|
85
|
+
throw new AtlasWebAuthFlowError('cancelled', error instanceof Error && error.message ? error.message : 'The sign-in window was closed.');
|
|
86
|
+
}
|
|
87
|
+
if (!returned)
|
|
88
|
+
throw new AtlasWebAuthFlowError('cancelled', 'The sign-in window was closed.');
|
|
89
|
+
// Only the answer to THIS request counts: our redirect URI, our state.
|
|
90
|
+
let callback;
|
|
91
|
+
try {
|
|
92
|
+
callback = new URL(returned);
|
|
93
|
+
}
|
|
94
|
+
catch {
|
|
95
|
+
throw new AtlasWebAuthFlowError('state_mismatch', 'The sign-in returned an unreadable URL.');
|
|
96
|
+
}
|
|
97
|
+
const expected = new URL(redirectUri);
|
|
98
|
+
if (callback.origin !== expected.origin || callback.pathname !== expected.pathname || callback.searchParams.get('state') !== state) {
|
|
99
|
+
throw new AtlasWebAuthFlowError('state_mismatch', 'The sign-in returned to an unexpected URL or with the wrong state.');
|
|
100
|
+
}
|
|
101
|
+
const oauthError = callback.searchParams.get('error');
|
|
102
|
+
if (oauthError) {
|
|
103
|
+
const description = callback.searchParams.get('error_description');
|
|
104
|
+
throw new AtlasWebAuthFlowError('authorization_error', description ? `${oauthError}: ${description}` : oauthError);
|
|
105
|
+
}
|
|
106
|
+
const code = callback.searchParams.get('code');
|
|
107
|
+
if (!code)
|
|
108
|
+
throw new AtlasWebAuthFlowError('authorization_error', 'The sign-in returned no authorization code.');
|
|
109
|
+
let accessToken;
|
|
110
|
+
try {
|
|
111
|
+
const res = await fetchImpl(`${baseUrl}/oauth2/token`, {
|
|
112
|
+
method: 'POST',
|
|
113
|
+
headers: { 'content-type': 'application/x-www-form-urlencoded' },
|
|
114
|
+
body: new URLSearchParams({
|
|
115
|
+
grant_type: 'authorization_code',
|
|
116
|
+
client_id: options.clientId,
|
|
117
|
+
redirect_uri: redirectUri,
|
|
118
|
+
code,
|
|
119
|
+
code_verifier: verifier,
|
|
120
|
+
}).toString(),
|
|
121
|
+
});
|
|
122
|
+
const body = (await res.json().catch(() => null));
|
|
123
|
+
if (!res.ok) {
|
|
124
|
+
throw new AtlasWebAuthFlowError('token_error', `Token request failed (${res.status}${typeof body?.error === 'string' ? ` ${body.error}` : ''})${typeof body?.error_description === 'string' ? `: ${body.error_description}` : ''}.`);
|
|
125
|
+
}
|
|
126
|
+
accessToken = body?.access_token;
|
|
127
|
+
}
|
|
128
|
+
catch (error) {
|
|
129
|
+
if (error instanceof AtlasWebAuthFlowError)
|
|
130
|
+
throw error;
|
|
131
|
+
throw new AtlasWebAuthFlowError('token_error', 'The token request could not be sent.');
|
|
132
|
+
}
|
|
133
|
+
if (typeof accessToken !== 'string')
|
|
134
|
+
throw new AtlasWebAuthFlowError('token_error', 'The token response had no access_token.');
|
|
135
|
+
const session = await (0, js_1.exchangeForSession)({ baseUrl, clientId: options.clientId, accessToken, fetchImpl });
|
|
136
|
+
if (!session) {
|
|
137
|
+
throw new AtlasWebAuthFlowError('session_exchange_failed', 'The access token could not be exchanged for a session. The OAuth client must be first-party.');
|
|
138
|
+
}
|
|
139
|
+
const manager = await (0, native_session_1.createNativeSessionManager)({
|
|
140
|
+
frontendApi: baseUrl,
|
|
141
|
+
publishableKey: options.publishableKey,
|
|
142
|
+
clientId: options.clientId,
|
|
143
|
+
...(options.store ? { store: options.store } : {}),
|
|
144
|
+
fetchImpl,
|
|
145
|
+
});
|
|
146
|
+
manager.setSession(session);
|
|
147
|
+
return manager;
|
|
148
|
+
}
|
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CAAC,CAAC;IACH,OAAO,CAAC,UAAU,CAAC,OAAO,CAAC,CAAC;IAC5B,OAAO,OAAO,CAAC;AACjB,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@atlasauth/chrome-extension",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"main": "./dist/index.js",
|
|
5
5
|
"types": "./dist/index.d.ts",
|
|
6
6
|
"exports": {
|
|
@@ -18,8 +18,8 @@
|
|
|
18
18
|
}
|
|
19
19
|
},
|
|
20
20
|
"dependencies": {
|
|
21
|
-
"@atlasauth/js": "0.
|
|
22
|
-
"@atlasauth/react": "^0.
|
|
21
|
+
"@atlasauth/js": "0.27.0",
|
|
22
|
+
"@atlasauth/react": "^0.24.0"
|
|
23
23
|
},
|
|
24
24
|
"peerDependencies": {
|
|
25
25
|
"react": ">=18"
|
|
@@ -40,12 +40,13 @@
|
|
|
40
40
|
},
|
|
41
41
|
"files": [
|
|
42
42
|
"dist",
|
|
43
|
+
"!dist/*.tsbuildinfo",
|
|
43
44
|
"README.md",
|
|
44
45
|
"CHANGELOG.md"
|
|
45
46
|
],
|
|
46
47
|
"scripts": {
|
|
47
48
|
"build": "tsc -p tsconfig.build.json",
|
|
48
|
-
"typecheck": "
|
|
49
|
+
"typecheck": "tsgo -p tsconfig.json --noEmit --module preserve --moduleResolution bundler --incremental --tsBuildInfoFile node_modules/.cache/typecheck.tsbuildinfo",
|
|
49
50
|
"test": "vitest run"
|
|
50
51
|
}
|
|
51
52
|
}
|