-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
43 lines (32 loc) · 1.15 KB
/
Copy pathindex.js
File metadata and controls
43 lines (32 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
require('dotenv').config()
const express = require('express')
const app = express()
const request = require('request-promise')
const OktaJwtVerifier = require('@okta/jwt-verifier')
const oktaJwtVerifier = new OktaJwtVerifier({
issuer: 'https://dev-867352.oktapreview.com/oauth2/ausgfx18siTvANpfk0h7',
})
const { ISSUER} = process.env
app.get('/secure', async (req, res) => {
try {
const { authorization } = req.headers
if (!authorization) throw new Error('You must send an Authorization header')
const [authType, token] = authorization.split(' ')
if (authType !== 'Bearer') throw new Error('Expected a Bearer token')
await oktaJwtVerifier.verifyAccessToken(token)
const userinforesponse = await request({
uri: `${ISSUER}/v1/userinfo`,
json: true,
method: 'POST',
headers: {
authorization: `Bearer ${token}`,
},
})
res.json('Hello ' + userinforesponse.name)
} catch (error) {
res.json({ error: error.message })
}
})
app.get('/hello', (req, res) => res.send('Hello World!'))
const port = process.env.PORT || 3000
app.listen(port, () => console.log(`Listening on port ${port}`))