forked from membrane/api-gateway
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJavaScriptClient.html
More file actions
57 lines (52 loc) · 1.87 KB
/
JavaScriptClient.html
File metadata and controls
57 lines (52 loc) · 1.87 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Javascript client</title>
</head>
<body>
<script>
var redirect = function(){
window.location = 'http://localhost:7000/oauth2/auth?response_type=token&client_id=' + encodeURIComponent('abc') + '&redirect_uri='
+ encodeURIComponent('http://localhost:2000/oauth2callback') + '&scope=' + 'profile';
};
var getParams = function(){
var querysRaw = window.location.search.substring(1).split('&');
var result = new Object();
for(var i = 0; i < querysRaw.length;i++){
var pair = querysRaw[i].split("=");
result[pair[0]] = pair[1];
}
sessionStorage.setItem("auth",result.token_type + " " + result.access_token)
return result;
};
var validateAccessToken = function(auth){
var xmlhttp = new XMLHttpRequest();
xmlhttp.open('GET',"http://localhost:7000/oauth2/userinfo",true);
xmlhttp.setRequestHeader("Authorization", auth);
xmlhttp.send(null);
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4) {
if ( xmlhttp.status == 200) {
alert("Validation successful.")
}
else {
sessionStorage.clear();
redirect();
}
}
}
}
var accessToken = sessionStorage.getItem("auth");
if(accessToken){
validateAccessToken(accessToken);
}
else if (window.location.pathname == "/oauth2callback") {
getParams();
validateAccessToken( sessionStorage.getItem("auth"));
} else{
redirect();
}
</script>
</body>
</html>