@atproto/oauth-client 0.5.14 → 0.6.1

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.
Files changed (45) hide show
  1. package/CHANGELOG.md +20 -0
  2. package/dist/oauth-authorization-server-metadata-resolver.d.ts +1 -1
  3. package/dist/oauth-authorization-server-metadata-resolver.d.ts.map +1 -1
  4. package/dist/oauth-authorization-server-metadata-resolver.js +1 -1
  5. package/dist/oauth-authorization-server-metadata-resolver.js.map +1 -1
  6. package/dist/oauth-client.d.ts +7 -8
  7. package/dist/oauth-client.d.ts.map +1 -1
  8. package/dist/oauth-client.js +27 -26
  9. package/dist/oauth-client.js.map +1 -1
  10. package/dist/oauth-protected-resource-metadata-resolver.d.ts +3 -3
  11. package/dist/oauth-protected-resource-metadata-resolver.d.ts.map +1 -1
  12. package/dist/oauth-protected-resource-metadata-resolver.js +4 -0
  13. package/dist/oauth-protected-resource-metadata-resolver.js.map +1 -1
  14. package/dist/oauth-resolver.d.ts +1 -1
  15. package/dist/oauth-resolver.d.ts.map +1 -1
  16. package/dist/oauth-resolver.js +3 -0
  17. package/dist/oauth-resolver.js.map +1 -1
  18. package/dist/oauth-server-factory.d.ts +1 -1
  19. package/dist/oauth-server-factory.d.ts.map +1 -1
  20. package/dist/oauth-server-factory.js +0 -7
  21. package/dist/oauth-server-factory.js.map +1 -1
  22. package/dist/oauth-session.d.ts.map +1 -1
  23. package/dist/oauth-session.js +1 -4
  24. package/dist/oauth-session.js.map +1 -1
  25. package/dist/session-getter.d.ts +16 -21
  26. package/dist/session-getter.d.ts.map +1 -1
  27. package/dist/session-getter.js +65 -60
  28. package/dist/session-getter.js.map +1 -1
  29. package/dist/state-store.d.ts +13 -3
  30. package/dist/state-store.d.ts.map +1 -1
  31. package/dist/state-store.js.map +1 -1
  32. package/dist/util.d.ts +0 -10
  33. package/dist/util.d.ts.map +1 -1
  34. package/dist/util.js +1 -64
  35. package/dist/util.js.map +1 -1
  36. package/package.json +11 -11
  37. package/src/oauth-authorization-server-metadata-resolver.ts +2 -2
  38. package/src/oauth-client.ts +47 -50
  39. package/src/oauth-protected-resource-metadata-resolver.ts +9 -4
  40. package/src/oauth-resolver.ts +5 -1
  41. package/src/oauth-server-factory.ts +2 -16
  42. package/src/oauth-session.ts +1 -4
  43. package/src/session-getter.ts +85 -102
  44. package/src/state-store.ts +13 -3
  45. package/src/util.ts +0 -67
@@ -1,7 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.OAuthServerFactory = void 0;
4
- const oauth_client_auth_js_1 = require("./oauth-client-auth.js");
5
4
  const oauth_server_agent_js_1 = require("./oauth-server-agent.js");
6
5
  class OAuthServerFactory {
7
6
  constructor(clientMetadata, runtime, resolver, fetch, keyset, dpopNonceCache) {
@@ -53,12 +52,6 @@ class OAuthServerFactory {
53
52
  */
54
53
  async fromIssuer(issuer, authMethod, dpopKey, options) {
55
54
  const serverMetadata = await this.resolver.getAuthorizationServerMetadata(issuer, options);
56
- if (authMethod === 'legacy') {
57
- // @NOTE Because we were previously not storing the authMethod in the
58
- // session data, we provide a backwards compatible implementation by
59
- // computing it here.
60
- authMethod = (0, oauth_client_auth_js_1.negotiateClientAuthMethod)(serverMetadata, this.clientMetadata, this.keyset);
61
- }
62
55
  return this.fromMetadata(serverMetadata, authMethod, dpopKey);
63
56
  }
64
57
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"oauth-server-factory.js","sourceRoot":"","sources":["../src/oauth-server-factory.ts"],"names":[],"mappings":";;;AAIA,iEAG+B;AAE/B,mEAA0E;AAI1E,MAAa,kBAAkB;IAC7B,YACW,cAA8B,EAC9B,OAAgB,EAChB,QAAuB,EACvB,KAAY,EACZ,MAA0B,EAC1B,cAA8B;QALvC;;;;mBAAS,cAAc;WAAgB;QACvC;;;;mBAAS,OAAO;WAAS;QACzB;;;;mBAAS,QAAQ;WAAe;QAChC;;;;mBAAS,KAAK;WAAO;QACrB;;;;mBAAS,MAAM;WAAoB;QACnC;;;;mBAAS,cAAc;WAAgB;IACtC,CAAC;IAEJ;;;;;;;;OAQG;IACH,KAAK,CAAC,UAAU,CACd,MAAc,EACd,UAAuC,EACvC,OAAY,EACZ,OAA0B;QAE1B,MAAM,cAAc,GAAG,MAAM,IAAI,CAAC,QAAQ,CAAC,8BAA8B,CACvE,MAAM,EACN,OAAO,CACR,CAAA;QAED,IAAI,UAAU,KAAK,QAAQ,EAAE,CAAC;YAC5B,qEAAqE;YACrE,oEAAoE;YACpE,qBAAqB;YACrB,UAAU,GAAG,IAAA,gDAAyB,EACpC,cAAc,EACd,IAAI,CAAC,cAAc,EACnB,IAAI,CAAC,MAAM,CACZ,CAAA;QACH,CAAC;QAED,OAAO,IAAI,CAAC,YAAY,CAAC,cAAc,EAAE,UAAU,EAAE,OAAO,CAAC,CAAA;IAC/D,CAAC;IAED;;OAEG;IACH,KAAK,CAAC,YAAY,CAChB,cAAgD,EAChD,UAA4B,EAC5B,OAAY;QAEZ,OAAO,IAAI,wCAAgB,CACzB,UAAU,EACV,OAAO,EACP,cAAc,EACd,IAAI,CAAC,cAAc,EACnB,IAAI,CAAC,cAAc,EACnB,IAAI,CAAC,QAAQ,EACb,IAAI,CAAC,OAAO,EACZ,IAAI,CAAC,MAAM,EACX,IAAI,CAAC,KAAK,CACX,CAAA;IACH,CAAC;CACF;AAhED,gDAgEC","sourcesContent":["import { Key, Keyset } from '@atproto/jwk'\nimport { OAuthAuthorizationServerMetadata } from '@atproto/oauth-types'\nimport { Fetch } from '@atproto-labs/fetch'\nimport { GetCachedOptions } from './oauth-authorization-server-metadata-resolver.js'\nimport {\n ClientAuthMethod,\n negotiateClientAuthMethod,\n} from './oauth-client-auth.js'\nimport { OAuthResolver } from './oauth-resolver.js'\nimport { DpopNonceCache, OAuthServerAgent } from './oauth-server-agent.js'\nimport { Runtime } from './runtime.js'\nimport { ClientMetadata } from './types.js'\n\nexport class OAuthServerFactory {\n constructor(\n readonly clientMetadata: ClientMetadata,\n readonly runtime: Runtime,\n readonly resolver: OAuthResolver,\n readonly fetch: Fetch,\n readonly keyset: Keyset | undefined,\n readonly dpopNonceCache: DpopNonceCache,\n ) {}\n\n /**\n * @param authMethod `undefined` means that we are restoring a session that\n * was created before we started storing the `authMethod` in the session. In\n * that case, we will use the first key from the keyset.\n *\n * Support for this might be removed in the future.\n *\n * @throws see {@link OAuthServerFactory.fromMetadata}\n */\n async fromIssuer(\n issuer: string,\n authMethod: 'legacy' | ClientAuthMethod,\n dpopKey: Key,\n options?: GetCachedOptions,\n ) {\n const serverMetadata = await this.resolver.getAuthorizationServerMetadata(\n issuer,\n options,\n )\n\n if (authMethod === 'legacy') {\n // @NOTE Because we were previously not storing the authMethod in the\n // session data, we provide a backwards compatible implementation by\n // computing it here.\n authMethod = negotiateClientAuthMethod(\n serverMetadata,\n this.clientMetadata,\n this.keyset,\n )\n }\n\n return this.fromMetadata(serverMetadata, authMethod, dpopKey)\n }\n\n /**\n * @throws see {@link OAuthServerAgent}\n */\n async fromMetadata(\n serverMetadata: OAuthAuthorizationServerMetadata,\n authMethod: ClientAuthMethod,\n dpopKey: Key,\n ) {\n return new OAuthServerAgent(\n authMethod,\n dpopKey,\n serverMetadata,\n this.clientMetadata,\n this.dpopNonceCache,\n this.resolver,\n this.runtime,\n this.keyset,\n this.fetch,\n )\n }\n}\n"]}
1
+ {"version":3,"file":"oauth-server-factory.js","sourceRoot":"","sources":["../src/oauth-server-factory.ts"],"names":[],"mappings":";;;AAMA,mEAA0E;AAI1E,MAAa,kBAAkB;IAC7B,YACW,cAA8B,EAC9B,OAAgB,EAChB,QAAuB,EACvB,KAAY,EACZ,MAA0B,EAC1B,cAA8B;QALvC;;;;mBAAS,cAAc;WAAgB;QACvC;;;;mBAAS,OAAO;WAAS;QACzB;;;;mBAAS,QAAQ;WAAe;QAChC;;;;mBAAS,KAAK;WAAO;QACrB;;;;mBAAS,MAAM;WAAoB;QACnC;;;;mBAAS,cAAc;WAAgB;IACtC,CAAC;IAEJ;;;;;;;;OAQG;IACH,KAAK,CAAC,UAAU,CACd,MAAc,EACd,UAA4B,EAC5B,OAAY,EACZ,OAA0B;QAE1B,MAAM,cAAc,GAAG,MAAM,IAAI,CAAC,QAAQ,CAAC,8BAA8B,CACvE,MAAM,EACN,OAAO,CACR,CAAA;QAED,OAAO,IAAI,CAAC,YAAY,CAAC,cAAc,EAAE,UAAU,EAAE,OAAO,CAAC,CAAA;IAC/D,CAAC;IAED;;OAEG;IACH,KAAK,CAAC,YAAY,CAChB,cAAgD,EAChD,UAA4B,EAC5B,OAAY;QAEZ,OAAO,IAAI,wCAAgB,CACzB,UAAU,EACV,OAAO,EACP,cAAc,EACd,IAAI,CAAC,cAAc,EACnB,IAAI,CAAC,cAAc,EACnB,IAAI,CAAC,QAAQ,EACb,IAAI,CAAC,OAAO,EACZ,IAAI,CAAC,MAAM,EACX,IAAI,CAAC,KAAK,CACX,CAAA;IACH,CAAC;CACF;AArDD,gDAqDC","sourcesContent":["import { Key, Keyset } from '@atproto/jwk'\nimport { OAuthAuthorizationServerMetadata } from '@atproto/oauth-types'\nimport { Fetch } from '@atproto-labs/fetch'\nimport { GetCachedOptions } from './oauth-authorization-server-metadata-resolver.js'\nimport { ClientAuthMethod } from './oauth-client-auth.js'\nimport { OAuthResolver } from './oauth-resolver.js'\nimport { DpopNonceCache, OAuthServerAgent } from './oauth-server-agent.js'\nimport { Runtime } from './runtime.js'\nimport { ClientMetadata } from './types.js'\n\nexport class OAuthServerFactory {\n constructor(\n readonly clientMetadata: ClientMetadata,\n readonly runtime: Runtime,\n readonly resolver: OAuthResolver,\n readonly fetch: Fetch,\n readonly keyset: Keyset | undefined,\n readonly dpopNonceCache: DpopNonceCache,\n ) {}\n\n /**\n * @param authMethod `undefined` means that we are restoring a session that\n * was created before we started storing the `authMethod` in the session. In\n * that case, we will use the first key from the keyset.\n *\n * Support for this might be removed in the future.\n *\n * @throws see {@link OAuthServerFactory.fromMetadata}\n */\n async fromIssuer(\n issuer: string,\n authMethod: ClientAuthMethod,\n dpopKey: Key,\n options?: GetCachedOptions,\n ) {\n const serverMetadata = await this.resolver.getAuthorizationServerMetadata(\n issuer,\n options,\n )\n\n return this.fromMetadata(serverMetadata, authMethod, dpopKey)\n }\n\n /**\n * @throws see {@link OAuthServerAgent}\n */\n async fromMetadata(\n serverMetadata: OAuthAuthorizationServerMetadata,\n authMethod: ClientAuthMethod,\n dpopKey: Key,\n ) {\n return new OAuthServerAgent(\n authMethod,\n dpopKey,\n serverMetadata,\n this.clientMetadata,\n this.dpopNonceCache,\n this.resolver,\n this.runtime,\n this.keyset,\n this.fetch,\n )\n }\n}\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"oauth-session.d.ts","sourceRoot":"","sources":["../src/oauth-session.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAA;AACzC,OAAO,EACL,iBAAiB,EACjB,gCAAgC,EACjC,MAAM,sBAAsB,CAAA;AAC7B,OAAO,EAAE,KAAK,EAAa,MAAM,qBAAqB,CAAA;AAItD,OAAO,EAAE,gBAAgB,EAAE,QAAQ,EAAE,MAAM,yBAAyB,CAAA;AACpE,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAA;AAMnD,YAAY,EAAE,UAAU,EAAE,iBAAiB,EAAE,CAAA;AAC7C,MAAM,MAAM,SAAS,GAAG;IACtB,SAAS,CAAC,EAAE,IAAI,CAAA;IAChB,OAAO,CAAC,EAAE,OAAO,CAAA;IACjB,KAAK,EAAE,iBAAiB,CAAA;IACxB,GAAG,EAAE,MAAM,CAAA;IACX,GAAG,EAAE,MAAM,CAAA;IACX,GAAG,EAAE,UAAU,CAAA;CAChB,CAAA;AAED,qBAAa,YAAY;aAIL,MAAM,EAAE,gBAAgB;aACxB,GAAG,EAAE,UAAU;IAC/B,OAAO,CAAC,QAAQ,CAAC,aAAa;IALhC,SAAS,CAAC,SAAS,EAAE,KAAK,CAAC,OAAO,CAAC,CAAA;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,UAAU;IAC/B,OAAO,CAAC,QAAQ,CAAC,aAAa;IALhC,SAAS,CAAC,SAAS,EAAE,KAAK,CAAC,OAAO,CAAC,CAAA;gBAGjB,MAAM,EAAE,gBAAgB,EACxB,GAAG,EAAE,UAAU,EACd,aAAa,EAAE,aAAa,EAC7C,KAAK,GAAE,KAAwB;IAYjC,IAAI,GAAG,IAAI,UAAU,CAEpB;IAED,IAAI,cAAc,IAAI,QAAQ,CAAC,gCAAgC,CAAC,CAE/D;IAED;;;;;OAKG;cACa,WAAW,CAAC,OAAO,EAAE,OAAO,GAAG,MAAM,GAAG,OAAO,CAAC,QAAQ,CAAC;IAMnE,YAAY,CAAC,OAAO,GAAE,OAAO,GAAG,MAAe,GAAG,OAAO,CAAC,SAAS,CAAC;IAmBpE,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;IAYxB,YAAY,CAAC,QAAQ,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,QAAQ,CAAC;CA2D5E"}
@@ -54,10 +54,7 @@ class OAuthSession {
54
54
  * if, and only if, they are (about to be) expired. Defaults to `undefined`.
55
55
  */
56
56
  async getTokenSet(refresh) {
57
- const { tokenSet } = await this.sessionGetter.get(this.sub, {
58
- noCache: refresh === true,
59
- allowStale: refresh === false,
60
- });
57
+ const { tokenSet } = await this.sessionGetter.getSession(this.sub, refresh);
61
58
  return tokenSet;
62
59
  }
63
60
  async getTokenInfo(refresh = 'auto') {
@@ -1 +1 @@
1
- {"version":3,"file":"oauth-session.js","sourceRoot":"","sources":["../src/oauth-session.ts"],"names":[],"mappings":";;;AAKA,+CAAsD;AACtD,4EAAmE;AACnE,4EAAmE;AACnE,mDAAkD;AAIlD,MAAM,cAAc,GAAG,UAAU,CAAC,cAErB,CAAA;AAYb,MAAa,YAAY;IAGvB,YACkB,MAAwB,EACxB,GAAe,EACd,aAA4B,EAC7C,QAAe,UAAU,CAAC,KAAK;QAH/B;;;;mBAAgB,MAAM;WAAkB;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AG;QAAE,OAAO,KAAK,CAAA;IACzC,MAAM,OAAO,GAAG,QAAQ,CAAC,OAAO,CAAC,GAAG,CAAC,kBAAkB,CAAC,CAAA;IACxD,OAAO,CACL,OAAO,IAAI,IAAI;QACf,CAAC,OAAO,CAAC,UAAU,CAAC,SAAS,CAAC,IAAI,OAAO,CAAC,UAAU,CAAC,OAAO,CAAC,CAAC;QAC9D,OAAO,CAAC,QAAQ,CAAC,uBAAuB,CAAC,CAC1C,CAAA;AACH,CAAC","sourcesContent":["import { AtprotoDid } from '@atproto/did'\nimport {\n AtprotoOAuthScope,\n OAuthAuthorizationServerMetadata,\n} from '@atproto/oauth-types'\nimport { Fetch, bindFetch } from '@atproto-labs/fetch'\nimport { TokenInvalidError } from './errors/token-invalid-error.js'\nimport { TokenRevokedError } from './errors/token-revoked-error.js'\nimport { dpopFetchWrapper } from './fetch-dpop.js'\nimport { OAuthServerAgent, TokenSet } from './oauth-server-agent.js'\nimport { SessionGetter } from './session-getter.js'\n\nconst ReadableStream = globalThis.ReadableStream as\n | typeof globalThis.ReadableStream\n | undefined\n\nexport type { AtprotoDid, AtprotoOAuthScope }\nexport type TokenInfo = {\n expiresAt?: Date\n expired?: boolean\n scope: AtprotoOAuthScope\n iss: string\n aud: string\n sub: AtprotoDid\n}\n\nexport class OAuthSession {\n protected dpopFetch: Fetch<unknown>\n\n constructor(\n public readonly server: OAuthServerAgent,\n public readonly sub: AtprotoDid,\n private readonly sessionGetter: SessionGetter,\n fetch: Fetch = globalThis.fetch,\n ) {\n this.dpopFetch = dpopFetchWrapper<void>({\n fetch: bindFetch(fetch),\n key: server.dpopKey,\n supportedAlgs: server.serverMetadata.dpop_signing_alg_values_supported,\n sha256: async (v) => server.runtime.sha256(v),\n nonces: server.dpopNonces,\n isAuthServer: false,\n })\n }\n\n get did(): AtprotoDid {\n return this.sub\n }\n\n get serverMetadata(): Readonly<OAuthAuthorizationServerMetadata> {\n return this.server.serverMetadata\n }\n\n /**\n * @param refresh When `true`, the credentials will be refreshed even if they\n * are not expired. When `false`, the credentials will not be refreshed even\n * if they are expired. When `undefined`, the credentials will be refreshed\n * if, and only if, they are (about to be) expired. Defaults to `undefined`.\n */\n protected async getTokenSet(refresh: boolean | 'auto'): Promise<TokenSet> {\n const { tokenSet } = await this.sessionGetter.get(this.sub, {\n noCache: refresh === true,\n allowStale: refresh === false,\n })\n\n return tokenSet\n }\n\n async getTokenInfo(refresh: boolean | 'auto' = 'auto'): Promise<TokenInfo> {\n const tokenSet = await this.getTokenSet(refresh)\n const expiresAt =\n tokenSet.expires_at == null ? undefined : new Date(tokenSet.expires_at)\n\n return {\n expiresAt,\n get expired() {\n return expiresAt == null\n ? undefined\n : expiresAt.getTime() < Date.now() - 5e3\n },\n scope: tokenSet.scope,\n iss: tokenSet.iss,\n aud: tokenSet.aud,\n sub: tokenSet.sub,\n }\n }\n\n async signOut(): Promise<void> {\n try {\n const tokenSet = await this.getTokenSet(false)\n await this.server.revoke(tokenSet.access_token)\n } finally {\n await this.sessionGetter.delStored(\n this.sub,\n new TokenRevokedError(this.sub),\n )\n }\n }\n\n async fetchHandler(pathname: string, init?: RequestInit): Promise<Response> {\n // This will try and refresh the token if it is known to be expired\n const tokenSet = await this.getTokenSet('auto')\n\n const initialUrl = new URL(pathname, tokenSet.aud satisfies string)\n const initialAuth = `${tokenSet.token_type} ${tokenSet.access_token}`\n\n const headers = new Headers(init?.headers)\n headers.set('Authorization', initialAuth)\n\n const initialResponse = await this.dpopFetch(initialUrl, {\n ...init,\n headers,\n })\n\n // If the token is not expired, we don't need to refresh it\n if (!isInvalidTokenResponse(initialResponse)) {\n return initialResponse\n }\n\n let tokenSetFresh: TokenSet\n try {\n // Force a refresh\n tokenSetFresh = await this.getTokenSet(true)\n } catch (err) {\n return initialResponse\n }\n\n // The stream was already consumed. We cannot retry the request. A solution\n // would be to tee() the input stream but that would bufferize the entire\n // stream in memory which can lead to memory starvation. Instead, we will\n // return the original response and let the calling code handle retries.\n if (ReadableStream && init?.body instanceof ReadableStream) {\n return initialResponse\n }\n\n const finalAuth = `${tokenSetFresh.token_type} ${tokenSetFresh.access_token}`\n const finalUrl = new URL(pathname, tokenSetFresh.aud)\n\n headers.set('Authorization', finalAuth)\n\n const finalResponse = await this.dpopFetch(finalUrl, { ...init, headers })\n\n // The token was successfully refreshed, but is still not accepted by the\n // resource server. This might be due to the resource server not accepting\n // credentials from the authorization server (e.g. because some migration\n // occurred). Any ways, there is no point in keeping the session.\n if (isInvalidTokenResponse(finalResponse)) {\n // @TODO Is there a \"softer\" way to handle this, e.g. by marking the\n // session as \"expired\" in the session store, allowing the user to trigger\n // a new login (using login_hint)?\n await this.sessionGetter.delStored(\n this.sub,\n new TokenInvalidError(this.sub),\n )\n }\n\n return finalResponse\n }\n}\n\n/**\n * @see {@link https://datatracker.ietf.org/doc/html/rfc6750#section-3}\n * @see {@link https://datatracker.ietf.org/doc/html/rfc9449#name-resource-server-provided-no}\n */\nfunction isInvalidTokenResponse(response: Response) {\n if (response.status !== 401) return false\n const wwwAuth = response.headers.get('WWW-Authenticate')\n return (\n wwwAuth != null &&\n (wwwAuth.startsWith('Bearer ') || wwwAuth.startsWith('DPoP ')) &&\n wwwAuth.includes('error=\"invalid_token\"')\n )\n}\n"]}
1
+ {"version":3,"file":"oauth-session.js","sourceRoot":"","sources":["../src/oauth-session.ts"],"names":[],"mappings":";;;AAKA,+CAAsD;AACtD,4EAAmE;AACnE,4EAAmE;AACnE,mDAAkD;AAIlD,MAAM,cAAc,GAAG,UAAU,CAAC,cAErB,CAAA;AAYb,MAAa,YAAY;IAGvB,YACkB,MAAwB,EACxB,GAAe,EACd,aAA4B,EAC7C,QAAe,UAAU,CAAC,KAAK;QAH/B;;;;mBAAgB,MAAM;WAAkB;QACxC;;;;mBAAgB,GAAG;WAAY;QAC/B;;;;mBAAiB,aAAa;WAAe;QALrC;;;;;WAAyB;QAQjC,IAAI,CAAC,SAAS,GAAG,IAAA,gCAAgB,EAAO;YACtC,KAAK,EAAE,IAAA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,CAAC,CAAC;QAC9D,OAAO,CAAC,QAAQ,CAAC,uBAAuB,CAAC,CAC1C,CAAA;AACH,CAAC","sourcesContent":["import { AtprotoDid } from '@atproto/did'\nimport {\n AtprotoOAuthScope,\n OAuthAuthorizationServerMetadata,\n} from '@atproto/oauth-types'\nimport { Fetch, bindFetch } from '@atproto-labs/fetch'\nimport { TokenInvalidError } from './errors/token-invalid-error.js'\nimport { TokenRevokedError } from './errors/token-revoked-error.js'\nimport { dpopFetchWrapper } from './fetch-dpop.js'\nimport { OAuthServerAgent, TokenSet } from './oauth-server-agent.js'\nimport { SessionGetter } from './session-getter.js'\n\nconst ReadableStream = globalThis.ReadableStream as\n | typeof globalThis.ReadableStream\n | undefined\n\nexport type { AtprotoDid, AtprotoOAuthScope }\nexport type TokenInfo = {\n expiresAt?: Date\n expired?: boolean\n scope: AtprotoOAuthScope\n iss: string\n aud: string\n sub: AtprotoDid\n}\n\nexport class OAuthSession {\n protected dpopFetch: Fetch<unknown>\n\n constructor(\n public readonly server: OAuthServerAgent,\n public readonly sub: AtprotoDid,\n private readonly sessionGetter: SessionGetter,\n fetch: Fetch = globalThis.fetch,\n ) {\n this.dpopFetch = dpopFetchWrapper<void>({\n fetch: bindFetch(fetch),\n key: server.dpopKey,\n supportedAlgs: server.serverMetadata.dpop_signing_alg_values_supported,\n sha256: async (v) => server.runtime.sha256(v),\n nonces: server.dpopNonces,\n isAuthServer: false,\n })\n }\n\n get did(): AtprotoDid {\n return this.sub\n }\n\n get serverMetadata(): Readonly<OAuthAuthorizationServerMetadata> {\n return this.server.serverMetadata\n }\n\n /**\n * @param refresh When `true`, the credentials will be refreshed even if they\n * are not expired. When `false`, the credentials will not be refreshed even\n * if they are expired. When `undefined`, the credentials will be refreshed\n * if, and only if, they are (about to be) expired. Defaults to `undefined`.\n */\n protected async getTokenSet(refresh: boolean | 'auto'): Promise<TokenSet> {\n const { tokenSet } = await this.sessionGetter.getSession(this.sub, refresh)\n\n return tokenSet\n }\n\n async getTokenInfo(refresh: boolean | 'auto' = 'auto'): Promise<TokenInfo> {\n const tokenSet = await this.getTokenSet(refresh)\n const expiresAt =\n tokenSet.expires_at == null ? undefined : new Date(tokenSet.expires_at)\n\n return {\n expiresAt,\n get expired() {\n return expiresAt == null\n ? undefined\n : expiresAt.getTime() < Date.now() - 5e3\n },\n scope: tokenSet.scope,\n iss: tokenSet.iss,\n aud: tokenSet.aud,\n sub: tokenSet.sub,\n }\n }\n\n async signOut(): Promise<void> {\n try {\n const tokenSet = await this.getTokenSet(false)\n await this.server.revoke(tokenSet.access_token)\n } finally {\n await this.sessionGetter.delStored(\n this.sub,\n new TokenRevokedError(this.sub),\n )\n }\n }\n\n async fetchHandler(pathname: string, init?: RequestInit): Promise<Response> {\n // This will try and refresh the token if it is known to be expired\n const tokenSet = await this.getTokenSet('auto')\n\n const initialUrl = new URL(pathname, tokenSet.aud satisfies string)\n const initialAuth = `${tokenSet.token_type} ${tokenSet.access_token}`\n\n const headers = new Headers(init?.headers)\n headers.set('Authorization', initialAuth)\n\n const initialResponse = await this.dpopFetch(initialUrl, {\n ...init,\n headers,\n })\n\n // If the token is not expired, we don't need to refresh it\n if (!isInvalidTokenResponse(initialResponse)) {\n return initialResponse\n }\n\n let tokenSetFresh: TokenSet\n try {\n // Force a refresh\n tokenSetFresh = await this.getTokenSet(true)\n } catch (err) {\n return initialResponse\n }\n\n // The stream was already consumed. We cannot retry the request. A solution\n // would be to tee() the input stream but that would bufferize the entire\n // stream in memory which can lead to memory starvation. Instead, we will\n // return the original response and let the calling code handle retries.\n if (ReadableStream && init?.body instanceof ReadableStream) {\n return initialResponse\n }\n\n const finalAuth = `${tokenSetFresh.token_type} ${tokenSetFresh.access_token}`\n const finalUrl = new URL(pathname, tokenSetFresh.aud)\n\n headers.set('Authorization', finalAuth)\n\n const finalResponse = await this.dpopFetch(finalUrl, { ...init, headers })\n\n // The token was successfully refreshed, but is still not accepted by the\n // resource server. This might be due to the resource server not accepting\n // credentials from the authorization server (e.g. because some migration\n // occurred). Any ways, there is no point in keeping the session.\n if (isInvalidTokenResponse(finalResponse)) {\n // @TODO Is there a \"softer\" way to handle this, e.g. by marking the\n // session as \"expired\" in the session store, allowing the user to trigger\n // a new login (using login_hint)?\n await this.sessionGetter.delStored(\n this.sub,\n new TokenInvalidError(this.sub),\n )\n }\n\n return finalResponse\n }\n}\n\n/**\n * @see {@link https://datatracker.ietf.org/doc/html/rfc6750#section-3}\n * @see {@link https://datatracker.ietf.org/doc/html/rfc9449#name-resource-server-provided-no}\n */\nfunction isInvalidTokenResponse(response: Response) {\n if (response.status !== 401) return false\n const wwwAuth = response.headers.get('WWW-Authenticate')\n return (\n wwwAuth != null &&\n (wwwAuth.startsWith('Bearer ') || wwwAuth.startsWith('DPoP ')) &&\n wwwAuth.includes('error=\"invalid_token\"')\n )\n}\n"]}
@@ -1,6 +1,7 @@
1
1
  import { AtprotoDid } from '@atproto/did';
2
2
  import { Key } from '@atproto/jwk';
3
- import { CachedGetter, GetCachedOptions, SimpleStore } from '@atproto-labs/simple-store';
3
+ import { CachedGetter, GetCachedOptions, GetOptions, SimpleStore } from '@atproto-labs/simple-store';
4
+ import { AuthMethodUnsatisfiableError } from './errors/auth-method-unsatisfiable-error.js';
4
5
  import { TokenInvalidError } from './errors/token-invalid-error.js';
5
6
  import { TokenRefreshError } from './errors/token-refresh-error.js';
6
7
  import { TokenRevokedError } from './errors/token-revoked-error.js';
@@ -10,23 +11,15 @@ import { OAuthServerFactory } from './oauth-server-factory.js';
10
11
  import { Runtime } from './runtime.js';
11
12
  export type Session = {
12
13
  dpopKey: Key;
13
- /**
14
- * Previous implementation of this lib did not define an `authMethod`
15
- */
16
- authMethod?: ClientAuthMethod;
14
+ authMethod: ClientAuthMethod;
17
15
  tokenSet: TokenSet;
18
16
  };
19
17
  export type SessionStore = SimpleStore<string, Session>;
20
- export type SessionEventMap = {
21
- updated: {
22
- sub: string;
23
- } & Session;
24
- deleted: {
25
- sub: string;
26
- cause: TokenRefreshError | TokenRevokedError | TokenInvalidError | unknown;
27
- };
18
+ export type SessionHooks = {
19
+ onUpdate?: (sub: AtprotoDid, session: Session) => void;
20
+ onDelete?: (sub: AtprotoDid, cause: TokenRefreshError | TokenRevokedError | TokenInvalidError | unknown) => void;
28
21
  };
29
- export type SessionEventListener<T extends keyof SessionEventMap = keyof SessionEventMap> = (event: CustomEvent<SessionEventMap[T]>) => void;
22
+ export declare function isExpectedSessionError(err: unknown): err is TypeError | AuthMethodUnsatisfiableError | TokenRevokedError | TokenRefreshError | TokenInvalidError;
30
23
  /**
31
24
  * There are several advantages to wrapping the sessionStore in a (single)
32
25
  * CachedGetter, the main of which is that the cached getter will ensure that at
@@ -36,13 +29,16 @@ export type SessionEventListener<T extends keyof SessionEventMap = keyof Session
36
29
  */
37
30
  export declare class SessionGetter extends CachedGetter<AtprotoDid, Session> {
38
31
  private readonly runtime;
39
- private readonly eventTarget;
40
- constructor(sessionStore: SessionStore, serverFactory: OAuthServerFactory, runtime: Runtime);
41
- addEventListener<T extends keyof SessionEventMap>(type: T, callback: SessionEventListener<T>, options?: AddEventListenerOptions | boolean): void;
42
- removeEventListener<T extends keyof SessionEventMap>(type: T, callback: SessionEventListener<T>, options?: EventListenerOptions | boolean): void;
43
- dispatchEvent<T extends keyof SessionEventMap>(type: T, detail: SessionEventMap[T]): boolean;
44
- setStored(sub: string, session: Session): Promise<void>;
32
+ private readonly hooks;
33
+ constructor(sessionStore: SessionStore, serverFactory: OAuthServerFactory, runtime: Runtime, hooks?: SessionHooks);
34
+ getStored(sub: AtprotoDid, options?: GetOptions): Promise<Session | undefined>;
35
+ setStored(sub: AtprotoDid, session: Session): Promise<void>;
45
36
  delStored(sub: AtprotoDid, cause?: unknown): Promise<void>;
37
+ /**
38
+ * @deprecated Use {@link getSession} instead
39
+ * @internal (not really deprecated)
40
+ */
41
+ get(sub: AtprotoDid, options?: GetCachedOptions): Promise<Session>;
46
42
  /**
47
43
  * @param refresh When `true`, the credentials will be refreshed even if they
48
44
  * are not expired. When `false`, the credentials will not be refreshed even
@@ -50,6 +46,5 @@ export declare class SessionGetter extends CachedGetter<AtprotoDid, Session> {
50
46
  * if, and only if, they are (about to be) expired. Defaults to `undefined`.
51
47
  */
52
48
  getSession(sub: AtprotoDid, refresh?: boolean | 'auto'): Promise<Session>;
53
- get(sub: AtprotoDid, options?: GetCachedOptions): Promise<Session>;
54
49
  }
55
50
  //# sourceMappingURL=session-getter.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"session-getter.d.ts","sourceRoot":"","sources":["../src/session-getter.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAA;AACzC,OAAO,EAAE,GAAG,EAAE,MAAM,cAAc,CAAA;AAClC,OAAO,EACL,YAAY,EACZ,gBAAgB,EAChB,WAAW,EACZ,MAAM,4BAA4B,CAAA;AAEnC,OAAO,EAAE,iBAAiB,EAAE,MAAM,iCAAiC,CAAA;AACnE,OAAO,EAAE,iBAAiB,EAAE,MAAM,iCAAiC,CAAA;AACnE,OAAO,EAAE,iBAAiB,EAAE,MAAM,iCAAiC,CAAA;AACnE,OAAO,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAA;AAEzD,OAAO,EAAE,QAAQ,EAAE,MAAM,yBAAyB,CAAA;AAClD,OAAO,EAAE,kBAAkB,EAAE,MAAM,2BAA2B,CAAA;AAC9D,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAA;AAGtC,MAAM,MAAM,OAAO,GAAG;IACpB,OAAO,EAAE,GAAG,CAAA;IACZ;;OAEG;IACH,UAAU,CAAC,EAAE,gBAAgB,CAAA;IAC7B,QAAQ,EAAE,QAAQ,CAAA;CACnB,CAAA;AAED,MAAM,MAAM,YAAY,GAAG,WAAW,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;AAEvD,MAAM,MAAM,eAAe,GAAG;IAC5B,OAAO,EAAE;QACP,GAAG,EAAE,MAAM,CAAA;KACZ,GAAG,OAAO,CAAA;I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O,KACvE,IAAI,CAAA;CACV,CAAA;AAED,wBAAgB,sBAAsB,CAAC,GAAG,EAAE,OAAO,+GAUlD;AAED;;;;;;GAMG;AACH,qBAAa,aAAc,SAAQ,YAAY,CAAC,UAAU,EAAE,OAAO,CAAC;IAIhE,OAAO,CAAC,QAAQ,CAAC,OAAO;IACxB,OAAO,CAAC,QAAQ,CAAC,KAAK;gBAHtB,YAAY,EAAE,YAAY,EAC1B,aAAa,EAAE,kBAAkB,EAChB,OAAO,EAAE,OAAO,EAChB,KAAK,GAAE,YAAiB;IA4J5B,SAAS,CACtB,GAAG,EAAE,UAAU,EACf,OAAO,CAAC,EAAE,UAAU,GACnB,OAAO,CAAC,OAAO,GAAG,SAAS,CAAC;IAIhB,SAAS,CAAC,GAAG,EAAE,UAAU,EAAE,OAAO,EAAE,OAAO;IAS3C,SAAS,CAAC,GAAG,EAAE,UAAU,EAAE,KAAK,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC,IAAI,CAAC;IAKzE;;;OAGG;IACY,GAAG,CAChB,GAAG,EAAE,UAAU,EACf,OAAO,CAAC,EAAE,gBAAgB,GACzB,OAAO,CAAC,OAAO,CAAC;IAyBnB;;;;;OAKG;IACG,UAAU,CAAC,GAAG,EAAE,UAAU,EAAE,OAAO,GAAE,OAAO,GAAG,MAAe;CAMrE"}
@@ -53,6 +53,7 @@ var __disposeResources = (this && this.__disposeResources) || (function (Suppres
53
53
  });
54
54
  Object.defineProperty(exports, "__esModule", { value: true });
55
55
  exports.SessionGetter = void 0;
56
+ exports.isExpectedSessionError = isExpectedSessionError;
56
57
  const simple_store_1 = require("@atproto-labs/simple-store");
57
58
  const auth_method_unsatisfiable_error_js_1 = require("./errors/auth-method-unsatisfiable-error.js");
58
59
  const token_invalid_error_js_1 = require("./errors/token-invalid-error.js");
@@ -60,6 +61,15 @@ const token_refresh_error_js_1 = require("./errors/token-refresh-error.js");
60
61
  const token_revoked_error_js_1 = require("./errors/token-revoked-error.js");
61
62
  const oauth_response_error_js_1 = require("./oauth-response-error.js");
62
63
  const util_js_1 = require("./util.js");
64
+ function isExpectedSessionError(err) {
65
+ return (err instanceof token_refresh_error_js_1.TokenRefreshError ||
66
+ err instanceof token_revoked_error_js_1.TokenRevokedError ||
67
+ err instanceof token_invalid_error_js_1.TokenInvalidError ||
68
+ err instanceof auth_method_unsatisfiable_error_js_1.AuthMethodUnsatisfiableError ||
69
+ // The stored session is invalid (e.g. missing properties) and cannot
70
+ // be used properly
71
+ err instanceof TypeError);
72
+ }
63
73
  /**
64
74
  * There are several advantages to wrapping the sessionStore in a (single)
65
75
  * CachedGetter, the main of which is that the cached getter will ensure that at
@@ -68,8 +78,8 @@ const util_js_1 = require("./util.js");
68
78
  * localStorage/indexedDB, will sync across multiple tabs (for a given sub).
69
79
  */
70
80
  class SessionGetter extends simple_store_1.CachedGetter {
71
- constructor(sessionStore, serverFactory, runtime) {
72
- super(async (sub, options, storedSession) => {
81
+ constructor(sessionStore, serverFactory, runtime, hooks = {}) {
82
+ super(async (sub, { signal }, storedSession) => {
73
83
  // There needs to be a previous session to be able to refresh. If
74
84
  // storedSession is undefined, it means that the store does not contain
75
85
  // a session for the given sub.
@@ -81,15 +91,13 @@ class SessionGetter extends simple_store_1.CachedGetter {
81
91
  // make sure an event is dispatched here if this occurs.
82
92
  const msg = 'The session was deleted by another process';
83
93
  const cause = new token_refresh_error_js_1.TokenRefreshError(sub, msg);
84
- this.dispatchEvent('deleted', { sub, cause });
94
+ await hooks.onDelete?.call(null, sub, cause);
85
95
  throw cause;
86
96
  }
87
- // From this point forward, throwing a TokenRefreshError will result in
88
- // this.delStored() being called, resulting in an event being
89
- // dispatched, even if the session was removed from the store through a
90
- // concurrent access (which, normally, should not happen if a proper
91
- // runtime lock was provided).
92
- const { dpopKey, authMethod = 'legacy', tokenSet } = storedSession;
97
+ // @NOTE Throwing a TokenRefreshError (or any other error class defined
98
+ // in the deleteOnError options) will result in this.delStored() being
99
+ // called.
100
+ const { dpopKey, authMethod, tokenSet } = storedSession;
93
101
  if (sub !== tokenSet.sub) {
94
102
  // Fool-proofing (e.g. against invalid session storage)
95
103
  throw new token_refresh_error_js_1.TokenRefreshError(sub, 'Stored session sub mismatch');
@@ -97,22 +105,13 @@ class SessionGetter extends simple_store_1.CachedGetter {
97
105
  if (!tokenSet.refresh_token) {
98
106
  throw new token_refresh_error_js_1.TokenRefreshError(sub, 'No refresh token available');
99
107
  }
100
- // Since refresh tokens can only be used once, we might run into
101
- // concurrency issues if multiple instances (e.g. browser tabs) are
102
- // trying to refresh the same token simultaneously. The chances of this
103
- // happening when multiple instances are started simultaneously is
104
- // reduced by randomizing the expiry time (see isStale() below). The
105
- // best solution is to use a mutex/lock to ensure that only one instance
106
- // is refreshing the token at a time (runtime.usingLock) but that is not
107
- // always possible. If no lock implementation is provided, we will use
108
- // the store to check if a concurrent refresh occurred.
109
108
  const server = await serverFactory.fromIssuer(tokenSet.iss, authMethod, dpopKey);
110
109
  // Because refresh tokens can only be used once, we must not use the
111
110
  // "signal" to abort the refresh, or throw any abort error beyond this
112
111
  // point. Any thrown error beyond this point will prevent the
113
112
  // TokenGetter from obtaining, and storing, the new token set,
114
113
  // effectively rendering the currently saved session unusable.
115
- options?.signal?.throwIfAborted();
114
+ signal?.throwIfAborted();
116
115
  try {
117
116
  const newTokenSet = await server.refresh(tokenSet);
118
117
  if (sub !== newTokenSet.sub) {
@@ -126,9 +125,16 @@ class SessionGetter extends simple_store_1.CachedGetter {
126
125
  };
127
126
  }
128
127
  catch (cause) {
129
- // If the refresh token is invalid, let's try to recover from
130
- // concurrency issues, or make sure the session is deleted by throwing
131
- // a TokenRefreshError.
128
+ // Since refresh tokens can only be used once, we might run into
129
+ // concurrency issues if multiple instances (e.g. browser tabs) are
130
+ // trying to refresh the same token simultaneously. The chances of
131
+ // this happening when multiple instances are started simultaneously
132
+ // is reduced by randomizing the expiry time (see isStale() below).
133
+ // The best solution is to use a mutex/lock to ensure that only one
134
+ // instance is refreshing the token at a time (runtime.usingLock) but
135
+ // that is not always possible. Let's try to recover from concurrency
136
+ // issues, or force the session to be deleted by throwing a
137
+ // TokenRefreshError.
132
138
  if (cause instanceof oauth_response_error_js_1.OAuthResponseError &&
133
139
  cause.status === 400 &&
134
140
  cause.error === 'invalid_grant') {
@@ -176,25 +182,26 @@ class SessionGetter extends simple_store_1.CachedGetter {
176
182
  // instances trying to refresh the token at the same.
177
183
  30e3 * Math.random());
178
184
  },
179
- onStoreError: async (err, sub, { tokenSet, dpopKey, authMethod = 'legacy' }) => {
180
- if (!(err instanceof auth_method_unsatisfiable_error_js_1.AuthMethodUnsatisfiableError)) {
181
- // If the error was an AuthMethodUnsatisfiableError, there is no
182
- // point in trying to call `fromIssuer`.
183
- try {
184
- // If the token data cannot be stored, let's revoke it
185
- const server = await serverFactory.fromIssuer(tokenSet.iss, authMethod, dpopKey);
186
- await server.revoke(tokenSet.refresh_token ?? tokenSet.access_token);
187
- }
188
- catch {
189
- // Let the original error propagate
190
- }
185
+ onStoreError: async (err, sub, { tokenSet, dpopKey, authMethod }) => {
186
+ // If the token data cannot be stored, let's revoke it
187
+ try {
188
+ const server = await serverFactory.fromIssuer(tokenSet.iss, authMethod, dpopKey);
189
+ await server.revoke(tokenSet.refresh_token ?? tokenSet.access_token);
190
+ }
191
+ catch {
192
+ // At least we tried...
193
+ }
194
+ // Attempt to delete the session from the store. Note that this might
195
+ // fail if the store is not available, which is fine.
196
+ try {
197
+ await this.delStored(sub, err);
198
+ }
199
+ catch {
200
+ // Ignore (better to propagate the original storage error)
191
201
  }
192
202
  throw err;
193
203
  },
194
- deleteOnError: async (err) => err instanceof token_refresh_error_js_1.TokenRefreshError ||
195
- err instanceof token_revoked_error_js_1.TokenRevokedError ||
196
- err instanceof token_invalid_error_js_1.TokenInvalidError ||
197
- err instanceof auth_method_unsatisfiable_error_js_1.AuthMethodUnsatisfiableError,
204
+ deleteOnError: isExpectedSessionError,
198
205
  });
199
206
  Object.defineProperty(this, "runtime", {
200
207
  enumerable: true,
@@ -202,21 +209,15 @@ class SessionGetter extends simple_store_1.CachedGetter {
202
209
  writable: true,
203
210
  value: runtime
204
211
  });
205
- Object.defineProperty(this, "eventTarget", {
212
+ Object.defineProperty(this, "hooks", {
206
213
  enumerable: true,
207
214
  configurable: true,
208
215
  writable: true,
209
- value: new util_js_1.CustomEventTarget()
216
+ value: hooks
210
217
  });
211
218
  }
212
- addEventListener(type, callback, options) {
213
- this.eventTarget.addEventListener(type, callback, options);
214
- }
215
- removeEventListener(type, callback, options) {
216
- this.eventTarget.removeEventListener(type, callback, options);
217
- }
218
- dispatchEvent(type, detail) {
219
- return this.eventTarget.dispatchCustomEvent(type, detail);
219
+ async getStored(sub, options) {
220
+ return super.getStored(sub, options);
220
221
  }
221
222
  async setStored(sub, session) {
222
223
  // Prevent tampering with the stored value
@@ -224,24 +225,16 @@ class SessionGetter extends simple_store_1.CachedGetter {
224
225
  throw new TypeError('Token set does not match the expected sub');
225
226
  }
226
227
  await super.setStored(sub, session);
227
- this.dispatchEvent('updated', { sub, ...session });
228
+ await this.hooks.onUpdate?.call(null, sub, session);
228
229
  }
229
230
  async delStored(sub, cause) {
230
231
  await super.delStored(sub, cause);
231
- this.dispatchEvent('deleted', { sub, cause });
232
+ await this.hooks.onDelete?.call(null, sub, cause);
232
233
  }
233
234
  /**
234
- * @param refresh When `true`, the credentials will be refreshed even if they
235
- * are not expired. When `false`, the credentials will not be refreshed even
236
- * if they are expired. When `undefined`, the credentials will be refreshed
237
- * if, and only if, they are (about to be) expired. Defaults to `undefined`.
235
+ * @deprecated Use {@link getSession} instead
236
+ * @internal (not really deprecated)
238
237
  */
239
- async getSession(sub, refresh = 'auto') {
240
- return this.get(sub, {
241
- noCache: refresh === true,
242
- allowStale: refresh === false,
243
- });
244
- }
245
238
  async get(sub, options) {
246
239
  const session = await this.runtime.usingLock(`@atproto-oauth-client-${sub}`, async () => {
247
240
  const env_1 = { stack: [], error: void 0, hasError: false };
@@ -269,6 +262,18 @@ class SessionGetter extends simple_store_1.CachedGetter {
269
262
  }
270
263
  return session;
271
264
  }
265
+ /**
266
+ * @param refresh When `true`, the credentials will be refreshed even if they
267
+ * are not expired. When `false`, the credentials will not be refreshed even
268
+ * if they are expired. When `undefined`, the credentials will be refreshed
269
+ * if, and only if, they are (about to be) expired. Defaults to `undefined`.
270
+ */
271
+ async getSession(sub, refresh = 'auto') {
272
+ return this.get(sub, {
273
+ noCache: refresh === true,
274
+ allowStale: refresh === false,
275
+ });
276
+ }
272
277
  }
273
278
  exports.SessionGetter = SessionGetter;
274
279
  //# sourceMappingURL=session-getter.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"session-getter.js","sourceRoot":"","sources":["../src/session-getter.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAEA,6DAImC;AACnC,oGAA0F;AAC1F,4EAAmE;AACnE,4EAAmE;AACnE,4EAAmE;AAEnE,uEAA8D;AAI9D,uCAA6D;AA2B7D;;;;;;GAMG;AACH,MAAa,aAAc,SAAQ,2BAAiC;IAGlE,YACE,YAA0B,EAC1B,aAAiC,EAChB,OAAgB;QAEjC,KAAK,CACH,KAAK,EAAE,GAAG,EAAE,OAAO,EAAE,aAAa,EAAE,EAAE;YACpC,iEAAiE;YACjE,uEAAuE;YACvE,+BAA+B;YAC/B,IAAI,aAAa,KAAK,SAAS,EAAE,CAAC;gBAChC,mEAAmE;gBACnE,iEAAiE;gBACjE,iEAAiE;gBACjE,mEAAmE;gBACnE,wDAAwD;gBACxD,MAAM,GAAG,GAAG,4CAA4C,CAAA;gBACxD,MAAM,KAAK,GAAG,IAAI,0CAAiB,CAAC,GAAG,EAAE,GAAG,CAAC,CAAA;gBAC7C,IAAI,CAAC,aAAa,CAAC,SAAS,EAAE,EAAE,GAAG,EAAE,KAAK,EAAE,CAAC,CAAA;gBAC7C,MAAM,KAAK,CAAA;YACb,CAAC;YAED,uEAAuE;YACvE,6DAA6D;YAC7D,uEAAuE;YAC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,EAC9B,KAAK,IAAI,EAAE;;;gBACT,iEAAiE;gBACjE,sDAAsD;gBACtD,MAAM,MAAM,GAAG,WAAW,CAAC,OAAO,CAAC,IAAI,CAAC,CAAA;gBAExC,MAAM,eAAe,kCAAG,IAAA,wBAAc,EAAC,CAAC,OAAO,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC,QAAA,CAAA;gBAEjE,OAAO,MAAM,KAAK,CAAC,GAAG,CAAC,GAAG,EAAE;oBAC1B,GAAG,OAAO;oBACV,MAAM,EAAE,eAAe,CAAC,MAAM;iBAC/B,CAAC,CAAA;;;;;;;;;SACH,CACF,CAAA;QAED,IAAI,GAAG,KAAK,OAAO,CAAC,QAAQ,CAAC,GAAG,EAAE,CAAC;YACjC,uDAAuD;YACvD,MAAM,IAAI,KAAK,CAAC,2CAA2C,CAAC,CAAA;QAC9D,CAAC;QAED,OAAO,OAAO,CAAA;IAChB,CAAC;CACF;AAvPD,sCAuPC","sourcesContent":["import { AtprotoDid } from '@atproto/did'\nimport { Key } from '@atproto/jwk'\nimport {\n CachedGetter,\n GetCachedOptions,\n SimpleStore,\n} from '@atproto-labs/simple-store'\nimport { AuthMethodUnsatisfiableError } from './errors/auth-method-unsatisfiable-error.js'\nimport { TokenInvalidError } from './errors/token-invalid-error.js'\nimport { TokenRefreshError } from './errors/token-refresh-error.js'\nimport { TokenRevokedError } from './errors/token-revoked-error.js'\nimport { ClientAuthMethod } from './oauth-client-auth.js'\nimport { OAuthResponseError } from './oauth-response-error.js'\nimport { TokenSet } from './oauth-server-agent.js'\nimport { OAuthServerFactory } from './oauth-server-factory.js'\nimport { Runtime } from './runtime.js'\nimport { CustomEventTarget, combineSignals } from './util.js'\n\nexport type Session = {\n dpopKey: Key\n /**\n * Previous implementation of this lib did not define an `authMethod`\n */\n authMethod?: ClientAuthMethod\n tokenSet: TokenSet\n}\n\nexport type SessionStore = SimpleStore<string, Session>\n\nexport type SessionEventMap = {\n updated: {\n sub: string\n } & Session\n deleted: {\n sub: string\n cause: TokenRefreshError | TokenRevokedError | TokenInvalidError | unknown\n }\n}\n\nexport type SessionEventListener<\n T extends keyof SessionEventMap = keyof SessionEventMap,\n> = (event: CustomEvent<SessionEventMap[T]>) => void\n\n/**\n * There are several advantages to wrapping the sessionStore in a (single)\n * CachedGetter, the main of which is that the cached getter will ensure that at\n * most one fresh call is ever being made. Another advantage, is that it\n * contains the logic for reading from the cache which, if the cache is based on\n * localStorage/indexedDB, will sync across multiple tabs (for a given sub).\n */\nexport class SessionGetter extends CachedGetter<AtprotoDid, Session> {\n private readonly eventTarget = new CustomEventTarget<SessionEventMap>()\n\n constructor(\n sessionStore: SessionStore,\n serverFactory: OAuthServerFactory,\n private readonly runtime: Runtime,\n ) {\n super(\n async (sub, options, storedSession) => {\n // There needs to be a previous session to be able to refresh. If\n // storedSession is undefined, it means that the store does not contain\n // a session for the given sub.\n if (storedSession === undefined) {\n // Because the session is not in the store, this.delStored() method\n // will not be called by the CachedGetter class (because there is\n // nothing to delete). This would typically happen if there is no\n // synchronization mechanism between instances of this class. Let's\n // make sure an event is dispatched here if this occurs.\n const msg = 'The session was deleted by another process'\n const cause = new TokenRefreshError(sub, msg)\n this.dispatchEvent('deleted', { sub, cause })\n throw cause\n }\n\n // From this point forward, throwing a TokenRefreshError will result in\n // this.delStored() being called, resulting in an event being\n // dispatched, even if the session was removed from the store through a\n // concurrent access (which, normally, should not happen if a proper\n // runtime lock was provided).\n\n const { dpopKey, authMethod = 'legacy', tokenSet } = storedSession\n\n if (sub !== tokenSet.sub) {\n // Fool-proofing (e.g. against invalid session storage)\n throw new TokenRefreshError(sub, 'Stored session sub mismatch')\n }\n\n if (!tokenSet.refresh_token) {\n throw new TokenRefreshError(sub, 'No refresh token available')\n }\n\n // Since refresh tokens can only be used once, we might run into\n // concurrency issues if multiple instances (e.g. browser tabs) are\n // trying to refresh the same token simultaneously. The chances of this\n // happening when multiple instances are started simultaneously is\n // reduced by randomizing the expiry time (see isStale() below). The\n // best solution is to use a mutex/lock to ensure that only one instance\n // is refreshing the token at a time (runtime.usingLock) but that is not\n // always possible. If no lock implementation is provided, we will use\n // the store to check if a concurrent refresh occurred.\n\n const server = await serverFactory.fromIssuer(\n tokenSet.iss,\n authMethod,\n dpopKey,\n )\n\n // Because refresh tokens can only be used once, we must not use the\n // \"signal\" to abort the refresh, or throw any abort error beyond this\n // point. Any thrown error beyond this point will prevent the\n // TokenGetter from obtaining, and storing, the new token set,\n // effectively rendering the currently saved session unusable.\n options?.signal?.throwIfAborted()\n\n try {\n const newTokenSet = await server.refresh(tokenSet)\n\n if (sub !== newTokenSet.sub) {\n // The server returned another sub. Was the tokenSet manipulated?\n throw new TokenRefreshError(sub, 'Token set sub mismatch')\n }\n\n return {\n dpopKey,\n tokenSet: newTokenSet,\n authMethod: server.authMethod,\n }\n } catch (cause) {\n // If the refresh token is invalid, let's try to recover from\n // concurrency issues, or make sure the session is deleted by throwing\n // a TokenRefreshError.\n if (\n cause instanceof OAuthResponseError &&\n cause.status === 400 &&\n cause.error === 'invalid_grant'\n ) {\n // In case there is no lock implementation in the runtime, we will\n // wait for a short time to give the other concurrent instances a\n // chance to finish their refreshing of the token. If a concurrent\n // refresh did occur, we will pretend that this one succeeded.\n if (!runtime.hasImplementationLock) {\n await new Promise((r) => setTimeout(r, 1000))\n\n const stored = await this.getStored(sub)\n if (stored === undefined) {\n // A concurrent refresh occurred and caused the session to be\n // deleted (for a reason we can't know at this point).\n\n // Using a distinct error message mainly for debugging\n // purposes. Also, throwing a TokenRefreshError to trigger\n // deletion through the deleteOnError callback.\n const msg = 'The session was deleted by another process'\n throw new TokenRefreshError(sub, msg, { cause })\n } else if (\n stored.tokenSet.access_token !== tokenSet.access_token ||\n stored.tokenSet.refresh_token !== tokenSet.refresh_token\n ) {\n // A concurrent refresh occurred. Pretend this one succeeded.\n return stored\n } else {\n // There were no concurrent refresh. The token is (likely)\n // simply no longer valid.\n }\n }\n\n // Make sure the session gets deleted from the store\n const msg = cause.errorDescription ?? 'The session was revoked'\n throw new TokenRefreshError(sub, msg, { cause })\n }\n\n throw cause\n }\n },\n sessionStore,\n {\n isStale: (sub, { tokenSet }) => {\n return (\n tokenSet.expires_at != null &&\n new Date(tokenSet.expires_at).getTime() <\n Date.now() +\n // Add some lee way to ensure the token is not expired when it\n // reaches the server.\n 10e3 +\n // Add some randomness to reduce the chances of multiple\n // instances trying to refresh the token at the same.\n 30e3 * Math.random()\n )\n },\n onStoreError: async (\n err,\n sub,\n { tokenSet, dpopKey, authMethod = 'legacy' as const },\n ) => {\n if (!(err instanceof AuthMethodUnsatisfiableError)) {\n // If the error was an AuthMethodUnsatisfiableError, there is no\n // point in trying to call `fromIssuer`.\n try {\n // If the token data cannot be stored, let's revoke it\n const server = await serverFactory.fromIssuer(\n tokenSet.iss,\n authMethod,\n dpopKey,\n )\n await server.revoke(\n tokenSet.refresh_token ?? tokenSet.access_token,\n )\n } catch {\n // Let the original error propagate\n }\n }\n\n throw err\n },\n deleteOnError: async (err) =>\n err instanceof TokenRefreshError ||\n err instanceof TokenRevokedError ||\n err instanceof TokenInvalidError ||\n err instanceof AuthMethodUnsatisfiableError,\n },\n )\n }\n\n addEventListener<T extends keyof SessionEventMap>(\n type: T,\n callback: SessionEventListener<T>,\n options?: AddEventListenerOptions | boolean,\n ) {\n this.eventTarget.addEventListener(type, callback, options)\n }\n\n removeEventListener<T extends keyof SessionEventMap>(\n type: T,\n callback: SessionEventListener<T>,\n options?: EventListenerOptions | boolean,\n ) {\n this.eventTarget.removeEventListener(type, callback, options)\n }\n\n dispatchEvent<T extends keyof SessionEventMap>(\n type: T,\n detail: SessionEventMap[T],\n ): boolean {\n return this.eventTarget.dispatchCustomEvent(type, detail)\n }\n\n async setStored(sub: string, session: Session) {\n // Prevent tampering with the stored value\n if (sub !== session.tokenSet.sub) {\n throw new TypeError('Token set does not match the expected sub')\n }\n await super.setStored(sub, session)\n this.dispatchEvent('updated', { sub, ...session })\n }\n\n override async delStored(sub: AtprotoDid, cause?: unknown): Promise<void> {\n await super.delStored(sub, cause)\n this.dispatchEvent('deleted', { sub, cause })\n }\n\n /**\n * @param refresh When `true`, the credentials will be refreshed even if they\n * are not expired. When `false`, the credentials will not be refreshed even\n * if they are expired. When `undefined`, the credentials will be refreshed\n * if, and only if, they are (about to be) expired. Defaults to `undefined`.\n */\n async getSession(sub: AtprotoDid, refresh: boolean | 'auto' = 'auto') {\n return this.get(sub, {\n noCache: refresh === true,\n allowStale: refresh === false,\n })\n }\n\n async get(sub: AtprotoDid, options?: GetCachedOptions): Promise<Session> {\n const session = await this.runtime.usingLock(\n `@atproto-oauth-client-${sub}`,\n async () => {\n // Make sure, even if there is no signal in the options, that the\n // request will be cancelled after at most 30 seconds.\n const signal = AbortSignal.timeout(30e3)\n\n using abortController = combineSignals([options?.signal, signal])\n\n return await super.get(sub, {\n ...options,\n signal: abortController.signal,\n })\n },\n )\n\n if (sub !== session.tokenSet.sub) {\n // Fool-proofing (e.g. against invalid session storage)\n throw new Error('Token set does not match the expected sub')\n }\n\n return session\n }\n}\n"]}
1
+ {"version":3,"file":"session-getter.js","sourceRoot":"","sources":["../src/session-getter.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAmCA,wDAUC;AA3CD,6DAKmC;AACnC,oGAA0F;AAC1F,4EAAmE;AACnE,4EAAmE;AACnE,4EAAmE;AAEnE,uEAA8D;AAI9D,uCAA0C;AAkB1C,SAAgB,sBAAsB,CAAC,GAAY;IACjD,OAAO,CACL,GAAG,YAAY,0CAAiB;QAChC,GAAG,YAAY,0CAAiB;QAChC,GAAG,YAAY,0CAAiB;QAChC,GAAG,YAAY,iEAA4B;QAC3C,qEAAqE;QACrE,mBAAmB;QACnB,GAAG,YAAY,SAAS,CACzB,CAAA;AACH,CAAC;AAED;;;;;;GAMG;AACH,MAAa,aAAc,SAAQ,2BAAiC;IAClE,YACE,YAA0B,EAC1B,aAAiC,EAChB,OAAgB,EAChB,QAAsB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,OAAO,CAAC,IAAI,CAAC,CAAA;gBAExC,MAAM,eAAe,kCAAG,IAAA,wBAAc,EAAC,CAAC,OAAO,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC,QAAA,CAAA;gBAEjE,OAAO,MAAM,KAAK,CAAC,GAAG,CAAC,GAAG,EAAE;oBAC1B,GAAG,OAAO;oBACV,MAAM,EAAE,eAAe,CAAC,MAAM;iBAC/B,CAAC,CAAA;;;;;;;;;SACH,CACF,CAAA;QAED,IAAI,GAAG,KAAK,OAAO,CAAC,QAAQ,CAAC,GAAG,EAAE,CAAC;YACjC,uDAAuD;YACvD,MAAM,IAAI,KAAK,CAAC,2CAA2C,CAAC,CAAA;QAC9D,CAAC;QAED,OAAO,OAAO,CAAA;IAChB,CAAC;IAED;;;;;OAKG;IACH,KAAK,CAAC,UAAU,CAAC,GAAe,EAAE,UAA4B,MAAM;QAClE,OAAO,IAAI,CAAC,GAAG,CAAC,GAAG,EAAE;YACnB,OAAO,EAAE,OAAO,KAAK,IAAI;YACzB,UAAU,EAAE,OAAO,KAAK,KAAK;SAC9B,CAAC,CAAA;IACJ,CAAC;CACF;AAlOD,sCAkOC","sourcesContent":["import { AtprotoDid } from '@atproto/did'\nimport { Key } from '@atproto/jwk'\nimport {\n CachedGetter,\n GetCachedOptions,\n GetOptions,\n SimpleStore,\n} from '@atproto-labs/simple-store'\nimport { AuthMethodUnsatisfiableError } from './errors/auth-method-unsatisfiable-error.js'\nimport { TokenInvalidError } from './errors/token-invalid-error.js'\nimport { TokenRefreshError } from './errors/token-refresh-error.js'\nimport { TokenRevokedError } from './errors/token-revoked-error.js'\nimport { ClientAuthMethod } from './oauth-client-auth.js'\nimport { OAuthResponseError } from './oauth-response-error.js'\nimport { TokenSet } from './oauth-server-agent.js'\nimport { OAuthServerFactory } from './oauth-server-factory.js'\nimport { Runtime } from './runtime.js'\nimport { combineSignals } from './util.js'\n\nexport type Session = {\n dpopKey: Key\n authMethod: ClientAuthMethod\n tokenSet: TokenSet\n}\n\nexport type SessionStore = SimpleStore<string, Session>\n\nexport type SessionHooks = {\n onUpdate?: (sub: AtprotoDid, session: Session) => void\n onDelete?: (\n sub: AtprotoDid,\n cause: TokenRefreshError | TokenRevokedError | TokenInvalidError | unknown,\n ) => void\n}\n\nexport function isExpectedSessionError(err: unknown) {\n return (\n err instanceof TokenRefreshError ||\n err instanceof TokenRevokedError ||\n err instanceof TokenInvalidError ||\n err instanceof AuthMethodUnsatisfiableError ||\n // The stored session is invalid (e.g. missing properties) and cannot\n // be used properly\n err instanceof TypeError\n )\n}\n\n/**\n * There are several advantages to wrapping the sessionStore in a (single)\n * CachedGetter, the main of which is that the cached getter will ensure that at\n * most one fresh call is ever being made. Another advantage, is that it\n * contains the logic for reading from the cache which, if the cache is based on\n * localStorage/indexedDB, will sync across multiple tabs (for a given sub).\n */\nexport class SessionGetter extends CachedGetter<AtprotoDid, Session> {\n constructor(\n sessionStore: SessionStore,\n serverFactory: OAuthServerFactory,\n private readonly runtime: Runtime,\n private readonly hooks: SessionHooks = {},\n ) {\n super(\n async (sub, { signal }, storedSession) => {\n // There needs to be a previous session to be able to refresh. If\n // storedSession is undefined, it means that the store does not contain\n // a session for the given sub.\n if (storedSession === undefined) {\n // Because the session is not in the store, this.delStored() method\n // will not be called by the CachedGetter class (because there is\n // nothing to delete). This would typically happen if there is no\n // synchronization mechanism between instances of this class. Let's\n // make sure an event is dispatched here if this occurs.\n const msg = 'The session was deleted by another process'\n const cause = new TokenRefreshError(sub, msg)\n await hooks.onDelete?.call(null, sub, cause)\n throw cause\n }\n\n // @NOTE Throwing a TokenRefreshError (or any other error class defined\n // in the deleteOnError options) will result in this.delStored() being\n // called.\n\n const { dpopKey, authMethod, tokenSet } = storedSession\n\n if (sub !== tokenSet.sub) {\n // Fool-proofing (e.g. against invalid session storage)\n throw new TokenRefreshError(sub, 'Stored session sub mismatch')\n }\n\n if (!tokenSet.refresh_token) {\n throw new TokenRefreshError(sub, 'No refresh token available')\n }\n\n const server = await serverFactory.fromIssuer(\n tokenSet.iss,\n authMethod,\n dpopKey,\n )\n\n // Because refresh tokens can only be used once, we must not use the\n // \"signal\" to abort the refresh, or throw any abort error beyond this\n // point. Any thrown error beyond this point will prevent the\n // TokenGetter from obtaining, and storing, the new token set,\n // effectively rendering the currently saved session unusable.\n signal?.throwIfAborted()\n\n try {\n const newTokenSet = await server.refresh(tokenSet)\n\n if (sub !== newTokenSet.sub) {\n // The server returned another sub. Was the tokenSet manipulated?\n throw new TokenRefreshError(sub, 'Token set sub mismatch')\n }\n\n return {\n dpopKey,\n tokenSet: newTokenSet,\n authMethod: server.authMethod,\n }\n } catch (cause) {\n // Since refresh tokens can only be used once, we might run into\n // concurrency issues if multiple instances (e.g. browser tabs) are\n // trying to refresh the same token simultaneously. The chances of\n // this happening when multiple instances are started simultaneously\n // is reduced by randomizing the expiry time (see isStale() below).\n // The best solution is to use a mutex/lock to ensure that only one\n // instance is refreshing the token at a time (runtime.usingLock) but\n // that is not always possible. Let's try to recover from concurrency\n // issues, or force the session to be deleted by throwing a\n // TokenRefreshError.\n if (\n cause instanceof OAuthResponseError &&\n cause.status === 400 &&\n cause.error === 'invalid_grant'\n ) {\n // In case there is no lock implementation in the runtime, we will\n // wait for a short time to give the other concurrent instances a\n // chance to finish their refreshing of the token. If a concurrent\n // refresh did occur, we will pretend that this one succeeded.\n if (!runtime.hasImplementationLock) {\n await new Promise((r) => setTimeout(r, 1000))\n\n const stored = await this.getStored(sub)\n if (stored === undefined) {\n // A concurrent refresh occurred and caused the session to be\n // deleted (for a reason we can't know at this point).\n\n // Using a distinct error message mainly for debugging\n // purposes. Also, throwing a TokenRefreshError to trigger\n // deletion through the deleteOnError callback.\n const msg = 'The session was deleted by another process'\n throw new TokenRefreshError(sub, msg, { cause })\n } else if (\n stored.tokenSet.access_token !== tokenSet.access_token ||\n stored.tokenSet.refresh_token !== tokenSet.refresh_token\n ) {\n // A concurrent refresh occurred. Pretend this one succeeded.\n return stored\n } else {\n // There were no concurrent refresh. The token is (likely)\n // simply no longer valid.\n }\n }\n\n // Make sure the session gets deleted from the store\n const msg = cause.errorDescription ?? 'The session was revoked'\n throw new TokenRefreshError(sub, msg, { cause })\n }\n\n throw cause\n }\n },\n sessionStore,\n {\n isStale: (sub, { tokenSet }) => {\n return (\n tokenSet.expires_at != null &&\n new Date(tokenSet.expires_at).getTime() <\n Date.now() +\n // Add some lee way to ensure the token is not expired when it\n // reaches the server.\n 10e3 +\n // Add some randomness to reduce the chances of multiple\n // instances trying to refresh the token at the same.\n 30e3 * Math.random()\n )\n },\n onStoreError: async (err, sub, { tokenSet, dpopKey, authMethod }) => {\n // If the token data cannot be stored, let's revoke it\n try {\n const server = await serverFactory.fromIssuer(\n tokenSet.iss,\n authMethod,\n dpopKey,\n )\n await server.revoke(tokenSet.refresh_token ?? tokenSet.access_token)\n } catch {\n // At least we tried...\n }\n\n // Attempt to delete the session from the store. Note that this might\n // fail if the store is not available, which is fine.\n try {\n await this.delStored(sub, err)\n } catch {\n // Ignore (better to propagate the original storage error)\n }\n\n throw err\n },\n deleteOnError: isExpectedSessionError,\n },\n )\n }\n\n override async getStored(\n sub: AtprotoDid,\n options?: GetOptions,\n ): Promise<Session | undefined> {\n return super.getStored(sub, options)\n }\n\n override async setStored(sub: AtprotoDid, session: Session) {\n // Prevent tampering with the stored value\n if (sub !== session.tokenSet.sub) {\n throw new TypeError('Token set does not match the expected sub')\n }\n await super.setStored(sub, session)\n await this.hooks.onUpdate?.call(null, sub, session)\n }\n\n override async delStored(sub: AtprotoDid, cause?: unknown): Promise<void> {\n await super.delStored(sub, cause)\n await this.hooks.onDelete?.call(null, sub, cause)\n }\n\n /**\n * @deprecated Use {@link getSession} instead\n * @internal (not really deprecated)\n */\n override async get(\n sub: AtprotoDid,\n options?: GetCachedOptions,\n ): Promise<Session> {\n const session = await this.runtime.usingLock(\n `@atproto-oauth-client-${sub}`,\n async () => {\n // Make sure, even if there is no signal in the options, that the\n // request will be cancelled after at most 30 seconds.\n const signal = AbortSignal.timeout(30e3)\n\n using abortController = combineSignals([options?.signal, signal])\n\n return await super.get(sub, {\n ...options,\n signal: abortController.signal,\n })\n },\n )\n\n if (sub !== session.tokenSet.sub) {\n // Fool-proofing (e.g. against invalid session storage)\n throw new Error('Token set does not match the expected sub')\n }\n\n return session\n }\n\n /**\n * @param refresh When `true`, the credentials will be refreshed even if they\n * are not expired. When `false`, the credentials will not be refreshed even\n * if they are expired. When `undefined`, the credentials will be refreshed\n * if, and only if, they are (about to be) expired. Defaults to `undefined`.\n */\n async getSession(sub: AtprotoDid, refresh: boolean | 'auto' = 'auto') {\n return this.get(sub, {\n noCache: refresh === true,\n allowStale: refresh === false,\n })\n }\n}\n"]}
@@ -4,10 +4,20 @@ import { ClientAuthMethod } from './oauth-client-auth.js';
4
4
  export type InternalStateData = {
5
5
  iss: string;
6
6
  dpopKey: Key;
7
- /** @note optional for legacy reasons */
8
- authMethod?: ClientAuthMethod;
9
- verifier?: string;
7
+ authMethod: ClientAuthMethod;
8
+ verifier: string;
10
9
  appState?: string;
11
10
  };
11
+ /**
12
+ * A store pending oauth authorization flows. The key is the "state" parameter
13
+ * used in the authorization request, and the value is an object containing the
14
+ * necessary information to complete the flow once the user is redirected back
15
+ * to the client.
16
+ *
17
+ * @note The data stored in this store is typically short-lived. It should be
18
+ * automatically cleared after a certain period of time (e.g. 1 hour) to prevent
19
+ * the store from growing indefinitely. It is up to the implementation to
20
+ * implement this cleanup mechanism.
21
+ */
12
22
  export type StateStore = SimpleStore<string, InternalStateData>;
13
23
  //# sourceMappingURL=state-store.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"state-store.d.ts","sourceRoot":"","sources":["../src/state-store.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,cAAc,CAAA;AAClC,OAAO,EAAE,WAAW,EAAE,MAAM,4BAA4B,CAAA;AACxD,OAAO,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAA;AAEzD,MAAM,MAAM,iBAAiB,GAAG;IAC9B,GAAG,EAAE,MAAM,CAAA;IACX,OAAO,EAAE,GAAG,CAAA;IACZ,wCAAwC;IACxC,UAAU,CAAC,EAAE,gBAAgB,CAAA;IAC7B,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,QAAQ,CAAC,EAAE,MAAM,CAAA;CAClB,CAAA;AAED,MAAM,MAAM,UAAU,GAAG,WAAW,CAAC,MAAM,EAAE,iBAAiB,CAAC,CAAA"}
1
+ {"version":3,"file":"state-store.d.ts","sourceRoot":"","sources":["../src/state-store.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,cAAc,CAAA;AAClC,OAAO,EAAE,WAAW,EAAE,MAAM,4BAA4B,CAAA;AACxD,OAAO,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAA;AAEzD,MAAM,MAAM,iBAAiB,GAAG;IAC9B,GAAG,EAAE,MAAM,CAAA;IACX,OAAO,EAAE,GAAG,CAAA;IACZ,UAAU,EAAE,gBAAgB,CAAA;IAC5B,QAAQ,EAAE,MAAM,CAAA;IAChB,QAAQ,CAAC,EAAE,MAAM,CAAA;CAClB,CAAA;AAED;;;;;;;;;;GAUG;AACH,MAAM,MAAM,UAAU,GAAG,WAAW,CAAC,MAAM,EAAE,iBAAiB,CAAC,CAAA"}
@@ -1 +1 @@
1
- {"version":3,"file":"state-store.js","sourceRoot":"","sources":["../src/state-store.ts"],"names":[],"mappings":"","sourcesContent":["import { Key } from '@atproto/jwk'\nimport { SimpleStore } from '@atproto-labs/simple-store'\nimport { ClientAuthMethod } from './oauth-client-auth.js'\n\nexport type InternalStateData = {\n iss: string\n dpopKey: Key\n /** @note optional for legacy reasons */\n authMethod?: ClientAuthMethod\n verifier?: string\n appState?: string\n}\n\nexport type StateStore = SimpleStore<string, InternalStateData>\n"]}
1
+ {"version":3,"file":"state-store.js","sourceRoot":"","sources":["../src/state-store.ts"],"names":[],"mappings":"","sourcesContent":["import { Key } from '@atproto/jwk'\nimport { SimpleStore } from '@atproto-labs/simple-store'\nimport { ClientAuthMethod } from './oauth-client-auth.js'\n\nexport type InternalStateData = {\n iss: string\n dpopKey: Key\n authMethod: ClientAuthMethod\n verifier: string\n appState?: string\n}\n\n/**\n * A store pending oauth authorization flows. The key is the \"state\" parameter\n * used in the authorization request, and the value is an object containing the\n * necessary information to complete the flow once the user is redirected back\n * to the client.\n *\n * @note The data stored in this store is typically short-lived. It should be\n * automatically cleared after a certain period of time (e.g. 1 hour) to prevent\n * the store from growing indefinitely. It is up to the implementation to\n * implement this cleanup mechanism.\n */\nexport type StateStore = SimpleStore<string, InternalStateData>\n"]}
package/dist/util.d.ts CHANGED
@@ -4,15 +4,5 @@ export type Simplify<T> = {
4
4
  } & NonNullable<unknown>;
5
5
  export declare const ifString: <V>(v: V) => (V & string) | undefined;
6
6
  export declare function contentMime(headers: Headers): string | undefined;
7
- /**
8
- * Ponyfill for `CustomEvent` constructor.
9
- */
10
- export declare const CustomEvent: typeof globalThis.CustomEvent;
11
- export declare class CustomEventTarget<EventDetailMap extends Record<string, unknown>> {
12
- readonly eventTarget: EventTarget;
13
- addEventListener<T extends Extract<keyof EventDetailMap, string>>(type: T, callback: (event: CustomEvent<EventDetailMap[T]>) => void, options?: AddEventListenerOptions | boolean): void;
14
- removeEventListener<T extends Extract<keyof EventDetailMap, string>>(type: T, callback: (event: CustomEvent<EventDetailMap[T]>) => void, options?: EventListenerOptions | boolean): void;
15
- dispatchCustomEvent<T extends Extract<keyof EventDetailMap, string>>(type: T, detail: EventDetailMap[T], init?: EventInit): boolean;
16
- }
17
7
  export declare function combineSignals(signals: readonly (AbortSignal | undefined)[]): AbortController & Disposable;
18
8
  //# sourceMappingURL=util.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"util.d.ts","sourceRoot":"","sources":["../src/util.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,SAAS,CAAC,CAAC,IAAI,CAAC,GAAG,WAAW,CAAC,CAAC,CAAC,CAAA;AAC7C,MAA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