@auth-strategy-manager/rest 1.0.0
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 +135 -0
- package/dist/core/src/auth-strategy-manager.d.ts +14 -0
- package/dist/core/src/auth-strategy-manager.js +88 -0
- package/dist/core/src/auth-strategy-manager.js.map +1 -0
- package/dist/core/src/constants.d.ts +3 -0
- package/dist/core/src/constants.js +7 -0
- package/dist/core/src/constants.js.map +1 -0
- package/dist/core/src/errors.d.ts +20 -0
- package/dist/core/src/errors.js +41 -0
- package/dist/core/src/errors.js.map +1 -0
- package/dist/core/src/helpers/index.d.ts +1 -0
- package/dist/core/src/helpers/index.js +18 -0
- package/dist/core/src/helpers/index.js.map +1 -0
- package/dist/core/src/helpers/strategy-helper.d.ts +10 -0
- package/dist/core/src/helpers/strategy-helper.js +47 -0
- package/dist/core/src/helpers/strategy-helper.js.map +1 -0
- package/dist/core/src/index.d.ts +5 -0
- package/dist/core/src/index.js +22 -0
- package/dist/core/src/index.js.map +1 -0
- package/dist/core/src/strategies/empty-strategy.d.ts +11 -0
- package/dist/core/src/strategies/empty-strategy.js +37 -0
- package/dist/core/src/strategies/empty-strategy.js.map +1 -0
- package/dist/core/src/strategies/index.d.ts +1 -0
- package/dist/core/src/strategies/index.js +18 -0
- package/dist/core/src/strategies/index.js.map +1 -0
- package/dist/core/src/types.d.ts +23 -0
- package/dist/core/src/types.js +3 -0
- package/dist/core/src/types.js.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +6 -0
- package/dist/index.js.map +1 -0
- package/dist/rest/src/index.d.ts +2 -0
- package/dist/rest/src/index.js +6 -0
- package/dist/rest/src/index.js.map +1 -0
- package/dist/rest/src/rest-strategy.d.ts +27 -0
- package/dist/rest/src/rest-strategy.js +153 -0
- package/dist/rest/src/rest-strategy.js.map +1 -0
- package/dist/rest/src/types.d.ts +14 -0
- package/dist/rest/src/types.js +3 -0
- package/dist/rest/src/types.js.map +1 -0
- package/dist/rest-strategy.d.ts +27 -0
- package/dist/rest-strategy.js +153 -0
- package/dist/rest-strategy.js.map +1 -0
- package/dist/types.d.ts +14 -0
- package/dist/types.js +3 -0
- package/dist/types.js.map +1 -0
- package/package.json +42 -0
package/README.md
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
# @auth-strategy-manager/rest
|
|
2
|
+
|
|
3
|
+
REST API strategy for auth-strategy-manager.
|
|
4
|
+
|
|
5
|
+
## ๐ Documentation in Other Languages
|
|
6
|
+
|
|
7
|
+
- [๐ท๐บ ะ ัััะบะธะน (Russian)](README_RU.md)
|
|
8
|
+
- [๐บ๐ธ English (Current)](README.md)
|
|
9
|
+
|
|
10
|
+
## Installation
|
|
11
|
+
|
|
12
|
+
```bash
|
|
13
|
+
npm install @auth-strategy-manager/rest @auth-strategy-manager/core axios
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
or via meta-package:
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
npm install auth-strategy-manager @auth-strategy-manager/rest axios
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
> The meta-package will automatically install `@auth-strategy-manager/core` for you.
|
|
23
|
+
|
|
24
|
+
## Usage
|
|
25
|
+
|
|
26
|
+
```typescript
|
|
27
|
+
import { AuthStrategyManager } from '@auth-strategy-manager/core';
|
|
28
|
+
import { RestStrategy } from '@auth-strategy-manager/rest';
|
|
29
|
+
import axios from 'axios';
|
|
30
|
+
|
|
31
|
+
// Create custom axios instance
|
|
32
|
+
const axiosInstance = axios.create({
|
|
33
|
+
baseURL: 'https://api.example.com',
|
|
34
|
+
timeout: 5000
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
// Create REST strategy
|
|
38
|
+
const restStrategy = new RestStrategy({
|
|
39
|
+
name: 'my-rest',
|
|
40
|
+
tokenKey: 'access_token',
|
|
41
|
+
signInUrl: 'https://myapp.com/login',
|
|
42
|
+
axiosInstance,
|
|
43
|
+
|
|
44
|
+
// URL endpoints
|
|
45
|
+
check: { url: '/auth/check', method: 'GET' },
|
|
46
|
+
signIn: { url: '/auth/login', method: 'POST' },
|
|
47
|
+
signUp: { url: '/auth/register', method: 'POST' },
|
|
48
|
+
signOut: { url: '/auth/logout', method: 'POST' },
|
|
49
|
+
refresh: { url: '/auth/refresh', method: 'POST' },
|
|
50
|
+
|
|
51
|
+
// Custom token extraction function
|
|
52
|
+
getToken: (response: any) => response.data?.access_token || response.access_token
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
// Use with strategy manager
|
|
56
|
+
const authManager = new AuthStrategyManager([restStrategy]);
|
|
57
|
+
|
|
58
|
+
// Sign in with custom data
|
|
59
|
+
const loginResult = await restStrategy.signIn({
|
|
60
|
+
data: {
|
|
61
|
+
username: 'user@example.com',
|
|
62
|
+
password: 'password123'
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## Configuration
|
|
68
|
+
|
|
69
|
+
### RestConfig
|
|
70
|
+
|
|
71
|
+
```typescript
|
|
72
|
+
type RestConfig = {
|
|
73
|
+
check: UrlConfig;
|
|
74
|
+
signIn: UrlConfig;
|
|
75
|
+
signUp: UrlConfig;
|
|
76
|
+
signOut: UrlConfig;
|
|
77
|
+
refresh: UrlConfig;
|
|
78
|
+
name?: string;
|
|
79
|
+
tokenKey?: string;
|
|
80
|
+
signInUrl?: string;
|
|
81
|
+
axiosInstance?: AxiosInstance;
|
|
82
|
+
getToken?: (response: unknown, url?: string) => string;
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
type UrlConfig = {
|
|
86
|
+
url: string;
|
|
87
|
+
method?: string;
|
|
88
|
+
};
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
### Parameters
|
|
92
|
+
|
|
93
|
+
- `check` - Endpoint for checking authentication
|
|
94
|
+
- `signIn` - Endpoint for user sign in
|
|
95
|
+
- `signUp` - Endpoint for user registration
|
|
96
|
+
- `signOut` - Endpoint for user sign out
|
|
97
|
+
- `refresh` - Endpoint for token refresh
|
|
98
|
+
- `name` - Strategy name (default: 'rest')
|
|
99
|
+
- `tokenKey` - Storage key for token (default: 'access')
|
|
100
|
+
- `signInUrl` - URL for redirect after logout
|
|
101
|
+
- `axiosInstance` - Custom axios instance
|
|
102
|
+
- `getToken` - Custom function for extracting token from response
|
|
103
|
+
|
|
104
|
+
## API
|
|
105
|
+
|
|
106
|
+
### RestStrategy
|
|
107
|
+
|
|
108
|
+
#### Constructor
|
|
109
|
+
|
|
110
|
+
```typescript
|
|
111
|
+
constructor(config: RestConfig)
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
#### Methods
|
|
115
|
+
|
|
116
|
+
- `check(): Promise<boolean>` - Check authentication
|
|
117
|
+
- `signIn<D, T>(config?: AxiosRequestConfig<D>): Promise<T>` - Sign in user
|
|
118
|
+
- `signUp<D, T>(config?: AxiosRequestConfig<D>): Promise<T>` - Sign up user
|
|
119
|
+
- `signOut(): Promise<void>` - Sign out user
|
|
120
|
+
- `refreshToken(): Promise<void>` - Refresh token
|
|
121
|
+
|
|
122
|
+
#### Properties
|
|
123
|
+
|
|
124
|
+
- `name: string` - Strategy name
|
|
125
|
+
- `axiosInstance: AxiosInstance` - Axios instance
|
|
126
|
+
- `token?: string` - Current token
|
|
127
|
+
- `isAuthenticated: boolean` - Authentication status
|
|
128
|
+
|
|
129
|
+
## Token Storage
|
|
130
|
+
|
|
131
|
+
Tokens are stored in `sessionStorage` with the configured `tokenKey`.
|
|
132
|
+
|
|
133
|
+
## License
|
|
134
|
+
|
|
135
|
+
ISC
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { AuthStrategyManagerInterface, Strategy } from './types';
|
|
2
|
+
export declare class AuthStrategyManager implements AuthStrategyManagerInterface {
|
|
3
|
+
readonly strategiesCount: number;
|
|
4
|
+
private strategies;
|
|
5
|
+
private readonly helper;
|
|
6
|
+
constructor(strategies: Strategy[]);
|
|
7
|
+
get strategy(): Strategy;
|
|
8
|
+
get startUrl(): string | undefined;
|
|
9
|
+
set startUrl(url: string);
|
|
10
|
+
check: () => Promise<boolean>;
|
|
11
|
+
setStrategies: (strategies: Strategy[]) => Promise<void>;
|
|
12
|
+
use: (strategyName: string) => void;
|
|
13
|
+
clear: () => void;
|
|
14
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.AuthStrategyManager = void 0;
|
|
13
|
+
const errors_1 = require("./errors");
|
|
14
|
+
const constants_1 = require("./constants");
|
|
15
|
+
const helpers_1 = require("./helpers");
|
|
16
|
+
const strategies_1 = require("./strategies");
|
|
17
|
+
const emptyStrategy = new strategies_1.EmptyStrategy();
|
|
18
|
+
const protocol = window.location.protocol;
|
|
19
|
+
const [baseUrl] = window.location.href.replace(`${protocol}//`, '').split('/');
|
|
20
|
+
const startUrl = `${protocol}//${baseUrl}`;
|
|
21
|
+
class AuthStrategyManager {
|
|
22
|
+
constructor(strategies) {
|
|
23
|
+
this.check = () => __awaiter(this, void 0, void 0, function* () {
|
|
24
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j;
|
|
25
|
+
const strategyNames = Object.keys(this.strategies);
|
|
26
|
+
const strategyName = strategyNames[0];
|
|
27
|
+
if (strategyNames.length === 1) {
|
|
28
|
+
return yield this.strategies[strategyName].check();
|
|
29
|
+
}
|
|
30
|
+
const actives = yield Promise.allSettled(strategyNames.map((strategyName) => this.strategies[strategyName].check()));
|
|
31
|
+
let isAuthenticated = false;
|
|
32
|
+
for (let index = 0; index < actives.length; index++) {
|
|
33
|
+
const active = actives[index];
|
|
34
|
+
if (active.status === 'fulfilled' && active.value === true) {
|
|
35
|
+
this.helper.activeStrategyName = strategyNames[index];
|
|
36
|
+
isAuthenticated = true;
|
|
37
|
+
break;
|
|
38
|
+
}
|
|
39
|
+
if (active.status === 'rejected' &&
|
|
40
|
+
((_b = (_a = active.reason) === null || _a === void 0 ? void 0 : _a.code) !== null && _b !== void 0 ? _b : (_c = active === null || active === void 0 ? void 0 : active.reason) === null || _c === void 0 ? void 0 : _c.message) === constants_1.NETWORK_ERROR_CODE) {
|
|
41
|
+
throw new errors_1.NetworkError((_d = active === null || active === void 0 ? void 0 : active.reason) === null || _d === void 0 ? void 0 : _d.message);
|
|
42
|
+
}
|
|
43
|
+
if (active.status === 'rejected' &&
|
|
44
|
+
((_f = (_e = active.reason) === null || _e === void 0 ? void 0 : _e.code) !== null && _f !== void 0 ? _f : (_g = active === null || active === void 0 ? void 0 : active.reason) === null || _g === void 0 ? void 0 : _g.message) === constants_1.TIMEOUT_3RD_PARTY_ERROR_CODE) {
|
|
45
|
+
throw new errors_1.Timeout3rdPartyError((_h = active === null || active === void 0 ? void 0 : active.reason) === null || _h === void 0 ? void 0 : _h.message);
|
|
46
|
+
}
|
|
47
|
+
if (active.status === 'rejected' && ((_j = active.reason) === null || _j === void 0 ? void 0 : _j.code) === constants_1.CERT_ERROR_CODE) {
|
|
48
|
+
throw new errors_1.CertError();
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
return isAuthenticated;
|
|
52
|
+
});
|
|
53
|
+
this.setStrategies = (strategies) => __awaiter(this, void 0, void 0, function* () {
|
|
54
|
+
this.strategies = strategies.reduce((acc, strategy) => {
|
|
55
|
+
acc[strategy.name] = strategy;
|
|
56
|
+
return acc;
|
|
57
|
+
}, {});
|
|
58
|
+
});
|
|
59
|
+
this.use = (strategyName) => {
|
|
60
|
+
this.helper.activeStrategyName = strategyName;
|
|
61
|
+
};
|
|
62
|
+
this.clear = () => {
|
|
63
|
+
this.helper.activeStrategyName = emptyStrategy.name;
|
|
64
|
+
this.startUrl = startUrl;
|
|
65
|
+
};
|
|
66
|
+
this.helper = helpers_1.strategyHelper;
|
|
67
|
+
this.strategiesCount = strategies.length;
|
|
68
|
+
this.strategies = strategies.reduce((acc, strategy) => {
|
|
69
|
+
acc[strategy.name] = strategy;
|
|
70
|
+
return acc;
|
|
71
|
+
}, {});
|
|
72
|
+
}
|
|
73
|
+
get strategy() {
|
|
74
|
+
var _a;
|
|
75
|
+
if (!this.helper.activeStrategyName || !this.strategies) {
|
|
76
|
+
return emptyStrategy;
|
|
77
|
+
}
|
|
78
|
+
return (_a = this.strategies[this.helper.activeStrategyName]) !== null && _a !== void 0 ? _a : emptyStrategy;
|
|
79
|
+
}
|
|
80
|
+
get startUrl() {
|
|
81
|
+
return this.helper.startUrl;
|
|
82
|
+
}
|
|
83
|
+
set startUrl(url) {
|
|
84
|
+
this.helper.startUrl = url;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
exports.AuthStrategyManager = AuthStrategyManager;
|
|
88
|
+
//# sourceMappingURL=auth-strategy-manager.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"auth-strategy-manager.js","sourceRoot":"","sources":["../../../../core/src/auth-strategy-manager.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,qCAAyE;AACzE,2CAAgG;AAChG,uCAA2D;AAC3D,6CAA6C;AAI7C,MAAM,aAAa,GAAG,IAAI,0BAAa,EAAE,CAAC;AAC1C,MAAM,QAAQ,GAAG,MAAM,CAAC,QAAQ,CAAC,QAAQ,CAAC;AAC1C,MAAM,CAAC,OAAO,CAAC,GAAG,MAAM,CAAC,QAAQ,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AAQ,CAAC;QAC3B,CAAC,CAAC;QAvFA,IAAI,CAAC,MAAM,GAAG,wBAAc,CAAC;QAC7B,IAAI,CAAC,eAAe,GAAG,UAAU,CAAC,MAAM,CAAC;QACzC,IAAI,CAAC,UAAU,GAAG,UAAU,CAAC,MAAM,CAAgC,CAAC,GAAG,EAAE,QAAQ,EAAE,EAAE;YACnF,GAAG,CAAC,QAAQ,CAAC,IAAI,CAAC,GAAG,QAAQ,CAAC;YAE9B,OAAO,GAAG,CAAC;QACb,CAAC,EAAE,EAAE,CAAC,CAAC;IACT,CAAC;IAED,IAAI,QAAQ;;QACV,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,kBAAkB,IAAI,CAAC,IAAI,CAAC,UAAU,EAAE;YACvD,OAAO,aAAa,CAAC;SACtB;QAED,OAAO,MAAA,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,MAAM,CAAC,kBAAkB,CAAC,mCAAI,aAAa,CAAC;IAC1E,CAAC;IAED,IAAI,QAAQ;QACV,OAAO,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC;IAC9B,CAAC;IAED,IAAI,QAAQ,CAAC,GAAW;QACtB,IAAI,CAAC,MAAM,CAAC,QAAQ,GAAG,GAAG,CAAC;IAC7B,CAAC;CAiEF;AA/FD,kDA+FC"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.TIMEOUT_3RD_PARTY_ERROR_CODE = exports.NETWORK_ERROR_CODE = exports.CERT_ERROR_CODE = void 0;
|
|
4
|
+
exports.CERT_ERROR_CODE = 'ERR_CERT_AUTHORITY_INVALID';
|
|
5
|
+
exports.NETWORK_ERROR_CODE = 'ERR_NETWORK';
|
|
6
|
+
exports.TIMEOUT_3RD_PARTY_ERROR_CODE = 'Timeout when waiting for 3rd party check iframe message.';
|
|
7
|
+
//# sourceMappingURL=constants.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constants.js","sourceRoot":"","sources":["../../../../core/src/constants.ts"],"names":[],"mappings":";;;AAAa,QAAA,eAAe,GAAG,4BAA4B,CAAC;AAC/C,QAAA,kBAAkB,GAAG,aAAa,CAAC;AACnC,QAAA,4BAA4B,GACvC,0DAA0D,CAAC"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
type ResponseErrorData = {
|
|
2
|
+
status: number;
|
|
3
|
+
message: string;
|
|
4
|
+
};
|
|
5
|
+
export declare class ResponseError {
|
|
6
|
+
readonly status: number;
|
|
7
|
+
readonly message: string;
|
|
8
|
+
constructor(data: ResponseErrorData);
|
|
9
|
+
toString: () => string;
|
|
10
|
+
}
|
|
11
|
+
export declare class CertError extends ResponseError {
|
|
12
|
+
constructor();
|
|
13
|
+
}
|
|
14
|
+
export declare class NetworkError extends ResponseError {
|
|
15
|
+
constructor(message?: string);
|
|
16
|
+
}
|
|
17
|
+
export declare class Timeout3rdPartyError extends ResponseError {
|
|
18
|
+
constructor(message?: string);
|
|
19
|
+
}
|
|
20
|
+
export {};
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Timeout3rdPartyError = exports.NetworkError = exports.CertError = exports.ResponseError = void 0;
|
|
4
|
+
class ResponseError {
|
|
5
|
+
constructor(data) {
|
|
6
|
+
this.toString = () => {
|
|
7
|
+
return `${this.status ? `Status ${this.status}: ` : ''}${this.message}`;
|
|
8
|
+
};
|
|
9
|
+
this.status = data.status;
|
|
10
|
+
this.message = data.message;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
exports.ResponseError = ResponseError;
|
|
14
|
+
class CertError extends ResponseError {
|
|
15
|
+
constructor() {
|
|
16
|
+
super({
|
|
17
|
+
status: 0,
|
|
18
|
+
message: 'ERR_CERT_AUTHORITY_INVALID',
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
exports.CertError = CertError;
|
|
23
|
+
class NetworkError extends ResponseError {
|
|
24
|
+
constructor(message) {
|
|
25
|
+
super({
|
|
26
|
+
status: 0,
|
|
27
|
+
message: message !== null && message !== void 0 ? message : 'NETWORK ERROR',
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
exports.NetworkError = NetworkError;
|
|
32
|
+
class Timeout3rdPartyError extends ResponseError {
|
|
33
|
+
constructor(message) {
|
|
34
|
+
super({
|
|
35
|
+
status: 0,
|
|
36
|
+
message: message !== null && message !== void 0 ? message : 'Timeout when waiting for 3rd party check iframe message.',
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
exports.Timeout3rdPartyError = Timeout3rdPartyError;
|
|
41
|
+
//# sourceMappingURL=errors.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../../../../core/src/errors.ts"],"names":[],"mappings":";;;AAKA,MAAa,aAAa;IAIxB,YAAY,IAAuB;QAKnC,aAAQ,GAAG,GAAG,EAAE;YACd,OAAO,GAAG,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,UAAU,IAAI,CAAC,MAAM,IAAI,CAAC,CAAC,CAAC,EAAE,GAAG,IAAI,CAAC,OAAO,EAAE,CAAC;QAC1E,CAAC,CAAC;QANA,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC;QAC1B,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC,OAAO,CAAC;IAC9B,CAAC;CAKF;AAZD,sCAYC;AAED,MAAa,SAAU,SAAQ,aAAa;IAC1C;QACE,KAAK,CAAC;YACJ,MAAM,EAAE,CAAC;YACT,OAAO,EAAE,4BAA4B;SACtC,CAAC,CAAC;IACL,CAAC;CACF;AAPD,8BAOC;AAED,MAAa,YAAa,SAAQ,aAAa;IAC7C,YAAY,OAAgB;QAC1B,KAAK,CAAC;YACJ,MAAM,EAAE,CAAC;YACT,OAAO,EAAE,OAAO,aAAP,OAAO,cAAP,OAAO,GAAI,eAAe;SACpC,CAAC,CAAC;IACL,CAAC;CACF;AAPD,oCAOC;AAED,MAAa,oBAAqB,SAAQ,aAAa;IACrD,YAAY,OAAgB;QAC1B,KAAK,CAAC;YACJ,MAAM,EAAE,CAAC;YACT,OAAO,EAAE,OAAO,aAAP,OAAO,cAAP,OAAO,GAAI,0DAA0D;SAC/E,CAAC,CAAC;IACL,CAAC;CACF;AAPD,oDAOC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './strategy-helper';
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./strategy-helper"), exports);
|
|
18
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../core/src/helpers/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,oDAAkC"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export declare class StrategyHelper {
|
|
2
|
+
isAuthenticated: boolean;
|
|
3
|
+
get activeStrategyName(): string;
|
|
4
|
+
set activeStrategyName(name: string);
|
|
5
|
+
get startUrl(): string | undefined;
|
|
6
|
+
set startUrl(url: string | undefined);
|
|
7
|
+
clearStorage: () => void;
|
|
8
|
+
reset: () => void;
|
|
9
|
+
}
|
|
10
|
+
export declare const strategyHelper: StrategyHelper;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.strategyHelper = exports.StrategyHelper = void 0;
|
|
4
|
+
const TYPE_KEY = 'authStrategyName';
|
|
5
|
+
const START_URL_KEY = 'startUrl';
|
|
6
|
+
const protocol = window.location.protocol;
|
|
7
|
+
const [baseUrl] = window.location.href.replace(`${protocol}//`, '').split('/');
|
|
8
|
+
const startUrl = `${protocol}//${baseUrl}`;
|
|
9
|
+
class StrategyHelper {
|
|
10
|
+
constructor() {
|
|
11
|
+
this.isAuthenticated = false;
|
|
12
|
+
this.clearStorage = () => {
|
|
13
|
+
localStorage.removeItem(TYPE_KEY);
|
|
14
|
+
};
|
|
15
|
+
this.reset = () => {
|
|
16
|
+
this.clearStorage();
|
|
17
|
+
this.isAuthenticated = false;
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
get activeStrategyName() {
|
|
21
|
+
var _a;
|
|
22
|
+
return (_a = localStorage.getItem(TYPE_KEY)) !== null && _a !== void 0 ? _a : '';
|
|
23
|
+
}
|
|
24
|
+
set activeStrategyName(name) {
|
|
25
|
+
if (!name) {
|
|
26
|
+
localStorage.removeItem(TYPE_KEY);
|
|
27
|
+
}
|
|
28
|
+
else {
|
|
29
|
+
localStorage.setItem(TYPE_KEY, name);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
get startUrl() {
|
|
33
|
+
var _a;
|
|
34
|
+
return (_a = localStorage.getItem(START_URL_KEY)) !== null && _a !== void 0 ? _a : startUrl;
|
|
35
|
+
}
|
|
36
|
+
set startUrl(url) {
|
|
37
|
+
if (url) {
|
|
38
|
+
localStorage.setItem(START_URL_KEY, url);
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
localStorage.removeItem(START_URL_KEY);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
exports.StrategyHelper = StrategyHelper;
|
|
46
|
+
exports.strategyHelper = new StrategyHelper();
|
|
47
|
+
//# sourceMappingURL=strategy-helper.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"strategy-helper.js","sourceRoot":"","sources":["../../../../../core/src/helpers/strategy-helper.ts"],"names":[],"mappings":";;;AAAA,MAAM,QAAQ,GAAG,kBAAkB,CAAC;AACpC,MAAM,aAAa,GAAG,UAAU,CAAC;AAEjC,MAAM,QAAQ,GAAG,MAAM,CAAC,QAAQ,CAAC,QAAQ,CAAC;AAC1C,MAAM,CAAC,OAAO,CAAC,GAAG,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAC,OAAO,CAAC,GAAG,QAAQ,IAAI,EAAE,EAAE,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;AAE/E,MAAM,QAAQ,GAAG,GAAG,QAAQ,KAAK,OAAO,EAAE,CAAC;AAE3C,MAAa,cAAc;IAA3B;QACE,oBAAe,GAAG,KAAK,CAAC;QA0BjB,iBAAY,GAAG,GAAS,EAAE;YAC/B,YAAY,CAAC,UAAU,CAAC,QAAQ,CAAC,CAAC;QACpC,CAAC,CAAC;QAEK,UAAK,GAAG,GAAG,EAAE;YAClB,IAAI,CAAC,YAAY,EAAE,CAAC;YACpB,IAAI,CAAC,eAAe,GAAG,KAAK,CAAC;QAC/B,CAAC,CAAC;IACJ,CAAC;IAhCC,IAAI,kBAAkB;;QACpB,OAAO,MAAA,YAAY,CAAC,OAAO,CAAC,QAAQ,CAAC,mCAAI,EAAE,CAAC;IAC9C,CAAC;IAED,IAAI,kBAAkB,CAAC,IAAY;QACjC,IAAI,CAAC,IAAI,EAAE;YACT,YAAY,CAAC,UAAU,CAAC,QAAQ,CAAC,CAAC;SACnC;aAAM;YACL,YAAY,CAAC,OAAO,CAAC,QAAQ,EAAE,IAAI,CAAC,CAAC;SACtC;IACH,CAAC;IAED,IAAI,QAAQ;;QACV,OAAO,MAAA,YAAY,CAAC,OAAO,CAAC,aAAa,CAAC,mCAAI,QAAQ,CAAC;IACzD,CAAC;IAED,IAAI,QAAQ,CAAC,GAAuB;QAClC,IAAI,GAAG,EAAE;YACP,YAAY,CAAC,OAAO,CAAC,aAAa,EAAE,GAAG,CAAC,CAAC;SAC1C;aAAM;YACL,YAAY,CAAC,UAAU,CAAC,aAAa,CAAC,CAAC;SACxC;IACH,CAAC;CAUF;AAnCD,wCAmCC;AAEY,QAAA,cAAc,GAAG,IAAI,cAAc,EAAE,CAAC"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./auth-strategy-manager"), exports);
|
|
18
|
+
__exportStar(require("./strategies"), exports);
|
|
19
|
+
__exportStar(require("./types"), exports);
|
|
20
|
+
__exportStar(require("./errors"), exports);
|
|
21
|
+
__exportStar(require("./helpers"), exports);
|
|
22
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../core/src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,0DAAwC;AACxC,+CAA6B;AAC7B,0CAAwB;AACxB,2CAAyB;AACzB,4CAA0B"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { StrategyHelper } from '../helpers';
|
|
2
|
+
import { Strategy } from '../types';
|
|
3
|
+
export declare class EmptyStrategy extends StrategyHelper implements Strategy {
|
|
4
|
+
readonly name = "empty";
|
|
5
|
+
check: () => Promise<boolean>;
|
|
6
|
+
signIn: <T>() => Promise<T>;
|
|
7
|
+
signUp: <T>() => Promise<T>;
|
|
8
|
+
signOut: () => Promise<void>;
|
|
9
|
+
refreshToken: () => Promise<void>;
|
|
10
|
+
getUserProfile: <T>() => Promise<T>;
|
|
11
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.EmptyStrategy = void 0;
|
|
13
|
+
const helpers_1 = require("../helpers");
|
|
14
|
+
class EmptyStrategy extends helpers_1.StrategyHelper {
|
|
15
|
+
constructor() {
|
|
16
|
+
super(...arguments);
|
|
17
|
+
this.name = 'empty';
|
|
18
|
+
this.check = () => __awaiter(this, void 0, void 0, function* () {
|
|
19
|
+
return false;
|
|
20
|
+
});
|
|
21
|
+
this.signIn = () => __awaiter(this, void 0, void 0, function* () {
|
|
22
|
+
return false;
|
|
23
|
+
});
|
|
24
|
+
this.signUp = () => __awaiter(this, void 0, void 0, function* () {
|
|
25
|
+
return false;
|
|
26
|
+
});
|
|
27
|
+
this.signOut = () => __awaiter(this, void 0, void 0, function* () {
|
|
28
|
+
this.clearStorage();
|
|
29
|
+
});
|
|
30
|
+
this.refreshToken = () => __awaiter(this, void 0, void 0, function* () { });
|
|
31
|
+
this.getUserProfile = () => __awaiter(this, void 0, void 0, function* () {
|
|
32
|
+
return undefined;
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
exports.EmptyStrategy = EmptyStrategy;
|
|
37
|
+
//# sourceMappingURL=empty-strategy.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"empty-strategy.js","sourceRoot":"","sources":["../../../../../core/src/strategies/empty-strategy.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,wCAA4C;AAG5C,MAAa,aAAc,SAAQ,wBAAc;IAAjD;;QACW,SAAI,GAAG,OAAO,CAAC;QAExB,UAAK,GAAG,GAA2B,EAAE;YACnC,OAAO,KAAK,CAAC;QACf,CAAC,CAAA,CAAC;QAEF,WAAM,GAAG,GAAwB,EAAE;YACjC,OAAO,KAAU,CAAC;QACpB,CAAC,CAAA,CAAC;QAEF,WAAM,GAAG,GAAwB,EAAE;YACjC,OAAO,KAAU,CAAC;QACpB,CAAC,CAAA,CAAC;QAEF,YAAO,GAAG,GAAwB,EAAE;YAClC,IAAI,CAAC,YAAY,EAAE,CAAC;QACtB,CAAC,CAAA,CAAC;QAEF,iBAAY,GAAG,GAAwB,EAAE,gDAAE,CAAC,CAAA,CAAC;QAE7C,mBAAc,GAAG,GAAwB,EAAE;YACzC,OAAO,SAAc,CAAC;QACxB,CAAC,CAAA,CAAC;IACJ,CAAC;CAAA;AAxBD,sCAwBC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './empty-strategy';
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./empty-strategy"), exports);
|
|
18
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../core/src/strategies/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,mDAAiC"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { AxiosRequestConfig } from 'axios';
|
|
2
|
+
export type AuthStrategyManagerStrategies = Record<string, Strategy>;
|
|
3
|
+
export type Strategy = {
|
|
4
|
+
name: string;
|
|
5
|
+
token?: string;
|
|
6
|
+
isAuthenticated?: boolean;
|
|
7
|
+
startUrl?: string;
|
|
8
|
+
signInUrl?: string;
|
|
9
|
+
check: () => Promise<boolean>;
|
|
10
|
+
signIn: <D, T>(config?: AxiosRequestConfig<D>) => Promise<T>;
|
|
11
|
+
signUp: <D, T>(config?: AxiosRequestConfig<D>) => Promise<T>;
|
|
12
|
+
signOut: () => Promise<void>;
|
|
13
|
+
refreshToken: <T>(args?: T) => Promise<void>;
|
|
14
|
+
};
|
|
15
|
+
export type AuthStrategyManagerInterface = {
|
|
16
|
+
strategiesCount: number;
|
|
17
|
+
strategy: Strategy;
|
|
18
|
+
startUrl: string | undefined;
|
|
19
|
+
check: () => Promise<boolean>;
|
|
20
|
+
setStrategies: (strategies: Strategy[]) => Promise<void>;
|
|
21
|
+
use: (strategyName: string) => void;
|
|
22
|
+
clear: () => void;
|
|
23
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../../../core/src/types.ts"],"names":[],"mappings":""}
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RestStrategy = void 0;
|
|
4
|
+
var rest_strategy_1 = require("./rest-strategy");
|
|
5
|
+
Object.defineProperty(exports, "RestStrategy", { enumerable: true, get: function () { return rest_strategy_1.RestStrategy; } });
|
|
6
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,iDAA+C;AAAtC,6GAAA,YAAY,OAAA"}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RestStrategy = void 0;
|
|
4
|
+
var rest_strategy_1 = require("./rest-strategy");
|
|
5
|
+
Object.defineProperty(exports, "RestStrategy", { enumerable: true, get: function () { return rest_strategy_1.RestStrategy; } });
|
|
6
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/index.ts"],"names":[],"mappings":";;;AAAA,iDAA+C;AAAtC,6GAAA,YAAY,OAAA"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { AxiosInstance, AxiosRequestConfig } from 'axios';
|
|
2
|
+
import { Strategy } from '@auth-strategy-manager/core';
|
|
3
|
+
import { Config, UrlName } from './types';
|
|
4
|
+
export declare class RestStrategy implements Strategy {
|
|
5
|
+
readonly name: string;
|
|
6
|
+
readonly axiosInstance: AxiosInstance;
|
|
7
|
+
readonly urls: Partial<Record<UrlName, any>>;
|
|
8
|
+
signInUrl?: string;
|
|
9
|
+
private readonly tokenKey;
|
|
10
|
+
private readonly getToken?;
|
|
11
|
+
private readonly helper;
|
|
12
|
+
private currentRefresh;
|
|
13
|
+
constructor(config: Config);
|
|
14
|
+
get startUrl(): string | undefined;
|
|
15
|
+
set startUrl(url: string);
|
|
16
|
+
get token(): string | undefined;
|
|
17
|
+
set token(token: string);
|
|
18
|
+
get isAuthenticated(): boolean;
|
|
19
|
+
check: () => Promise<boolean>;
|
|
20
|
+
signIn: <D, T>(config?: AxiosRequestConfig<D> | undefined) => Promise<T>;
|
|
21
|
+
signUp: <D, T>(config?: AxiosRequestConfig<D> | undefined) => Promise<T>;
|
|
22
|
+
signOut: () => Promise<void>;
|
|
23
|
+
refreshToken: () => Promise<void>;
|
|
24
|
+
private extractToken;
|
|
25
|
+
private setAuthParams;
|
|
26
|
+
private clearAuthData;
|
|
27
|
+
}
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __rest = (this && this.__rest) || function (s, e) {
|
|
12
|
+
var t = {};
|
|
13
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
14
|
+
t[p] = s[p];
|
|
15
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
16
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
17
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
18
|
+
t[p[i]] = s[p[i]];
|
|
19
|
+
}
|
|
20
|
+
return t;
|
|
21
|
+
};
|
|
22
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
23
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
24
|
+
};
|
|
25
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
exports.RestStrategy = void 0;
|
|
27
|
+
const axios_1 = __importDefault(require("axios"));
|
|
28
|
+
const core_1 = require("@auth-strategy-manager/core");
|
|
29
|
+
const DEFAULT_NAME = 'rest';
|
|
30
|
+
const DEFAULT_TOKEN_KEY = 'access';
|
|
31
|
+
class RestStrategy {
|
|
32
|
+
constructor(config) {
|
|
33
|
+
var _a;
|
|
34
|
+
this.currentRefresh = null;
|
|
35
|
+
this.check = () => __awaiter(this, void 0, void 0, function* () {
|
|
36
|
+
if (!this.token) {
|
|
37
|
+
return false;
|
|
38
|
+
}
|
|
39
|
+
if (!this.urls.check) {
|
|
40
|
+
throw new Error('Check URL is not defined');
|
|
41
|
+
}
|
|
42
|
+
const { url, method } = this.urls.check;
|
|
43
|
+
const response = yield this.axiosInstance(url, { method });
|
|
44
|
+
const token = this.extractToken(response, url);
|
|
45
|
+
const isAuthenticated = Boolean(token);
|
|
46
|
+
if (isAuthenticated) {
|
|
47
|
+
this.helper.activeStrategyName = this.name;
|
|
48
|
+
this.setAuthParams(token);
|
|
49
|
+
}
|
|
50
|
+
return isAuthenticated;
|
|
51
|
+
});
|
|
52
|
+
this.signIn = (config) => __awaiter(this, void 0, void 0, function* () {
|
|
53
|
+
if (!this.urls.signIn) {
|
|
54
|
+
throw new Error('Sign in URL is not defined');
|
|
55
|
+
}
|
|
56
|
+
const { url, method } = this.urls.signIn;
|
|
57
|
+
const response = yield this.axiosInstance(url, Object.assign(Object.assign({}, (config !== null && config !== void 0 ? config : {})), { method }));
|
|
58
|
+
const token = this.extractToken(response, url);
|
|
59
|
+
if (token) {
|
|
60
|
+
this.setAuthParams(token);
|
|
61
|
+
}
|
|
62
|
+
return response;
|
|
63
|
+
});
|
|
64
|
+
this.signUp = (config) => __awaiter(this, void 0, void 0, function* () {
|
|
65
|
+
if (!this.urls.signUp) {
|
|
66
|
+
throw new Error('Sign up URL is not defined');
|
|
67
|
+
}
|
|
68
|
+
const { url, method } = this.urls.signUp;
|
|
69
|
+
const response = yield this.axiosInstance(url, Object.assign(Object.assign({}, (config !== null && config !== void 0 ? config : {})), { method }));
|
|
70
|
+
const token = this.extractToken(response, url);
|
|
71
|
+
if (token) {
|
|
72
|
+
this.setAuthParams(token);
|
|
73
|
+
}
|
|
74
|
+
return response;
|
|
75
|
+
});
|
|
76
|
+
this.signOut = () => __awaiter(this, void 0, void 0, function* () {
|
|
77
|
+
if (!this.urls.signOut) {
|
|
78
|
+
throw new Error('Sign out URL is not defined');
|
|
79
|
+
}
|
|
80
|
+
const { url, method } = this.urls.signOut;
|
|
81
|
+
if (!url) {
|
|
82
|
+
this.clearAuthData();
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
yield this.axiosInstance(url, { method });
|
|
86
|
+
this.clearAuthData();
|
|
87
|
+
});
|
|
88
|
+
this.refreshToken = () => __awaiter(this, void 0, void 0, function* () {
|
|
89
|
+
if (!this.urls.refresh) {
|
|
90
|
+
throw new Error('Refresh token URL is not defined');
|
|
91
|
+
}
|
|
92
|
+
const { url, method } = this.urls.refresh;
|
|
93
|
+
if (!url) {
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
if (this.currentRefresh) {
|
|
97
|
+
return yield this.currentRefresh;
|
|
98
|
+
}
|
|
99
|
+
let resolver = () => { };
|
|
100
|
+
this.currentRefresh = new Promise((resolve) => {
|
|
101
|
+
resolver = resolve;
|
|
102
|
+
});
|
|
103
|
+
const response = yield this.axiosInstance(url, { method });
|
|
104
|
+
const token = this.extractToken(response, url);
|
|
105
|
+
if (token) {
|
|
106
|
+
this.setAuthParams(token);
|
|
107
|
+
}
|
|
108
|
+
resolver();
|
|
109
|
+
this.currentRefresh = null;
|
|
110
|
+
});
|
|
111
|
+
this.extractToken = (response, url) => {
|
|
112
|
+
if (typeof response === 'string') {
|
|
113
|
+
return response;
|
|
114
|
+
}
|
|
115
|
+
return this.getToken ? this.getToken(response, url) : '';
|
|
116
|
+
};
|
|
117
|
+
this.setAuthParams = (token) => {
|
|
118
|
+
window.sessionStorage.setItem(this.tokenKey, token);
|
|
119
|
+
this.helper.activeStrategyName = this.name;
|
|
120
|
+
this.helper.isAuthenticated = true;
|
|
121
|
+
};
|
|
122
|
+
this.clearAuthData = () => {
|
|
123
|
+
window.sessionStorage.clear();
|
|
124
|
+
this.helper.reset();
|
|
125
|
+
};
|
|
126
|
+
const { name, tokenKey, getToken, signInUrl: loginUrl } = config, urls = __rest(config, ["name", "tokenKey", "getToken", "signInUrl"]);
|
|
127
|
+
this.helper = new core_1.StrategyHelper();
|
|
128
|
+
this.name = name || DEFAULT_NAME;
|
|
129
|
+
this.tokenKey = tokenKey || DEFAULT_TOKEN_KEY;
|
|
130
|
+
this.getToken = getToken;
|
|
131
|
+
this.signInUrl = loginUrl;
|
|
132
|
+
this.urls = urls;
|
|
133
|
+
this.axiosInstance = (_a = config.axiosInstance) !== null && _a !== void 0 ? _a : axios_1.default.create();
|
|
134
|
+
}
|
|
135
|
+
get startUrl() {
|
|
136
|
+
return this.helper.startUrl;
|
|
137
|
+
}
|
|
138
|
+
set startUrl(url) {
|
|
139
|
+
this.helper.startUrl = url;
|
|
140
|
+
}
|
|
141
|
+
get token() {
|
|
142
|
+
var _a;
|
|
143
|
+
return (_a = window.sessionStorage.getItem(this.tokenKey)) !== null && _a !== void 0 ? _a : undefined;
|
|
144
|
+
}
|
|
145
|
+
set token(token) {
|
|
146
|
+
window.sessionStorage.setItem(this.tokenKey, token);
|
|
147
|
+
}
|
|
148
|
+
get isAuthenticated() {
|
|
149
|
+
return this.helper.isAuthenticated;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
exports.RestStrategy = RestStrategy;
|
|
153
|
+
//# sourceMappingURL=rest-strategy.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rest-strategy.js","sourceRoot":"","sources":["../../../src/rest-strategy.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,kDAAiE;AACjE,sDAAuE;AAGvE,MAAM,YAAY,GAAG,MAAM,CAAC;AAC5B,MAAM,iBAAiB,GAAG,QAAQ,CAAC;AAEnC,MAAa,YAAY;IAWvB,YAAY,MAAc;;QAFlB,mBAAc,GAAyB,IAAI,CAAC;QAmCpD,UAAK,GAAG,GAA2B,EAAE;YACnC,IAAI,CAAC,IAAI,CAAC,KAAK,EAAE;gBACf,OAAO,KAAK,CAAC;aACd;YAED,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,EAAE;gBACpB,MAAM,IAAI,KAAK,CAAC,0BAA0B,CAAC,CAAC;aAC7C;YAED,MAAM,EAAE,GAAG,EA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xiosRequestConfig['method'];
|
|
5
|
+
};
|
|
6
|
+
export type UrlName = 'check' | 'signIn' | 'signUp' | 'signOut' | 'refresh';
|
|
7
|
+
export type Config = Record<UrlName, UrlConfig> & {
|
|
8
|
+
name?: string;
|
|
9
|
+
tokenKey?: string;
|
|
10
|
+
/** URL for redirecting to the authorization page */
|
|
11
|
+
signInUrl?: string;
|
|
12
|
+
axiosInstance?: any;
|
|
13
|
+
getToken?: (response: unknown, url?: string) => string;
|
|
14
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../../src/types.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { AxiosInstance, AxiosRequestConfig } from 'axios';
|
|
2
|
+
import { Strategy } from '@auth-strategy-manager/core';
|
|
3
|
+
import { Config, UrlName } from './types';
|
|
4
|
+
export declare class RestStrategy implements Strategy {
|
|
5
|
+
readonly name: string;
|
|
6
|
+
readonly axiosInstance: AxiosInstance;
|
|
7
|
+
readonly urls: Partial<Record<UrlName, any>>;
|
|
8
|
+
signInUrl?: string;
|
|
9
|
+
private readonly tokenKey;
|
|
10
|
+
private readonly getToken?;
|
|
11
|
+
private readonly helper;
|
|
12
|
+
private currentRefresh;
|
|
13
|
+
constructor(config: Config);
|
|
14
|
+
get startUrl(): string | undefined;
|
|
15
|
+
set startUrl(url: string);
|
|
16
|
+
get token(): string | undefined;
|
|
17
|
+
set token(token: string);
|
|
18
|
+
get isAuthenticated(): boolean;
|
|
19
|
+
check: () => Promise<boolean>;
|
|
20
|
+
signIn: <D, T>(config?: AxiosRequestConfig<D> | undefined) => Promise<T>;
|
|
21
|
+
signUp: <D, T>(config?: AxiosRequestConfig<D> | undefined) => Promise<T>;
|
|
22
|
+
signOut: () => Promise<void>;
|
|
23
|
+
refreshToken: () => Promise<void>;
|
|
24
|
+
private extractToken;
|
|
25
|
+
private setAuthParams;
|
|
26
|
+
private clearAuthData;
|
|
27
|
+
}
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __rest = (this && this.__rest) || function (s, e) {
|
|
12
|
+
var t = {};
|
|
13
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
14
|
+
t[p] = s[p];
|
|
15
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
16
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
17
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
18
|
+
t[p[i]] = s[p[i]];
|
|
19
|
+
}
|
|
20
|
+
return t;
|
|
21
|
+
};
|
|
22
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
23
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
24
|
+
};
|
|
25
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
exports.RestStrategy = void 0;
|
|
27
|
+
const axios_1 = __importDefault(require("axios"));
|
|
28
|
+
const core_1 = require("@auth-strategy-manager/core");
|
|
29
|
+
const DEFAULT_NAME = 'rest';
|
|
30
|
+
const DEFAULT_TOKEN_KEY = 'access';
|
|
31
|
+
class RestStrategy {
|
|
32
|
+
constructor(config) {
|
|
33
|
+
var _a;
|
|
34
|
+
this.currentRefresh = null;
|
|
35
|
+
this.check = () => __awaiter(this, void 0, void 0, function* () {
|
|
36
|
+
if (!this.token) {
|
|
37
|
+
return false;
|
|
38
|
+
}
|
|
39
|
+
if (!this.urls.check) {
|
|
40
|
+
throw new Error('Check URL is not defined');
|
|
41
|
+
}
|
|
42
|
+
const { url, method } = this.urls.check;
|
|
43
|
+
const response = yield this.axiosInstance(url, { method });
|
|
44
|
+
const token = this.extractToken(response, url);
|
|
45
|
+
const isAuthenticated = Boolean(token);
|
|
46
|
+
if (isAuthenticated) {
|
|
47
|
+
this.helper.activeStrategyName = this.name;
|
|
48
|
+
this.setAuthParams(token);
|
|
49
|
+
}
|
|
50
|
+
return isAuthenticated;
|
|
51
|
+
});
|
|
52
|
+
this.signIn = (config) => __awaiter(this, void 0, void 0, function* () {
|
|
53
|
+
if (!this.urls.signIn) {
|
|
54
|
+
throw new Error('Sign in URL is not defined');
|
|
55
|
+
}
|
|
56
|
+
const { url, method } = this.urls.signIn;
|
|
57
|
+
const response = yield this.axiosInstance(url, Object.assign(Object.assign({}, (config !== null && config !== void 0 ? config : {})), { method }));
|
|
58
|
+
const token = this.extractToken(response, url);
|
|
59
|
+
if (token) {
|
|
60
|
+
this.setAuthParams(token);
|
|
61
|
+
}
|
|
62
|
+
return response;
|
|
63
|
+
});
|
|
64
|
+
this.signUp = (config) => __awaiter(this, void 0, void 0, function* () {
|
|
65
|
+
if (!this.urls.signUp) {
|
|
66
|
+
throw new Error('Sign up URL is not defined');
|
|
67
|
+
}
|
|
68
|
+
const { url, method } = this.urls.signUp;
|
|
69
|
+
const response = yield this.axiosInstance(url, Object.assign(Object.assign({}, (config !== null && config !== void 0 ? config : {})), { method }));
|
|
70
|
+
const token = this.extractToken(response, url);
|
|
71
|
+
if (token) {
|
|
72
|
+
this.setAuthParams(token);
|
|
73
|
+
}
|
|
74
|
+
return response;
|
|
75
|
+
});
|
|
76
|
+
this.signOut = () => __awaiter(this, void 0, void 0, function* () {
|
|
77
|
+
if (!this.urls.signOut) {
|
|
78
|
+
throw new Error('Sign out URL is not defined');
|
|
79
|
+
}
|
|
80
|
+
const { url, method } = this.urls.signOut;
|
|
81
|
+
if (!url) {
|
|
82
|
+
this.clearAuthData();
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
yield this.axiosInstance(url, { method });
|
|
86
|
+
this.clearAuthData();
|
|
87
|
+
});
|
|
88
|
+
this.refreshToken = () => __awaiter(this, void 0, void 0, function* () {
|
|
89
|
+
if (!this.urls.refresh) {
|
|
90
|
+
throw new Error('Refresh token URL is not defined');
|
|
91
|
+
}
|
|
92
|
+
const { url, method } = this.urls.refresh;
|
|
93
|
+
if (!url) {
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
if (this.currentRefresh) {
|
|
97
|
+
return yield this.currentRefresh;
|
|
98
|
+
}
|
|
99
|
+
let resolver = () => { };
|
|
100
|
+
this.currentRefresh = new Promise((resolve) => {
|
|
101
|
+
resolver = resolve;
|
|
102
|
+
});
|
|
103
|
+
const response = yield this.axiosInstance(url, { method });
|
|
104
|
+
const token = this.extractToken(response, url);
|
|
105
|
+
if (token) {
|
|
106
|
+
this.setAuthParams(token);
|
|
107
|
+
}
|
|
108
|
+
resolver();
|
|
109
|
+
this.currentRefresh = null;
|
|
110
|
+
});
|
|
111
|
+
this.extractToken = (response, url) => {
|
|
112
|
+
if (typeof response === 'string') {
|
|
113
|
+
return response;
|
|
114
|
+
}
|
|
115
|
+
return this.getToken ? this.getToken(response, url) : '';
|
|
116
|
+
};
|
|
117
|
+
this.setAuthParams = (token) => {
|
|
118
|
+
window.sessionStorage.setItem(this.tokenKey, token);
|
|
119
|
+
this.helper.activeStrategyName = this.name;
|
|
120
|
+
this.helper.isAuthenticated = true;
|
|
121
|
+
};
|
|
122
|
+
this.clearAuthData = () => {
|
|
123
|
+
window.sessionStorage.clear();
|
|
124
|
+
this.helper.reset();
|
|
125
|
+
};
|
|
126
|
+
const { name, tokenKey, getToken, signInUrl: loginUrl } = config, urls = __rest(config, ["name", "tokenKey", "getToken", "signInUrl"]);
|
|
127
|
+
this.helper = new core_1.StrategyHelper();
|
|
128
|
+
this.name = name || DEFAULT_NAME;
|
|
129
|
+
this.tokenKey = tokenKey || DEFAULT_TOKEN_KEY;
|
|
130
|
+
this.getToken = getToken;
|
|
131
|
+
this.signInUrl = loginUrl;
|
|
132
|
+
this.urls = urls;
|
|
133
|
+
this.axiosInstance = (_a = config.axiosInstance) !== null && _a !== void 0 ? _a : axios_1.default.create();
|
|
134
|
+
}
|
|
135
|
+
get startUrl() {
|
|
136
|
+
return this.helper.startUrl;
|
|
137
|
+
}
|
|
138
|
+
set startUrl(url) {
|
|
139
|
+
this.helper.startUrl = url;
|
|
140
|
+
}
|
|
141
|
+
get token() {
|
|
142
|
+
var _a;
|
|
143
|
+
return (_a = window.sessionStorage.getItem(this.tokenKey)) !== null && _a !== void 0 ? _a : undefined;
|
|
144
|
+
}
|
|
145
|
+
set token(token) {
|
|
146
|
+
window.sessionStorage.setItem(this.tokenKey, token);
|
|
147
|
+
}
|
|
148
|
+
get isAuthenticated() {
|
|
149
|
+
return this.helper.isAuthenticated;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
exports.RestStrategy = RestStrategy;
|
|
153
|
+
//# sourceMappingURL=rest-strategy.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rest-strategy.js","sourceRoot":"","sources":["../src/rest-strategy.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,kDAAiE;AACjE,sDAAuE;AAGvE,MAAM,YAAY,GAAG,MAAM,CAAC;AAC5B,MAAM,iBAAiB,GAAG,QAAQ,CAAC;AAEnC,MAAa,YAAY;IAWvB,YAAY,MAAc;;QAFlB,mBAAc,GAAyB,IAAI,CAAC;QAmCpD,UAAK,GAAG,GAA2B,EAAE;YACnC,IAAI,CAAC,IAAI,CAAC,KAAK,EAAE;gBACf,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IACrC,CAAC;CAwHF;AAlKD,oCAkKC"}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { AxiosRequestConfig } from 'axios';
|
|
2
|
+
export type UrlConfig = {
|
|
3
|
+
url: string;
|
|
4
|
+
method?: AxiosRequestConfig['method'];
|
|
5
|
+
};
|
|
6
|
+
export type UrlName = 'check' | 'signIn' | 'signUp' | 'signOut' | 'refresh';
|
|
7
|
+
export type Config = Record<UrlName, UrlConfig> & {
|
|
8
|
+
name?: string;
|
|
9
|
+
tokenKey?: string;
|
|
10
|
+
/** URL for redirecting to the authorization page */
|
|
11
|
+
signInUrl?: string;
|
|
12
|
+
axiosInstance?: any;
|
|
13
|
+
getToken?: (response: unknown, url?: string) => string;
|
|
14
|
+
};
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}
|
package/package.json
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@auth-strategy-manager/rest",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "REST API strategy for auth-strategy-manager",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"build": "tsc",
|
|
9
|
+
"prepare": "npm run build",
|
|
10
|
+
"prepublishOnly": "npm run build"
|
|
11
|
+
},
|
|
12
|
+
"files": [
|
|
13
|
+
"dist/**/*",
|
|
14
|
+
"README.md"
|
|
15
|
+
],
|
|
16
|
+
"dependencies": {
|
|
17
|
+
"@auth-strategy-manager/core": "^1.0.0",
|
|
18
|
+
"axios": ">=1.5.1"
|
|
19
|
+
},
|
|
20
|
+
"peerDependencies": {
|
|
21
|
+
"axios": ">=1.5.1"
|
|
22
|
+
},
|
|
23
|
+
"keywords": [
|
|
24
|
+
"authentication",
|
|
25
|
+
"authorization",
|
|
26
|
+
"strategy",
|
|
27
|
+
"rest",
|
|
28
|
+
"api",
|
|
29
|
+
"auth",
|
|
30
|
+
"http"
|
|
31
|
+
],
|
|
32
|
+
"author": "azarov-serge <s.a.azarov@gmail.com>",
|
|
33
|
+
"license": "ISC",
|
|
34
|
+
"repository": {
|
|
35
|
+
"type": "git",
|
|
36
|
+
"url": "git+https://github.com/azarov-serge/auth-strategy-manager.git"
|
|
37
|
+
},
|
|
38
|
+
"bugs": {
|
|
39
|
+
"url": "https://github.com/azarov-serge/auth-strategy-manager/issues"
|
|
40
|
+
},
|
|
41
|
+
"homepage": "https://github.com/azarov-serge/auth-strategy-manager#readme"
|
|
42
|
+
}
|