platform/server/token/src/token.ts
Andrey Sobolev 659063dd01 Update 'secret' token usage
1. Put 'secret' into one place, server-token
2. Fix server crash on rare unknown workspaces cases.
3. Upgrade now kick all clients and reload transactions.
4. Fix Front shutdown

Signed-off-by: Andrey Sobolev <haiodo@gmail.com>
2022-01-27 16:07:27 +07:00

33 lines
751 B
TypeScript

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