forked from Nigh7Sh4de/passport-twitch-new
-
Notifications
You must be signed in to change notification settings - Fork 0
/
oauth2.js
executable file
·98 lines (89 loc) · 3.03 KB
/
oauth2.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
/**
* Module dependencies.
*/
var OAuth2Strategy = require("passport-oauth2")
var InternalOAuthError = OAuth2Strategy.InternalOAuthError
/**
* `Strategy` constructor.
*
* The Twitch authentication strategy authenticates requests by delegating to
* Twitch using the OAuth 2.0 protocol.
*
* Applications must supply a `verify` callback which accepts an `accessToken`,
* `refreshToken` and service-specific `profile`, and then calls the `done`
* callback supplying a `user`, which should be set to `false` if the
* credentials are not valid. If an exception occured, `err` should be set.
*
* Options:
* - `clientID` your Twitch application"s client id
* - `clientSecret` your Twitch application"s client secret
* - `callbackURL` URL to which Twitch will redirect the user after granting authorization
* - `pem` Signing certificate used for decoding a user's OIDC token
*
* Examples:
*
* passport.use(new TwitchStrategy({
* clientID: "123-456-789",
* clientSecret: "shhh-its-a-secret"
* callbackURL: "https://www.example.net/auth/twitch/callback"
* },
* function(accessToken, refreshToken, profile, done) {
* User.findOrCreate(..., function (err, user) {
* done(err, user)
* })
* }
* ))
*
* @param {Object} options
* @param {Function} verify
* @api public
*/
class Strategy extends OAuth2Strategy {
constructor(options, verify) {
options = options || {}
options.authorizationURL = options.authorizationURL || "https://id.twitch.tv/oauth2/authorize"
options.tokenURL = options.tokenURL || "https://id.twitch.tv/oauth2/token"
options.customHeaders = options.customHeaders || {}
options.customHeaders['Client-ID'] = options.clientID
super(options, verify)
this.name = "twitch"
this.pem = options.pem
this._oauth2.setAuthMethod("Bearer")
this._oauth2.useAuthorizationHeaderforGET(true)
}
/**
* Retrieve user profile from Twitch.
*
* This function constructs a normalized profile, with the following properties:
*
* - `provider` always set to `twitch`
* - `id`
* - `username`
* - `displayName`
*
* @param {String} accessToken
* @param {Function} done
* @api protected
*/
userProfile(accessToken, done) {
this._oauth2.get("https://api.twitch.tv/helix/users", accessToken, function (err, body, res) {
if (err) { return done(new InternalOAuthError("failed to fetch user profile", err)); }
try {
done(null, {
...JSON.parse(body).data[0],
provider: 'twitch'
});
} catch(e) {
done(e);
}
});
}
authorizationParams(options) {
var params = {}
if (typeof options.forceVerify !== "undefined") {
params.force_verify = !!options.forceVerify
}
return params
}
}
module.exports = Strategy