supaapps-auth 1.0.3 → 1.0.5

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.
@@ -3,10 +3,12 @@ export declare class AuthManager {
3
3
  private readonly authServer;
4
4
  private readonly realmName;
5
5
  private readonly redirectUri;
6
- constructor(authServer: string, realmName: string, redirectUri: string);
6
+ private readonly loginCallback;
7
+ constructor(authServer: string, realmName: string, redirectUri: string, loginCallback: () => void);
7
8
  static getInstance<T>(): AuthManager;
8
9
  private toBase64Url;
9
10
  private generatePKCEPair;
11
+ mustBeLoggedIn(): Promise<void>;
10
12
  getLoginWithGoogleUri(): string;
11
13
  isLoggedIn(): Promise<boolean>;
12
14
  getAccessToken(): Promise<string>;
@@ -12,10 +12,11 @@ Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.AuthManager = void 0;
13
13
  const crypto_1 = require("crypto");
14
14
  class AuthManager {
15
- constructor(authServer, realmName, redirectUri) {
15
+ constructor(authServer, realmName, redirectUri, loginCallback) {
16
16
  this.authServer = null;
17
17
  this.realmName = null;
18
18
  this.redirectUri = null;
19
+ this.loginCallback = () => { };
19
20
  this.toBase64Url = (base64String) => {
20
21
  return base64String
21
22
  .replace(/\+/g, '-')
@@ -26,17 +27,18 @@ class AuthManager {
26
27
  const NUM_OF_BYTES = 32; // This will generate a verifier of sufficient length
27
28
  const HASH_ALG = 'sha256';
28
29
  // Generate code verifier
29
- const codeVerifier = this.toBase64Url((0, crypto_1.randomBytes)(NUM_OF_BYTES).toString('base64'));
30
+ const newCodeVerifier = this.toBase64Url((0, crypto_1.randomBytes)(NUM_OF_BYTES).toString('base64'));
30
31
  // Generate code challenge
31
32
  const hash = (0, crypto_1.createHash)(HASH_ALG)
32
- .update(codeVerifier)
33
+ .update(newCodeVerifier)
33
34
  .digest('base64');
34
- const codeChallenge = this.toBase64Url(hash);
35
- return { codeVerifier, codeChallenge };
35
+ const newCodeChallenge = this.toBase64Url(hash);
36
+ return { newCodeVerifier, newCodeChallenge };
36
37
  };
37
38
  this.authServer = authServer;
38
39
  this.realmName = realmName;
39
40
  this.redirectUri = redirectUri;
41
+ this.loginCallback = loginCallback;
40
42
  AuthManager.instance = this;
41
43
  }
42
44
  static getInstance() {
@@ -45,8 +47,18 @@ class AuthManager {
45
47
  }
46
48
  return AuthManager.instance;
47
49
  }
50
+ mustBeLoggedIn() {
51
+ return __awaiter(this, void 0, void 0, function* () {
52
+ if (!(yield this.isLoggedIn())) {
53
+ this.loginCallback();
54
+ }
55
+ });
56
+ }
48
57
  getLoginWithGoogleUri() {
49
- const { codeVerifier, codeChallenge } = this.generatePKCEPair();
58
+ // get or create codeVerifier and codeChallenge from localstorage
59
+ const { newCodeVerifier, newCodeChallenge } = this.generatePKCEPair();
60
+ let codeVerifier = localStorage.getItem('codeVerifier') || newCodeVerifier;
61
+ let codeChallenge = localStorage.getItem('codeChallenge') || newCodeChallenge;
50
62
  localStorage.setItem('codeVerifier', codeVerifier);
51
63
  localStorage.setItem('codeChallenge', codeChallenge);
52
64
  if (this.authServer && this.realmName && this.redirectUri) {
@@ -128,6 +140,8 @@ class AuthManager {
128
140
  }),
129
141
  })
130
142
  .then((response) => {
143
+ localStorage.removeItem('codeVerifier');
144
+ localStorage.removeItem('codeChallenge');
131
145
  if (response.status !== 200) {
132
146
  throw new Error('Failed to exchange code for token');
133
147
  }
@@ -139,6 +153,8 @@ class AuthManager {
139
153
  resolve();
140
154
  })
141
155
  .catch((error) => {
156
+ localStorage.removeItem('codeVerifier');
157
+ localStorage.removeItem('codeChallenge');
142
158
  reject(error);
143
159
  });
144
160
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "supaapps-auth",
3
- "version": "1.0.3",
3
+ "version": "1.0.5",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -9,10 +9,13 @@ export class AuthManager {
9
9
  private readonly realmName: string | null = null;
10
10
 
11
11
  private readonly redirectUri: string | null = null;
12
- public constructor(authServer: string, realmName: string, redirectUri: string) {
12
+ private readonly loginCallback: () => void = () => {};
13
+
14
+ public constructor(authServer: string, realmName: string, redirectUri: string, loginCallback: () => void) {
13
15
  this.authServer = authServer;
14
16
  this.realmName = realmName;
15
17
  this.redirectUri = redirectUri;
18
+ this.loginCallback = loginCallback;
16
19
  AuthManager.instance = this;
17
20
  }
18
21
 
@@ -34,20 +37,29 @@ export class AuthManager {
34
37
  const HASH_ALG = 'sha256';
35
38
 
36
39
  // Generate code verifier
37
- const codeVerifier = this.toBase64Url(
40
+ const newCodeVerifier = this.toBase64Url(
38
41
  randomBytes(NUM_OF_BYTES).toString('base64'),
39
42
  );
40
43
 
41
44
  // Generate code challenge
42
45
  const hash = createHash(HASH_ALG)
43
- .update(codeVerifier)
46
+ .update(newCodeVerifier)
44
47
  .digest('base64');
45
- const codeChallenge = this.toBase64Url(hash);
48
+ const newCodeChallenge = this.toBase64Url(hash);
46
49
 
47
- return { codeVerifier, codeChallenge };
50
+ return { newCodeVerifier, newCodeChallenge };
48
51
  };
52
+
53
+ public async mustBeLoggedIn(): Promise<void> {
54
+ if (!await this.isLoggedIn()) {
55
+ this.loginCallback();
56
+ }
57
+ }
49
58
  public getLoginWithGoogleUri(): string {
50
- const { codeVerifier, codeChallenge } = this.generatePKCEPair();
59
+ // get or create codeVerifier and codeChallenge from localstorage
60
+ const { newCodeVerifier, newCodeChallenge } = this.generatePKCEPair();
61
+ let codeVerifier = localStorage.getItem('codeVerifier') || newCodeVerifier;
62
+ let codeChallenge = localStorage.getItem('codeChallenge') || newCodeChallenge;
51
63
  localStorage.setItem('codeVerifier', codeVerifier);
52
64
  localStorage.setItem('codeChallenge', codeChallenge);
53
65
 
@@ -127,6 +139,8 @@ export class AuthManager {
127
139
  }),
128
140
  })
129
141
  .then((response) => {
142
+ localStorage.removeItem('codeVerifier');
143
+ localStorage.removeItem('codeChallenge');
130
144
  if (response.status !== 200) {
131
145
  throw new Error('Failed to exchange code for token');
132
146
  }
@@ -138,6 +152,8 @@ export class AuthManager {
138
152
  resolve();
139
153
  })
140
154
  .catch((error) => {
155
+ localStorage.removeItem('codeVerifier');
156
+ localStorage.removeItem('codeChallenge');
141
157
  reject(error);
142
158
  });
143
159
  }