This repository has been archived by the owner on May 31, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 46
/
index.js
168 lines (145 loc) · 5.57 KB
/
index.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
const express = require('express')
const unirest = require('unirest')
const dotenv = require('dotenv')
const app = express()
const env = process.env
const log = console.log
// Set your port number for localhost
// Could be 3000, 5000, 8000 or 8080. Other ports are not whitelisted.
const port = env.PORT || 5000
/**
* Load environment variables from .env file
* when running in a non-production environment.
* When running on localhost, create a ".env" file in the
* root directory, and provide the following:-
* CLIENT_ID=<value>
* CLIENT_SECRET=<value>
*/
if (env.NODE_ENV !== 'production') dotenv.config()
/**
* CONFIGURATION
* Values provided at the time of Heroku deployment will be used by default.
*/
/**
* SafeTrek OAuth account authorization URL
* Production: https://account.safetrek.io
* Sandbox: https://account-sandbox.safetrek.io
*/
const AUTH_URL = 'https://account-sandbox.safetrek.io'
/**
* SafeTrek OAuth token URL
* Production: https://login.safetrek.io/oauth/token
* Sandbox: https://login-sandbox.safetrek.io/oauth/token
*/
const TOKEN_URL = 'https://login-sandbox.safetrek.io/oauth/token'
/**
* Default Callback path (with leading slash)
* This path is whitelisted on all herokuapp.com sub-domains.
* To get your custom domain or paths whitelisted, please contact us.
*/
const CALLBACK_PATH = '/callback'
// Default scope. DO NOT ALTER.
const SCOPE = 'openid phone offline_access'
// Default response type. DO NOT ALTER.
const RESPONSE_TYPE = 'code'
/**
* Your SafeTrek Client ID (read from the environment)
* Set it in a ".env" file in the root directory if running on localhost
* DO NOT enter it below! You might accidentally commit and make it public.
*/
const CLIENT_ID = env.CLIENT_ID || ''
/**
* Your SafeTrek Client Secret (read from the environment)
* Set it in a ".env" file in the root directory if running on localhost
* DO NOT enter it below! You might accidentally commit and make it public.
*/
const CLIENT_SECRET = env.CLIENT_SECRET || ''
// Enter where you want to redirect after retrieving your 'access_token' and 'refresh_token'.
// Should have a trailing slash. Read from environment if not set below.
// For debugging, you can set this to be a RequestBin URL from https://requestb.in
const REDIRECT_URL = ''
// OAuth demo URL. Will be used as REDIRECT_URL if none is provided.
const DEMO_URL = '/'
// Middleware to filter all but GET method requests
app.use((req, res, next) => {
if(req.method !== 'GET') {
res.status(405).send('Method not Allowed. Only GET requests are acceptable.')
} else {
next()
}
})
// Heroku proxies requests to add SSL layer.
// Enabling this ensures reporting of proper protocol.
app.enable('trust proxy')
// Enabling Pug templating
app.set('views', 'views')
app.set('view engine', 'pug')
// Middleware to parse JSON data in requests
app.use(express.json())
app.get(DEMO_URL, function (req, res) {
let appUrl = `${req.protocol}://${req.get('host')}${CALLBACK_PATH}`
let localhost = req.hostname === 'localhost' ? 'true' : 'false'
res.render('index', {
company_name: 'SafeTrek',
auth_url: `${AUTH_URL}/authorize?client_id=${CLIENT_ID}&scope=${SCOPE}&response_type=${RESPONSE_TYPE}&redirect_uri=`,
redirect_uri: appUrl,
protocol: req.protocol,
callback: CALLBACK_PATH,
localhost: localhost,
port: port
})
})
app.get(CALLBACK_PATH, function (req, res) {
if(req.query.code) {
let appUrl = `${req.protocol}://${req.get('host')}${CALLBACK_PATH}`
let redirectUrl = REDIRECT_URL || env.REDIRECT_URL || ''
let responseUrl = redirectUrl || DEMO_URL
// Pass authorization_code in query parameters for demo app only
let showAuthcode = redirectUrl ? false : true
unirest.post(TOKEN_URL)
.headers({'Accept': 'application/json', 'Content-Type': 'application/json'})
.send({
"grant_type": "authorization_code",
"code": req.query.code,
"client_id": CLIENT_ID,
"client_secret": CLIENT_SECRET,
"redirect_uri": appUrl
})
.end((response) => {
if(response.body.access_token && response.body.refresh_token && response.body.expires_in && responseUrl) {
let redirectLink = `${responseUrl}?`
redirectLink += `access_token=${response.body.access_token}`
redirectLink += `&expires_in=${response.body.expires_in}`
redirectLink += `&refresh_token=${response.body.refresh_token}`
redirectLink += showAuthcode ? `&authorization_code=${req.query.code}` : '' // For demo app only
res.redirect(redirectLink)
} else {
res.status(500).send('Internal Server Error. Something went wrong. Please try again')
}
})
} else if(req.query.refresh_token) {
unirest.post(TOKEN_URL)
.headers({'Accept': 'application/json', 'Content-Type': 'application/json'})
.send({
"grant_type": "refresh_token",
"refresh_token": req.query.refresh_token,
"client_id": CLIENT_ID,
"client_secret": CLIENT_SECRET
})
.end((response) => {
if(response.body.access_token && response.body.expires_in) {
res.json({
access_token: response.body.access_token,
expires_in: response.body.expires_in
})
} else {
res.status(500).send('Internal Server Error. Something went wrong. Please try again')
}
})
} else {
res.status(422).send('Unprocessable Entity. A required parameter is missing.')
}
})
// Fallback to public for loading assets
app.use(express.static('./public/'))
app.listen(port, () => { log(`Node (Express) server started on port ${port}`) })