Skip to content

Commit

Permalink
Move .well-known/* endpoints from apiRouter to new wellKnownRouter
Browse files Browse the repository at this point in the history
  • Loading branch information
OtterleyW committed Jan 12, 2021
1 parent e3e8d34 commit 201da51
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 18 deletions.
18 changes: 0 additions & 18 deletions server/apiRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ const createUserWithIdp = require('./api/auth/createUserWithIdp');
const { authenticateFacebook, authenticateFacebookCallback } = require('./api/auth/facebook');
const { authenticateGoogle, authenticateGoogleCallback } = require('./api/auth/google');

const { openIdConfiguration, jwksUri } = require('./api-util/idToken');

const router = express.Router();

// ================ API router middleware: ================ //
Expand Down Expand Up @@ -82,20 +80,4 @@ router.get('/auth/google', authenticateGoogle);
// loginWithIdp endpoint in Flex API to authenticate user to Flex
router.get('/auth/google/callback', authenticateGoogleCallback);

// These endpoints will be used if you FTW as OIDC proxy
// https://www.sharetribe.com/docs/cookbook-social-logins-and-sso/setup-open-id-connect-proxy/
// All identity providers should provide an OpenID Connect discovery document:
// https://openid.net/specs/openid-connect-discovery-1_0.html
// And in the discovery document we need to define jwks_uri attribute
// which denotes the location of public signing keys

const rsaSecretKey = process.env.RSA_SECRET_KEY;
const rsaPublicKey = process.env.RSA_PUBLIC_KEY;
const keyId = process.env.KEY_ID;

if (rsaPublicKey && rsaSecretKey) {
router.get('/.well-known/openid-configuration', openIdConfiguration);
router.get('/.well-known/jwks.json', jwksUri([{ alg: 'RS256', rsaPublicKey, keyId }]));
}

module.exports = router;
2 changes: 2 additions & 0 deletions server/apiServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const cors = require('cors');
const apiRouter = require('./apiRouter');
const wellKnownRouter = require('./wellKnownRouter');

const radix = 10;
const PORT = parseInt(process.env.REACT_APP_DEV_API_SERVER_PORT, radix);
Expand All @@ -23,6 +24,7 @@ app.use(
})
);
app.use(cookieParser());
app.use('/.well-known', wellKnownRouter);
app.use('/api', apiRouter);

app.listen(PORT, () => {
Expand Down
7 changes: 7 additions & 0 deletions server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const sitemap = require('express-sitemap');
const passport = require('passport');
const auth = require('./auth');
const apiRouter = require('./apiRouter');
const wellKnownRouter = require('./wellKnownRouter');
const renderer = require('./renderer');
const dataLoader = require('./dataLoader');
const fs = require('fs');
Expand Down Expand Up @@ -131,6 +132,12 @@ app.use('/static', express.static(path.join(buildPath, 'static')));
app.use('/robots.txt', express.static(path.join(buildPath, 'robots.txt')));
app.use(cookieParser());

// These .well-known/* endpoints will be enabled if you are using FTW as OIDC proxy
// https://www.sharetribe.com/docs/cookbook-social-logins-and-sso/setup-open-id-connect-proxy/
// We need to handle these endpoints separately so that they are accessible by Flex
// even if you have enabled basic authentication e.g. in staging environment.
app.use('/.well-known', wellKnownRouter);

// Use basic authentication when not in dev mode. This is
// intentionally after the static middleware to skip basic auth for
// static resources.
Expand Down
17 changes: 17 additions & 0 deletions server/wellKnownRouter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const express = require('express');
const { openIdConfiguration, jwksUri } = require('./api-util/idToken');

const rsaPrivateKey = process.env.RSA_PRIVATE_KEY;
const rsaPublicKey = process.env.RSA_PUBLIC_KEY;
const keyId = process.env.KEY_ID;

const router = express.Router();

// These .well-known/* endpoints will be enabled if you are using FTW as OIDC proxy
// https://www.sharetribe.com/docs/cookbook-social-logins-and-sso/setup-open-id-connect-proxy/
if (rsaPublicKey && rsaPrivateKey) {
router.get('/openid-configuration', openIdConfiguration);
router.get('/jwks.json', jwksUri([{ alg: 'RS256', rsaPublicKey, keyId }]));
}

module.exports = router;

0 comments on commit 201da51

Please sign in to comment.