@expressive-analytics/deep-thought-authentication
Version:
Typescript conversion of Deep Thought Authentication
44 lines (38 loc) • 1.36 kB
text/typescript
import {DT,DTModel} from '@expressive-analytics/deep-thought-js'
const crypto = require('crypto')
const dt_password_charset = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789"
const dt_salt_charset = "abcdef0123456789"
export class DTUserModel extends DTModel {
protected static $T = "user"
protected static _strict_properties = true
protected encryptPassword(pass:string,salt?:string){
if(salt===undefined)
salt = this.generateString(5,dt_password_charset)
salt = salt.substring(0,10)
const sha1 = crypto.createHash("sha1")
sha1.update(pass+salt)
return sha1.digest('hex').substring(salt.length)+salt // encrypted = tail of sha1 + salt
}
protected generateString(len=8,charset=dt_password_charset):string{
let str = ""
for(let i=0;i<len;i++){
let n = Math.random()*charset.length
str += charset.substring(n,1)
}
return str
}
verifyPassword(given,salt_len=5){
const encrypted = this.$get<string>('password') // accessor returns null
const salt = salt_len>0?encrypted.substring(encrypted.length-salt_len,salt_len):""
const password = this.encryptPassword(given,salt)
return encrypted===password
}
}
export interface DTUser extends DT {
alias: string
created_at?: string
created_at_ms?: number
is_admin: number
is_active: number
is_deleted: number
}