Files
huly-platform/server/token/src/token.ts
T
2022-11-16 22:03:03 +07:00

34 lines
930 B
TypeScript

import { getWorkspaceId, WorkspaceId } from '@hcengineering/core'
import { getMetadata } from '@hcengineering/platform'
import { decode, encode } from 'jwt-simple'
import serverPlugin from './plugin'
/**
* @public
*/
export interface Token {
email: string
workspace: WorkspaceId
extra?: Record<string, string>
}
const getSecret = (): string => {
return getMetadata(serverPlugin.metadata.Secret) ?? 'secret'
}
/**
* @public
*/
export function generateToken (email: string, workspace: WorkspaceId, extra?: Record<string, string>): string {
return encode({ ...(extra ?? {}), email, workspace: workspace.name, productId: workspace.productId }, getSecret())
}
/**
* @public
*/
export function decodeToken (token: string): Token {
const value = decode(token, getSecret(), false)
const { email, workspace, productId, ...extra } = value
return { email, workspace: getWorkspaceId(workspace, productId), extra }
}