@dehwyyy/auth 1.0.3 → 1.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/dist/client/client.d.ts +1 -1
- package/dist/client/client.js +2 -2
- package/dist/client/middleware.d.ts +3 -1
- package/dist/client/middleware.js +9 -4
- package/dist/guard/index.d.ts +5 -14
- package/dist/guard/index.js +8 -27
- package/dist/index.d.ts +9 -15
- package/dist/index.js +25 -9
- package/dist/types.d.ts +16 -0
- package/dist/types.js +2 -0
- package/package.json +1 -1
package/dist/client/client.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import type { paths } from './schema';
|
|
2
|
-
export declare const getClient: (app: string, baseUrl: string) => import("openapi-fetch").Client<paths, `${string}/${string}`>;
|
|
2
|
+
export declare const getClient: (app: string, baseUrl: string, redirectBaseUrl: string, redirectUriPrefix?: string) => import("openapi-fetch").Client<paths, `${string}/${string}`>;
|
package/dist/client/client.js
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import createClient from 'openapi-fetch';
|
|
2
2
|
import { Middleware } from './middleware';
|
|
3
3
|
let client = null;
|
|
4
|
-
export const getClient = (app, baseUrl) => {
|
|
4
|
+
export const getClient = (app, baseUrl, redirectBaseUrl, redirectUriPrefix = "") => {
|
|
5
5
|
if (!client) {
|
|
6
6
|
client = createClient({
|
|
7
7
|
baseUrl,
|
|
8
8
|
credentials: 'include',
|
|
9
9
|
});
|
|
10
|
-
const middleware = new Middleware(app, baseUrl);
|
|
10
|
+
const middleware = new Middleware(app, baseUrl, redirectBaseUrl, redirectUriPrefix);
|
|
11
11
|
client.use(middleware.AuthorizationHeaderAttacher, middleware.TokenRefresher);
|
|
12
12
|
}
|
|
13
13
|
return client;
|
|
@@ -2,7 +2,9 @@ import type { Middleware as OpenAPIMiddleware } from 'openapi-fetch';
|
|
|
2
2
|
export declare class Middleware {
|
|
3
3
|
private app;
|
|
4
4
|
private baseUrl;
|
|
5
|
-
|
|
5
|
+
private redirectBaseUrl;
|
|
6
|
+
private redirectUriPrefix;
|
|
7
|
+
constructor(app: string, baseUrl: string, redirectBaseUrl: string, redirectUriPrefix?: string);
|
|
6
8
|
get AuthorizationHeaderAttacher(): OpenAPIMiddleware;
|
|
7
9
|
get TokenRefresher(): OpenAPIMiddleware;
|
|
8
10
|
}
|
|
@@ -2,15 +2,19 @@ import { GetAuthService } from '../index';
|
|
|
2
2
|
export class Middleware {
|
|
3
3
|
app;
|
|
4
4
|
baseUrl;
|
|
5
|
-
|
|
5
|
+
redirectBaseUrl;
|
|
6
|
+
redirectUriPrefix;
|
|
7
|
+
constructor(app, baseUrl, redirectBaseUrl, redirectUriPrefix = "") {
|
|
6
8
|
this.app = app;
|
|
7
9
|
this.baseUrl = baseUrl;
|
|
10
|
+
this.redirectBaseUrl = redirectBaseUrl;
|
|
11
|
+
this.redirectUriPrefix = redirectUriPrefix;
|
|
8
12
|
}
|
|
9
13
|
get AuthorizationHeaderAttacher() {
|
|
10
|
-
const
|
|
14
|
+
const auth = GetAuthService(this.app, this.baseUrl, this.redirectBaseUrl, this.redirectUriPrefix);
|
|
11
15
|
return {
|
|
12
16
|
async onRequest({ request }) {
|
|
13
|
-
return
|
|
17
|
+
return auth.WithAuthorizationToken(request);
|
|
14
18
|
},
|
|
15
19
|
};
|
|
16
20
|
}
|
|
@@ -21,7 +25,8 @@ export class Middleware {
|
|
|
21
25
|
if (response.status !== 401 || request.url.includes('/auth/refresh')) {
|
|
22
26
|
return response;
|
|
23
27
|
}
|
|
24
|
-
|
|
28
|
+
const auth = GetAuthService(t.app, t.baseUrl, t.redirectBaseUrl, t.redirectUriPrefix);
|
|
29
|
+
return auth.RefreshAndRetry(request, response);
|
|
25
30
|
},
|
|
26
31
|
};
|
|
27
32
|
}
|
package/dist/guard/index.d.ts
CHANGED
|
@@ -1,20 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
export declare class Guards {
|
|
6
|
-
private app;
|
|
7
|
-
private apiURL;
|
|
8
|
-
private redirectBaseUrl;
|
|
9
|
-
private redirectUriPrefix;
|
|
1
|
+
import { GetAuthService } from '../index';
|
|
2
|
+
import { Route } from '../types';
|
|
3
|
+
export declare class Guard {
|
|
4
|
+
private getAuthService;
|
|
10
5
|
private onUserRetrieve?;
|
|
11
|
-
constructor(
|
|
6
|
+
constructor(getAuthService: () => ReturnType<typeof GetAuthService>, onUserRetrieve?: ((user: {
|
|
12
7
|
userId: string;
|
|
13
8
|
roles: string[];
|
|
14
9
|
}) => void) | undefined);
|
|
15
|
-
GetLoginRedirectPath(to: Route, from?: Route): {
|
|
16
|
-
redirect: () => string;
|
|
17
|
-
};
|
|
18
10
|
Auth: (roles?: string[]) => (to: Route, from: Route) => Promise<string | true>;
|
|
19
11
|
}
|
|
20
|
-
export {};
|
package/dist/guard/index.js
CHANGED
|
@@ -1,45 +1,26 @@
|
|
|
1
1
|
import { Storage, StorageKey } from '../client/storage/localStorage';
|
|
2
|
-
import { GetAuthService } from '../index';
|
|
3
2
|
let meCache = null;
|
|
4
3
|
const ME_CACHE_TTL_MS = 15 * 1000;
|
|
5
4
|
function arrayIntercept(arr1, arr2) {
|
|
6
5
|
return arr1.filter((item) => arr2.includes(item));
|
|
7
6
|
}
|
|
8
|
-
export class
|
|
9
|
-
|
|
10
|
-
apiURL;
|
|
11
|
-
redirectBaseUrl;
|
|
12
|
-
redirectUriPrefix;
|
|
7
|
+
export class Guard {
|
|
8
|
+
getAuthService;
|
|
13
9
|
onUserRetrieve;
|
|
14
|
-
constructor(
|
|
15
|
-
this.
|
|
16
|
-
this.apiURL = apiURL;
|
|
17
|
-
this.redirectBaseUrl = redirectBaseUrl;
|
|
18
|
-
this.redirectUriPrefix = redirectUriPrefix;
|
|
10
|
+
constructor(getAuthService, onUserRetrieve) {
|
|
11
|
+
this.getAuthService = getAuthService;
|
|
19
12
|
this.onUserRetrieve = onUserRetrieve;
|
|
20
13
|
}
|
|
21
|
-
GetLoginRedirectPath(to, from) {
|
|
22
|
-
const redirectUri = from?.query['redirect_uri'] || encodeURIComponent(to.fullPath);
|
|
23
|
-
const redirectPath = `${this.redirectBaseUrl}?redirect_uri=${this.redirectUriPrefix}${redirectUri}`;
|
|
24
|
-
return {
|
|
25
|
-
redirect: () => {
|
|
26
|
-
if (redirectPath.startsWith('http')) {
|
|
27
|
-
window.location.href = redirectPath;
|
|
28
|
-
return "";
|
|
29
|
-
}
|
|
30
|
-
return "";
|
|
31
|
-
}
|
|
32
|
-
};
|
|
33
|
-
}
|
|
34
14
|
Auth = (roles = []) => {
|
|
35
15
|
return async (to, from) => {
|
|
36
|
-
const
|
|
16
|
+
const auth = this.getAuthService();
|
|
17
|
+
const loginRedirect = auth.GetLoginRedirectPath(to, from);
|
|
37
18
|
let token = Storage.Get(StorageKey.ACCESS_TOKEN);
|
|
38
19
|
if (!token) {
|
|
39
20
|
if (roles.length === 0)
|
|
40
21
|
return true;
|
|
41
22
|
// try to refresh
|
|
42
|
-
token = await
|
|
23
|
+
token = await auth.Refresh();
|
|
43
24
|
if (token) {
|
|
44
25
|
Storage.Set(StorageKey.ACCESS_TOKEN, token);
|
|
45
26
|
}
|
|
@@ -55,7 +36,7 @@ export class Guards {
|
|
|
55
36
|
}
|
|
56
37
|
return true;
|
|
57
38
|
}
|
|
58
|
-
const response = await
|
|
39
|
+
const response = await auth.GetMe();
|
|
59
40
|
if (!response) {
|
|
60
41
|
console.warn('Access denied');
|
|
61
42
|
return loginRedirect.redirect();
|
package/dist/index.d.ts
CHANGED
|
@@ -1,32 +1,26 @@
|
|
|
1
|
-
|
|
2
|
-
roles: string[];
|
|
3
|
-
userId: string;
|
|
4
|
-
active: boolean;
|
|
5
|
-
info?: {
|
|
6
|
-
verified: boolean;
|
|
7
|
-
avatar?: string;
|
|
8
|
-
data?: Record<string, unknown>;
|
|
9
|
-
email?: string;
|
|
10
|
-
username?: string;
|
|
11
|
-
};
|
|
12
|
-
}
|
|
1
|
+
import { GetMeResponse, Route } from './types';
|
|
13
2
|
declare class AuthService {
|
|
14
3
|
private app;
|
|
15
4
|
private apiURL;
|
|
5
|
+
private redirectBaseUrl;
|
|
6
|
+
private redirectUriPrefix;
|
|
16
7
|
private refreshPromise;
|
|
17
|
-
constructor(app: string, apiURL: string);
|
|
18
|
-
withApp(app: string): this;
|
|
8
|
+
constructor(app: string, apiURL: string, redirectBaseUrl: string, redirectUriPrefix?: string);
|
|
19
9
|
private getClient;
|
|
20
10
|
/**
|
|
21
11
|
* @description Refresh access token
|
|
22
12
|
* @returns `accessToken` -> refresh ok. `null` -> refresh failed
|
|
23
13
|
**/
|
|
24
14
|
private doRefresh;
|
|
15
|
+
withApp(app: string): this;
|
|
25
16
|
WithAuthorizationToken(request: Request, token?: string | null): Request;
|
|
17
|
+
GetLoginRedirectPath(to: Route, from?: Route): {
|
|
18
|
+
redirect: () => string;
|
|
19
|
+
};
|
|
26
20
|
GetMe(verbose?: boolean): Promise<GetMeResponse | null>;
|
|
27
21
|
Logout(): Promise<boolean>;
|
|
28
22
|
Refresh(): Promise<string | null>;
|
|
29
23
|
RefreshAndRetry(request: Request, response: Response): Promise<Response>;
|
|
30
24
|
}
|
|
31
|
-
export declare function GetAuthService(app: string, apiURL: string): AuthService;
|
|
25
|
+
export declare function GetAuthService(app: string, apiURL: string, redirectBaseUrl: string, redirectUriPrefix: string): AuthService;
|
|
32
26
|
export {};
|
package/dist/index.js
CHANGED
|
@@ -1,21 +1,20 @@
|
|
|
1
1
|
import { getClient } from './client/client';
|
|
2
2
|
import { Storage, StorageKey } from './client/storage/localStorage';
|
|
3
|
-
;
|
|
4
3
|
class AuthService {
|
|
5
4
|
app;
|
|
6
5
|
apiURL;
|
|
6
|
+
redirectBaseUrl;
|
|
7
|
+
redirectUriPrefix;
|
|
7
8
|
// теперь обещание возвращает сам новый токен или null
|
|
8
9
|
refreshPromise = null;
|
|
9
|
-
constructor(app, apiURL) {
|
|
10
|
+
constructor(app, apiURL, redirectBaseUrl, redirectUriPrefix = "") {
|
|
10
11
|
this.app = app;
|
|
11
12
|
this.apiURL = apiURL;
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
this.app = app;
|
|
15
|
-
return this;
|
|
13
|
+
this.redirectBaseUrl = redirectBaseUrl;
|
|
14
|
+
this.redirectUriPrefix = redirectUriPrefix;
|
|
16
15
|
}
|
|
17
16
|
getClient() {
|
|
18
|
-
return getClient(this.app, this.apiURL);
|
|
17
|
+
return getClient(this.app, this.apiURL, this.redirectBaseUrl, this.redirectUriPrefix);
|
|
19
18
|
}
|
|
20
19
|
/**
|
|
21
20
|
* @description Refresh access token
|
|
@@ -35,6 +34,10 @@ class AuthService {
|
|
|
35
34
|
Storage.Delete(StorageKey.ACCESS_TOKEN);
|
|
36
35
|
return null;
|
|
37
36
|
}
|
|
37
|
+
withApp(app) {
|
|
38
|
+
this.app = app;
|
|
39
|
+
return this;
|
|
40
|
+
}
|
|
38
41
|
WithAuthorizationToken(request, token = null) {
|
|
39
42
|
token ??= Storage.Get(StorageKey.ACCESS_TOKEN);
|
|
40
43
|
if (token) {
|
|
@@ -42,6 +45,19 @@ class AuthService {
|
|
|
42
45
|
}
|
|
43
46
|
return request;
|
|
44
47
|
}
|
|
48
|
+
GetLoginRedirectPath(to, from) {
|
|
49
|
+
const redirectUri = from?.query['redirect_uri'] || encodeURIComponent(to.fullPath);
|
|
50
|
+
const redirectPath = `${this.redirectBaseUrl}?redirect_uri=${this.redirectUriPrefix}${redirectUri}`;
|
|
51
|
+
return {
|
|
52
|
+
redirect: () => {
|
|
53
|
+
if (redirectPath.startsWith('http')) {
|
|
54
|
+
window.location.href = redirectPath;
|
|
55
|
+
return "";
|
|
56
|
+
}
|
|
57
|
+
return redirectPath;
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
}
|
|
45
61
|
async GetMe(verbose = false) {
|
|
46
62
|
let token = Storage.Get(StorageKey.ACCESS_TOKEN);
|
|
47
63
|
if (!token) {
|
|
@@ -136,9 +152,9 @@ class AuthService {
|
|
|
136
152
|
}
|
|
137
153
|
}
|
|
138
154
|
let auth = null;
|
|
139
|
-
export function GetAuthService(app, apiURL) {
|
|
155
|
+
export function GetAuthService(app, apiURL, redirectBaseUrl, redirectUriPrefix) {
|
|
140
156
|
if (!auth) {
|
|
141
|
-
auth = new AuthService(app, apiURL);
|
|
157
|
+
auth = new AuthService(app, apiURL, redirectBaseUrl, redirectUriPrefix);
|
|
142
158
|
}
|
|
143
159
|
return auth.withApp(app);
|
|
144
160
|
}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export interface GetMeResponse {
|
|
2
|
+
roles: string[];
|
|
3
|
+
userId: string;
|
|
4
|
+
active: boolean;
|
|
5
|
+
info?: {
|
|
6
|
+
verified: boolean;
|
|
7
|
+
avatar?: string;
|
|
8
|
+
data?: Record<string, unknown>;
|
|
9
|
+
email?: string;
|
|
10
|
+
username?: string;
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
export interface Route {
|
|
14
|
+
query: Record<string, unknown>;
|
|
15
|
+
fullPath: string;
|
|
16
|
+
}
|
package/dist/types.js
ADDED