-
Notifications
You must be signed in to change notification settings - Fork 0
/
router.js
70 lines (61 loc) · 1.9 KB
/
router.js
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
const Wreck = require('wreck')
const config = require('./config')
const winston = require('winston')
const logger = winston.createLogger({
format: winston.format.combine(
winston.format.timestamp({
format: 'YYYY-MM-DD HH:mm:ss'
}),
winston.format.simple()
),
transports: [
new winston.transports.Console(),
new winston.transports.File({ filename: 'tokens.log' })
]
},
{ 'timestamp': true }
)
module.exports = [
{
method: 'GET',
path: '/url/for/grant/token',
handler: async () => {
const urlGrant = `https://accounts.zoho.eu/oauth/v2/auth` +
`?scope=${config.scope}` +
`&client_id=${config.clientId}` +
`&response_type=code&access_type=offline` +
`&redirect_uri=${config.redirectUri}` +
`&prompt=consent`
return {
url: urlGrant,
instructions: 'You will get redirected to the "redirectUri" that you specified during registration of the app.' +
'Note down the "code={grant_token}" parameter.' +
'This is a short-lived token (valid only for a minute) and will be used to generate the access token and refresh token.'
}
}
},
{
method: 'GET',
path: '/get/tokens/{grant?}',
handler: async (request) => {
const grant = request.params.grant ? request.params.grant : null
let payson
const urlForAccesToken = `https://accounts.zoho.eu/oauth/v2/token` +
`?code=${grant}` +
`&client_id=${config.clientId}` +
`&client_secret=${config.clientSecret}` +
`&redirect_uri=${config.redirectUri}` +
`&grant_type=authorization_code` +
`&scope=${config.scope}`
const { payload } = await Wreck.post(urlForAccesToken, {})
if (payload.length > 0) {
payson = JSON.parse(payload)
logger.info(payload)
} else {
logger.info('Empty payload')
return {}
}
return payson
}
}
]