/
Twitter.js
136 lines (117 loc) · 3.19 KB
/
Twitter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
'use strict'
/*
* adonis-ally
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
const OAuthScheme = require('../Schemes/OAuth')
const CE = require('../Exceptions')
const AllyUser = require('../AllyUser')
const utils = require('../../lib/utils')
const _ = utils.mixLodash(require('lodash'))
class Twitter extends OAuthScheme {
constructor (Config) {
const config = Config.get('services.ally.twitter')
if (!_.hasAll(config, ['clientId', 'clientSecret', 'redirectUri'])) {
throw CE.OAuthException.missingConfig('twitter')
}
super(config.clientId, config.clientSecret, config.redirectUri)
}
/**
* Injections to be made by the IoC container
*
* @return {Array}
*/
static get inject () {
return ['Adonis/Src/Config']
}
/**
* Url to be used for fetching user profile
*
* @return {String}
*/
get profileUrl () {
return 'https://api.twitter.com/1.1/account/verify_credentials.json?include_email=true'
}
/**
* Url to be used for generating the request token
*
* @return {String} [description]
*/
get requestTokenUrl () {
return 'https://api.twitter.com/oauth/request_token'
}
/**
* Url to be used for redirecting
* user.
*
* @return {String} [description]
*/
get authorizeUrl () {
return 'https://api.twitter.com/oauth/authenticate'
}
/**
* Url to be used for exchanging
* access token.
*
* @return {String}
*/
get accessTokenUrl () {
return 'https://api.twitter.com/oauth/access_token'
}
/**
* Returns the redirect url for a given provider
*
* @return {String}
*/
* getRedirectUrl () {
return yield this.getUrl()
}
/**
* Parses the redirect errors returned by github
* and returns the error message.
*
* @return {String}
*/
parseRedirectError () {
return 'Oauth failed during redirect'
}
/**
* Returns the user profile with it's access token, refresh token
* and token expiry
*
* @param {Object} queryParams
*
* @return {Object}
*/
* getUser (queryParams) {
const oauthToken = queryParams.oauth_token
const oauthVerifier = queryParams.oauth_verifier
/**
* Throw an exception when query string does not have
* oauth_token or oauth_verifier
*/
if (!oauthToken || !oauthVerifier) {
const errorMessage = this.parseRedirectError(queryParams)
throw CE.OAuthException.tokenExchangeException(errorMessage, null, errorMessage)
}
const accessTokenResponse = yield this.getAccessToken(queryParams.oauth_token, queryParams.oauth_verifier)
const userProfile = yield this.getUserProfile(accessTokenResponse.accessToken, accessTokenResponse.tokenSecret)
const user = new AllyUser()
user
.setOriginal(userProfile)
.setFields(
userProfile.id,
userProfile.screen_name,
userProfile.email,
userProfile.name,
userProfile.profile_image_url.replace('_normal.jpg', '.jpg')
)
.setToken(accessTokenResponse.accessToken, null, accessTokenResponse.tokenSecret, null)
return user
}
}
module.exports = Twitter