Compare commits
27 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 6dda660e78 | |||
| 534772b315 | |||
| 3bdfe7bf0e | |||
| edba76d0ab | |||
| 5289b4fa23 | |||
| eef7168f37 | |||
| 596967ff72 | |||
| 5f2385a92f | |||
| a5653c8ea6 | |||
| 75ec899c99 | |||
| cb31381be2 | |||
| 9ee344311a | |||
| d7e3b10e80 | |||
| 7b306dd500 | |||
| 22d096a2be | |||
| 858cb96e10 | |||
| e8dd55208c | |||
| dbf5206c1b | |||
| 4229508bba | |||
| b4d5dbe9e3 | |||
| b476cf0e36 | |||
| 37ac46759d | |||
| 424e500d6b | |||
| 9539d9b38d | |||
| a9ea2dace4 | |||
| aad18ad000 | |||
| 68d3f0df82 |
@@ -15,6 +15,7 @@ let issuer = process.env.ISSUER || 'localhost:3333'
|
|||||||
let jwksOrigin = `https://${issuer}/`
|
let jwksOrigin = `https://${issuer}/`
|
||||||
const audience = process.env.AUDIENCE || 'https://generic-audience'
|
const audience = process.env.AUDIENCE || 'https://generic-audience'
|
||||||
const adminCustomClaim = process.env.ADMIN_CUSTOM_CLAIM || 'https://unbound.se/admin'
|
const adminCustomClaim = process.env.ADMIN_CUSTOM_CLAIM || 'https://unbound.se/admin'
|
||||||
|
const emailCustomClaim = process.env.EMAIL_CUSTOM_CLAIM || 'https://unbound.se/email'
|
||||||
|
|
||||||
const debug = Debug('app')
|
const debug = Debug('app')
|
||||||
|
|
||||||
@@ -27,10 +28,29 @@ const corsOpts = (req, cb) => {
|
|||||||
cb(null, { origin: req.headers.origin })
|
cb(null, { origin: req.headers.origin })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const addCustomClaims = (email, customClaims, token) => {
|
||||||
|
const emailClaim = {}
|
||||||
|
emailClaim[emailCustomClaim] = email
|
||||||
|
return [...customClaims, emailClaim].reduce((acc, claim) => {
|
||||||
|
return {
|
||||||
|
...acc,
|
||||||
|
...claim
|
||||||
|
}
|
||||||
|
}, token)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
const signToken = (token) => {
|
||||||
|
return jwt.sign(Buffer.from(JSON.stringify(token)), privateKey, {
|
||||||
|
algorithm: 'RS256',
|
||||||
|
keyid: thumbprint
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
// Configure our small auth0-mock-server
|
// Configure our small auth0-mock-server
|
||||||
app.options('*', cors(corsOpts))
|
app.options('*', cors(corsOpts))
|
||||||
.use(cors())
|
.use(cors())
|
||||||
.use(bodyParser.json())
|
.use(bodyParser.json({ strict: false }))
|
||||||
.use(bodyParser.urlencoded({ extended: true }))
|
.use(bodyParser.urlencoded({ extended: true }))
|
||||||
.use(cookieParser())
|
.use(cookieParser())
|
||||||
.use(express.static(`${__dirname}/public`))
|
.use(express.static(`${__dirname}/public`))
|
||||||
@@ -38,53 +58,73 @@ app.options('*', cors(corsOpts))
|
|||||||
|
|
||||||
// This route can be used to generate a valid jwt-token.
|
// This route can be used to generate a valid jwt-token.
|
||||||
app.post('/oauth/token', (req, res) => {
|
app.post('/oauth/token', (req, res) => {
|
||||||
const code = req.body.code
|
|
||||||
const session = sessions[code]
|
|
||||||
|
|
||||||
let date = Math.floor(Date.now() / 1000)
|
let date = Math.floor(Date.now() / 1000)
|
||||||
let accessToken = jwt.sign(Buffer.from(JSON.stringify({
|
if (req.body.grant_type === 'client_credentials' && req.body.client_id) {
|
||||||
iss: jwksOrigin,
|
let accessToken = signToken({
|
||||||
aud: [audience],
|
iss: jwksOrigin,
|
||||||
sub: 'auth0|' + session.email,
|
aud: [audience],
|
||||||
iat: date,
|
sub: 'auth0|management',
|
||||||
exp: date + 7200,
|
iat: date,
|
||||||
azp: session.clientId
|
exp: date + 7200,
|
||||||
})), privateKey, {
|
azp: req.body.client_id
|
||||||
algorithm: 'RS256',
|
})
|
||||||
keyid: thumbprint
|
|
||||||
})
|
|
||||||
|
|
||||||
const token = session.customClaims.reduce((acc, claim) => {
|
let idToken = signToken({
|
||||||
return {
|
iss: jwksOrigin,
|
||||||
...acc,
|
aud: req.body.client_id,
|
||||||
...claim
|
sub: 'auth0|management',
|
||||||
}
|
iat: date,
|
||||||
}, {
|
exp: date + 7200,
|
||||||
iss: jwksOrigin,
|
azp: req.body.client_id,
|
||||||
aud: session.clientId,
|
name: 'Management API'
|
||||||
nonce: session.nonce,
|
})
|
||||||
sub: 'auth0|' + session.email,
|
|
||||||
iat: date,
|
|
||||||
exp: date + 7200,
|
|
||||||
azp: session.clientId,
|
|
||||||
name: 'Example Person',
|
|
||||||
picture: 'https://cdn.playbuzz.com/cdn/5458360f-32ea-460e-a707-1a2d26760558/70bda687-cb84-4756-8a44-8cf735ed87b3.jpg'
|
|
||||||
})
|
|
||||||
let idToken = jwt.sign(Buffer.from(JSON.stringify(token)), privateKey, {
|
|
||||||
algorithm: 'RS256',
|
|
||||||
keyid: thumbprint
|
|
||||||
})
|
|
||||||
|
|
||||||
debug('Signed token for ' + session.email)
|
debug('Signed token for management API')
|
||||||
// res.json({ token });
|
|
||||||
|
|
||||||
res.json({
|
res.json({
|
||||||
access_token: accessToken,
|
access_token: accessToken,
|
||||||
id_token: idToken,
|
id_token: idToken,
|
||||||
scope: 'openid%20profile%20email',
|
scope: 'openid%20profile%20email',
|
||||||
expires_in: 7200,
|
expires_in: 7200,
|
||||||
token_type: 'Bearer'
|
token_type: 'Bearer'
|
||||||
})
|
})
|
||||||
|
} else if (req.body.code) {
|
||||||
|
const code = req.body.code
|
||||||
|
const session = sessions[code]
|
||||||
|
let accessToken = signToken(addCustomClaims(session.email, session.customClaims, {
|
||||||
|
iss: jwksOrigin,
|
||||||
|
aud: [audience],
|
||||||
|
sub: 'auth0|' + session.email,
|
||||||
|
iat: date,
|
||||||
|
exp: date + 7200,
|
||||||
|
azp: session.clientId
|
||||||
|
}))
|
||||||
|
|
||||||
|
let idToken = signToken(addCustomClaims(session.email, session.customClaims, {
|
||||||
|
iss: jwksOrigin,
|
||||||
|
aud: session.clientId,
|
||||||
|
nonce: session.nonce,
|
||||||
|
sub: 'auth0|' + session.email,
|
||||||
|
iat: date,
|
||||||
|
exp: date + 7200,
|
||||||
|
azp: session.clientId,
|
||||||
|
name: 'Example Person',
|
||||||
|
picture: 'https://cdn.playbuzz.com/cdn/5458360f-32ea-460e-a707-1a2d26760558/70bda687-cb84-4756-8a44-8cf735ed87b3.jpg'
|
||||||
|
}))
|
||||||
|
|
||||||
|
debug('Signed token for ' + session.email)
|
||||||
|
|
||||||
|
res.json({
|
||||||
|
access_token: accessToken,
|
||||||
|
id_token: idToken,
|
||||||
|
scope: 'openid%20profile%20email',
|
||||||
|
expires_in: 7200,
|
||||||
|
token_type: 'Bearer'
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
res.status(401)
|
||||||
|
res.send('Missing client_id or client_secret')
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
// This route can be used to generate a valid jwt-token.
|
// This route can be used to generate a valid jwt-token.
|
||||||
@@ -257,7 +297,7 @@ app.post('/issuer', (req, res) => {
|
|||||||
}
|
}
|
||||||
issuer = req.body.issuer
|
issuer = req.body.issuer
|
||||||
jwksOrigin = `https://${issuer}/`
|
jwksOrigin = `https://${issuer}/`
|
||||||
const { privateKey: key, certDer: der, thumbPrint: thumb, exponent: exp, modulus: mod } = cert(jwksOrigin)
|
const { privateKey: key, certDer: der, thumbprint: thumb, exponent: exp, modulus: mod } = cert(jwksOrigin)
|
||||||
privateKey = key
|
privateKey = key
|
||||||
certDer = der
|
certDer = der
|
||||||
thumbprint = thumb
|
thumbprint = thumb
|
||||||
@@ -267,6 +307,28 @@ app.post('/issuer', (req, res) => {
|
|||||||
res.send('ok')
|
res.send('ok')
|
||||||
})
|
})
|
||||||
|
|
||||||
|
app.get('/api/v2/users-by-email', (req, res) => {
|
||||||
|
res.json([])
|
||||||
|
})
|
||||||
|
|
||||||
|
app.post('/api/v2/users', (req, res) => {
|
||||||
|
const email = req.body.email
|
||||||
|
res.json({
|
||||||
|
user_id: `auth0|${email}`
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
app.post('/api/v2/tickets/password-change', (req, res) => {
|
||||||
|
res.json({
|
||||||
|
ticket: `https://some-url`
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
app.use(function(req, res, next) {
|
||||||
|
console.log('404', req.path)
|
||||||
|
res.status(404).send('error: 404 Not Found ' + req.path)
|
||||||
|
})
|
||||||
|
|
||||||
app.listen(3333, () => {
|
app.listen(3333, () => {
|
||||||
debug('Auth0-Mock-Server listening on port 3333!')
|
debug('Auth0-Mock-Server listening on port 3333!')
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -1,132 +1,132 @@
|
|||||||
const base64url = require('base64-url');
|
const base64url = require('base64-url')
|
||||||
const createHash = require('crypto').createHash;
|
const createHash = require('crypto').createHash
|
||||||
const forge = require('node-forge');
|
const forge = require('node-forge')
|
||||||
const NodeRSA = require('node-rsa');
|
const NodeRSA = require('node-rsa')
|
||||||
|
|
||||||
const PRIVATE_KEY_PEM =
|
const PRIVATE_KEY_PEM =
|
||||||
'-----BEGIN RSA PRIVATE KEY-----\n' +
|
'-----BEGIN RSA PRIVATE KEY-----\n' +
|
||||||
'MIIEpAIBAAKCAQEApoocpO3bbUF6o8eyJlQCfwLahEsunWdVF++yOEyKu4Lp1j0m\n' +
|
'MIIEpAIBAAKCAQEApoocpO3bbUF6o8eyJlQCfwLahEsunWdVF++yOEyKu4Lp1j0m\n' +
|
||||||
'2j/P7iHOtxBAkjdM2X2oW3qO1mR0sIFefqnm93g0q2nRuYEoS+W3o6X50wjOVm8f\n' +
|
'2j/P7iHOtxBAkjdM2X2oW3qO1mR0sIFefqnm93g0q2nRuYEoS+W3o6X50wjOVm8f\n' +
|
||||||
'r/tLqELzy5BoET0AQl7Axp1DNsb0HNOBcoIBt+xVY4I+k6uXJJJMzbgvahAgSLZ9\n' +
|
'r/tLqELzy5BoET0AQl7Axp1DNsb0HNOBcoIBt+xVY4I+k6uXJJJMzbgvahAgSLZ9\n' +
|
||||||
'RW0Z0W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Sq4Q6Lomc0WA5C5nBw9RKEUOV9A==\n' +
|
||||||
'-----END RSA PRIVATE KEY-----\n';
|
'-----END RSA PRIVATE KEY-----\n'
|
||||||
|
|
||||||
const PUBLIC_KEY_PEM =
|
const PUBLIC_KEY_PEM =
|
||||||
'-----BEGIN PUBLIC KEY-----\n' +
|
'-----BEGIN PUBLIC KEY-----\n' +
|
||||||
'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEApoocpO3bbUF6o8eyJlQC\n' +
|
'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEApoocpO3bbUF6o8eyJlQC\n' +
|
||||||
'fwLahEsunWdVF++yOEyKu4Lp1j0m2j/P7iHOtxBAkjdM2X2oW3qO1mR0sIFefqnm\n' +
|
'fwLahEsunWdVF++yOEyKu4Lp1j0m2j/P7iHOtxBAkjdM2X2oW3qO1mR0sIFefqnm\n' +
|
||||||
'93g0q2nRuYEoS+W3o6X50wjOVm8fr/tLqELzy5BoET0AQl7Axp1DNsb0HNOBcoIB\n' +
|
'93g0q2nRuYEoS+W3o6X50wjOVm8fr/tLqELzy5BoET0AQl7Axp1DNsb0HNOBcoIB\n' +
|
||||||
't+xVY4I+k6uXJJJMzbgvahAgSLZ9RW0Z0WT+dCHZpZUj0nLxNXIPdci65Bw6Iogn\n' +
|
't+xVY4I+k6uXJJJMzbgvahAgSLZ9RW0Z0WT+dCHZpZUj0nLxNXIPdci65Bw6Iogn\n' +
|
||||||
'qXHP6AwKZXpT6jCzjzq9uyHxVcudqw6j0kQw48/A5A6AN5fIVy1cKnd0sKdqRX1N\n' +
|
'qXHP6AwKZXpT6jCzjzq9uyHxVcudqw6j0kQw48/A5A6AN5fIVy1cKnd0sKdqRX1N\n' +
|
||||||
'UqVoiOrO4jaDB1IdLD+YmRE/JjOHsWIMElYCPxKqnsNo6VCslGX/ziinArHhqRBr\n' +
|
'UqVoiOrO4jaDB1IdLD+YmRE/JjOHsWIMElYCPxKqnsNo6VCslGX/ziinArHhqRBr\n' +
|
||||||
'HwIDAQAB\n' +
|
'HwIDAQAB\n' +
|
||||||
'-----END PUBLIC KEY-----\n';
|
'-----END PUBLIC KEY-----\n'
|
||||||
|
|
||||||
const createCertificate = ({
|
const createCertificate = ({
|
||||||
publicKey,
|
publicKey,
|
||||||
privateKey,
|
privateKey,
|
||||||
jwksOrigin,
|
jwksOrigin
|
||||||
}) => {
|
}) => {
|
||||||
const cert = forge.pki.createCertificate();
|
const cert = forge.pki.createCertificate()
|
||||||
cert.publicKey = publicKey;
|
cert.publicKey = publicKey
|
||||||
cert.serialNumber = '123';
|
cert.serialNumber = '123'
|
||||||
const attrs = [
|
const attrs = [
|
||||||
{
|
{
|
||||||
name: 'commonName',
|
name: 'commonName',
|
||||||
value: `${jwksOrigin}`,
|
value: `${jwksOrigin}`
|
||||||
},
|
}
|
||||||
];
|
]
|
||||||
cert.validity.notBefore = new Date();
|
cert.validity.notBefore = new Date()
|
||||||
cert.validity.notAfter = new Date();
|
cert.validity.notAfter = new Date()
|
||||||
cert.validity.notAfter.setFullYear(cert.validity.notBefore.getFullYear() + 1);
|
cert.validity.notAfter.setFullYear(cert.validity.notBefore.getFullYear() + 1)
|
||||||
cert.setSubject(attrs);
|
cert.setSubject(attrs)
|
||||||
cert.setIssuer(attrs);
|
cert.setIssuer(attrs)
|
||||||
cert.sign(privateKey);
|
cert.sign(privateKey)
|
||||||
return forge.pki.certificateToPem(cert)
|
return forge.pki.certificateToPem(cert)
|
||||||
};
|
}
|
||||||
|
|
||||||
const getCertThumbprint = (certificate) => {
|
const getCertThumbprint = (certificate) => {
|
||||||
const shasum = createHash('sha1');
|
const shasum = createHash('sha1')
|
||||||
const der = Buffer.from(certificate).toString('binary');
|
const der = Buffer.from(certificate).toString('binary')
|
||||||
shasum.update(der);
|
shasum.update(der)
|
||||||
return shasum.digest('base64')
|
return shasum.digest('base64')
|
||||||
};
|
}
|
||||||
|
|
||||||
const createKeyPair = () => {
|
const createKeyPair = () => {
|
||||||
const privateKey = forge.pki.privateKeyFromPem(PRIVATE_KEY_PEM);
|
const privateKey = forge.pki.privateKeyFromPem(PRIVATE_KEY_PEM)
|
||||||
const publicKey = forge.pki.publicKeyFromPem(PUBLIC_KEY_PEM);
|
const publicKey = forge.pki.publicKeyFromPem(PUBLIC_KEY_PEM)
|
||||||
return {
|
return {
|
||||||
privateKey,
|
privateKey,
|
||||||
publicKey,
|
publicKey
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
const bnToB64 = (bn) => {
|
const bnToB64 = (bn) => {
|
||||||
let hex = BigInt(bn).toString(16);
|
let hex = BigInt(bn).toString(16)
|
||||||
if (hex.length % 2) {
|
if (hex.length % 2) {
|
||||||
hex = '0' + hex;
|
hex = '0' + hex
|
||||||
}
|
}
|
||||||
|
|
||||||
const bin = [];
|
const bin = []
|
||||||
let i = 0;
|
let i = 0
|
||||||
let d;
|
let d
|
||||||
let b;
|
let b
|
||||||
while (i < hex.length) {
|
while (i < hex.length) {
|
||||||
d = parseInt(hex.slice(i, i + 2), 16);
|
d = parseInt(hex.slice(i, i + 2), 16)
|
||||||
b = String.fromCharCode(d);
|
b = String.fromCharCode(d)
|
||||||
bin.push(b);
|
bin.push(b)
|
||||||
i += 2;
|
i += 2
|
||||||
}
|
}
|
||||||
|
|
||||||
return Buffer.from(bin.join(''), 'binary').toString('base64');
|
return Buffer.from(bin.join(''), 'binary').toString('base64')
|
||||||
};
|
}
|
||||||
|
|
||||||
const setup = (jwksOrigin) => {
|
const setup = (jwksOrigin) => {
|
||||||
const {privateKey, publicKey} = createKeyPair();
|
const { privateKey, publicKey } = createKeyPair()
|
||||||
const certPem = createCertificate({
|
const certPem = createCertificate({
|
||||||
jwksOrigin,
|
jwksOrigin,
|
||||||
privateKey,
|
privateKey,
|
||||||
publicKey,
|
publicKey
|
||||||
});
|
})
|
||||||
const certDer = forge.util.encode64(
|
const certDer = forge.util.encode64(
|
||||||
forge.asn1
|
forge.asn1
|
||||||
.toDer(forge.pki.certificateToAsn1(forge.pki.certificateFromPem(certPem)))
|
.toDer(forge.pki.certificateToAsn1(forge.pki.certificateFromPem(certPem)))
|
||||||
.getBytes()
|
.getBytes()
|
||||||
);
|
)
|
||||||
const thumbprint = base64url.encode(getCertThumbprint(certDer));
|
const thumbprint = base64url.encode(getCertThumbprint(certDer))
|
||||||
|
|
||||||
const helperKey = new NodeRSA();
|
const helperKey = new NodeRSA()
|
||||||
helperKey.importKey(forge.pki.privateKeyToPem(privateKey));
|
helperKey.importKey(forge.pki.privateKeyToPem(privateKey))
|
||||||
const {n: modulus, e: exponent} = helperKey.exportKey('components');
|
const { n: modulus, e: exponent } = helperKey.exportKey('components')
|
||||||
|
|
||||||
return {
|
return {
|
||||||
privateKey: forge.pki.privateKeyToPem(privateKey),
|
privateKey: forge.pki.privateKeyToPem(privateKey),
|
||||||
certDer: certDer,
|
certDer,
|
||||||
thumbPrint: thumbprint,
|
thumbprint: thumbprint.toString(),
|
||||||
exponent: bnToB64(exponent),
|
exponent: bnToB64(exponent),
|
||||||
modulus: modulus.toString('base64')
|
modulus: modulus.toString('base64')
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
module.exports = setup;
|
module.exports = setup
|
||||||
|
|||||||
+4
-4
@@ -12,17 +12,17 @@
|
|||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"base64-url": "^2.3.3",
|
"base64-url": "^2.3.3",
|
||||||
"body-parser": "^1.20.0",
|
"body-parser": "^1.20.1",
|
||||||
"buffer": "^6.0.3",
|
"buffer": "^6.0.3",
|
||||||
"cookie-parser": "^1.4.6",
|
"cookie-parser": "^1.4.6",
|
||||||
"cors": "^2.8.3",
|
"cors": "^2.8.3",
|
||||||
"debug": "^4.3.4",
|
"debug": "^4.3.4",
|
||||||
"express": "^4.17.3",
|
"express": "^4.18.2",
|
||||||
"https-localhost": "^4.7.1",
|
"https-localhost": "^4.7.1",
|
||||||
"jsonwebtoken": "^8.5.1",
|
"jsonwebtoken": "^9.0.0",
|
||||||
"node-forge": "^1.3.1",
|
"node-forge": "^1.3.1",
|
||||||
"node-rsa": "^1.1.1",
|
"node-rsa": "^1.1.1",
|
||||||
"nodemon": "^2.0.15",
|
"nodemon": "^2.0.20",
|
||||||
"serve-favicon": "^2.4.2"
|
"serve-favicon": "^2.4.2"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user