Skip to content

Commit

Permalink
Merge branch 'feature/token_refresh' into feature/clan-scrape
Browse files Browse the repository at this point in the history
  • Loading branch information
fcaps committed Nov 27, 2023
2 parents 4184912 + 8f0694c commit 3e9fae0
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 41 deletions.
64 changes: 30 additions & 34 deletions fafApp.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,38 @@ const FileStore = require('session-file-store')(session)
const passport = require('passport')
const flash = require('connect-flash')
const middleware = require('./routes/middleware')
const defaultRouter = require('./routes/views/defaultRouter')
const authRouter = require('./routes/views/auth')
const staticMarkdownRouter = require('./routes/views/staticMarkdownRouter')
const newsRouter = require('./routes/views/news')
const leaderboardRouter = require('./routes/views/leaderboardRouter')
const clanRouter = require('./routes/views/clanRouter')
const accountRouter = require('./routes/views/accountRouter')
const dataRouter = require('./routes/views/dataRouter')
const setupCronJobs = require('./scripts/cron-jobs')
const defaultRouter = require("./routes/views/defaultRouter")
const authRouter = require("./routes/views/auth")
const staticMarkdownRouter = require("./routes/views/staticMarkdownRouter")
const newsRouter = require("./routes/views/news")
const leaderboardRouter = require("./routes/views/leaderboardRouter")
const clanRouter = require("./routes/views/clanRouter")
const accountRouter = require("./routes/views/accountRouter")
const dataRouter = require('./routes/views/dataRouter');
const setupCronJobs = require("./scripts/cron-jobs")
const OidcStrategy = require('passport-openidconnect')
const refresh = require('passport-oauth2-refresh')
const JavaApiClientFactory = require('./lib/JavaApiClient')
const UserRepository = require('./lib/UserRepository')

const copyFlashHandler = (req, res, next) => {
res.locals.message = req.flash()
next()
res.locals.message = req.flash();
next();
}
const notFoundHandler = (req, res) => {
res.status(404).render('errors/404')
res.status(404).render('errors/404');
}

const errorHandler = (err, req, res, next) => {
console.error('[error] Incoming request to"', req.originalUrl, '"failed with error "', err.toString(), '"')
if (res.headersSent) {
return next(err)
return next(err);
}

res.status(500).render('errors/500')
res.status(500).render('errors/500');
}

const loadAuth = () => {
const configureAuth = () => {
passport.serializeUser((user, done) => done(null, user))
passport.deserializeUser((user, done) => done(null, user))

Expand All @@ -51,24 +51,20 @@ const loadAuth = () => {
callbackURL: `${appConfig.host}/${appConfig.oauth.callback}`,
scope: ['openid', 'offline', 'public_profile', 'write_account_data']
}, async function (iss, sub, profile, jwtClaims, accessToken, refreshToken, params, verified) {
const oAuthPassport = {
token: accessToken,
refreshToken
}
const apiClient = JavaApiClientFactory(appConfig.apiUrl, oAuthPassport)
const userRepository = new UserRepository(apiClient)

const apiClient = JavaApiClientFactory(appConfig.apiUrl, oAuthPassport)
const userRepository = new UserRepository(apiClient)
try {
const user = await userRepository.fetchUser(oAuthPassport)

try {
const user = await userRepository.fetchUser(oAuthPassport)
return verified(null, user)
} catch (e) {
console.error('[Error] oAuth verify failed with "' + e.toString() + '"')

return verified(null, user)
} catch (e) {
console.error('[Error] oAuth verify failed with "' + e.toString() + '"')

return verified(null, null)
}
})
return verified(null, null)
}
}
)

passport.use(appConfig.oauth.strategy, authStrategy)
refresh.use(appConfig.oauth.strategy, authStrategy)
Expand All @@ -80,8 +76,8 @@ module.exports.setupCronJobs = () => {

module.exports.startServer = (app) => {
app.listen(appConfig.expressPort, () => {
console.log(`Express listening on port ${appConfig.expressPort}`)
})
console.log(`Express listening on port ${appConfig.expressPort}`);
});
}

module.exports.loadRouters = (app) => {
Expand Down Expand Up @@ -119,7 +115,7 @@ module.exports.setup = (app) => {

app.use(express.json())
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.urlencoded({extended: false}))

app.use(session({
resave: false,
Expand All @@ -133,7 +129,7 @@ module.exports.setup = (app) => {
}))
app.use(passport.initialize())
app.use(passport.session())
loadAuth()
configureAuth()

app.use(middleware.injectServices)

Expand Down
2 changes: 0 additions & 2 deletions lib/JavaApiClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ module.exports = (javaApiBaseURL, oAuthPassport) => {
oAuthPassport.token = token
oAuthPassport.refreshToken = refreshToken

res.config.headers.Authorization = `Bearer ${token}`

return client.request(res.config)
}

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"jest": "^29.7.0",
"load-grunt-config": "4.0.1",
"load-grunt-tasks": "5.1.0",
"nock": "^13.3.8",
"octokit": "^3.1.2",
"webpack": "^5.89.0",
"webpack-cli": "^5.1.4",
Expand Down
2 changes: 1 addition & 1 deletion routes/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ exports.isAuthenticated = (redirectUrlAfterLogin = null, isApiRequest = false) =
}
}

exports.injectServices = function (req, res, next) {
exports.injectServices = function(req, res, next) {
req.services = {
wordpressService: wordpressService
}
Expand Down
8 changes: 5 additions & 3 deletions tests/JavaApiClient.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,11 @@ test('refresh will not loop to death', async () => {
.times(1)
.reply(200, { access_token: 'new_tok', refresh_token: 'new_ref' })

const response = await client.get('/example')

expect(response.status).toBe(401)
try {
await client.get('/example')
} catch (e) {
expect(e).toBeInstanceOf(AuthFailed)
}

apiScope.done()
authScope.done()
Expand Down
21 changes: 20 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5271,7 +5271,7 @@ json-stable-stringify-without-jsonify@^1.0.1:
resolved "https://registry.yarnpkg.com/json-stable-stringify-without-jsonify/-/json-stable-stringify-without-jsonify-1.0.1.tgz#9db7b59496ad3f3cfef30a75142d2d930ad72651"
integrity sha512-Bdboy+l7tA3OGW6FjyFHWkP5LuByj1Tk33Ljyq0axyzdk9//JSi2u3fP1QSmd1KNwq6VOKYGlAu87CisVir6Pw==

json-stringify-safe@~5.0.1:
json-stringify-safe@^5.0.1, json-stringify-safe@~5.0.1:
version "5.0.1"
resolved "https://registry.yarnpkg.com/json-stringify-safe/-/json-stringify-safe-5.0.1.tgz#1296a2d58fd45f19a0f6ce01d65701e2c735b6eb"
integrity sha512-ZClg6AaYvamvYEE82d3Iyd3vSSIjQ+odgjaTzRuO3s7toCdFKczob2i0zCh7JE8kWn17yvAWhUVxvqGwUalsRA==
Expand Down Expand Up @@ -5912,6 +5912,15 @@ neo-async@^2.6.2:
resolved "https://registry.yarnpkg.com/neo-async/-/neo-async-2.6.2.tgz#b4aafb93e3aeb2d8174ca53cf163ab7d7308305f"
integrity sha512-Yd3UES5mWCSqR+qNT93S3UoYUkqAZ9lLg8a7g9rimsWmYGK8cVToA4/sF3RrshdyV3sAGMXVUmpMYOw+dLpOuw==

nock@^13.3.8:
version "13.3.8"
resolved "https://registry.yarnpkg.com/nock/-/nock-13.3.8.tgz#7adf3c66f678b02ef0a78d5697ae8bc2ebde0142"
integrity sha512-96yVFal0c/W1lG7mmfRe7eO+hovrhJYd2obzzOZ90f6fjpeU/XNvd9cYHZKZAQJumDfhXgoTpkpJ9pvMj+hqHw==
dependencies:
debug "^4.1.0"
json-stringify-safe "^5.0.1"
propagate "^2.0.0"

node-cache@^5.1.2:
version "5.1.2"
resolved "https://registry.yarnpkg.com/node-cache/-/node-cache-5.1.2.tgz#f264dc2ccad0a780e76253a694e9fd0ed19c398d"
Expand Down Expand Up @@ -6376,6 +6385,11 @@ pascalcase@^0.1.1:
resolved "https://registry.yarnpkg.com/pascalcase/-/pascalcase-0.1.1.tgz#b363e55e8006ca6fe21784d2db22bd15d7917f14"
integrity sha512-XHXfu/yOQRy9vYOtUDVMN60OEJjW013GoObG1o+xwQTpB9eYJX/BjXMsdW13ZDPruFhYYn0AG22w0xgQMwl3Nw==

passport-oauth2-refresh@^2.2.0:
version "2.2.0"
resolved "https://registry.yarnpkg.com/passport-oauth2-refresh/-/passport-oauth2-refresh-2.2.0.tgz#e60dd4e84e8df3c6ead87b6aab0754dec7a89aca"
integrity sha512-yXwXHL7ZZH0s2oknnjugfvwzCB5mpJ5ZNpzkb+b/sTsHeZFbx2BXfzvwsoD4aq6gq/aWuCxBV89ef+L/cjjrjg==

passport-openidconnect@^0.1.1:
version "0.1.1"
resolved "https://registry.yarnpkg.com/passport-openidconnect/-/passport-openidconnect-0.1.1.tgz#83921ff5f87f634079f65262dada834af1972244"
Expand Down Expand Up @@ -6661,6 +6675,11 @@ prompts@^2.0.1:
kleur "^3.0.3"
sisteransi "^1.0.5"

propagate@^2.0.0:
version "2.0.1"
resolved "https://registry.yarnpkg.com/propagate/-/propagate-2.0.1.tgz#40cdedab18085c792334e64f0ac17256d38f9a45"
integrity sha512-vGrhOavPSTz4QVNuBNdcNXePNdNMaO1xj9yBeH1ScQPjk/rhg9sSlCXPhMkFuaNNW/syTvYqsnbIJxMBfRbbag==

proxy-addr@~2.0.7:
version "2.0.7"
resolved "https://registry.yarnpkg.com/proxy-addr/-/proxy-addr-2.0.7.tgz#f19fe69ceab311eeb94b42e70e8c2070f9ba1025"
Expand Down

0 comments on commit 3e9fae0

Please sign in to comment.