Skip to content

Commit

Permalink
linting
Browse files Browse the repository at this point in the history
  • Loading branch information
jkyberneees committed Aug 9, 2023
1 parent a7874e2 commit 2ed4134
Showing 1 changed file with 53 additions and 55 deletions.
108 changes: 53 additions & 55 deletions libs/Token.ts
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)
}
}

0 comments on commit 2ed4134

Please sign in to comment.