kauth-sdk-node
Advanced tools
Comparing version 0.1.16 to 0.1.17
@@ -0,1 +1,10 @@ | ||
## [0.1.17](https://gitlab-ag.marmot-cloud.com/marmot/kauth-sdk-node/compare/v0.1.16...v0.1.17) (2021-03-01) | ||
### Features | ||
* jwt refresh ([61aa920](https://gitlab-ag.marmot-cloud.com/marmot/kauth-sdk-node/commit/61aa920bb0614b3c9e6b21e47a0bb04853ab8d50)) | ||
## [0.1.16](https://gitlab-ag.marmot-cloud.com/marmot/kauth-sdk-node/compare/v0.1.15...v0.1.16) (2021-02-09) | ||
@@ -2,0 +11,0 @@ |
@@ -6,5 +6,7 @@ import { AuthenticationInformation } from './base'; | ||
verify(token: string): Promise<number>; | ||
refresh(token: string): string; | ||
} | ||
export interface AuthenticationProvider { | ||
verify(token: string): Promise<AuthenticationInformation>; | ||
refresh(token: string): string; | ||
authenticate(authenticationInformation: AuthenticationInformation): string; | ||
@@ -14,4 +16,6 @@ } | ||
private secret; | ||
private duration; | ||
constructor(kauthConfig: KauthConfig); | ||
verify(token: string): Promise<number>; | ||
refresh(token: string): string; | ||
} | ||
@@ -24,3 +28,4 @@ export declare class JsonWebTokenAuthenticationProvider implements AuthenticationProvider { | ||
verify(token: string): Promise<AuthenticationInformation>; | ||
refresh(token: string): string; | ||
authenticate(authenticationInformation: AuthenticationInformation): string; | ||
} |
@@ -15,2 +15,3 @@ "use strict"; | ||
this.secret = kauthConfig.jwtSecret; | ||
this.duration = kauthConfig.jwtDuration; | ||
} | ||
@@ -26,2 +27,13 @@ async verify(token) { | ||
} | ||
refresh(token) { | ||
const { iat, ...claims } = jsonwebtoken_1.default.decode(token); | ||
// jwt life duration | ||
const duration = Math.floor(Date.now() / 1000) - iat; | ||
if (duration > this.duration / 2 && duration < this.duration) { | ||
const exp = dayjs_1.default().add(this.duration, 'second').unix(); | ||
claims[Exp] = exp; | ||
return jsonwebtoken_1.default.sign(claims, this.secret); | ||
} | ||
return null; | ||
} | ||
} | ||
@@ -45,2 +57,13 @@ exports.JsonWebTokenSubjectProvider = JsonWebTokenSubjectProvider; | ||
} | ||
refresh(token) { | ||
const { iat, ...claims } = jsonwebtoken_1.default.decode(token); | ||
// jwt life duration | ||
const duration = Math.floor(Date.now() / 1000) - iat; | ||
if (duration > this.duration / 2 && duration < this.duration) { | ||
const exp = dayjs_1.default().add(this.duration, 'second').unix(); | ||
claims[Exp] = exp; | ||
return jsonwebtoken_1.default.sign(claims, this.secret); | ||
} | ||
return null; | ||
} | ||
authenticate(authenticationInformation) { | ||
@@ -47,0 +70,0 @@ const exp = dayjs_1.default().add(this.duration, 'second').unix(); |
{ | ||
"name": "kauth-sdk-node", | ||
"version": "0.1.16", | ||
"version": "0.1.17", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
75984
1414