supaapps-auth 2.0.0-rc.7 → 2.0.0-rc.9
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/dist/AuthManager.d.ts +4 -3
- package/dist/AuthManager.js +31 -17
- package/dist/types.d.ts +1 -1
- package/package.json +2 -2
- package/src/AuthManager.ts +36 -18
- package/src/types.ts +1 -1
package/dist/AuthManager.d.ts
CHANGED
|
@@ -11,15 +11,16 @@ export declare class AuthManager {
|
|
|
11
11
|
private tokenToPayload;
|
|
12
12
|
private toBase64Url;
|
|
13
13
|
private generatePKCEPair;
|
|
14
|
-
refreshAccessToken(
|
|
14
|
+
refreshAccessToken(isInitialization?: boolean): Promise<string>;
|
|
15
15
|
checkAccessToken(isInitilization?: boolean): Promise<string>;
|
|
16
16
|
private isTokenExpired;
|
|
17
17
|
mustBeLoggedIn(): Promise<void>;
|
|
18
18
|
getLoginWithGoogleUri(): string;
|
|
19
19
|
isLoggedIn(): Promise<boolean>;
|
|
20
20
|
getAccessToken(mustBeLoggedIn?: boolean): Promise<string>;
|
|
21
|
-
|
|
22
|
-
|
|
21
|
+
platformCheck(email: string, token: string): Promise<boolean>;
|
|
22
|
+
verifyEmail(email: string, token: string): Promise<boolean>;
|
|
23
|
+
doPassReset(email: string, token: string, newPassword: string): Promise<boolean>;
|
|
23
24
|
changeEmail(email: string): Promise<boolean>;
|
|
24
25
|
initPasswordReset(email: string): Promise<boolean>;
|
|
25
26
|
changePassword(oldPassword: string, newPassword: string, email: string): Promise<boolean>;
|
package/dist/AuthManager.js
CHANGED
|
@@ -63,7 +63,7 @@ class AuthManager {
|
|
|
63
63
|
return { verifier, challenge };
|
|
64
64
|
}
|
|
65
65
|
refreshAccessToken() {
|
|
66
|
-
return __awaiter(this, arguments, void 0, function* (
|
|
66
|
+
return __awaiter(this, arguments, void 0, function* (isInitialization = false) {
|
|
67
67
|
try {
|
|
68
68
|
const refreshToken = localStorage.getItem('refresh_token');
|
|
69
69
|
if (!refreshToken) {
|
|
@@ -79,7 +79,7 @@ class AuthManager {
|
|
|
79
79
|
console.error(`Refresh token error, logging out: ${error}`);
|
|
80
80
|
localStorage.removeItem('access_token');
|
|
81
81
|
localStorage.removeItem('refresh_token');
|
|
82
|
-
if (!
|
|
82
|
+
if (!isInitialization) {
|
|
83
83
|
// throw refresh fail only if not initialization
|
|
84
84
|
this.onStateChange({ type: types_1.AuthEventType.REFRESH_FAILED });
|
|
85
85
|
}
|
|
@@ -139,12 +139,24 @@ class AuthManager {
|
|
|
139
139
|
}
|
|
140
140
|
});
|
|
141
141
|
}
|
|
142
|
-
|
|
142
|
+
platformCheck(email, token) {
|
|
143
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
144
|
+
const response = yield axios_1.default.post(`${this.authServer}auth/email/platform_check`, {
|
|
145
|
+
realm_name: this.realmName,
|
|
146
|
+
email,
|
|
147
|
+
});
|
|
148
|
+
if (response.data.error || response.data.errors) {
|
|
149
|
+
throw new Error(response.data.error || response.data.message);
|
|
150
|
+
}
|
|
151
|
+
return (response.status === 200) ? response.data : { 'platforms': [] };
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
verifyEmail(email, token) {
|
|
143
155
|
return __awaiter(this, void 0, void 0, function* () {
|
|
144
156
|
const response = yield axios_1.default.post(`${this.authServer}auth/email/verify`, {
|
|
145
157
|
realm_name: this.realmName,
|
|
146
158
|
email,
|
|
147
|
-
|
|
159
|
+
token,
|
|
148
160
|
});
|
|
149
161
|
if (response.data.error || response.data.errors) {
|
|
150
162
|
throw new Error(response.data.error || response.data.message);
|
|
@@ -152,11 +164,13 @@ class AuthManager {
|
|
|
152
164
|
return response.status === 200;
|
|
153
165
|
});
|
|
154
166
|
}
|
|
155
|
-
doPassReset(email,
|
|
167
|
+
doPassReset(email, token, newPassword) {
|
|
156
168
|
return __awaiter(this, void 0, void 0, function* () {
|
|
157
169
|
const response = yield axios_1.default.post(`${this.authServer}auth/email/do_pass_reset`, {
|
|
158
170
|
realm_name: this.realmName,
|
|
159
171
|
email,
|
|
172
|
+
token,
|
|
173
|
+
new_password: newPassword,
|
|
160
174
|
});
|
|
161
175
|
if (response.data.error || response.data.errors) {
|
|
162
176
|
throw new Error(response.data.error || response.data.message);
|
|
@@ -306,23 +320,23 @@ class AuthManager {
|
|
|
306
320
|
throw new Error('Not a valid jwt token');
|
|
307
321
|
}
|
|
308
322
|
const userToken = {
|
|
309
|
-
id: decodedToken
|
|
310
|
-
iss: decodedToken
|
|
311
|
-
sub: parseInt(decodedToken
|
|
312
|
-
first_name: decodedToken
|
|
313
|
-
last_name: decodedToken
|
|
314
|
-
email: decodedToken
|
|
315
|
-
aud: decodedToken
|
|
316
|
-
iat: decodedToken
|
|
317
|
-
exp: decodedToken
|
|
318
|
-
scopes: decodedToken
|
|
319
|
-
realm: decodedToken
|
|
323
|
+
id: decodedToken.id,
|
|
324
|
+
iss: decodedToken.iss,
|
|
325
|
+
sub: typeof decodedToken.sub === 'string' ? parseInt(decodedToken.sub) : decodedToken.sub,
|
|
326
|
+
first_name: decodedToken.first_name,
|
|
327
|
+
last_name: decodedToken.last_name,
|
|
328
|
+
email: decodedToken.email,
|
|
329
|
+
aud: decodedToken.aud,
|
|
330
|
+
iat: decodedToken.iat,
|
|
331
|
+
exp: decodedToken.exp,
|
|
332
|
+
scopes: decodedToken.scopes,
|
|
333
|
+
realm: decodedToken.realm,
|
|
320
334
|
};
|
|
321
335
|
const { data: publicKey } = yield axios_1.default.get(`${authServer}public/public_key`);
|
|
322
336
|
const { data: algo } = yield axios_1.default.get(`${authServer}public/algo`);
|
|
323
337
|
(0, jsonwebtoken_1.verify)(bearerToken, publicKey, { algorithms: [algo] });
|
|
324
338
|
const { data: revokedIds } = yield axios_1.default.get(`${authServer}public/revoked_ids`);
|
|
325
|
-
if (revokedIds.includes(decodedToken
|
|
339
|
+
if (revokedIds.includes(decodedToken.id)) {
|
|
326
340
|
throw new Error('Token is revoked');
|
|
327
341
|
}
|
|
328
342
|
return userToken;
|
package/dist/types.d.ts
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "supaapps-auth",
|
|
3
|
-
"version": "2.0.0-rc.
|
|
3
|
+
"version": "2.0.0-rc.9",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
"@typescript-eslint/eslint-plugin": "^6.21.0",
|
|
26
26
|
"@typescript-eslint/parser": "^6.21.0",
|
|
27
27
|
"axios-mock-adapter": "^1.22.0",
|
|
28
|
-
"eslint": "^8.57.
|
|
28
|
+
"eslint": "^8.57.1",
|
|
29
29
|
"eslint-config-airbnb-base": "^15.0.0",
|
|
30
30
|
"eslint-config-airbnb-typescript": "^17.1.0",
|
|
31
31
|
"eslint-config-next": "^13.5.6",
|
package/src/AuthManager.ts
CHANGED
|
@@ -95,7 +95,7 @@ export class AuthManager {
|
|
|
95
95
|
return { verifier, challenge };
|
|
96
96
|
}
|
|
97
97
|
|
|
98
|
-
public async refreshAccessToken(
|
|
98
|
+
public async refreshAccessToken(isInitialization: boolean = false): Promise<string> {
|
|
99
99
|
try {
|
|
100
100
|
const refreshToken = localStorage.getItem('refresh_token');
|
|
101
101
|
if (!refreshToken) {
|
|
@@ -114,7 +114,7 @@ export class AuthManager {
|
|
|
114
114
|
console.error(`Refresh token error, logging out: ${error}`);
|
|
115
115
|
localStorage.removeItem('access_token');
|
|
116
116
|
localStorage.removeItem('refresh_token');
|
|
117
|
-
if (!
|
|
117
|
+
if (!isInitialization) {
|
|
118
118
|
// throw refresh fail only if not initialization
|
|
119
119
|
this.onStateChange({ type: AuthEventType.REFRESH_FAILED });
|
|
120
120
|
}
|
|
@@ -170,13 +170,29 @@ export class AuthManager {
|
|
|
170
170
|
}
|
|
171
171
|
}
|
|
172
172
|
|
|
173
|
-
|
|
173
|
+
|
|
174
|
+
public async platformCheck(email: string, token: string): Promise<boolean> {
|
|
175
|
+
const response = await axios.post(
|
|
176
|
+
`${this.authServer}auth/email/platform_check`,
|
|
177
|
+
{
|
|
178
|
+
realm_name: this.realmName,
|
|
179
|
+
email,
|
|
180
|
+
},
|
|
181
|
+
);
|
|
182
|
+
if (response.data.error || response.data.errors) {
|
|
183
|
+
throw new Error(response.data.error || response.data.message);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
return (response.status === 200) ? response.data : {'platforms': []};
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
public async verifyEmail(email: string, token: string): Promise<boolean> {
|
|
174
190
|
const response = await axios.post(
|
|
175
191
|
`${this.authServer}auth/email/verify`,
|
|
176
192
|
{
|
|
177
193
|
realm_name: this.realmName,
|
|
178
194
|
email,
|
|
179
|
-
|
|
195
|
+
token,
|
|
180
196
|
},
|
|
181
197
|
);
|
|
182
198
|
if (response.data.error || response.data.errors) {
|
|
@@ -186,12 +202,14 @@ export class AuthManager {
|
|
|
186
202
|
return response.status === 200;
|
|
187
203
|
}
|
|
188
204
|
|
|
189
|
-
public async doPassReset(email: string,
|
|
205
|
+
public async doPassReset(email: string, token: string, newPassword: string): Promise<boolean> {
|
|
190
206
|
const response = await axios.post(
|
|
191
207
|
`${this.authServer}auth/email/do_pass_reset`,
|
|
192
208
|
{
|
|
193
209
|
realm_name: this.realmName,
|
|
194
210
|
email,
|
|
211
|
+
token,
|
|
212
|
+
new_password: newPassword,
|
|
195
213
|
},
|
|
196
214
|
);
|
|
197
215
|
if (response.data.error || response.data.errors) {
|
|
@@ -369,24 +387,24 @@ export class AuthManager {
|
|
|
369
387
|
// @todo add caching for public key and algo
|
|
370
388
|
const decodedToken = jwtDecode(bearerToken, {
|
|
371
389
|
complete: true,
|
|
372
|
-
})?.payload;
|
|
390
|
+
})?.payload as unknown as UserTokenPayload;
|
|
373
391
|
|
|
374
392
|
if (!decodedToken) {
|
|
375
393
|
throw new Error('Not a valid jwt token');
|
|
376
394
|
}
|
|
377
395
|
|
|
378
396
|
const userToken: UserTokenPayload = {
|
|
379
|
-
id: decodedToken
|
|
380
|
-
iss: decodedToken
|
|
381
|
-
sub: parseInt(decodedToken
|
|
382
|
-
first_name: decodedToken
|
|
383
|
-
last_name: decodedToken
|
|
384
|
-
email: decodedToken
|
|
385
|
-
aud: decodedToken
|
|
386
|
-
iat: decodedToken
|
|
387
|
-
exp: decodedToken
|
|
388
|
-
scopes: decodedToken
|
|
389
|
-
realm: decodedToken
|
|
397
|
+
id: decodedToken.id,
|
|
398
|
+
iss: decodedToken.iss,
|
|
399
|
+
sub: typeof decodedToken.sub === 'string' ? parseInt(decodedToken.sub) : decodedToken.sub,
|
|
400
|
+
first_name: decodedToken.first_name,
|
|
401
|
+
last_name: decodedToken.last_name,
|
|
402
|
+
email: decodedToken.email,
|
|
403
|
+
aud: decodedToken.aud,
|
|
404
|
+
iat: decodedToken.iat,
|
|
405
|
+
exp: decodedToken.exp,
|
|
406
|
+
scopes: decodedToken.scopes,
|
|
407
|
+
realm: decodedToken.realm,
|
|
390
408
|
}
|
|
391
409
|
|
|
392
410
|
const { data: publicKey } = await axios.get(
|
|
@@ -401,7 +419,7 @@ export class AuthManager {
|
|
|
401
419
|
const { data: revokedIds } = await axios.get(
|
|
402
420
|
`${authServer}public/revoked_ids`,
|
|
403
421
|
);
|
|
404
|
-
if(revokedIds.includes(decodedToken
|
|
422
|
+
if(revokedIds.includes(decodedToken.id)){
|
|
405
423
|
throw new Error('Token is revoked');
|
|
406
424
|
}
|
|
407
425
|
return userToken;
|