forked from sharetribe/ftw-daily
-
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.
Merge pull request #5 from Gnito/update-from-ftw-daily
Update from ftw-daily
- Loading branch information
Showing
12 changed files
with
184 additions
and
20 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
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
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,100 @@ | ||
const crypto = require('crypto'); | ||
const { default: fromKeyLike } = require('jose/jwk/from_key_like'); | ||
const { default: SignJWT } = require('jose/jwt/sign'); | ||
|
||
const radix = 10; | ||
const PORT = parseInt(process.env.REACT_APP_DEV_API_SERVER_PORT, radix); | ||
const rootUrl = process.env.REACT_APP_CANONICAL_ROOT_URL; | ||
const useDevApiServer = process.env.NODE_ENV === 'development' && !!PORT; | ||
|
||
const issuerUrl = useDevApiServer ? `http://localhost:${PORT}` : `${rootUrl}`; | ||
|
||
/** | ||
* Gets user information and creates the signed jwt for id token. | ||
* | ||
* @param {string} idpClientId the client id of the idp provider in Console | ||
* @param {Object} options signing options containing signingAlg and required key information | ||
* @param {Object} user user information containing at least firstName, lastName, email and emailVerified | ||
* | ||
* @return {Promise} idToken | ||
*/ | ||
exports.createIdToken = (idpClientId, user, options) => { | ||
if (!idpClientId) { | ||
console.error('Missing idp client id!'); | ||
return; | ||
} | ||
if (!user) { | ||
console.error('Missing user information!'); | ||
return; | ||
} | ||
|
||
const signingAlg = options.signingAlg; | ||
|
||
// Currently Flex supports only RS256 signing algorithm. | ||
if (signingAlg !== 'RS256') { | ||
console.error(`${signingAlg} is not currently supported!`); | ||
return; | ||
} | ||
|
||
const { rsaPrivateKey, keyId } = options; | ||
|
||
if (!rsaPrivateKey) { | ||
console.error('Missing RSA private key!'); | ||
return; | ||
} | ||
|
||
// We use jose library which requires the RSA key | ||
// to be KeyLike format: | ||
// https://github.com/panva/jose/blob/master/docs/types/_types_d_.keylike.md | ||
const privateKey = crypto.createPrivateKey(rsaPrivateKey); | ||
|
||
const { userId, firstName, lastName, email, emailVerified } = user; | ||
|
||
const jwt = new SignJWT({ | ||
given_name: firstName, | ||
family_name: lastName, | ||
email: email, | ||
email_verified: emailVerified, | ||
}) | ||
.setProtectedHeader({ alg: signingAlg, kid: keyId }) | ||
.setIssuedAt() | ||
.setIssuer(issuerUrl) | ||
.setSubject(userId) | ||
.setAudience(idpClientId) | ||
.setExpirationTime('1h') | ||
.sign(privateKey); | ||
|
||
return jwt; | ||
}; | ||
|
||
// Serves the discovery document in json format | ||
// this document is expected to be found from | ||
// api/.well-known/openid-configuration endpoint | ||
exports.openIdConfiguration = (req, res) => { | ||
res.json({ | ||
issuer: issuerUrl, | ||
jwks_uri: `${issuerUrl}/.well-known/jwks.json`, | ||
subject_types_supported: ['public'], | ||
id_token_signing_alg_values_supported: ['RS256'], | ||
}); | ||
}; | ||
|
||
/** | ||
* @param {String} signingAlg signing algorithm, currently only RS256 is supported | ||
* @param {Array} list containing keys to be served in json endpoint | ||
* | ||
* // Serves the RSA public key as JWK | ||
// this document is expected to be found from | ||
// api/.well-known/jwks.json endpoint as stated in discovery document | ||
*/ | ||
exports.jwksUri = keys => (req, res) => { | ||
const jwkKeys = keys.map(key => { | ||
return fromKeyLike(crypto.createPublicKey(key.rsaPublicKey)).then(res => { | ||
return { alg: key.alg, kid: key.keyId, ...res }; | ||
}); | ||
}); | ||
|
||
Promise.all(jwkKeys).then(resolvedJwkKeys => { | ||
res.json({ keys: resolvedJwkKeys }); | ||
}); | ||
}; |
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
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,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; |
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 |
---|---|---|
|
@@ -7634,6 +7634,11 @@ [email protected]: | |
import-local "^3.0.2" | ||
jest-cli "^26.6.0" | ||
|
||
[email protected]: | ||
version "3.1.0" | ||
resolved "https://registry.yarnpkg.com/jose/-/jose-3.1.0.tgz#31a48b76a2e0f5da4e9a1be261e430e0bfaa4a43" | ||
integrity sha512-TLZFF0qAPlG0GZDrPw9HAiWKJcDuUbOp1WdjuS5cJ0reTzd1zS718zrUPOt7BIOViTA6PZpEnMt5cMQttJq3QA== | ||
|
||
js-cookie@^2.1.3: | ||
version "2.2.1" | ||
resolved "https://registry.yarnpkg.com/js-cookie/-/js-cookie-2.2.1.tgz#69e106dc5d5806894562902aa5baec3744e9b2b8" | ||
|
@@ -8687,9 +8692,9 @@ node-modules-regexp@^1.0.0: | |
integrity sha1-jZ2+KJZKSsVxLpExZCEHxx6Q7EA= | ||
|
||
node-notifier@^8.0.0: | ||
version "8.0.0" | ||
resolved "https://registry.yarnpkg.com/node-notifier/-/node-notifier-8.0.0.tgz#a7eee2d51da6d0f7ff5094bc7108c911240c1620" | ||
integrity sha512-46z7DUmcjoYdaWyXouuFNNfUo6eFa94t23c53c+lG/9Cvauk4a98rAUp9672X5dxGdQmLpPzTxzu8f/OeEPaFA== | ||
version "8.0.1" | ||
resolved "https://registry.yarnpkg.com/node-notifier/-/node-notifier-8.0.1.tgz#f86e89bbc925f2b068784b31f382afdc6ca56be1" | ||
integrity sha512-BvEXF+UmsnAfYfoapKM9nGxnP+Wn7P91YfXmrKnfcYCx6VBeoN5Ez5Ogck6I8Bi5k4RlpqRYaw75pAwzX9OphA== | ||
dependencies: | ||
growly "^1.3.0" | ||
is-wsl "^2.2.0" | ||
|
@@ -11530,7 +11535,7 @@ [email protected]: | |
resolved "https://registry.yarnpkg.com/semver/-/semver-7.0.0.tgz#5f3ca35761e47e05b206c6daff2cf814f0316b8e" | ||
integrity sha512-+GB6zVA9LWh6zovYQLALHwv5rb2PHGlJi3lfiqIHxR0uuwCgefcOJc59v9fv1w8GbStwxuuqqAjI9NMAOOgq1A== | ||
|
||
[email protected], semver@^7.2.1, semver@^7.3.2: | ||
[email protected]: | ||
version "7.3.2" | ||
resolved "https://registry.yarnpkg.com/semver/-/semver-7.3.2.tgz#604962b052b81ed0786aae84389ffba70ffd3938" | ||
integrity sha512-OrOb32TeeambH6UrhtShmF7CRDqhL6/5XpPNp2DuRH6+9QLw/orhp72j87v8Qa1ScDkvrrBNpZcDejAirJmfXQ== | ||
|
@@ -11540,6 +11545,13 @@ semver@^6.0.0, semver@^6.2.0, semver@^6.3.0: | |
resolved "https://registry.yarnpkg.com/semver/-/semver-6.3.0.tgz#ee0a64c8af5e8ceea67687b133761e1becbd1d3d" | ||
integrity sha512-b39TBaTSfV6yBrapU89p5fKekE2m/NwnDocOVruQFS1/veMgdzuPcnOM34M6CwxW8jH/lxEa5rBoDeUwu5HHTw== | ||
|
||
semver@^7.2.1, semver@^7.3.2: | ||
version "7.3.4" | ||
resolved "https://registry.yarnpkg.com/semver/-/semver-7.3.4.tgz#27aaa7d2e4ca76452f98d3add093a72c943edc97" | ||
integrity sha512-tCfb2WLjqFAtXn4KEdxIhalnRtoKFN7nAwj0B3ZXCbQloV2tq5eDbcTmT68JJD3nRJq24/XgxtQKFIpQdtvmVw== | ||
dependencies: | ||
lru-cache "^6.0.0" | ||
|
||
[email protected]: | ||
version "0.17.1" | ||
resolved "https://registry.yarnpkg.com/send/-/send-0.17.1.tgz#c1d8b059f7900f7466dd4938bdc44e11ddb376c8" | ||
|
@@ -13092,9 +13104,9 @@ uuid@^3.3.2, uuid@^3.4.0: | |
integrity sha512-HjSDRw6gZE5JMggctHBcjVak08+KEVhSIiDzFnT9S9aegmp85S/bReBVTb4QTFaRNptJ9kuYaNhnbNEOkbKb/A== | ||
|
||
uuid@^8.3.0: | ||
version "8.3.1" | ||
resolved "https://registry.yarnpkg.com/uuid/-/uuid-8.3.1.tgz#2ba2e6ca000da60fce5a196954ab241131e05a31" | ||
integrity sha512-FOmRr+FmWEIG8uhZv6C2bTgEVXsHk08kE7mPlrBbEe+c3r9pjceVPgupIfNIhc4yx55H69OXANrUaSuu9eInKg== | ||
version "8.3.2" | ||
resolved "https://registry.yarnpkg.com/uuid/-/uuid-8.3.2.tgz#80d5b5ced271bb9af6c445f21a1a04c606cefbe2" | ||
integrity sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg== | ||
|
||
v8-compile-cache@^2.0.3: | ||
version "2.1.1" | ||
|