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/twitter_helper.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/twitter_helper.js')
| -rw-r--r-- | lib/twitter_helper.js | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/lib/twitter_helper.js b/lib/twitter_helper.js new file mode 100644 index 0000000..3ab51fa --- /dev/null +++ b/lib/twitter_helper.js @@ -0,0 +1,78 @@ +'use strict'; + +const Co = require('co'); +const OAuth = require('oauth'); +const Pify = require('pify'); + +const internals = {}; + +internals.kRequestTokenUrl = 'https://api.twitter.com/oauth/request_token'; +internals.kAccessTokenUrl = 'https://api.twitter.com/oauth/access_token'; +internals.kVerifyCredentialsUrl = 'https://api.twitter.com/1.1/account/verify_credentials.json'; +internals.kOauthVersion = '1.0A'; +internals.kOauthSignatureMethod = 'HMAC-SHA1'; + +module.exports = internals.TwitterHelper = class TwitterHelper { + + constructor(config) { + + this._oAuth = new OAuth.OAuth( + internals.kRequestTokenUrl, + internals.kAccessTokenUrl, + config.consumerKey, + config.consumerSecret, + internals.kOauthVersion, + null, + internals.kOauthSignatureMethod + ); + } + + getRequestToken() { + + const self = this; + + return Co(function * () { + + const getOAuthRequestToken = Pify(self._oAuth.getOAuthRequestToken.bind(self._oAuth), { multiArgs: true }); + const [oAuthToken, oAuthTokenSecret] = yield getOAuthRequestToken(); + + return { + oAuthToken, + oAuthTokenSecret + }; + }); + } + + getAccessToken(oAuthToken, oAuthVerifier) { + + const self = this; + + return Co(function * () { + + const getOAuthAccessToken = Pify(self._oAuth.getOAuthAccessToken.bind(self._oAuth), { multiArgs: true }); + const [oAuthAccessToken, oAuthAccessTokenSecret] = yield getOAuthAccessToken(oAuthToken, + '', + oAuthVerifier); + + return { + oAuthAccessToken, + oAuthAccessTokenSecret + }; + }); + } + + getUser(oAuthAccessToken, oAuthAccessTokenSecret) { + + const self = this; + + return Co(function * () { + + const get = Pify(self._oAuth.get.bind(self._oAuth), { multiArgs: true }); + const [userResponse] = yield get(internals.kVerifyCredentialsUrl, + oAuthAccessToken, + oAuthAccessTokenSecret); + + return JSON.parse(userResponse); + }); + } +}; |