diff options
| author | Rubén Beltrán del Río <ben@nsovocal.com> | 2017-01-19 00:25:01 -0600 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2017-01-19 00:25:01 -0600 |
| commit | 287fa13b3e600b2340895a5463a288bf08101bb5 (patch) | |
| tree | 68b35cdd1bea40a6d51665ac7ed6dc4044eeda49 /lib/handlers/auth.js | |
| parent | cc69fef4b7e1587a91a0603d4bd1a46d0b133dd5 (diff) | |
Add Login (#2)
* Add dependencies
* Add barebones app
* Ignore .DS_Store
* Add base static assets
* Expose port on docker
* Ignore env files
* Add instructions to use env.dist
* Add twitter configs
* Use latest node version
* Read env file properly
* Add dependencies to start parsing twitter
* Add helper for twitter login operations
* Add handler for titter login routes
* Use twitter handler
* Add JWT related keys
* Add jwt dependencies
* Move index static file
* Rename twitter handler to auth, add jwt support
* Add new yarn lock
* Add instructions for twitter
* Remove twitter image
* Fix return value lint errors
* Ignore require-yield errors in linter
Diffstat (limited to 'lib/handlers/auth.js')
| -rw-r--r-- | lib/handlers/auth.js | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/lib/handlers/auth.js b/lib/handlers/auth.js new file mode 100644 index 0000000..d16e15d --- /dev/null +++ b/lib/handlers/auth.js @@ -0,0 +1,98 @@ +'use strict'; + +const Co = require('co'); +const TwitterHelper = require('../twitter_helper'); +const JsonWebToken = require('jsonwebtoken'); +const Pify = require('pify'); + +const internals = {}; + +internals.kRedirectUrl = 'https://api.twitter.com/oauth/authenticate?oauth_token='; +internals.kMainLocation = '/'; + +internals.signJsonWebToken = Pify(JsonWebToken.sign); + +module.exports = internals.AuthHandler = class AuthHandler { + + constructor(config) { + + this._twitterHelper = new TwitterHelper(config.twitter); + this._jwtConfig = config.jwt; + this._hostname = config.hostname; + } + + login() { + + const twitterHelper = this._twitterHelper; + + return function *handleLogin() { + + if (this.state.user) { + return this.redirect(internals.kMainLocation); + } + + const requestToken = yield twitterHelper.getRequestToken(); + this.redirect(`${internals.kRedirectUrl}${requestToken.oAuthToken}`); + }; + } + + callback() { + + const self = this; + + return function *handleCallback() { + + if (this.request.query.denied) { + return this.throw(401); + } + + const oAuthToken = this.request.query.oauth_token; + const oAuthVerifier = this.request.query.oauth_verifier; + let user; + + try { + const accessToken = yield self._twitterHelper.getAccessToken(oAuthToken, oAuthVerifier); + user = yield self._twitterHelper.getUser(accessToken.oAuthAccessToken, accessToken.oAuthAccessTokenSecret); + } + catch (err) { + console.error(err.stack || err.message || err); + return this.throw(401); + } + + yield self._setJWT(user, this); + + this.redirect(internals.kMainLocation); + }; + } + + logout() { + + const self = this; + + return function * () { + + this.cookies.set(self._jwtConfig.cookieName, null); + this.redirect(internals.kMainLocation); + }; + } + + // Sets a JSON Web Token Cookie + _setJWT(payload, context) { + + const self = this; + + return Co(function * () { + + const token = yield internals.signJsonWebToken(payload, self._jwtConfig.secret, { + expiresIn: self._jwtConfig.duration + }); + + context.cookies.set(self._jwtConfig.cookieName, token, { + maxAge: self._jwtConfig.duration * 1000, + signed: true, + domain: self._hostname, + overwrite: true + }); + }); + } +}; |