forked from Gaduo/FHIRSampleCreator
-
Notifications
You must be signed in to change notification settings - Fork 1
/
oauthtest.js
111 lines (84 loc) · 2.78 KB
/
oauthtest.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
//'use strict';
const express = require('express');
const simpleOauthModule = require('simple-oauth2');
var request = require('request');
var config = {};
process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = '0';
/*
//sandpit...
config.clientId = 'MKCc2K0oH801KotKrh7ooVk2tvJavJAj';
config.secret = 'skqfIkZNEf1uZADe';
config.callback = 'http://localhost:3000/callback'
config.baseUrl = 'https://orionhealth-sandbox-bellatrix.apigee.net';
*/
//MOH
config.clientId = 'C5Ay3GjNHXh5UL4uKqBZCGFgIAeAd9fi';
config.secret = 'N8ma3zIa7erpARG6';
config.callback = 'http://localhost:3000/callback'
config.baseUrl = 'https://auth.moh.orionhealth.io';
config.tokenEndPoint = '/oauth2/token';
const app = express();
const oauth2 = simpleOauthModule.create({
client: {
id: config.clientId,
secret: config.secret
},
auth: {
tokenHost: config.baseUrl,
tokenPath: '/oauth2/token',
authorizePath: '/oauth2/authorize'
}
});
// Authorization uri definition
const authorizationUri = oauth2.authorizationCode.authorizeURL({
redirect_uri: config.callback,
scope: 'notifications',
state: '3(#0/!~'
});
// Initial page redirecting to Github
app.get('/auth', function(req, res) {
console.log(authorizationUri);
res.redirect(authorizationUri);
});
// Callback service parsing the authorization token and asking for the access token
app.get('/callback', function(req, res) {
var code = req.query.code;
console.log('code='+code);
//call the token endpoint directly as the library is placing key data in both headers & body, causing a failure
var options = {
method:'POST',
uri : config.baseUrl+config.tokenEndPoint,
body : 'code='+code + "&grant_type=authorization_code&redirect_uri="+config.callback+"&client_id="+config.clientId+"&client_secret="+config.secret,
headers: {'content-type':'application/x-www-form-urlencoded'}
};
console.log(options)
request(options,function(error,response,body){
console.log(error)
if (response) {
console.log(response.statusCode)
}
console.log(body);
})
/*
oauth2.authorizationCode.getToken(tokenConfig, function(error, result) {
if (error) {
console.error('Access Token Error', error.message);
return res.json('Authentication failed');
}
console.log('The resulting token: ', result);
const token = oauth2.accessToken.create(result);
return res
.status(200)
.json(token);
});
*/
});
app.get('/success', function(req, res) {
res.send('');
});
app.get('/', function(req, res) {
res.send('Hello<br><a href="/auth">Login</a>');
});
app.listen(3000, function() {
console.log('Express server started on port 3000'); // eslint-disable-line
});