-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
82 lines (77 loc) · 3.07 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<title>Simple Auth App</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
</head>
<body>
<script>
// get the URL parameters received from the authorization server
var state = getUrlParameter("state"); // session key
var code = getUrlParameter("code"); // authorization code
// load the app parameters stored in the session
var params = JSON.parse(sessionStorage[state]); // load app session
var tokenUri = params.tokenUri;
var clientId = params.clientId;
var secret = params.secret;
var serviceUri = params.serviceUri;
var redirectUri = params.redirectUri;
// Prep the token exchange call parameters
var data = {
code: code,
grant_type: 'authorization_code',
redirect_uri: redirectUri
};
var options;
if (!secret) {
data['client_id'] = clientId;
}
options = {
url: tokenUri,
type: 'POST',
data: data
};
if (secret) {
options['headers'] = {'Authorization': 'Basic ' + btoa(clientId + ':' + secret)};
}
// obtain authorization token from the authorization service using the authorization code
$.ajax(options).done(function(res){
// should get back the access token and the patient ID
var accessToken = res.access_token;
var patientId = res.patient;
// and now we can use these to construct standard FHIR
// REST calls to obtain patient resources with the
// SMART on FHIR-specific authorization header...
// Let's, for example, grab the patient resource and
// print the patient name on the screen
var url = serviceUri + "/Patient/" + patientId;
$.ajax({
url: url,
type: "GET",
dataType: "json",
headers: {
"Authorization": "Bearer " + accessToken
},
}).done(function(pt){
var name = pt.name[0].given.join(" ") +" "+ pt.name[0].family.join(" ");
document.body.innerHTML += "<h3>Patient: " + name + "</h3>";
});
});
// Convenience function for parsing of URL parameters
// based on http://www.jquerybyexample.net/2012/06/get-url-parameters-using-jquery.html
function getUrlParameter(sParam)
{
var sPageURL = window.location.search.substring(1);
var sURLVariables = sPageURL.split('&');
for (var i = 0; i < sURLVariables.length; i++)
{
var sParameterName = sURLVariables[i].split('=');
if (sParameterName[0] == sParam) {
var res = sParameterName[1].replace(/\+/g, '%20');
return decodeURIComponent(res);
}
}
}
</script>
</body>
</html>