@flink-app/jwt-auth-plugin 0.12.1-alpha.33 → 0.12.1-alpha.34

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.
@@ -3,7 +3,7 @@ import jwtSimple from "jwt-simple";
3
3
  export interface JwtAuthPluginOptions {
4
4
  secret: string;
5
5
  algo?: jwtSimple.TAlgorithm;
6
- getUser: (tokenData: any) => Promise<FlinkAuthUser>;
6
+ getUser: (tokenData: any) => Promise<FlinkAuthUser | null | undefined>;
7
7
  passwordPolicy?: RegExp;
8
8
  tokenTTL?: number;
9
9
  rolePermissions: {
@@ -76,12 +76,8 @@ function jwtAuthPlugin(_a) {
76
76
  })];
77
77
  });
78
78
  }); },
79
- createToken: function (payload, roles) {
80
- return createToken(__assign(__assign({}, payload), { roles: roles }), { algo: algo, secret: secret, tokenTTL: tokenTTL });
81
- },
82
- createPasswordHashAndSalt: function (password) {
83
- return createPasswordHashAndSalt(password, passwordPolicy);
84
- },
79
+ createToken: function (payload, roles) { return createToken(__assign(__assign({}, payload), { roles: roles }), { algo: algo, secret: secret, tokenTTL: tokenTTL }); },
80
+ createPasswordHashAndSalt: function (password) { return createPasswordHashAndSalt(password, passwordPolicy); },
85
81
  validatePassword: validatePassword,
86
82
  };
87
83
  }
@@ -100,13 +96,11 @@ function authenticateRequest(req_1, routePermissions_1, rolePermissions_1, _a) {
100
96
  decodedToken = jwt_simple_1.default.decode(token, secret, false, algo);
101
97
  }
102
98
  catch (err) {
103
- flink_1.log.debug("Failed to decode token: ".concat(err));
99
+ flink_1.log.debug("[JWT AUTH PLUGIN] Failed to decode token: ".concat(err));
104
100
  decodedToken = null;
105
101
  }
106
102
  if (!decodedToken) return [3 /*break*/, 2];
107
- permissionsArr = Array.isArray(routePermissions)
108
- ? routePermissions
109
- : [routePermissions];
103
+ permissionsArr = Array.isArray(routePermissions) ? routePermissions : [routePermissions];
110
104
  if (permissionsArr && permissionsArr.length > 0) {
111
105
  validPerms = (0, PermissionValidator_1.hasValidPermissions)(decodedToken.roles || [], rolePermissions, permissionsArr);
112
106
  if (!validPerms) {
@@ -116,6 +110,10 @@ function authenticateRequest(req_1, routePermissions_1, rolePermissions_1, _a) {
116
110
  return [4 /*yield*/, getUser(decodedToken)];
117
111
  case 1:
118
112
  user = _c.sent();
113
+ if (!user) {
114
+ flink_1.log.debug("[JWT AUTH PLUGIN] User not returned from getUser callback");
115
+ return [2 /*return*/, false];
116
+ }
119
117
  req.user = user;
120
118
  return [2 /*return*/, true];
121
119
  case 2: return [2 /*return*/, false];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@flink-app/jwt-auth-plugin",
3
- "version": "0.12.1-alpha.33",
3
+ "version": "0.12.1-alpha.34",
4
4
  "description": "Flink plugin for JWT auth",
5
5
  "scripts": {
6
6
  "test": "node --preserve-symlinks -r ts-node/register -- node_modules/jasmine/bin/jasmine --config=./spec/support/jasmine.json",
@@ -31,5 +31,5 @@
31
31
  "tsc-watch": "^4.2.9",
32
32
  "typescript": "5.4.5"
33
33
  },
34
- "gitHead": "eec3a22c21db0e7fec84190bf7a74c1e430e5ec4"
34
+ "gitHead": "633fab738980da1433aff45adc9da8ab0755bd69"
35
35
  }
@@ -1,9 +1,4 @@
1
- import {
2
- FlinkAuthPlugin,
3
- FlinkAuthUser,
4
- FlinkRequest,
5
- log,
6
- } from "@flink-app/flink";
1
+ import { FlinkAuthPlugin, FlinkAuthUser, FlinkRequest, log } from "@flink-app/flink";
7
2
  import jwtSimple from "jwt-simple";
8
3
  import { encrypt, genSalt } from "./BcryptUtils";
9
4
  import { hasValidPermissions } from "./PermissionValidator";
@@ -15,166 +10,141 @@ import { hasValidPermissions } from "./PermissionValidator";
15
10
  const defaultPasswordPolicy = /^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,}$/;
16
11
 
17
12
  export interface JwtAuthPluginOptions {
18
- secret: string;
19
- algo?: jwtSimple.TAlgorithm;
20
- getUser: (tokenData: any) => Promise<FlinkAuthUser>;
21
- passwordPolicy?: RegExp;
22
- tokenTTL? : number;
23
- rolePermissions: {
24
- [role: string]: string[];
25
- };
13
+ secret: string;
14
+ algo?: jwtSimple.TAlgorithm;
15
+ getUser: (tokenData: any) => Promise<FlinkAuthUser | null | undefined>;
16
+ passwordPolicy?: RegExp;
17
+ tokenTTL?: number;
18
+ rolePermissions: {
19
+ [role: string]: string[];
20
+ };
26
21
  }
27
22
 
28
23
  export interface JwtAuthPlugin extends FlinkAuthPlugin {
29
- /**
30
- * Encodes and returns JWT token that includes provided payload.
31
- *
32
- * The payload can by anything but should in most cases be and object that
33
- * holds user information including an identifier such as the username or id.
34
- */
35
- createToken: (payload: any, roles: string[]) => Promise<string>;
36
-
37
- /**
38
- * Generates new password hash and salt for provided password.
39
- *
40
- * This method should be used when setting a new password. Both hash and salt needs
41
- * to be saved in database as both are needed to validate the password.
42
- *
43
- * Returns null if password does not match configured `passwordPolicy`.
44
- */
45
- createPasswordHashAndSalt: (
46
- password: string
47
- ) => Promise<{ hash: string; salt: string } | null>;
48
-
49
- /**
50
- * Validates that provided `password` is same as provided `hash`.
51
- */
52
- validatePassword: (
53
- password: string,
54
- passwordHash: string,
55
- salt: string
56
- ) => Promise<boolean>;
24
+ /**
25
+ * Encodes and returns JWT token that includes provided payload.
26
+ *
27
+ * The payload can by anything but should in most cases be and object that
28
+ * holds user information including an identifier such as the username or id.
29
+ */
30
+ createToken: (payload: any, roles: string[]) => Promise<string>;
31
+
32
+ /**
33
+ * Generates new password hash and salt for provided password.
34
+ *
35
+ * This method should be used when setting a new password. Both hash and salt needs
36
+ * to be saved in database as both are needed to validate the password.
37
+ *
38
+ * Returns null if password does not match configured `passwordPolicy`.
39
+ */
40
+ createPasswordHashAndSalt: (password: string) => Promise<{ hash: string; salt: string } | null>;
41
+
42
+ /**
43
+ * Validates that provided `password` is same as provided `hash`.
44
+ */
45
+ validatePassword: (password: string, passwordHash: string, salt: string) => Promise<boolean>;
57
46
  }
58
47
 
59
48
  /**
60
49
  * Configures and creates authentication plugin.
61
50
  */
62
51
  export function jwtAuthPlugin({
63
- secret,
64
- getUser,
65
- rolePermissions,
66
- algo = "HS256",
67
- passwordPolicy = defaultPasswordPolicy,
68
- tokenTTL = 1000 * 60 * 60 * 24 * 365 * 100, //Defaults to hundred year
52
+ secret,
53
+ getUser,
54
+ rolePermissions,
55
+ algo = "HS256",
56
+ passwordPolicy = defaultPasswordPolicy,
57
+ tokenTTL = 1000 * 60 * 60 * 24 * 365 * 100, //Defaults to hundred year
69
58
  }: JwtAuthPluginOptions): JwtAuthPlugin {
70
- return {
71
- authenticateRequest: async (req, permissions) =>
72
- authenticateRequest(req, permissions, rolePermissions, {
73
- algo,
74
- secret,
75
- getUser,
76
- }),
77
- createToken: (payload, roles) =>
78
- createToken({ ...payload, roles }, { algo, secret, tokenTTL }),
79
- createPasswordHashAndSalt: (password: string) =>
80
- createPasswordHashAndSalt(password, passwordPolicy),
81
- validatePassword,
82
- };
59
+ return {
60
+ authenticateRequest: async (req, permissions) =>
61
+ authenticateRequest(req, permissions, rolePermissions, {
62
+ algo,
63
+ secret,
64
+ getUser,
65
+ }),
66
+ createToken: (payload, roles) => createToken({ ...payload, roles }, { algo, secret, tokenTTL }),
67
+ createPasswordHashAndSalt: (password: string) => createPasswordHashAndSalt(password, passwordPolicy),
68
+ validatePassword,
69
+ };
83
70
  }
84
71
 
85
72
  async function authenticateRequest(
86
- req: FlinkRequest,
87
- routePermissions: string | string[],
88
- rolePermissions: { [x: string]: string[] },
89
- {
90
- secret,
91
- algo,
92
- getUser,
93
- }: Pick<JwtAuthPluginOptions, "algo" | "secret" | "getUser">
73
+ req: FlinkRequest,
74
+ routePermissions: string | string[],
75
+ rolePermissions: { [x: string]: string[] },
76
+ { secret, algo, getUser }: Pick<JwtAuthPluginOptions, "algo" | "secret" | "getUser">
94
77
  ) {
95
- const token = getTokenFromReq(req);
78
+ const token = getTokenFromReq(req);
96
79
 
97
- if (token) {
98
- let decodedToken;
80
+ if (token) {
81
+ let decodedToken;
99
82
 
100
- try {
101
- decodedToken = jwtSimple.decode(token, secret, false, algo);
102
- } catch (err) {
103
- log.debug(`Failed to decode token: ${err}`);
104
- decodedToken = null;
105
- }
83
+ try {
84
+ decodedToken = jwtSimple.decode(token, secret, false, algo);
85
+ } catch (err) {
86
+ log.debug(`[JWT AUTH PLUGIN] Failed to decode token: ${err}`);
87
+ decodedToken = null;
88
+ }
106
89
 
107
- if (decodedToken) {
108
- const permissionsArr = Array.isArray(routePermissions)
109
- ? routePermissions
110
- : [routePermissions];
90
+ if (decodedToken) {
91
+ const permissionsArr = Array.isArray(routePermissions) ? routePermissions : [routePermissions];
111
92
 
112
- if (permissionsArr && permissionsArr.length > 0) {
113
- const validPerms = hasValidPermissions(
114
- decodedToken.roles || [],
115
- rolePermissions,
116
- permissionsArr
117
- );
93
+ if (permissionsArr && permissionsArr.length > 0) {
94
+ const validPerms = hasValidPermissions(decodedToken.roles || [], rolePermissions, permissionsArr);
118
95
 
119
- if (!validPerms) {
120
- return false;
121
- }
122
- }
96
+ if (!validPerms) {
97
+ return false;
98
+ }
99
+ }
123
100
 
124
- const user = await getUser(decodedToken);
101
+ const user = await getUser(decodedToken);
125
102
 
103
+ if (!user) {
104
+ log.debug("[JWT AUTH PLUGIN] User not returned from getUser callback");
105
+ return false;
106
+ }
126
107
 
127
- req.user = user;
128
- return true;
108
+ req.user = user;
109
+ return true;
110
+ }
129
111
  }
130
- }
131
- return false;
112
+ return false;
132
113
  }
133
114
 
134
115
  function getTokenFromReq(req: FlinkRequest) {
135
- const authHeader = req.headers.authorization;
136
- if (authHeader) {
137
- const [, token] = authHeader.split("Bearer ");
138
- return token;
139
- }
140
- return;
116
+ const authHeader = req.headers.authorization;
117
+ if (authHeader) {
118
+ const [, token] = authHeader.split("Bearer ");
119
+ return token;
120
+ }
121
+ return;
141
122
  }
142
123
 
143
- async function createToken(
144
- payload: any,
145
- { secret, algo, tokenTTL }: Pick<JwtAuthPluginOptions, "algo" | "secret" | "tokenTTL" >
146
- ) {
147
- if (!payload) {
148
- throw new Error("Cannot create token - payload is missing");
149
- }
124
+ async function createToken(payload: any, { secret, algo, tokenTTL }: Pick<JwtAuthPluginOptions, "algo" | "secret" | "tokenTTL">) {
125
+ if (!payload) {
126
+ throw new Error("Cannot create token - payload is missing");
127
+ }
150
128
 
151
- return jwtSimple.encode({exp : _calculateExpiration(tokenTTL || 1000 * 60 * 60 * 24 * 365 * 100), ...payload}, secret, algo);
129
+ return jwtSimple.encode({ exp: _calculateExpiration(tokenTTL || 1000 * 60 * 60 * 24 * 365 * 100), ...payload }, secret, algo);
152
130
  }
153
131
 
154
- function _calculateExpiration(expiresInMs : number) {
132
+ function _calculateExpiration(expiresInMs: number) {
155
133
  return Math.floor((Date.now() + expiresInMs) / 1000);
156
134
  }
157
135
 
136
+ async function createPasswordHashAndSalt(password: string, passwordPolicy: RegExp) {
137
+ if (!passwordPolicy.test(password)) {
138
+ log.debug(`Password does not match password policy '${passwordPolicy}'`);
139
+ return null;
140
+ }
158
141
 
159
- async function createPasswordHashAndSalt(
160
- password: string,
161
- passwordPolicy: RegExp
162
- ) {
163
- if (!passwordPolicy.test(password)) {
164
- log.debug(`Password does not match password policy '${passwordPolicy}'`);
165
- return null;
166
- }
167
-
168
- const salt = await genSalt(10);
169
- const hash = await encrypt(password, salt);
170
- return { salt, hash };
142
+ const salt = await genSalt(10);
143
+ const hash = await encrypt(password, salt);
144
+ return { salt, hash };
171
145
  }
172
146
 
173
- async function validatePassword(
174
- password: string,
175
- passwordHash: string,
176
- salt: string
177
- ) {
178
- const hashCandidate = await encrypt(password, salt);
179
- return hashCandidate === passwordHash;
147
+ async function validatePassword(password: string, passwordHash: string, salt: string) {
148
+ const hashCandidate = await encrypt(password, salt);
149
+ return hashCandidate === passwordHash;
180
150
  }