-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
225 lines (201 loc) · 8.39 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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link href="//maxcdn.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.min.css" rel="stylesheet">
<style>
body {
margin: 10px;
text-align: center;
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
}
body > div{
margin-bottom: 2em;
}
.btn-2 {
background-color: #00AFD1;
}
.btn-2 .round {
background-color: #00c4eb;
}
a {
text-decoration: none;
-moz-border-radius: 30px;
-webkit-border-radius: 30px;
border-radius: 30px;
padding: 12px 53px 12px 23px;
color: #fff;
text-transform: uppercase;
font-family: sans-serif;
font-weight: bold;
position: relative;
-moz-transition: all 0.3s;
-o-transition: all 0.3s;
-webkit-transition: all 0.3s;
transition: all 0.3s;
display: inline-block;
}
a span {
position: relative;
z-index: 3;
}
a .round {
-moz-border-radius: 50%;
-webkit-border-radius: 50%;
border-radius: 50%;
width: 38px;
height: 38px;
position: absolute;
right: 3px;
top: 3px;
-moz-transition: all 0.3s ease-out;
-o-transition: all 0.3s ease-out;
-webkit-transition: all 0.3s ease-out;
transition: all 0.3s ease-out;
z-index: 2;
}
a .round i {
position: absolute;
top: 50%;
margin-top: -6px;
left: 50%;
margin-left: -4px;
-moz-transition: all 0.3s;
-o-transition: all 0.3s;
-webkit-transition: all 0.3s;
transition: all 0.3s;
}
.txt {
font-size: 14px;
line-height: 1.45;
}
.type-1 a:hover {
padding-left: 48px;
padding-right: 28px;
}
.type-1 a:hover .round {
width: calc(100% - 6px);
-moz-border-radius: 30px;
-webkit-border-radius: 30px;
border-radius: 30px;
}
.type-1 a:hover .round i {
left: 12%;
}
#fetch a{
text-transform: none;
color: #000;
}
</style>
</head>
<body>
<div class="type-1">
<div>
<a href="#" id="login" class="btn btn-2" style="display:none">
<span class="txt">Log in with SAML</span>
<span class="round"><i class="fa fa-chevron-right"></i></span>
</a>
</div>
</div>
<h1 id="user" style="display:none"></h1>
<h2 id="mail" style="display:none"></h2>
<p id="fetch">
See developers tools to see request (before and after login).<br />
<a href="#">Get private data</a>
</p>
<script
src="https://code.jquery.com/jquery-3.3.1.min.js"
integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
crossorigin="anonymous"></script>
<script src="endpoint.js"></script>
<script>
$(document).ready(function(){
let tokenTtl = window.localStorage.getItem("tokenTtl");
tokenTtl = tokenTtl ? tokenTtl*1000 : +new Date();
$("#fetch a").on("click", function(e){
e.preventDefault();
_request(function(response){
!response.message ? alert("Error. Not authorized") : alert(response.message);
})
});
if(tokenTtl>+new Date()){
$("#login").css("display", "none");
$("#user").css("display", "block");
$("#mail").css("display", "block");
$("#fetch").css("display", "block");
const message = window.localStorage.getItem("dtoken") ? JSON.parse(window.localStorage.getItem("dtoken")) : null;
if(message){
$("#user").html(`Hi, ${message["urn:oid:2.5.4.42"]}`);
$("#mail").html(`(${message["urn:oid:0.9.2342.19200300.100.1.3"]})`); //review your SAML response
}
}else{
$("#login").css("display", "block");
}
});
const jwtPath = "/demo/getJWT";
function decoder(token) {
let json_string = "";
try {
const base64 = token.replace('-', '+').replace('_', '/')
const utf8 = atob(base64)
const json = JSON.parse(utf8)
json_string = JSON.stringify(json, null, 4)
} catch (err) {
json_string = "Bad Section.\nError: " + err.message
}
return json_string
}
let loginWindow;
$("#login").on("click", function(e){
e.preventDefault();
const w=425, h=425;
// Fixes dual-screen position Most browsers Firefox
const dualScreenLeft = window.screenLeft != undefined ? window.screenLeft : screen.left;
const dualScreenTop = window.screenTop != undefined ? window.screenTop : screen.top;
const width = window.innerWidth ? window.innerWidth : document.documentElement.clientWidth ? document.documentElement.clientWidth : screen.width;
const height = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight ? document.documentElement.clientHeight : screen.height;
const left = ((width / 2) - (w / 2)) + dualScreenLeft;
const top = ((height / 2) - (h / 2)) + dualScreenTop;
loginWindow = window.open(endpoint+jwtPath, "Login", 'scrollbars=yes, width=' + w + ', height=' + h + ', top=' + top + ', left=' + left);
// Puts focus on the newWindow
if (window.focus) {
loginWindow.focus();
}
});
window.addEventListener('message', function(e) {
if(e.origin !== endpoint){
return;
}
loginWindow.close();
const message = JSON.parse(decoder(e.data.split(".")[1]));
window.localStorage.setItem("token",e.data);
window.localStorage.setItem("tokenTtl",message.exp);
window.localStorage.setItem("dtoken",JSON.stringify(message));
$("#user").html(`Hi, ${message["urn:oid:2.5.4.42"]}`);
$("#mail").html(`(${message["urn:oid:0.9.2342.19200300.100.1.3"]})`); //review your SAML response
$("#user").css("display", "block");
$("#mail").css("display", "block");
$("#fetch").css("display", "block");
$("#login").css("display", "none");
});
function _request(cb){
fetch(endpoint + "/demo/private", {
method: "GET",
headers: {
"Authorization": window.localStorage.getItem("token"),
"Content-Type": "application/json"
}
})
.then(resp => resp.json())
.then(function(data) {
cb(data);
})
.catch(function(error) {
console.log(error);
});
}
</script>
</body>
</html>