-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(general): access & refresh tokens
- Loading branch information
Sergey Peshkov
committed
Jan 31, 2020
1 parent
0bbdd06
commit d78a631
Showing
15 changed files
with
309 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
const { User, AccessToken, RefreshToken } = require('../models'); | ||
const { Sequelize } = require('../lib/sequelize'); | ||
const errors = require('../lib/errors'); | ||
|
||
module.exports.login = async (req, res) => { | ||
const user = await User.scope('withPassword').findOne({ | ||
[Sequelize.Op.or]: { | ||
email: req.body.login, | ||
username: req.body.login | ||
} | ||
}); | ||
|
||
if (!user) { | ||
return errors.makeNotFoundError(res, 'User is not found.'); | ||
} | ||
|
||
if (!await user.checkPassword(req.body.password)) { | ||
return errors.makeUnauthorizedError(res, 'Password is not valid.'); | ||
} | ||
|
||
if (!user.mail_confirmed_at) { | ||
return errors.makeUnauthorizedError(res, 'Please confirm your mail first.'); | ||
} | ||
|
||
const accessToken = await AccessToken.createForUser(user.id); | ||
const refreshToken = await RefreshToken.createForUser(user.id); | ||
|
||
return res.json({ | ||
success: true, | ||
data: { | ||
access_token: accessToken.value, | ||
refresh_token: refreshToken.value | ||
} | ||
}); | ||
}; | ||
|
||
module.exports.renew = async (req, res) => { | ||
const token = await RefreshToken.findOne({ | ||
value: req.body.token | ||
}); | ||
|
||
if (!token) { | ||
return res.status(401).json({ | ||
success: false, | ||
message: 'Token is not found.' | ||
}); | ||
} | ||
|
||
const accessToken = await AccessToken.createForUser(token.user_id); | ||
|
||
return res.json({ | ||
success: true, | ||
data: { | ||
access_token: accessToken.value, | ||
} | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
const { MailConfirmation, User } = require('../models'); | ||
const errors = require('../lib/errors'); | ||
|
||
|
||
module.exports.confirmEmail = async (req, res) => { | ||
if (!req.body.token) { | ||
return errors.makeBadRequestError(res, 'No token specified.'); | ||
} | ||
|
||
const confirmation = await MailConfirmation.findOne({ | ||
where: { value: req.body.token }, | ||
include: [User] | ||
}); | ||
|
||
if (!confirmation) { | ||
return errors.makeNotFoundError(res, 'The token is invalid.'); | ||
} | ||
|
||
if (confirmation.is_expired) { | ||
return errors.makeForbiddenError(res, 'The token has expired.'); | ||
} | ||
|
||
await confirmation.user.update({ mail_confirmed_at: new Date() }); | ||
await confirmation.destroy(); | ||
|
||
return res.json({ | ||
success: true, | ||
message: 'Your profile is activated.' | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
module.exports = { | ||
up: (queryInterface, Sequelize) => queryInterface.createTable('refresh_tokens', { | ||
id: { | ||
allowNull: false, | ||
autoIncrement: true, | ||
primaryKey: true, | ||
type: Sequelize.INTEGER | ||
}, | ||
user_id: { | ||
type: Sequelize.INTEGER, | ||
allowNull: false, | ||
references: { | ||
model: 'users', | ||
key: 'id' | ||
} | ||
}, | ||
value: { | ||
type: Sequelize.TEXT, | ||
allowNull: false | ||
}, | ||
created_at: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
}, | ||
updated_at: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
} | ||
}), | ||
down: (queryInterface) => queryInterface.dropTable('refresh_tokens') | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
module.exports = { | ||
up: (queryInterface, Sequelize) => queryInterface.createTable('access_tokens', { | ||
id: { | ||
allowNull: false, | ||
autoIncrement: true, | ||
primaryKey: true, | ||
type: Sequelize.INTEGER | ||
}, | ||
user_id: { | ||
type: Sequelize.INTEGER, | ||
allowNull: false, | ||
references: { | ||
model: 'users', | ||
key: 'id' | ||
} | ||
}, | ||
value: { | ||
type: Sequelize.TEXT, | ||
allowNull: false | ||
}, | ||
created_at: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
}, | ||
updated_at: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
}, | ||
expires_at: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
}, | ||
}), | ||
down: (queryInterface) => queryInterface.dropTable('access_tokens') | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
const crypto = require('crypto'); | ||
const util = require('util'); | ||
const moment = require('moment'); | ||
|
||
const { Sequelize, sequelize } = require('../lib/sequelize'); | ||
const config = require('../config'); | ||
|
||
const randomBytes = util.promisify(crypto.randomBytes); | ||
|
||
const AccessToken = sequelize.define('access_token', { | ||
user_id: { | ||
type: Sequelize.INTEGER, | ||
allowNull: false | ||
}, | ||
value: { | ||
type: Sequelize.TEXT, | ||
allowNull: false, | ||
defaultValue: '', | ||
validate: { | ||
notEmpty: { msg: 'Value should be set.' }, | ||
}, | ||
unique: true | ||
}, | ||
expires_at: { | ||
type: Sequelize.DATE, | ||
allowNull: false | ||
} | ||
}, { | ||
underscored: true, | ||
tableName: 'access_tokens', | ||
createdAt: 'created_at', | ||
updatedAt: 'updated_at' | ||
}); | ||
|
||
AccessToken.createForUser = async function createForUser(userId) { | ||
const value = (await randomBytes(64)).toString('hex'); | ||
const expiresAt = moment().add(config.ttl.access_token, 'seconds'); | ||
|
||
return AccessToken.create({ | ||
user_id: userId, | ||
value, | ||
expires_at: expiresAt | ||
}); | ||
}; | ||
|
||
module.exports = AccessToken; |
Oops, something went wrong.