Skip to content

Commit 87084c6

Browse files
authored
feat: Generic OAuth2 authentication implementation (#3094)
* OAuth2 authentication implementation This PR shoul fix #2392. Used `passport-oauth2` strategy. * indentations cleanup * cleanup code
1 parent 813df21 commit 87084c6

File tree

2 files changed

+109
-0
lines changed

2 files changed

+109
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
const _ = require('lodash')
2+
3+
/* global WIKI */
4+
5+
// ------------------------------------
6+
// OAuth2 Connect Account
7+
// ------------------------------------
8+
9+
const OAuth2Strategy = require('passport-oauth2').Strategy
10+
11+
module.exports = {
12+
init (passport, conf) {
13+
var client = new OAuth2Strategy({
14+
authorizationURL: conf.authorizationURL,
15+
tokenURL: conf.tokenURL,
16+
clientID: conf.clientId,
17+
clientSecret: conf.clientSecret,
18+
userInfoURL: conf.userInfoURL,
19+
callbackURL: conf.callbackURL,
20+
passReqToCallback: true,
21+
}, async (req, accessToken, refreshToken, profile, cb) => {
22+
try {
23+
const user = await WIKI.models.users.processProfile({
24+
providerKey: req.params.strategy,
25+
profile: {
26+
...profile,
27+
id: _.get(profile, conf.userId),
28+
displayName: _.get(profile, conf.displayName, ''),
29+
email: _.get(profile, conf.emailClaim)
30+
}
31+
})
32+
cb(null, user)
33+
} catch (err) {
34+
cb(err, null)
35+
}
36+
})
37+
38+
client.userProfile = function (accesstoken, done) {
39+
this._oauth2._useAuthorizationHeaderForGET = true;
40+
this._oauth2.get(conf.userInfoURL, accesstoken, (err, data) => {
41+
if (err) {
42+
return done(err)
43+
}
44+
try {
45+
data = JSON.parse(data)
46+
} catch(e) {
47+
return done(e)
48+
}
49+
done(null, data)
50+
})
51+
}
52+
passport.use('oauth2', client)
53+
}
54+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
key: oauth2
2+
title: OAuth2
3+
description: OAuth 2.0 protocol.
4+
author: requarks.io
5+
logo: https://static.requarks.io/logo/oauth2.svg
6+
color: blue-grey darken-2
7+
website: https://oauth.net/2/
8+
isAvailable: true
9+
useForm: false
10+
props:
11+
clientId:
12+
type: String
13+
title: Client ID
14+
hint: Application Client ID
15+
order: 1
16+
clientSecret:
17+
type: String
18+
title: Client Secret
19+
hint: Application Client Secret
20+
order: 2
21+
authorizationURL:
22+
type: String
23+
title: Authorization Endpoint URL
24+
hint: Application Authorization Endpoint URL
25+
order: 3
26+
tokenURL:
27+
type: String
28+
title: Token Endpoint URL
29+
hint: Application Token Endpoint URL
30+
order: 4
31+
userInfoURL:
32+
type: String
33+
title: User Info Endpoint URL
34+
hint: User Info Endpoint URL
35+
order: 5
36+
userId:
37+
type: String
38+
title: ID
39+
hint: User ID
40+
default: id
41+
order: 6
42+
displayName:
43+
type: String
44+
title: Display Name
45+
hint: Field containing display name
46+
default: displayName
47+
maxWidth: 500
48+
order: 7
49+
emailClaim:
50+
type: String
51+
title: Email Claim
52+
hint: Field containing the email address
53+
default: email
54+
maxWidth: 500
55+
order: 8

0 commit comments

Comments
 (0)