mirror of
https://github.com/Foltik/Shimapan
synced 2024-11-11 07:54:55 -05:00
30 lines
716 B
JavaScript
30 lines
716 B
JavaScript
|
var fs = require('fs');
|
||
|
var path = require('path');
|
||
|
var mongoose = require('mongoose');
|
||
|
var jwt = require('jsonwebtoken');
|
||
|
var jwtsign = require('jwt-sign');
|
||
|
|
||
|
var TokenSchema = mongoose.Schema({
|
||
|
scope: [String],
|
||
|
issuer: String,
|
||
|
issued: Date,
|
||
|
exp: Date
|
||
|
});
|
||
|
|
||
|
TokenSchema.methods.genJwt = function(expiry) {
|
||
|
var exp = new Date();
|
||
|
exp.setDate(exp.getDate() + expiry);
|
||
|
|
||
|
var payload = {
|
||
|
_id: this._id,
|
||
|
username: this.username,
|
||
|
scope: this.scope,
|
||
|
exp: parseInt(exp.getTime() / 1000)
|
||
|
};
|
||
|
|
||
|
var key = fs.readFilySync(path.join(__dirname, '../../jwt.pem'), 'utf8');
|
||
|
|
||
|
return jwt.sign(payload, key);
|
||
|
};
|
||
|
|
||
|
module.exports = mongoose.model('Token', TokenSchema);
|