forked from FAForever/website
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
moving passport into own file, using axios and introduce appConfig
- Loading branch information
Showing
5 changed files
with
99 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
require('dotenv').config(); | ||
|
||
const oauthUrl = process.env.OAUTH_URL || 'https://hydra.faforever.com' | ||
|
||
const appConfig = { | ||
nodeEnv: process.env.NODE_ENV || 'production', | ||
expressPort: process.env.PORT || '3000', | ||
host: process.env.HOST || 'http://localhost', | ||
session: { | ||
key: process.env.SESSION_SECRET_KEY || '12345', | ||
tokenLifespan: process.env.TOKEN_LIFESPAN || 43200 | ||
}, | ||
oauth: { | ||
clientId: process.env.OAUTH_CLIENT_ID || '12345', | ||
clientSecret: process.env.OAUTH_CLIENT_SECRET || '12345', | ||
url: oauthUrl, | ||
publicUrl: process.env.OAUTH_PUBLIC_URL || oauthUrl, | ||
callback: process.env.CALLBACK || 'callback', | ||
}, | ||
apiUrl: process.env.API_URL || 'https://api.faforever.com', | ||
extractorInterval: process.env.EXTRACTOR_INTERVAL || 5, | ||
playerCountInterval: process.env.PLAYER_COUNT_INTERVAL || 15 | ||
} | ||
|
||
module.exports = appConfig |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
const appConfig = require('../../config/app') | ||
const passport = require('passport'); | ||
const OidcStrategy = require('passport-openidconnect'); | ||
const express = require("express"); | ||
const axios = require("axios"); | ||
const router = express.Router(); | ||
|
||
passport.serializeUser((user, done) => done(null, user)) | ||
passport.deserializeUser((user, done) => done(null, user)) | ||
|
||
passport.use('faforever', new OidcStrategy({ | ||
issuer: appConfig.oauth.url + '/', | ||
tokenURL: appConfig.oauth.url + '/oauth2/token', | ||
authorizationURL: appConfig.oauth.publicUrl + '/oauth2/auth', | ||
userInfoURL: appConfig.oauth.url + '/userinfo?schema=openid', | ||
clientID: appConfig.oauth.clientId, | ||
clientSecret: appConfig.oauth.clientSecret, | ||
callbackURL: `${appConfig.host}/${appConfig.oauth.callback}`, | ||
scope: ['openid', 'public_profile', 'write_account_data'] | ||
}, function (iss, sub, profile, jwtClaims, accessToken, refreshToken, params, verified) { | ||
|
||
axios.get( | ||
appConfig.apiUrl + '/me', | ||
{ | ||
headers: {'Authorization': `Bearer ${accessToken}`} | ||
}).then((res) => { | ||
const user = res.data | ||
user.token = accessToken | ||
user.data.attributes.token = accessToken; | ||
user.data.id = user.data.attributes.userId; | ||
|
||
return verified(null, user); | ||
}).catch(e => { | ||
console.error('[Error] views/auth.js::passport::verify failed with "' + e.toString() + '"'); | ||
|
||
return verified(null, null); | ||
}); | ||
} | ||
)); | ||
|
||
router.get('/login', passport.authenticate('faforever')); | ||
|
||
router.get( | ||
'/' + appConfig.oauth.callback, | ||
passport.authenticate('faforever', {failureRedirect: '/login', failureFlash: true}), | ||
(req, res) => res.redirect('/') | ||
) | ||
|
||
router.get('/logout', (req, res, next) => { | ||
req.logout((err) => { | ||
if (err) { | ||
return next(err) | ||
} | ||
res.redirect('/') | ||
}) | ||
}) | ||
|
||
module.exports = router |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1186,14 +1186,6 @@ core-util-is@~1.0.0: | |
resolved "https://registry.yarnpkg.com/core-util-is/-/core-util-is-1.0.3.tgz#a6042d3634c2b27e9328f837b965fac83808db85" | ||
integrity sha512-ZQBvi1DcpJ4GDqanjucZ2Hj3wEO5pZDS89BWbkcrvdxksJorwUDDZamX9ldFkp9aw2lmBDLgkObEA4DWNJ9FYQ== | ||
|
||
cors@^2.8.5: | ||
version "2.8.5" | ||
resolved "https://registry.yarnpkg.com/cors/-/cors-2.8.5.tgz#eac11da51592dd86b9f06f6e7ac293b3df875d29" | ||
integrity sha512-KIHbLJqu73RGr/hnbrO9uBeixNGuvSQjul/jdFvS/KFSIH1hWVd1ng7zOHx+YrEfInLG7q4n6GHQ9cDtxv/P6g== | ||
dependencies: | ||
object-assign "^4" | ||
vary "^1" | ||
|
||
cosmiconfig@^7.1.0: | ||
version "7.1.0" | ||
resolved "https://registry.yarnpkg.com/cosmiconfig/-/cosmiconfig-7.1.0.tgz#1443b9afa596b670082ea46cbd8f6a62b84635f6" | ||
|
@@ -3801,7 +3793,7 @@ [email protected]: | |
resolved "https://registry.yarnpkg.com/oauth/-/oauth-0.9.15.tgz#bd1fefaf686c96b75475aed5196412ff60cfb9c1" | ||
integrity sha512-a5ERWK1kh38ExDEfoO6qUHJb32rd7aYmPHuyCu3Fta/cnICvYmgd2uhuKXvPD+PXB+gCEYYEaQdIRAjCOwAKNA== | ||
|
||
object-assign@^4, object-assign@^4.0.1, object-assign@^4.1.0, object-assign@^4.1.1: | ||
object-assign@^4.0.1, object-assign@^4.1.0, object-assign@^4.1.1: | ||
version "4.1.1" | ||
resolved "https://registry.yarnpkg.com/object-assign/-/object-assign-4.1.1.tgz#2109adc7965887cfc05cbbd442cac8bfbb360863" | ||
integrity sha512-rJgTQnkUnH1sFw8yT6VSU3zD3sWmu6sZhIseY8VX+GRu3P6F7Fu+JNDoXfklElbLJSnc3FUQHVe4cU5hj+BcUg== | ||
|
@@ -5772,7 +5764,7 @@ validator@^13.9.0: | |
resolved "https://registry.yarnpkg.com/validator/-/validator-13.11.0.tgz#23ab3fd59290c61248364eabf4067f04955fbb1b" | ||
integrity sha512-Ii+sehpSfZy+At5nPdnyMhx78fEoPDkR2XW/zimHEL3MyGJQOCQ7WeP20jPYRz7ZCpcKLB21NxuXHF3bxjStBQ== | ||
|
||
vary@^1, vary@~1.1.2: | ||
vary@~1.1.2: | ||
version "1.1.2" | ||
resolved "https://registry.yarnpkg.com/vary/-/vary-1.1.2.tgz#2299f02c6ded30d4a5961b0b9f74524a18f634fc" | ||
integrity sha512-BNGbWLfd0eUPabhkXUVm0j8uuvREyTh5ovRa/dyow/BqAbZJyC+5fU+IzQOzmAKzYqYRAISoRhdQr3eIZ/PXqg== | ||
|