@usync/oauth2 0.0.3 → 0.0.4

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/README.md CHANGED
@@ -4,4 +4,8 @@
4
4
  ![License](https://img.shields.io/npm/l/@usync/oauth2.svg)
5
5
  [![jsDocs.io](https://img.shields.io/badge/jsDocs.io-reference-blue)](https://www.jsdocs.io/package/@usync/oauth2)
6
6
 
7
- Handling OAuth2 login and generate access tokens.
7
+ OAuth2 support for provider-based authorization flows and token management.
8
+
9
+ This package focuses on the moving parts around OAuth2: turning provider configuration into authorizers, refreshing access tokens when needed, and completing interactive login flows when a user has not yet authorized access.
10
+
11
+ The design keeps authorization state and token handling separate from drive or sync logic, so storage adapters can reuse the same auth machinery without duplicating protocol code.
package/dist/index.d.ts CHANGED
@@ -1,7 +1,7 @@
1
- import { OAuth2Authorizer } from './providers';
2
- import { IOAuth2Account, IOAuth2Options } from './types';
3
- export * from './common';
4
- export * from './providers';
5
- export * from './types';
1
+ import { OAuth2Authorizer } from "./providers";
2
+ import type { IOAuth2Account, IOAuth2Options } from "./types";
3
+ export * from "./common";
4
+ export * from "./providers";
5
+ export * from "./types";
6
6
  export declare function getAuthorizer(options: IOAuth2Options, auth: IOAuth2Account): import("./providers").DropboxAuthorizer | import("./providers").GoogleAuthorizer | import("./providers").MicrosoftAuthorizer;
7
7
  export declare function ensureAccessToken(authorizer: OAuth2Authorizer, handleOAuth2?: (url: string) => Promise<string>): Promise<string>;
package/dist/index.js CHANGED
@@ -7,7 +7,7 @@ var t = 1, n = 2, r = 3, i = class extends Error {
7
7
  };
8
8
  h();
9
9
  //#endregion
10
- //#region ../../node_modules/.pnpm/bignumber.js@11.1.1/node_modules/bignumber.js/dist/bignumber.mjs
10
+ //#region ../../node_modules/.pnpm/bignumber.js@11.1.3/node_modules/bignumber.js/dist/bignumber.mjs
11
11
  var a = /^-?(?:\d+(?:\.\d*)?|\.\d+)(?:e[+-]?\d+)?$/i, o = Math.ceil, s = Math.floor, c = "[BigNumber Error] ", l = 0x5af3107a4000, u = 14, d = 9007199254740991, f = [
12
12
  1,
13
13
  10,
@@ -499,25 +499,25 @@ function h(e) {
499
499
  }, T.toFixed = function(e, t) {
500
500
  return H(this, e == null ? e : y(e, -m, m) + this.e + 1, t);
501
501
  }, T.toFormat = function(e, t, n) {
502
- var r, i, a, o = this;
502
+ var r, i, a, o, s = this;
503
503
  if (n == null) n = L, e != null && (t == null ? typeof e == "object" && !b(e) && (n = K(e), e = t = null) : typeof t == "object" && (n = K(t), t = null));
504
504
  else if (typeof n != "object") throw Error(c + "Argument not an object: " + n);
505
505
  else n = K(n);
506
506
  if (e != null) if (b(e) && e.length <= 2) {
507
- if (r = e[0], i = e[1], e = o.dp(), i != null && e > y(i, 0, m) && (e = i), r != null && y(r, 0, m) !== 0) {
508
- if (i != null && r > i) throw Error(c + "Minimum must not exceed maximum");
509
- e < r && (e = r);
507
+ if (i = e[0], a = e[1], e = s.dp(), a != null && e > y(a, 0, m) && (e = a), i != null && y(i, 0, m) !== 0) {
508
+ if (a != null && i > a) throw Error(c + "Minimum must not exceed maximum");
509
+ e < i && (e = i);
510
510
  }
511
511
  } else y(e, -m, m);
512
- if (a = o.toFixed(e, t), o.s < 0 && (a = a.slice(1)), o.c) {
513
- var s, l = a.split("."), u = +n.groupSize, d = +n.secondaryGroupSize, f = n.groupSeparator || "", p = l[0], h = l[1], g = p.length;
514
- if (d && (s = u, u = d, d = s, g -= s), u > 0 && g > 0) {
515
- for (s = g % u || u, a = p.substr(0, s); s < g; s += u) a += f + p.substr(s, u);
516
- d > 0 && (a += f + p.slice(s));
512
+ if (o = s.toFixed(e, t), r = o.charCodeAt(0) === 45, r && (o = o.slice(1)), s.c) {
513
+ var l, u = o.split("."), d = +n.groupSize, f = +n.secondaryGroupSize, p = n.groupSeparator || "", h = u[0], g = u[1], _ = h.length;
514
+ if (f && (l = d, d = f, f = l, _ -= l), d > 0 && _ > 0) {
515
+ for (l = _ % d || d, o = h.substr(0, l); l < _; l += d) o += p + h.substr(l, d);
516
+ f > 0 && (o += p + h.slice(l));
517
517
  }
518
- h && (s = +n.fractionGroupSize, s && (h = h.replace(RegExp("\\d{" + s + "}\\B", "g"), "$&" + (n.fractionGroupSeparator || ""))), a += (n.decimalSeparator || "") + h);
518
+ g && (l = +n.fractionGroupSize, l && (g = g.replace(RegExp("\\d{" + l + "}\\B", "g"), "$&" + (n.fractionGroupSeparator || ""))), o += (n.decimalSeparator || "") + g);
519
519
  }
520
- return (n.prefix || "") + (o.s < 0 ? n.negativeSign || "" : o.s > 0 && n.positiveSign || "") + a + (n.suffix || "");
520
+ return (n.prefix || "") + (r ? n.negativeSign || "" : s.s > 0 && n.positiveSign || "") + o + (n.suffix || "");
521
521
  }, T.toFraction = function(e) {
522
522
  var n, r, i, a, o, s, l, d, p, m, h, g, v = this, y = v.c;
523
523
  if (e != null && (l = new z(e), !l.isInteger() && (l.c || l.s !== 1) || l.lt(E))) throw Error(c + "Argument " + (l.isInteger() ? "out of range: " : "not an integer: ") + J(l));
@@ -590,7 +590,7 @@ function C(e, t, n) {
590
590
  return e;
591
591
  }
592
592
  //#endregion
593
- //#region ../../node_modules/.pnpm/@gera2ld+common@0.0.5/node_modules/@gera2ld/common/dist/index.js
593
+ //#region ../../node_modules/.pnpm/@gera2ld+common@0.0.20/node_modules/@gera2ld/common/dist/index.js
594
594
  var w = "=", T = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/", E;
595
595
  function D() {
596
596
  return E ||= T.slice(0, -2) + "-_", E;
@@ -629,14 +629,34 @@ async function M(e) {
629
629
  //#endregion
630
630
  //#region src/providers/base.ts
631
631
  var N = class {
632
- constructor(e) {
633
- this.options = e;
632
+ constructor(e, t) {
633
+ this.options = e, this._accessToken = null, this._refreshToken = null, t && (this.setRefreshToken(t.refreshToken), this.setAccessToken(t.accessToken));
634
+ }
635
+ _getValidToken(e) {
636
+ return e && (!e.expiresAt || Date.now() < e.expiresAt) ? e.token : void 0;
637
+ }
638
+ _updateAccessToken(e) {
639
+ this.setAccessToken(e), this.options.onSetAccessToken?.(e);
640
+ }
641
+ _updateRefreshToken(e) {
642
+ this.setRefreshToken(e), this.options.onSetRefreshToken?.(e);
643
+ }
644
+ getAccessToken() {
645
+ if (!this._accessToken) throw new i(3);
646
+ let e = this._getValidToken(this._accessToken);
647
+ if (!e) throw new i(1);
648
+ return e;
649
+ }
650
+ getRefreshToken() {
651
+ let e = this._getValidToken(this._refreshToken);
652
+ if (!e) throw new i(3, "Invalid refresh token");
653
+ return e;
654
+ }
655
+ setAccessToken(e) {
656
+ this._accessToken = e ?? null;
634
657
  }
635
- async getToken() {
636
- let e = await this.options.storage.get("accessToken");
637
- if (!e) throw new i(3);
638
- if (e.expiresAt < Date.now()) throw new i(1);
639
- return e.token;
658
+ setRefreshToken(e) {
659
+ this._refreshToken = e ?? null;
640
660
  }
641
661
  }, P = class extends N {
642
662
  static {
@@ -685,22 +705,21 @@ var N = class {
685
705
  data: a
686
706
  };
687
707
  if (!a.refresh_token) throw new i(2, "Failed to get refresh_token");
688
- return await this.options.storage.set("refreshToken", {
708
+ return this._updateRefreshToken({
689
709
  token: a.refresh_token,
690
710
  scope: a.scope
691
- }), await this.options.storage.set("accessToken", {
711
+ }), this._updateAccessToken({
692
712
  token: a.access_token,
693
713
  expiresAt: Date.now() + a.expires_in * 1e3
694
714
  }), a.access_token;
695
715
  }
696
716
  async refreshToken() {
697
- let e = new URLSearchParams(), t = await this.options.storage.get("refreshToken");
698
- if (!t) throw new i(3, "Invalid refresh token");
717
+ let e = new URLSearchParams(), t = this.getRefreshToken();
699
718
  Object.entries({
700
719
  client_id: this.options.clientId,
701
720
  client_secret: this.options.clientSecret,
702
721
  grant_type: "refresh_token",
703
- refresh_token: t.token
722
+ refresh_token: t
704
723
  }).forEach(([t, n]) => {
705
724
  e.append(t, n);
706
725
  });
@@ -712,7 +731,7 @@ var N = class {
712
731
  status: n.status,
713
732
  data: r
714
733
  };
715
- return await this.options.storage.set("accessToken", {
734
+ return this._updateAccessToken({
716
735
  token: r.access_token,
717
736
  expiresAt: Date.now() + r.expires_in * 1e3
718
737
  }), r.access_token;
@@ -770,22 +789,21 @@ var N = class {
770
789
  data: a
771
790
  };
772
791
  if (!a.refresh_token) throw new i(2, "Failed to get refresh_token");
773
- return await this.options.storage.set("refreshToken", {
792
+ return this._updateRefreshToken({
774
793
  token: a.refresh_token,
775
794
  scope: a.scope
776
- }), await this.options.storage.set("accessToken", {
795
+ }), this._updateAccessToken({
777
796
  token: a.access_token,
778
797
  expiresAt: Date.now() + a.expires_in * 1e3
779
798
  }), a.access_token;
780
799
  }
781
800
  async refreshToken() {
782
- let e = new URLSearchParams(), t = await this.options.storage.get("refreshToken");
783
- if (!t) throw new i(3, "Invalid refresh token");
801
+ let e = new URLSearchParams(), t = this.getRefreshToken();
784
802
  Object.entries({
785
803
  client_id: this.options.clientId,
786
804
  client_secret: this.options.clientSecret,
787
805
  grant_type: "refresh_token",
788
- refresh_token: t.token,
806
+ refresh_token: t,
789
807
  scope: this.options.scope
790
808
  }).forEach(([t, n]) => {
791
809
  n != null && e.append(t, n);
@@ -798,7 +816,7 @@ var N = class {
798
816
  status: n.status,
799
817
  data: r
800
818
  };
801
- return await this.options.storage.set("accessToken", {
819
+ return this._updateAccessToken({
802
820
  token: r.access_token,
803
821
  expiresAt: Date.now() + r.expires_in * 1e3
804
822
  }), r.access_token;
@@ -854,22 +872,21 @@ var N = class {
854
872
  data: a
855
873
  };
856
874
  if (!a.refresh_token) throw new i(2, "Failed to get refresh_token");
857
- return await this.options.storage.set("refreshToken", {
875
+ return this._updateRefreshToken({
858
876
  token: a.refresh_token,
859
877
  scope: a.scope
860
- }), await this.options.storage.set("accessToken", {
878
+ }), this._updateAccessToken({
861
879
  token: a.access_token,
862
880
  expiresAt: Date.now() + a.expires_in * 1e3
863
881
  }), a.access_token;
864
882
  }
865
883
  async refreshToken() {
866
- let e = new URLSearchParams(), t = await this.options.storage.get("refreshToken");
867
- if (!t) throw new i(3, "Invalid refresh token");
884
+ let e = new URLSearchParams(), t = this.getRefreshToken();
868
885
  Object.entries({
869
886
  client_id: this.options.clientId,
870
887
  client_secret: this.options.clientSecret,
871
888
  grant_type: "refresh_token",
872
- refresh_token: t.token,
889
+ refresh_token: t,
873
890
  scope: this.options.scope
874
891
  }).forEach(([t, n]) => {
875
892
  n && e.append(t, n);
@@ -882,7 +899,7 @@ var N = class {
882
899
  status: n.status,
883
900
  data: r
884
901
  };
885
- return await this.options.storage.set("accessToken", {
902
+ return this._updateAccessToken({
886
903
  token: r.access_token,
887
904
  expiresAt: Date.now() + r.expires_in * 1e3
888
905
  }), r.access_token;
@@ -900,7 +917,7 @@ function R(e, t) {
900
917
  async function z(e, t) {
901
918
  let n;
902
919
  try {
903
- n = await e.getToken();
920
+ n = e.getAccessToken();
904
921
  } catch (r) {
905
922
  if (!(r instanceof i)) throw r;
906
923
  switch (r.code) {
@@ -1,9 +1,20 @@
1
- import { IOAuth2Options } from "../types";
1
+ import type { IOAuth2Options, TokenData } from "../types";
2
2
  export declare abstract class OAuth2Authorizer {
3
3
  protected options: IOAuth2Options;
4
4
  abstract buildAuthUrl(): Promise<string>;
5
5
  abstract finishAuth(url: URL): Promise<string>;
6
6
  abstract refreshToken(): Promise<string>;
7
- constructor(options: IOAuth2Options);
8
- getToken(): Promise<string>;
7
+ protected _accessToken: TokenData | null;
8
+ protected _refreshToken: TokenData | null;
9
+ constructor(options: IOAuth2Options, initialData?: {
10
+ accessToken?: TokenData;
11
+ refreshToken?: TokenData;
12
+ });
13
+ protected _getValidToken(value?: TokenData | null): string | undefined;
14
+ protected _updateAccessToken(value: TokenData | null): void;
15
+ protected _updateRefreshToken(value: TokenData | null): void;
16
+ getAccessToken(): string;
17
+ getRefreshToken(): string;
18
+ setAccessToken(value?: TokenData | null): void;
19
+ setRefreshToken(value?: TokenData | null): void;
9
20
  }
@@ -1,7 +1,7 @@
1
- import { DropboxAuthorizer } from './dropbox';
2
- import { GoogleAuthorizer } from './google';
3
- import { MicrosoftAuthorizer } from './microsoft';
4
- export * from './base';
1
+ import { DropboxAuthorizer } from "./dropbox";
2
+ import { GoogleAuthorizer } from "./google";
3
+ import { MicrosoftAuthorizer } from "./microsoft";
4
+ export * from "./base";
5
5
  export { DropboxAuthorizer, GoogleAuthorizer, MicrosoftAuthorizer };
6
6
  export declare const OAuth2Authorizers: {
7
7
  dropbox: typeof DropboxAuthorizer;
package/dist/types.d.ts CHANGED
@@ -1,14 +1,16 @@
1
- import { OAuth2Authorizers } from './providers';
2
- export interface IStorage {
3
- get<T>(key: string): Promise<T | undefined>;
4
- set<T>(key: string, value: T): Promise<void>;
1
+ import { OAuth2Authorizers } from "./providers";
2
+ export interface TokenData {
3
+ token: string;
4
+ expiresAt?: number;
5
+ scope?: string;
5
6
  }
6
7
  export interface IOAuth2Options {
7
8
  clientId: string;
8
9
  clientSecret: string;
9
10
  redirectUrl: string;
10
- storage: IStorage;
11
11
  scope?: string;
12
+ onSetAccessToken?: (value: TokenData | null) => void;
13
+ onSetRefreshToken?: (value: TokenData | null) => void;
12
14
  }
13
15
  export interface IOAuth2Account {
14
16
  provider: keyof typeof OAuth2Authorizers;
package/package.json CHANGED
@@ -1,6 +1,9 @@
1
1
  {
2
2
  "name": "@usync/oauth2",
3
- "version": "0.0.3",
3
+ "version": "0.0.4",
4
+ "files": [
5
+ "dist"
6
+ ],
4
7
  "type": "module",
5
8
  "exports": {
6
9
  ".": {
@@ -8,15 +11,12 @@
8
11
  "types": "./dist/index.d.ts"
9
12
  }
10
13
  },
11
- "files": [
12
- "dist"
13
- ],
14
14
  "publishConfig": {
15
15
  "access": "public",
16
16
  "registry": "https://registry.npmjs.org/"
17
17
  },
18
18
  "dependencies": {
19
- "nanoid": "^5.1.6"
19
+ "nanoid": "^5.1.11"
20
20
  },
21
21
  "scripts": {
22
22
  "clean": "del-cli dist tsconfig.tsbuildinfo",