Compare commits

..

2 Commits

+93 -38
View File
@@ -40,10 +40,17 @@ const addCustomClaims = (email, customClaims, 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`))
@@ -51,47 +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(addCustomClaims(session.email, session.customClaims, { 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
})
let idToken = jwt.sign(Buffer.from(JSON.stringify(addCustomClaims(session.email, session.customClaims, { let idToken = signToken({
iss: jwksOrigin, iss: jwksOrigin,
aud: session.clientId, aud: req.body.client_id,
nonce: session.nonce, sub: 'auth0|management',
sub: 'auth0|' + session.email, iat: date,
iat: date, exp: date + 7200,
exp: date + 7200, azp: req.body.client_id,
azp: session.clientId, name: 'Management API'
name: 'Example Person', })
picture: 'https://cdn.playbuzz.com/cdn/5458360f-32ea-460e-a707-1a2d26760558/70bda687-cb84-4756-8a44-8cf735ed87b3.jpg'
}))), 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.
@@ -274,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!')
}) })