-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a7874e2
commit 2ed4134
Showing
1 changed file
with
53 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,85 +1,83 @@ | ||
import { decode } from 'jsonwebtoken' | ||
|
||
export interface ITokenContent { | ||
[key: string]: any, | ||
[key: string]: any | ||
|
||
/** | ||
/** | ||
* Authorization server’s identifier | ||
*/ | ||
iss: string, | ||
iss: string | ||
|
||
/** | ||
/** | ||
* User’s identifier | ||
*/ | ||
sub: string, | ||
sub: string | ||
|
||
/** | ||
/** | ||
* Client’s identifier | ||
*/ | ||
aud: string | string[], | ||
aud: string | string[] | ||
|
||
/** | ||
/** | ||
* Expiration time of the ID token | ||
*/ | ||
exp: number, | ||
exp: number | ||
|
||
/** | ||
/** | ||
* Time at which JWT was issued | ||
*/ | ||
iat: number, | ||
|
||
family_name?: string, | ||
given_name?: string, | ||
name?: string, | ||
email?: string, | ||
preferred_username?: string, | ||
email_verified?: boolean, | ||
iat: number | ||
|
||
family_name?: string | ||
given_name?: string | ||
name?: string | ||
email?: string | ||
preferred_username?: string | ||
email_verified?: boolean | ||
|
||
} | ||
|
||
export class Token { | ||
public readonly token: string | ||
public readonly content: ITokenContent | ||
|
||
constructor(token: string) { | ||
this.token = token | ||
const jwtPayload = decode(this.token, {json: true}); | ||
if ( | ||
jwtPayload !== null && | ||
jwtPayload.iss !== undefined && | ||
jwtPayload.sub !== undefined && | ||
jwtPayload.aud !== undefined && | ||
jwtPayload.exp !== undefined && | ||
jwtPayload.iat !== undefined | ||
) { | ||
this.content = { | ||
...jwtPayload, | ||
iss: jwtPayload.iss, | ||
sub: jwtPayload.sub, | ||
aud: jwtPayload.aud, | ||
exp: jwtPayload.exp, | ||
iat: jwtPayload.iat, | ||
} | ||
} else { | ||
throw new Error('Invalid token'); | ||
} | ||
} | ||
public readonly token: string | ||
public readonly content: ITokenContent | ||
|
||
isExpired(): boolean { | ||
return (this.content.exp * 1000) <= Date.now() | ||
constructor (token: string) { | ||
this.token = token | ||
const payload = decode(this.token, { json: true }) | ||
if ( | ||
payload?.iss !== undefined && | ||
payload?.sub !== undefined && | ||
payload?.aud !== undefined && | ||
payload?.exp !== undefined && | ||
payload?.iat !== undefined | ||
) { | ||
this.content = { | ||
...payload, | ||
iss: payload.iss, | ||
sub: payload.sub, | ||
aud: payload.aud, | ||
exp: payload.exp, | ||
iat: payload.iat | ||
} | ||
} else { | ||
throw new Error('Invalid token') | ||
} | ||
} | ||
|
||
hasApplicationRole(appName: string, roleName: string): boolean { | ||
const appRoles = this.content.resource_access[appName] | ||
if (appRoles == null) { | ||
return false | ||
} | ||
isExpired (): boolean { | ||
return (this.content.exp * 1000) <= Date.now() | ||
} | ||
|
||
return (appRoles.roles.indexOf(roleName) >= 0) | ||
hasApplicationRole (appName: string, roleName: string): boolean { | ||
const appRoles = this.content.resource_access[appName] | ||
if (appRoles == null) { | ||
return false | ||
} | ||
|
||
hasRealmRole(roleName: string): boolean { | ||
return (this.content.realm_access.roles.indexOf(roleName) >= 0) | ||
} | ||
return (appRoles.roles.indexOf(roleName) >= 0) | ||
} | ||
|
||
hasRealmRole (roleName: string): boolean { | ||
return (this.content.realm_access.roles.indexOf(roleName) >= 0) | ||
} | ||
} |