60 lines
1.5 KiB
TypeScript
60 lines
1.5 KiB
TypeScript
|
|
import { DateHelper } from "../../browser/date/DateHelper";
|
||
|
|
import { DateMath } from "../../browser/date/DateMath";
|
||
|
|
import { ISOTimeStamp } from "../../browser/date/ISOTimeStamp";
|
||
|
|
import { CryptIO } from "../crypt/CryptIO";
|
||
|
|
import { Token } from "./Token";
|
||
|
|
import { UserManagementServer } from "./UserManagementServer";
|
||
|
|
|
||
|
|
export class TokenDB
|
||
|
|
{
|
||
|
|
static _defaultExpirationDurationMinutes = 20;
|
||
|
|
|
||
|
|
_ums:UserManagementServer;
|
||
|
|
_tokens = new Map<string,Token>();
|
||
|
|
|
||
|
|
constructor( ums:UserManagementServer )
|
||
|
|
{
|
||
|
|
this._ums = ums;
|
||
|
|
}
|
||
|
|
|
||
|
|
async update()
|
||
|
|
{
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
async create( ip:string, expireDurationInMinutes:number = -1 ):Promise<Token>
|
||
|
|
{
|
||
|
|
expireDurationInMinutes = expireDurationInMinutes == -1 ? TokenDB._defaultExpirationDurationMinutes : expireDurationInMinutes;
|
||
|
|
|
||
|
|
let token = new Token();
|
||
|
|
token.id = CryptIO.createUUID();
|
||
|
|
token.hashedIP = await CryptIO.hash( ip );
|
||
|
|
token.expires = ISOTimeStamp.fromDate( DateMath.fromNowAddMinutes( expireDurationInMinutes ) );
|
||
|
|
|
||
|
|
this._tokens.set( token.id, token );
|
||
|
|
|
||
|
|
return Promise.resolve( token );
|
||
|
|
}
|
||
|
|
|
||
|
|
async validate( token:Token, ip:string ):Promise<boolean>
|
||
|
|
{
|
||
|
|
if ( this._tokens.get( token.id ) != token )
|
||
|
|
{
|
||
|
|
return Promise.resolve( false );
|
||
|
|
}
|
||
|
|
|
||
|
|
if ( DateMath.isExpired( ISOTimeStamp.toDate( token.expires ) ) )
|
||
|
|
{
|
||
|
|
return Promise.resolve( false );
|
||
|
|
}
|
||
|
|
|
||
|
|
let isVerified = await CryptIO.verifyHash( ip, token.hashedIP );
|
||
|
|
|
||
|
|
if ( isVerified )
|
||
|
|
{
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
return Promise.resolve( false );
|
||
|
|
}
|
||
|
|
}
|