import { marshall , Status , unmarshall } from "../deps.ts" ; import { DYNAMO_CLIENT } from "./clients.ts" ; import { DYNAMO_USER_TABLE } from "./constants.ts" ; import { generate } from "./token.ts" ; export interface User { id : number ; admin : boolean ; secret : string ; } export async function getUser ( id ) { const { Item : item } = await DYNAMO_CLIENT . getItem ( { TableName : DYNAMO_USER_TABLE , Key : marshall ( { id } ) } ) ; if ( item ) { return unmarshall ( item ) ; } } export async function createUser ( id , admin = false ) { const secret = generate ( ) ; try { await DYNAMO_CLIENT . putItem ( { TableName : DYNAMO_USER_TABLE , Item : marshall ( { id , secret , admin } ) } ) ; return { id , admin , secret } ; } catch ( e ) { } } export async function authenticate ( id , secret , admin = false ) { const user = await getUser ( id ) ; return ! ( user ?. secret !== secret && ( admin ? user ?. admin : true ) ) ; }