@soapjs/soap-auth 0.1.1 → 0.3.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 +120 -169
- package/build/factories/http-auth-strategy.factory.js +1 -1
- package/build/factories/index.d.ts +3 -0
- package/build/factories/index.js +19 -0
- package/build/index.d.ts +4 -25
- package/build/index.js +4 -25
- package/build/session/index.d.ts +3 -0
- package/build/session/index.js +19 -0
- package/build/soap-auth.d.ts +9 -9
- package/build/soap-auth.js +64 -34
- package/build/strategies/api-key/api-key.strategy.d.ts +4 -3
- package/build/strategies/api-key/api-key.strategy.js +9 -6
- package/build/strategies/api-key/api-key.types.d.ts +2 -4
- package/build/strategies/base-auth.strategy.d.ts +4 -3
- package/build/strategies/base-auth.strategy.js +18 -2
- package/build/strategies/basic/basic.strategy.d.ts +5 -11
- package/build/strategies/basic/basic.strategy.js +14 -19
- package/build/strategies/basic/basic.types.d.ts +2 -2
- package/build/strategies/{credential-based-auth.strategy.d.ts → credential-auth.strategy.d.ts} +15 -12
- package/build/strategies/{credential-based-auth.strategy.js → credential-auth.strategy.js} +95 -46
- package/build/strategies/index.d.ts +16 -0
- package/build/strategies/index.js +32 -0
- package/build/strategies/jwt/jwt.strategy.d.ts +17 -2
- package/build/strategies/jwt/jwt.strategy.js +118 -57
- package/build/strategies/jwt/jwt.tools.d.ts +7 -3
- package/build/strategies/jwt/jwt.tools.js +80 -41
- package/build/strategies/jwt/jwt.types.d.ts +3 -27
- package/build/strategies/local/local.strategy.d.ts +3 -9
- package/build/strategies/local/local.strategy.js +7 -58
- package/build/strategies/local/local.types.d.ts +2 -2
- package/build/strategies/oauth2/oauth2.strategy.d.ts +21 -7
- package/build/strategies/oauth2/oauth2.strategy.js +158 -49
- package/build/strategies/oauth2/oauth2.types.d.ts +8 -16
- package/build/strategies/token-auth.strategy.d.ts +25 -0
- package/build/strategies/token-auth.strategy.js +78 -0
- package/build/tools/index.d.ts +3 -0
- package/build/tools/index.js +19 -0
- package/build/types.d.ts +87 -57
- package/package.json +2 -1
- package/build/strategies/token-based-auth.strategy.d.ts +0 -25
- package/build/strategies/token-based-auth.strategy.js +0 -130
|
@@ -5,77 +5,138 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.JwtStrategy = void 0;
|
|
7
7
|
const jsonwebtoken_1 = __importDefault(require("jsonwebtoken"));
|
|
8
|
-
const
|
|
8
|
+
const token_auth_strategy_1 = require("../token-auth.strategy");
|
|
9
9
|
const errors_1 = require("../../errors");
|
|
10
10
|
const jwt_tools_1 = require("./jwt.tools");
|
|
11
|
-
class JwtStrategy extends
|
|
11
|
+
class JwtStrategy extends token_auth_strategy_1.TokenAuthStrategy {
|
|
12
12
|
config;
|
|
13
13
|
session;
|
|
14
14
|
logger;
|
|
15
|
+
accessTokenConfig;
|
|
16
|
+
refreshTokenConfig;
|
|
15
17
|
constructor(config, session, logger) {
|
|
16
|
-
if (!config.accessToken.secretKey) {
|
|
18
|
+
if (!config.accessToken.issuer.secretKey) {
|
|
17
19
|
throw new errors_1.UndefinedTokenSecretError("Access");
|
|
18
20
|
}
|
|
19
|
-
if (config.refreshToken && !config.refreshToken.secretKey) {
|
|
21
|
+
if (config.refreshToken && !config.refreshToken.issuer.secretKey) {
|
|
20
22
|
throw new errors_1.UndefinedTokenSecretError("Refresh");
|
|
21
23
|
}
|
|
22
|
-
|
|
23
|
-
const refreshTokenConfig = (0, jwt_tools_1.prepareRefreshTokenConfig)(config);
|
|
24
|
-
super(config, {
|
|
25
|
-
...accessTokenConfig,
|
|
26
|
-
generate(payload) {
|
|
27
|
-
return jsonwebtoken_1.default.sign(payload, accessTokenConfig.secretKey, accessTokenConfig.signOptions);
|
|
28
|
-
},
|
|
29
|
-
verify(token) {
|
|
30
|
-
try {
|
|
31
|
-
if (!token)
|
|
32
|
-
throw new errors_1.UndefinedTokenError("Access");
|
|
33
|
-
if (!accessTokenConfig.secretKey)
|
|
34
|
-
throw new errors_1.UndefinedTokenSecretError("Access");
|
|
35
|
-
return new Promise((resolve, reject) => {
|
|
36
|
-
jsonwebtoken_1.default.verify(token, accessTokenConfig.secretKey, accessTokenConfig.verifyOptions, (err, payload) => {
|
|
37
|
-
if (err)
|
|
38
|
-
reject(err);
|
|
39
|
-
else
|
|
40
|
-
resolve(payload);
|
|
41
|
-
});
|
|
42
|
-
});
|
|
43
|
-
}
|
|
44
|
-
catch (error) {
|
|
45
|
-
this.logger?.error("JWT verification failed:", error);
|
|
46
|
-
throw new errors_1.InvalidTokenError("Access");
|
|
47
|
-
}
|
|
48
|
-
},
|
|
49
|
-
}, {
|
|
50
|
-
...refreshTokenConfig,
|
|
51
|
-
generate(payload) {
|
|
52
|
-
return jsonwebtoken_1.default.sign(payload, refreshTokenConfig.secretKey, refreshTokenConfig.signOptions);
|
|
53
|
-
},
|
|
54
|
-
verify(token) {
|
|
55
|
-
try {
|
|
56
|
-
if (!token)
|
|
57
|
-
throw new errors_1.UndefinedTokenError("Refresh");
|
|
58
|
-
if (!refreshTokenConfig.secretKey)
|
|
59
|
-
throw new errors_1.UndefinedTokenSecretError("Refresh");
|
|
60
|
-
return new Promise((resolve, reject) => {
|
|
61
|
-
jsonwebtoken_1.default.verify(token, refreshTokenConfig.secretKey, refreshTokenConfig.verifyOptions, (err, payload) => {
|
|
62
|
-
if (err)
|
|
63
|
-
reject(err);
|
|
64
|
-
else
|
|
65
|
-
resolve(payload);
|
|
66
|
-
});
|
|
67
|
-
});
|
|
68
|
-
}
|
|
69
|
-
catch (error) {
|
|
70
|
-
this.logger?.error("JWT verification failed:", error);
|
|
71
|
-
throw new errors_1.InvalidTokenError("Refresh");
|
|
72
|
-
}
|
|
73
|
-
},
|
|
74
|
-
});
|
|
24
|
+
super(config, session, logger);
|
|
75
25
|
this.config = config;
|
|
76
26
|
this.session = session;
|
|
77
27
|
this.logger = logger;
|
|
28
|
+
this.accessTokenConfig = (0, jwt_tools_1.prepareAccessTokenConfig)(config.accessToken);
|
|
29
|
+
this.refreshTokenConfig = (0, jwt_tools_1.prepareRefreshTokenConfig)(config.refreshToken);
|
|
78
30
|
this.logger?.info("JWTStrategy initialized with provided configurations.");
|
|
79
31
|
}
|
|
32
|
+
async invalidateRefreshToken(token) {
|
|
33
|
+
await this.refreshTokenConfig.persistence.remove?.(token);
|
|
34
|
+
}
|
|
35
|
+
verifyAccessToken(token) {
|
|
36
|
+
try {
|
|
37
|
+
if (!token)
|
|
38
|
+
throw new errors_1.UndefinedTokenError("Access");
|
|
39
|
+
if (!this.accessTokenConfig.issuer.secretKey)
|
|
40
|
+
throw new errors_1.UndefinedTokenSecretError("Access");
|
|
41
|
+
return new Promise((resolve, reject) => {
|
|
42
|
+
jsonwebtoken_1.default.verify(token, this.accessTokenConfig.issuer.secretKey, this.accessTokenConfig.verifier.options, (err, payload) => {
|
|
43
|
+
if (err)
|
|
44
|
+
reject(err);
|
|
45
|
+
else
|
|
46
|
+
resolve(payload);
|
|
47
|
+
});
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
catch (error) {
|
|
51
|
+
this.logger?.error("JWT verification failed:", error);
|
|
52
|
+
throw new errors_1.InvalidTokenError("Access");
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
verifyRefreshToken(token) {
|
|
56
|
+
try {
|
|
57
|
+
if (!token)
|
|
58
|
+
throw new errors_1.UndefinedTokenError("Refresh");
|
|
59
|
+
if (!this.refreshTokenConfig.issuer.secretKey)
|
|
60
|
+
throw new errors_1.UndefinedTokenSecretError("Refresh");
|
|
61
|
+
return new Promise((resolve, reject) => {
|
|
62
|
+
jsonwebtoken_1.default.verify(token, this.refreshTokenConfig.issuer.secretKey, this.refreshTokenConfig.verifier.options, (err, payload) => {
|
|
63
|
+
if (err)
|
|
64
|
+
reject(err);
|
|
65
|
+
else
|
|
66
|
+
resolve(payload);
|
|
67
|
+
});
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
catch (error) {
|
|
71
|
+
this.logger?.error("JWT verification failed:", error);
|
|
72
|
+
throw new errors_1.InvalidTokenError("Refresh");
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
generateAccessToken(payload) {
|
|
76
|
+
const options = this.accessTokenConfig.issuer.options || {};
|
|
77
|
+
return jsonwebtoken_1.default.sign(payload, this.accessTokenConfig.issuer.secretKey, {
|
|
78
|
+
...options,
|
|
79
|
+
jti: payload.jti || crypto.randomUUID(),
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
generateRefreshToken(payload) {
|
|
83
|
+
const options = this.refreshTokenConfig.issuer.options || {};
|
|
84
|
+
return jsonwebtoken_1.default.sign(payload, this.refreshTokenConfig.issuer.secretKey, {
|
|
85
|
+
...options,
|
|
86
|
+
jti: payload.jti || crypto.randomUUID(),
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
async storeAccessToken(token, context) {
|
|
90
|
+
if (this.accessTokenConfig.persistence.store) {
|
|
91
|
+
await this.accessTokenConfig.persistence.store(token, null, this.accessTokenConfig.issuer.options.expiresIn);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
async storeRefreshToken(token, context) {
|
|
95
|
+
if (this.refreshTokenConfig.persistence.store) {
|
|
96
|
+
await this.refreshTokenConfig.persistence.store(token, null, this.refreshTokenConfig.issuer.options.expiresIn);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
embedAccessToken(token, context) {
|
|
100
|
+
if (this.accessTokenConfig.embed) {
|
|
101
|
+
this.accessTokenConfig.embed(context, token);
|
|
102
|
+
}
|
|
103
|
+
else {
|
|
104
|
+
(0, jwt_tools_1.setDefaultJwtHeader)(token, context);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
embedRefreshToken(token, context) {
|
|
108
|
+
if (this.refreshTokenConfig.embed) {
|
|
109
|
+
this.refreshTokenConfig.embed(context, token);
|
|
110
|
+
}
|
|
111
|
+
else {
|
|
112
|
+
(0, jwt_tools_1.setDefaultJwtCookie)(token, context);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
retrieveAccessToken(context) {
|
|
116
|
+
if (this.accessTokenConfig.retrieve) {
|
|
117
|
+
return this.accessTokenConfig.retrieve(context);
|
|
118
|
+
}
|
|
119
|
+
else {
|
|
120
|
+
return (context.req.headers.authorization?.split(" ")[1] ||
|
|
121
|
+
context.request.headers.authorization?.split(" ")[1] ||
|
|
122
|
+
context.headers.authorization?.split(" ")[1]);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
retrieveRefreshToken(context) {
|
|
126
|
+
if (this.refreshTokenConfig.retrieve) {
|
|
127
|
+
return this.refreshTokenConfig.retrieve(context);
|
|
128
|
+
}
|
|
129
|
+
return (context.req.cookies?.refreshToken ||
|
|
130
|
+
context.request.cookies?.refreshToken ||
|
|
131
|
+
context.cookies.refreshToken);
|
|
132
|
+
}
|
|
133
|
+
async logout(context) {
|
|
134
|
+
const refreshToken = await this.retrieveRefreshToken(context);
|
|
135
|
+
if (refreshToken) {
|
|
136
|
+
await this.invalidateRefreshToken(refreshToken);
|
|
137
|
+
(0, jwt_tools_1.clearDefaultJwtCookie)(context);
|
|
138
|
+
(0, jwt_tools_1.clearDefaultJwtHeader)(context);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
80
141
|
}
|
|
81
142
|
exports.JwtStrategy = JwtStrategy;
|
|
@@ -1,3 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export declare const prepareAccessTokenConfig: (config:
|
|
3
|
-
export declare const prepareRefreshTokenConfig: (config:
|
|
1
|
+
import { TokenConfig } from "../../types";
|
|
2
|
+
export declare const prepareAccessTokenConfig: <TContext = any>(config: TokenConfig<TContext>) => TokenConfig<TContext>;
|
|
3
|
+
export declare const prepareRefreshTokenConfig: <TContext = any>(config: TokenConfig<TContext>) => TokenConfig<TContext>;
|
|
4
|
+
export declare const setDefaultJwtCookie: (token: string, context: any) => void;
|
|
5
|
+
export declare const setDefaultJwtHeader: (token: string, context: any) => void;
|
|
6
|
+
export declare const clearDefaultJwtHeader: (context: any) => void;
|
|
7
|
+
export declare const clearDefaultJwtCookie: (context: any) => void;
|
|
@@ -23,57 +23,96 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
23
23
|
return result;
|
|
24
24
|
};
|
|
25
25
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
-
exports.prepareRefreshTokenConfig = exports.prepareAccessTokenConfig = void 0;
|
|
26
|
+
exports.clearDefaultJwtCookie = exports.clearDefaultJwtHeader = exports.setDefaultJwtHeader = exports.setDefaultJwtCookie = exports.prepareRefreshTokenConfig = exports.prepareAccessTokenConfig = void 0;
|
|
27
27
|
const Soap = __importStar(require("@soapjs/soap"));
|
|
28
28
|
const prepareAccessTokenConfig = (config) => {
|
|
29
29
|
return Soap.removeUndefinedProperties({
|
|
30
|
-
...config
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
30
|
+
...config,
|
|
31
|
+
generation: {
|
|
32
|
+
...config.issuer.options,
|
|
33
|
+
expiresIn: config.issuer.options.expiresIn || "1h",
|
|
34
|
+
algorithm: config.issuer.options.algorithm || "HS256",
|
|
35
|
+
},
|
|
36
|
+
verification: {
|
|
37
|
+
...config.verifier.options,
|
|
38
|
+
algorithms: config.verifier.options.algorithms || ["HS256"],
|
|
39
|
+
expiresIn: config.verifier.options.expiresIn || "1h",
|
|
40
40
|
},
|
|
41
|
-
verifyOptions: config.accessToken.verifyOptions
|
|
42
|
-
? {
|
|
43
|
-
...config.accessToken.verifyOptions,
|
|
44
|
-
algorithms: config.accessToken.verifyOptions.algorithms || ["HS256"],
|
|
45
|
-
expiresIn: config.accessToken.expiresIn || "1h",
|
|
46
|
-
audience: config.accessToken.audience,
|
|
47
|
-
issuer: config.accessToken.issuer,
|
|
48
|
-
subject: config.accessToken.subject,
|
|
49
|
-
}
|
|
50
|
-
: {},
|
|
51
41
|
});
|
|
52
42
|
};
|
|
53
43
|
exports.prepareAccessTokenConfig = prepareAccessTokenConfig;
|
|
54
44
|
const prepareRefreshTokenConfig = (config) => {
|
|
55
45
|
return Soap.removeUndefinedProperties({
|
|
56
|
-
...config
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
46
|
+
...config,
|
|
47
|
+
generation: {
|
|
48
|
+
...config.issuer,
|
|
49
|
+
expiresIn: config.issuer.options.expiresIn || "7d",
|
|
50
|
+
algorithm: config.issuer.options.algorithm || "HS256",
|
|
51
|
+
},
|
|
52
|
+
verification: {
|
|
53
|
+
...config.verifier.options,
|
|
54
|
+
algorithms: config.verifier.options.algorithms || ["HS256"],
|
|
55
|
+
expiresIn: config.verifier.options.expiresIn || "7d",
|
|
66
56
|
},
|
|
67
|
-
verifyOptions: config.refreshToken.verifyOptions
|
|
68
|
-
? {
|
|
69
|
-
...config.refreshToken.verifyOptions,
|
|
70
|
-
algorithm: config.refreshToken.verifyOptions.algorithms || ["HS256"],
|
|
71
|
-
expiresIn: config.refreshToken.expiresIn || "7d",
|
|
72
|
-
audience: config.refreshToken.audience,
|
|
73
|
-
issuer: config.refreshToken.issuer,
|
|
74
|
-
subject: config.refreshToken.subject,
|
|
75
|
-
}
|
|
76
|
-
: {},
|
|
77
57
|
});
|
|
78
58
|
};
|
|
79
59
|
exports.prepareRefreshTokenConfig = prepareRefreshTokenConfig;
|
|
60
|
+
const setDefaultJwtCookie = (token, context) => {
|
|
61
|
+
const options = {
|
|
62
|
+
httpOnly: true,
|
|
63
|
+
secure: true,
|
|
64
|
+
sameSite: "Strict",
|
|
65
|
+
maxAge: 7 * 24 * 60 * 60 * 1000,
|
|
66
|
+
};
|
|
67
|
+
if (context?.res) {
|
|
68
|
+
context.res.cookie("refreshToken", token, options);
|
|
69
|
+
}
|
|
70
|
+
else if (context?.response) {
|
|
71
|
+
context.response.cookie("refreshToken", token, options);
|
|
72
|
+
}
|
|
73
|
+
else if (context?.cookie) {
|
|
74
|
+
context.cookie("refreshToken", token, options);
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
exports.setDefaultJwtCookie = setDefaultJwtCookie;
|
|
78
|
+
const setDefaultJwtHeader = (token, context) => {
|
|
79
|
+
if (typeof context?.res?.setHeader === "function") {
|
|
80
|
+
context.res.setHeader("Authorization", `Bearer ${token}`);
|
|
81
|
+
}
|
|
82
|
+
else if (typeof context?.response?.setHeader === "function") {
|
|
83
|
+
context.response.setHeader("Authorization", `Bearer ${token}`);
|
|
84
|
+
}
|
|
85
|
+
else if (typeof context?.setHeader === "function") {
|
|
86
|
+
context.setHeader("Authorization", `Bearer ${token}`);
|
|
87
|
+
}
|
|
88
|
+
};
|
|
89
|
+
exports.setDefaultJwtHeader = setDefaultJwtHeader;
|
|
90
|
+
const clearDefaultJwtHeader = (context) => {
|
|
91
|
+
if (typeof context?.res?.setHeader === "function") {
|
|
92
|
+
context.res.setHeader("Authorization", ``);
|
|
93
|
+
}
|
|
94
|
+
else if (typeof context?.response?.setHeader === "function") {
|
|
95
|
+
context.response.setHeader("Authorization", ``);
|
|
96
|
+
}
|
|
97
|
+
else if (typeof context?.setHeader === "function") {
|
|
98
|
+
context.setHeader("Authorization", ``);
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
exports.clearDefaultJwtHeader = clearDefaultJwtHeader;
|
|
102
|
+
const clearDefaultJwtCookie = (context) => {
|
|
103
|
+
const options = {
|
|
104
|
+
httpOnly: true,
|
|
105
|
+
secure: true,
|
|
106
|
+
sameSite: "Strict",
|
|
107
|
+
};
|
|
108
|
+
if (typeof context?.res?.clearCookie === "function") {
|
|
109
|
+
context.res.clearCookie("refreshToken", options);
|
|
110
|
+
}
|
|
111
|
+
else if (typeof context?.response?.clearCookie === "function") {
|
|
112
|
+
context.response.clearCookie("refreshToken", options);
|
|
113
|
+
}
|
|
114
|
+
else if (typeof context?.clearCookie === "function") {
|
|
115
|
+
context.clearCookie("refreshToken", options);
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
exports.clearDefaultJwtCookie = clearDefaultJwtCookie;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { TokenAuthStrategyConfig } from "../../types";
|
|
2
2
|
export interface JwtVerifyOptions {
|
|
3
3
|
algorithms?: string[];
|
|
4
4
|
notBefore?: string | number;
|
|
@@ -12,36 +12,12 @@ export interface JwtVerifyOptions {
|
|
|
12
12
|
export interface JwtSignOptions {
|
|
13
13
|
algorithm?: "HS256" | "HS384" | "HS512" | "RS256" | "RS384" | "RS512" | "ES256" | "ES384" | "ES512" | "PS256" | "PS384" | "PS512" | "none";
|
|
14
14
|
notBefore?: string | number;
|
|
15
|
-
|
|
15
|
+
jti?: string;
|
|
16
16
|
issuedAt?: number;
|
|
17
17
|
mutatePayload?: (payload: Record<string, any>) => Record<string, any>;
|
|
18
18
|
noTimestamp?: boolean;
|
|
19
19
|
keyid?: string;
|
|
20
20
|
allowUnsafe?: boolean;
|
|
21
21
|
}
|
|
22
|
-
export
|
|
23
|
-
verifyOptions?: JwtVerifyOptions;
|
|
24
|
-
signOptions: JwtSignOptions;
|
|
25
|
-
} & TokenConfig;
|
|
26
|
-
export type JwtRefreshTokenConfig = {
|
|
27
|
-
verifyOptions?: JwtVerifyOptions;
|
|
28
|
-
signOptions: JwtSignOptions;
|
|
29
|
-
} & TokenConfig;
|
|
30
|
-
export interface JwtConfig<TContext = unknown, TUser = unknown> extends TokenBasedAuthStrategyConfig<TContext, TUser> {
|
|
31
|
-
accessToken: JwtAccessTokenConfig;
|
|
32
|
-
refreshToken?: JwtRefreshTokenConfig;
|
|
33
|
-
routes?: {
|
|
34
|
-
login?: {
|
|
35
|
-
path: string;
|
|
36
|
-
method?: "POST" | "GET";
|
|
37
|
-
};
|
|
38
|
-
logout?: {
|
|
39
|
-
path: string;
|
|
40
|
-
method?: "POST" | "GET";
|
|
41
|
-
};
|
|
42
|
-
refresh?: {
|
|
43
|
-
path: string;
|
|
44
|
-
method?: "POST" | "GET";
|
|
45
|
-
};
|
|
46
|
-
};
|
|
22
|
+
export interface JwtConfig<TContext = unknown, TUser = unknown> extends TokenAuthStrategyConfig<TContext, TUser> {
|
|
47
23
|
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import * as Soap from "@soapjs/soap";
|
|
2
|
-
import {
|
|
2
|
+
import { CredentialAuthStrategy } from "../credential-auth.strategy";
|
|
3
3
|
import { LocalStrategyConfig } from "./local.types";
|
|
4
4
|
import { SessionHandler } from "../../session/session-handler";
|
|
5
|
-
export declare class LocalStrategy<TContext = unknown, TUser = unknown> extends
|
|
5
|
+
export declare class LocalStrategy<TContext = unknown, TUser = unknown> extends CredentialAuthStrategy<TContext, TUser> {
|
|
6
6
|
protected config: LocalStrategyConfig<TContext, TUser>;
|
|
7
7
|
protected session?: SessionHandler;
|
|
8
8
|
protected logger?: Soap.Logger;
|
|
@@ -11,15 +11,9 @@ export declare class LocalStrategy<TContext = unknown, TUser = unknown> extends
|
|
|
11
11
|
identifier: string;
|
|
12
12
|
password: string;
|
|
13
13
|
}>;
|
|
14
|
-
protected verifyCredentials(
|
|
15
|
-
identifier: string;
|
|
16
|
-
password: string;
|
|
17
|
-
}): Promise<boolean>;
|
|
14
|
+
protected verifyCredentials(identifier: string, password: string): Promise<boolean>;
|
|
18
15
|
protected retrieveUser(credentials: {
|
|
19
16
|
identifier: string;
|
|
20
17
|
password: string;
|
|
21
18
|
}): Promise<TUser | null>;
|
|
22
|
-
requestPasswordReset(email: string): Promise<void>;
|
|
23
|
-
resetPassword(email: string, token: string, newPassword: string): Promise<void>;
|
|
24
|
-
changePassword(email: string, oldPassword: string, newPassword: string): Promise<void>;
|
|
25
19
|
}
|
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.LocalStrategy = void 0;
|
|
4
|
-
const
|
|
5
|
-
|
|
6
|
-
class LocalStrategy extends credential_based_auth_strategy_1.CredentialBasedAuthStrategy {
|
|
4
|
+
const credential_auth_strategy_1 = require("../credential-auth.strategy");
|
|
5
|
+
class LocalStrategy extends credential_auth_strategy_1.CredentialAuthStrategy {
|
|
7
6
|
config;
|
|
8
7
|
session;
|
|
9
8
|
logger;
|
|
@@ -13,64 +12,14 @@ class LocalStrategy extends credential_based_auth_strategy_1.CredentialBasedAuth
|
|
|
13
12
|
this.session = session;
|
|
14
13
|
this.logger = logger;
|
|
15
14
|
}
|
|
16
|
-
|
|
17
|
-
return this.config.
|
|
15
|
+
extractCredentials(context) {
|
|
16
|
+
return this.config.credentials.extractCredentials(context);
|
|
18
17
|
}
|
|
19
|
-
async verifyCredentials(
|
|
20
|
-
return this.config.
|
|
18
|
+
async verifyCredentials(identifier, password) {
|
|
19
|
+
return this.config.credentials.verifyCredentials(identifier, password);
|
|
21
20
|
}
|
|
22
21
|
async retrieveUser(credentials) {
|
|
23
|
-
return this.config.
|
|
24
|
-
}
|
|
25
|
-
async requestPasswordReset(email) {
|
|
26
|
-
try {
|
|
27
|
-
if (!this.config.passwordReset?.generateResetToken) {
|
|
28
|
-
throw new Error("Password reset token generation is not configured.");
|
|
29
|
-
}
|
|
30
|
-
const token = await this.config.passwordReset.generateResetToken(email);
|
|
31
|
-
await this.config.passwordReset.sendResetEmail?.(email, token);
|
|
32
|
-
this.logger?.info(`Password reset requested for email: ${email}`);
|
|
33
|
-
await this.config.passwordReset.onSuccess?.({ email });
|
|
34
|
-
}
|
|
35
|
-
catch (error) {
|
|
36
|
-
this.logger?.error("Password reset request error:", error);
|
|
37
|
-
await this.config.passwordReset.onFailure?.({ email, error });
|
|
38
|
-
throw new errors_1.AuthError(error, "Password reset request failed.");
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
async resetPassword(email, token, newPassword) {
|
|
42
|
-
try {
|
|
43
|
-
if (!this.config.passwordReset?.validateResetToken) {
|
|
44
|
-
throw new Error("Password reset token validation is not configured.");
|
|
45
|
-
}
|
|
46
|
-
const isValid = await this.config.passwordReset.validateResetToken(token);
|
|
47
|
-
if (!isValid)
|
|
48
|
-
throw new Error("Invalid or expired reset token.");
|
|
49
|
-
await this.config.passwordReset.updatePassword(email, newPassword);
|
|
50
|
-
this.logger?.info(`Password reset successful for email: ${email}`);
|
|
51
|
-
await this.config.passwordReset.onSuccess?.({ email });
|
|
52
|
-
}
|
|
53
|
-
catch (error) {
|
|
54
|
-
this.logger?.error("Password reset error:", error);
|
|
55
|
-
await this.config.passwordReset.onFailure?.({ email, error });
|
|
56
|
-
throw new errors_1.AuthError(error, "Password reset failed.");
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
async changePassword(email, oldPassword, newPassword) {
|
|
60
|
-
try {
|
|
61
|
-
const isAuthenticated = await this.config.login.verifyUserCredentials(email, oldPassword);
|
|
62
|
-
if (!isAuthenticated) {
|
|
63
|
-
throw new errors_1.InvalidCredentialsError();
|
|
64
|
-
}
|
|
65
|
-
await this.config.passwordReset?.updatePassword?.(email, newPassword);
|
|
66
|
-
this.logger?.info(`Password changed successfully for email: ${email}`);
|
|
67
|
-
await this.config.passwordReset?.onSuccess?.({ email });
|
|
68
|
-
}
|
|
69
|
-
catch (error) {
|
|
70
|
-
this.logger?.error("Change password error:", error);
|
|
71
|
-
await this.config.passwordReset?.onFailure?.({ email, error });
|
|
72
|
-
throw new errors_1.AuthError(error, "Change password failed.");
|
|
73
|
-
}
|
|
22
|
+
return this.config.user.getUserData(credentials.identifier);
|
|
74
23
|
}
|
|
75
24
|
}
|
|
76
25
|
exports.LocalStrategy = LocalStrategy;
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export interface LocalStrategyConfig<TContext = unknown, TUser = unknown> extends
|
|
1
|
+
import { CredentialAuthStrategyConfig } from "../../types";
|
|
2
|
+
export interface LocalStrategyConfig<TContext = unknown, TUser = unknown> extends CredentialAuthStrategyConfig<TContext, TUser> {
|
|
3
3
|
}
|
|
@@ -1,16 +1,30 @@
|
|
|
1
1
|
import * as Soap from "@soapjs/soap";
|
|
2
|
-
import {
|
|
3
|
-
import { TokenBasedAuthStrategy } from "../token-based-auth.strategy";
|
|
2
|
+
import { AuthResult } from "../../types";
|
|
4
3
|
import { OAuth2StrategyConfig } from "./oauth2.types";
|
|
5
4
|
import { SessionHandler } from "../../session/session-handler";
|
|
6
|
-
|
|
5
|
+
import { BaseAuthStrategy } from "../base-auth.strategy";
|
|
6
|
+
export declare class OAuth2Strategy<TContext = unknown, TUser = unknown> extends BaseAuthStrategy<TContext, TUser> {
|
|
7
7
|
protected config: OAuth2StrategyConfig<TContext, TUser>;
|
|
8
|
-
protected accessTokenConfig: TokenConfig;
|
|
9
|
-
protected refreshTokenConfig?: TokenConfig;
|
|
10
8
|
protected session?: SessionHandler;
|
|
11
9
|
protected logger?: Soap.Logger;
|
|
12
|
-
constructor(config: OAuth2StrategyConfig<TContext, TUser>,
|
|
10
|
+
constructor(config: OAuth2StrategyConfig<TContext, TUser>, session?: SessionHandler, logger?: Soap.Logger);
|
|
11
|
+
logout(context: TContext): Promise<void>;
|
|
12
|
+
protected getCredentialsForPasswordGrant(context: TContext): Promise<{
|
|
13
|
+
identifier: string;
|
|
14
|
+
password: string;
|
|
15
|
+
}>;
|
|
16
|
+
protected retrieveAccessToken(context: TContext): Promise<string | undefined>;
|
|
17
|
+
protected retrieveRefreshToken(context: TContext): Promise<string | undefined>;
|
|
18
|
+
protected storeAccessToken(token: string, context: TContext): Promise<void>;
|
|
19
|
+
protected storeRefreshToken(token: string, context: TContext): Promise<void>;
|
|
20
|
+
protected embedAccessToken(token: string, context: TContext): void;
|
|
21
|
+
protected embedRefreshToken(token: string, context: TContext): void;
|
|
22
|
+
isTokenExpired(token: string): Promise<boolean>;
|
|
13
23
|
authenticate(context: TContext): Promise<AuthResult<TUser>>;
|
|
24
|
+
protected processOAuthFlow(context: TContext): Promise<{
|
|
25
|
+
accessToken: string;
|
|
26
|
+
refreshToken?: string;
|
|
27
|
+
}>;
|
|
14
28
|
protected verifyAuthorizationCode(context: TContext, code: string): void;
|
|
15
29
|
protected extractAuthorizationCode(context: TContext): string | null;
|
|
16
30
|
protected redirectUser(context: TContext, authUrl: string): void;
|
|
@@ -24,7 +38,7 @@ export declare class OAuth2Strategy<TContext = unknown, TUser = unknown> extends
|
|
|
24
38
|
protected exchangeClientCredentials(): Promise<{
|
|
25
39
|
accessToken: string;
|
|
26
40
|
}>;
|
|
27
|
-
protected exchangePasswordGrant(): Promise<{
|
|
41
|
+
protected exchangePasswordGrant(username: string, password: string): Promise<{
|
|
28
42
|
accessToken: string;
|
|
29
43
|
}>;
|
|
30
44
|
refreshAccessToken(context: TContext): Promise<{
|