@flink-app/jwt-auth-plugin 0.5.3 → 0.6.1

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.
@@ -5,6 +5,7 @@ export interface JwtAuthPluginOptions {
5
5
  algo?: jwtSimple.TAlgorithm;
6
6
  getUser: (tokenData: any) => Promise<FlinkAuthUser>;
7
7
  passwordPolicy?: RegExp;
8
+ tokenTTL?: number;
8
9
  rolePermissions: {
9
10
  [role: string]: string[];
10
11
  };
@@ -37,4 +38,4 @@ export interface JwtAuthPlugin extends FlinkAuthPlugin {
37
38
  /**
38
39
  * Configures and creates authentication plugin.
39
40
  */
40
- export declare function jwtAuthPlugin({ secret, getUser, rolePermissions, algo, passwordPolicy, }: JwtAuthPluginOptions): JwtAuthPlugin;
41
+ export declare function jwtAuthPlugin({ secret, getUser, rolePermissions, algo, passwordPolicy, tokenTTL, }: JwtAuthPluginOptions): JwtAuthPlugin;
@@ -65,7 +65,7 @@ var defaultPasswordPolicy = /^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,}$/;
65
65
  */
66
66
  function jwtAuthPlugin(_a) {
67
67
  var _this = this;
68
- var secret = _a.secret, getUser = _a.getUser, rolePermissions = _a.rolePermissions, _b = _a.algo, algo = _b === void 0 ? "HS256" : _b, _c = _a.passwordPolicy, passwordPolicy = _c === void 0 ? defaultPasswordPolicy : _c;
68
+ var secret = _a.secret, getUser = _a.getUser, rolePermissions = _a.rolePermissions, _b = _a.algo, algo = _b === void 0 ? "HS256" : _b, _c = _a.passwordPolicy, passwordPolicy = _c === void 0 ? defaultPasswordPolicy : _c, _d = _a.tokenTTL, tokenTTL = _d === void 0 ? 1000 * 60 * 60 * 24 * 365 * 100 : _d;
69
69
  return {
70
70
  authenticateRequest: function (req, permissions) { return __awaiter(_this, void 0, void 0, function () {
71
71
  return __generator(this, function (_a) {
@@ -77,7 +77,7 @@ function jwtAuthPlugin(_a) {
77
77
  });
78
78
  }); },
79
79
  createToken: function (payload, roles) {
80
- return createToken(__assign(__assign({}, payload), { roles: roles }), { algo: algo, secret: secret });
80
+ return createToken(__assign(__assign({}, payload), { roles: roles }), { algo: algo, secret: secret, tokenTTL: tokenTTL });
81
81
  },
82
82
  createPasswordHashAndSalt: function (password) {
83
83
  return createPasswordHashAndSalt(password, passwordPolicy);
@@ -132,16 +132,19 @@ function getTokenFromReq(req) {
132
132
  return;
133
133
  }
134
134
  function createToken(payload, _a) {
135
- var secret = _a.secret, algo = _a.algo;
135
+ var secret = _a.secret, algo = _a.algo, tokenTTL = _a.tokenTTL;
136
136
  return __awaiter(this, void 0, void 0, function () {
137
137
  return __generator(this, function (_b) {
138
138
  if (!payload) {
139
139
  throw new Error("Cannot create token - payload is missing");
140
140
  }
141
- return [2 /*return*/, jwt_simple_1.default.encode(payload, secret, algo)];
141
+ return [2 /*return*/, jwt_simple_1.default.encode(__assign({ exp: _calculateExpiration(tokenTTL || 1000 * 60 * 60 * 24 * 365 * 100) }, payload), secret, algo)];
142
142
  });
143
143
  });
144
144
  }
145
+ function _calculateExpiration(expiresInMs) {
146
+ return Math.floor((Date.now() + expiresInMs) / 1000);
147
+ }
145
148
  function createPasswordHashAndSalt(password, passwordPolicy) {
146
149
  return __awaiter(this, void 0, void 0, function () {
147
150
  var salt, hash;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@flink-app/jwt-auth-plugin",
3
- "version": "0.5.3",
3
+ "version": "0.6.1",
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",
@@ -20,7 +20,7 @@
20
20
  "jwt-simple": "^0.5.6"
21
21
  },
22
22
  "devDependencies": {
23
- "@flink-app/flink": "^0.5.0",
23
+ "@flink-app/flink": "^0.6.0",
24
24
  "@types/bcrypt": "^5.0.0",
25
25
  "@types/jasmine": "^3.7.1",
26
26
  "@types/node": "^15.6.2",
@@ -31,5 +31,5 @@
31
31
  "tsc-watch": "^4.2.9",
32
32
  "typescript": "^4.2.4"
33
33
  },
34
- "gitHead": "c5e47bae95b9e157f77936d7b8996a2534c26a3f"
34
+ "gitHead": "86e5a311e8945c87f32d038a63658483076ffcff"
35
35
  }
@@ -19,6 +19,7 @@ export interface JwtAuthPluginOptions {
19
19
  algo?: jwtSimple.TAlgorithm;
20
20
  getUser: (tokenData: any) => Promise<FlinkAuthUser>;
21
21
  passwordPolicy?: RegExp;
22
+ tokenTTL? : number;
22
23
  rolePermissions: {
23
24
  [role: string]: string[];
24
25
  };
@@ -64,6 +65,7 @@ export function jwtAuthPlugin({
64
65
  rolePermissions,
65
66
  algo = "HS256",
66
67
  passwordPolicy = defaultPasswordPolicy,
68
+ tokenTTL = 1000 * 60 * 60 * 24 * 365 * 100, //Defaults to hundred year
67
69
  }: JwtAuthPluginOptions): JwtAuthPlugin {
68
70
  return {
69
71
  authenticateRequest: async (req, permissions) =>
@@ -73,7 +75,7 @@ export function jwtAuthPlugin({
73
75
  getUser,
74
76
  }),
75
77
  createToken: (payload, roles) =>
76
- createToken({ ...payload, roles }, { algo, secret }),
78
+ createToken({ ...payload, roles }, { algo, secret, tokenTTL }),
77
79
  createPasswordHashAndSalt: (password: string) =>
78
80
  createPasswordHashAndSalt(password, passwordPolicy),
79
81
  validatePassword,
@@ -140,15 +142,20 @@ function getTokenFromReq(req: FlinkRequest) {
140
142
 
141
143
  async function createToken(
142
144
  payload: any,
143
- { secret, algo }: Pick<JwtAuthPluginOptions, "algo" | "secret">
145
+ { secret, algo, tokenTTL }: Pick<JwtAuthPluginOptions, "algo" | "secret" | "tokenTTL" >
144
146
  ) {
145
147
  if (!payload) {
146
148
  throw new Error("Cannot create token - payload is missing");
147
149
  }
148
150
 
149
- return jwtSimple.encode(payload, secret, algo);
151
+ return jwtSimple.encode({exp : _calculateExpiration(tokenTTL || 1000 * 60 * 60 * 24 * 365 * 100), ...payload}, secret, algo);
150
152
  }
151
153
 
154
+ function _calculateExpiration(expiresInMs : number) {
155
+ return Math.floor((Date.now() + expiresInMs) / 1000);
156
+ }
157
+
158
+
152
159
  async function createPasswordHashAndSalt(
153
160
  password: string,
154
161
  passwordPolicy: RegExp