@expressive-analytics/deep-thought-authentication
Version:
Typescript conversion of Deep Thought Authentication
62 lines (54 loc) • 1.29 kB
text/typescript
const md5 = require('blueimp-md5')
import {DTModel} from '@expressive-analytics/deep-thought-js'
export class DTOAuthTokenModel extends DTModel {
protected static _strict_properties = true;
protected static $T = "tokens";
type = 0;
status = 0;
_token?
protected _secret;
protected consumer_id;
public user_id;
public authorized_at;
public oauth_callback; //only required when using a custom callback (e.g. DTSwift)
updateToAccessToken(db){
this["type"] = 1;
this["status"] = 0;
this["token"] = this.generateToken();
this["secret"] = this.generateToken();
this.clean();
this.update(db);
}
generateToken(){
return md5(Math.random()).md5(Math.random());
}
/**
* if we don't have a token, we should generate one
*/
token(){
if(this._token===undefined)
this.token = this.generateToken();
return this.token;
}
/**
* if we don't have a secret, we should generate one
*/
secret(){
if(this._secret===undefined)
this.secret = this.generateToken();
return this.secret;
}
authorize(db,user_id){
this.$set("status",1)
this.$set("user_id",user_id)
this.$set("authorized_at",db.now())
this.clean();
this.update(db);
}
invalidate(db){
this.$set("type",0)
this.$set("status",2)
this.clean()
this.update(db)
}
}