This repository was archived by the owner on Dec 4, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 877
/
Copy pathrenewToken.html
58 lines (52 loc) · 4.27 KB
/
renewToken.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
<html>
<head>
<title>Renew Token</title>
<script>
// jwt_decode from https://raw.githubusercontent.com/auth0/jwt-decode/master/build/jwt-decode.min.js
!function a(b,c,d){function e(g,h){if(!c[g]){if(!b[g]){var i="function"==typeof require&&require;if(!h&&i)return i(g,!0);if(f)return f(g,!0);throw new Error("Cannot find module '"+g+"'")}var j=c[g]={exports:{}};b[g][0].call(j.exports,function(a){var c=b[g][1][a];return e(c?c:a)},j,j.exports,a,b,c,d)}return c[g].exports}for(var f="function"==typeof require&&require,g=0;g<d.length;g++)e(d[g]);return e}({1:[function(a,b,c){function d(a){this.message=a}function e(a){var b=String(a).replace(/=+$/,"");if(b.length%4==1)throw new d("'atob' failed: The string to be decoded is not correctly encoded.");for(var c,e,g=0,h=0,i="";e=b.charAt(h++);~e&&(c=g%4?64*c+e:e,g++%4)?i+=String.fromCharCode(255&c>>(-2*g&6)):0)e=f.indexOf(e);return i}var f="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";d.prototype=new Error,d.prototype.name="InvalidCharacterError",b.exports="undefined"!=typeof window&&window.atob||e},{}],2:[function(a,b,c){function d(a){return decodeURIComponent(e(a).replace(/(.)/g,function(a,b){var c=b.charCodeAt(0).toString(16).toUpperCase();return c.length<2&&(c="0"+c),"%"+c}))}var e=a("./atob");b.exports=function(a){var b=a.replace(/-/g,"+").replace(/_/g,"/");switch(b.length%4){case 0:break;case 2:b+="==";break;case 3:b+="=";break;default:throw"Illegal base64url string!"}try{return d(b)}catch(c){return e(b)}}},{"./atob":1}],3:[function(a,b,c){"use strict";var d=a("./base64_url_decode");b.exports=function(a){if(!a)throw new Error("Invalid token specified");return JSON.parse(d(a.split(".")[1]))}},{"./base64_url_decode":2}],4:[function(a,b,c){var d="undefined"!=typeof self?self:"undefined"!=typeof window?window:{},e=a("./lib/index");"function"==typeof d.window.define&&d.window.define.amd?d.window.define("jwt_decode",function(){return e}):d.window&&(d.window.jwt_decode=e)},{"./lib/index":3}]},{},[4]);
</script>
<script>
'use strict';
var parsedToken = jwt_decode(window.localStorage.getItem("access_token"));
var timeoutMiliSeconds = (parsedToken.exp*1000 - 300000) - (new Date().getTime());
// parse QueryString
var params = {};
var queryString = "" ;
if (window.location.href.search("#") != -1) {
queryString = window.location.href.substring(window.location.href.search("#") + 1);
} else {
queryString = window.location.search.substring(1);
}
var a = queryString.split('&');
for (var i = 0; i < a.length; i++) {
var b = a[i].split('=');
params[decodeURIComponent(b[0])] = decodeURIComponent(b[1] || '');
}
if (params["id_token"] != null) {
window.localStorage.setItem("id_token", this.params["id_token"]);
// redirect to get access token here..
window.location.href = "https://login.microsoftonline.com/" + params["tenantID"] +
"/oauth2/authorize?response_type=token&client_id=" + params["clientID"] +
"&resource=" + params["resource"] +
"&redirect_uri=" + encodeURIComponent(window.location.href) +
"&prompt=none&state=" + params["state"] + "&nonce=SomeNonce";
}
else if (params["access_token"] != null) {
window.localStorage.setItem("access_token", this.params["access_token"]);
window.location.href = params["state"];
} else {
window.setTimeout(function() {
var idToken = window.localStorage.getItem("id_token");
var urlWithoutQueryString = window.location.href.substring(0, window.location.href.length - window.location.search.length)
window.location.href = "https://login.microsoftonline.com/" + params["tenantID"] +
"/oauth2/authorize?response_type=token&client_id=" + params["clientID"] +
"&resource=" + params["resource"] +
"&redirect_uri=" + encodeURIComponent(window.location.href) +
"&prompt=none&state=" + encodeURIComponent(window.location.href) + "&nonce=SomeNonce";
}, timeoutMiliSeconds);
}
</script>
</head>
<body>
</body>
</html>