@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
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@soapjs/soap-auth",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "",
|
|
5
5
|
"homepage": "https://docs.soapjs.com",
|
|
6
6
|
"repository": "https://github.com/soapjs/soap-auth",
|
|
@@ -28,6 +28,7 @@
|
|
|
28
28
|
"axios": "^1.7.9",
|
|
29
29
|
"bcrypt": "^5.1.1",
|
|
30
30
|
"jsonwebtoken": "^9.0.2",
|
|
31
|
+
"jwks-rsa": "^3.1.0",
|
|
31
32
|
"uuid": "^9.0.1"
|
|
32
33
|
}
|
|
33
34
|
}
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import * as Soap from "@soapjs/soap";
|
|
2
|
-
import { AuthResult, TokenBasedAuthStrategyConfig } from "../types";
|
|
3
|
-
import { BaseAuthStrategy } from "./base-auth.strategy";
|
|
4
|
-
import { TokenConfig } from "../types";
|
|
5
|
-
import { SessionHandler } from "../session/session-handler";
|
|
6
|
-
export declare abstract class TokenBasedAuthStrategy<TContext = unknown, TUser = unknown> extends BaseAuthStrategy<TContext, TUser> {
|
|
7
|
-
protected config: TokenBasedAuthStrategyConfig<TContext, TUser>;
|
|
8
|
-
protected accessTokenConfig?: TokenConfig;
|
|
9
|
-
protected refreshTokenConfig?: TokenConfig;
|
|
10
|
-
protected session?: SessionHandler;
|
|
11
|
-
protected logger?: Soap.Logger;
|
|
12
|
-
constructor(config: TokenBasedAuthStrategyConfig<TContext, TUser>, accessTokenConfig?: TokenConfig, refreshTokenConfig?: TokenConfig, session?: SessionHandler, logger?: Soap.Logger);
|
|
13
|
-
protected retrieveUser(decodedToken: any): Promise<TUser | null>;
|
|
14
|
-
authenticate(context: TContext): Promise<AuthResult<TUser>>;
|
|
15
|
-
logout(context: TContext): Promise<void>;
|
|
16
|
-
generateTokens(user: TUser, context: TContext): Promise<{
|
|
17
|
-
accessToken: string;
|
|
18
|
-
refreshToken?: string;
|
|
19
|
-
}>;
|
|
20
|
-
rotateToken(context: TContext): Promise<{
|
|
21
|
-
accessToken: string;
|
|
22
|
-
refreshToken?: string;
|
|
23
|
-
}>;
|
|
24
|
-
isTokenExpired(token: string): Promise<boolean>;
|
|
25
|
-
}
|
|
@@ -1,130 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.TokenBasedAuthStrategy = void 0;
|
|
4
|
-
const base_auth_strategy_1 = require("./base-auth.strategy");
|
|
5
|
-
const errors_1 = require("../errors");
|
|
6
|
-
class TokenBasedAuthStrategy extends base_auth_strategy_1.BaseAuthStrategy {
|
|
7
|
-
config;
|
|
8
|
-
accessTokenConfig;
|
|
9
|
-
refreshTokenConfig;
|
|
10
|
-
session;
|
|
11
|
-
logger;
|
|
12
|
-
constructor(config, accessTokenConfig, refreshTokenConfig, session, logger) {
|
|
13
|
-
super(config, session, logger);
|
|
14
|
-
this.config = config;
|
|
15
|
-
this.accessTokenConfig = accessTokenConfig;
|
|
16
|
-
this.refreshTokenConfig = refreshTokenConfig;
|
|
17
|
-
this.session = session;
|
|
18
|
-
this.logger = logger;
|
|
19
|
-
}
|
|
20
|
-
retrieveUser(decodedToken) {
|
|
21
|
-
return this.config.login.retrieveUserData(decodedToken);
|
|
22
|
-
}
|
|
23
|
-
async authenticate(context) {
|
|
24
|
-
try {
|
|
25
|
-
let accessToken = await this.accessTokenConfig.retrieve?.(context);
|
|
26
|
-
let refreshToken;
|
|
27
|
-
await this.checkRateLimit(context);
|
|
28
|
-
if (accessToken) {
|
|
29
|
-
try {
|
|
30
|
-
const decoded = await this.accessTokenConfig.verify?.(accessToken);
|
|
31
|
-
const user = await this.retrieveUser(decoded);
|
|
32
|
-
if (!user) {
|
|
33
|
-
throw new errors_1.UserNotFoundError();
|
|
34
|
-
}
|
|
35
|
-
await this.isAuthorized(user);
|
|
36
|
-
return { user, tokens: { accessToken } };
|
|
37
|
-
}
|
|
38
|
-
catch (error) {
|
|
39
|
-
this.logger?.warn("Access token is invalid or expired, trying refresh token...");
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
if (this.refreshTokenConfig) {
|
|
43
|
-
refreshToken = await this.refreshTokenConfig?.retrieve?.(context);
|
|
44
|
-
if (!refreshToken) {
|
|
45
|
-
throw new errors_1.MissingTokenError("Refresh");
|
|
46
|
-
}
|
|
47
|
-
accessToken = await this.refreshTokenConfig?.rotate?.(refreshToken);
|
|
48
|
-
if (!accessToken) {
|
|
49
|
-
throw new errors_1.MissingTokenError("Access");
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
this.accessTokenConfig.embed?.(context, accessToken);
|
|
53
|
-
const decoded = await this.accessTokenConfig.verify?.(accessToken);
|
|
54
|
-
const user = await this.retrieveUser(decoded);
|
|
55
|
-
if (!user) {
|
|
56
|
-
throw new errors_1.UserNotFoundError();
|
|
57
|
-
}
|
|
58
|
-
await this.isAuthorized(user);
|
|
59
|
-
return { user, tokens: { accessToken, refreshToken } };
|
|
60
|
-
}
|
|
61
|
-
catch (error) {
|
|
62
|
-
this.logger?.error("Authentication failed:", error);
|
|
63
|
-
throw new errors_1.AuthError(error, "Authentication failed.");
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
async logout(context) {
|
|
67
|
-
try {
|
|
68
|
-
await this.accessTokenConfig.remove?.(context);
|
|
69
|
-
if (this.refreshTokenConfig) {
|
|
70
|
-
await this.refreshTokenConfig.remove?.(context);
|
|
71
|
-
}
|
|
72
|
-
await this.config.logout.onSuccess?.(context);
|
|
73
|
-
this.logger?.info("User logged out successfully.");
|
|
74
|
-
}
|
|
75
|
-
catch (error) {
|
|
76
|
-
this.logger?.error("Error during logout:", error);
|
|
77
|
-
await this.config.logout.onFailure?.({ context, error });
|
|
78
|
-
throw new errors_1.AuthError(error, "Logout process failed.");
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
async generateTokens(user, context) {
|
|
82
|
-
const payload = { userId: user.id, roles: user.roles };
|
|
83
|
-
const accessToken = this.accessTokenConfig.generate?.(payload);
|
|
84
|
-
if (!accessToken)
|
|
85
|
-
throw new Error("Failed to generate access token.");
|
|
86
|
-
await this.accessTokenConfig.store?.(accessToken, user, +this.accessTokenConfig.expiresIn);
|
|
87
|
-
this.accessTokenConfig.embed?.(context, accessToken);
|
|
88
|
-
let refreshToken;
|
|
89
|
-
if (this.refreshTokenConfig) {
|
|
90
|
-
refreshToken = this.refreshTokenConfig.generate?.(payload);
|
|
91
|
-
if (refreshToken) {
|
|
92
|
-
await this.refreshTokenConfig.store?.(refreshToken, user, +this.refreshTokenConfig.expiresIn);
|
|
93
|
-
this.refreshTokenConfig.embed?.(context, refreshToken);
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
return { accessToken, refreshToken };
|
|
97
|
-
}
|
|
98
|
-
async rotateToken(context) {
|
|
99
|
-
try {
|
|
100
|
-
if (!this.refreshTokenConfig) {
|
|
101
|
-
throw new Error("Refresh token handler is not configured.");
|
|
102
|
-
}
|
|
103
|
-
const refreshToken = await this.refreshTokenConfig.retrieve?.(context);
|
|
104
|
-
if (!refreshToken) {
|
|
105
|
-
throw new errors_1.MissingTokenError("Refresh");
|
|
106
|
-
}
|
|
107
|
-
const newAccessToken = await this.refreshTokenConfig.rotate?.(refreshToken);
|
|
108
|
-
this.accessTokenConfig.embed?.(context, newAccessToken);
|
|
109
|
-
return { accessToken: newAccessToken, refreshToken };
|
|
110
|
-
}
|
|
111
|
-
catch (error) {
|
|
112
|
-
this.logger?.error("Token rotation failed:", error);
|
|
113
|
-
throw new errors_1.InvalidTokenError("Refresh");
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
async isTokenExpired(token) {
|
|
117
|
-
try {
|
|
118
|
-
const decoded = JSON.parse(Buffer.from(token.split(".")[1], "base64").toString());
|
|
119
|
-
if (!decoded.exp)
|
|
120
|
-
return false;
|
|
121
|
-
const currentTime = Math.floor(Date.now() / 1000);
|
|
122
|
-
return decoded.exp < currentTime;
|
|
123
|
-
}
|
|
124
|
-
catch (error) {
|
|
125
|
-
this.logger?.warn("Failed to decode token:", error);
|
|
126
|
-
return false;
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
exports.TokenBasedAuthStrategy = TokenBasedAuthStrategy;
|