· 9 years ago · Sep 28, 2016, 11:46 AM
1<!DOCTYPE html>
2<html>
3<head>
4 <title>Authorizing...</title>
5 <!--<link rel="stylesheet" type="text/css" href="./assets/css/spinkit.css">-->
6</head>
7<body>
8<!--
9<div class="preview-area">
10
11 <div class="spinner">
12 <div class="double-bounce1"></div>
13 <div class="double-bounce2"></div>
14 </div>
15
16</div>-->
17<script src ="./cordova.js" ></script>
18
19<script src ="./assets/lib/jquery-2.1.0.min.js" ></script>
20<script src="./assets/js/app.config.js" type="text/javascript"></script>
21
22<script type="text/javascript">
23 var subdomain,
24 origin,
25 dataToSend,
26 login_url,
27 urlConfig;
28
29 // Configuration urls for OAuth2
30 urlConfig = {
31 development:{
32 hybrid: {
33 application_id: "75298ef60b24f9018eb2e7e788918e10dd58a077a75ec9eb8cec9fcc8866c9a0",
34 secret: "4ee48ab58af1c2058ff85b4f03dc03f7433a1b0d109b71657eb4390f85a14793",
35 redirect_uri: "urn:ietf:wg:oauth:2.0:oob",
36 base_path: "http://hogwarts.lvh.me:3000"
37 },
38 web:{
39 application_id: "e7390355ec7c7d7b55cf7dabbe17d2e9a37f22e21844c61831267a9f5706f230",
40 secret: "4466b27a267062cdd4472153551bf7217b123b81fff743ceb129467ecb6d3d30",
41 redirect_uri: "http://hogwarts.lvh.me:3000/diary_ajs/authorize.html"
42 }
43 },
44 staging:{
45 hybrid: {
46 application_id: "e7390355ec7c7d7b55cf7dabbe17d2e9a37f22e21844c61831267a9f5706f230",
47 secret: "4466b27a267062cdd4472153551bf7217b123b81fff743ceb129467ecb6d3d30",
48 redirect_uri: "urn:ietf:wg:oauth:2.0:oob",
49 base_path: "https://hogwarts.studentediary.com.au"
50
51 },
52 web:{
53 application_id: "49fda0b901182517e5ad99906a42ba8ab2bb886beb6f18dd1147d7ea5231ad76",
54 secret: "ddf4d78c779ba32ec36d7ee81747906182638a541fe47aa5ea6b823abe898e98",
55 redirect_uri: "https://studentediary.com.au/diary_ajs/authorize.html"
56
57 }
58 },
59 production:{
60 hybrid: {
61 application_id: "48095b4dacc0c3c3b3af1a28b0f9351dafeb25debef1771405d658e1fe54804f",
62 secret: "4ac9e01e637b3eac2fceb1a4724acb8ff972134716733895a38b1f22d26ea649",
63 redirect_uri: "urn:ietf:wg:oauth:2.0:oob",
64 base_path: "https://samplehighschool.myediary.com.au"
65 },
66 web:{
67 application_id: "49fda0b901182517e5ad99906a42ba8ab2bb886beb6f18dd1147d7ea5231ad76",
68 secret: "ddf4d78c779ba32ec36d7ee81747906182638a541fe47aa5ea6b823abe898e98",
69 redirect_uri: "urn:ietf:wg:oauth:2.0:oob"
70 }
71 }
72 }
73
74 // if (navigator.userAgent.match(/iPhone|iPad|iPod|Android/i)) {
75 if (navigator.userAgent.match(/iPhone|iPad|iPod|Android/i) && cordova) {
76
77 subdomain = "hogwarts";
78 // origin = "https://hogwarts.studentediary.com.au";
79 origin = "http://hogwarts.lvh.me:3000";
80
81 appConfig.basePath = urlConfig.staging.hybrid.base_path;
82
83 dataToSend = {
84 client_id: urlConfig.staging.hybrid.application_id,
85 redirect_uri: urlConfig.staging.hybrid.redirect_uri,
86 response_type: "code",
87 scope: "manage",
88 display_myediary_layout: true
89 }
90
91 login_url = urlConfig.staging.hybrid.base_path + '/oauth/authorize' + '?mobile=true&' + $.param(dataToSend);
92
93 document.addEventListener("deviceready", function() {
94
95 var inAppBrowserRef = undefined;
96
97 // Hide Device Status bar
98 StatusBar.hide();
99
100 function getAuthorizationCode(access_token) {
101 dataToSend = {
102 client_id: urlConfig.staging.hybrid.application_id,
103 client_secret: urlConfig.staging.hybrid.secret,
104 code: access_token,
105 grant_type: "authorization_code",
106 redirect_uri: urlConfig.staging.hybrid.redirect_uri
107 }
108
109 $.ajax({
110 method: "POST",
111 url: urlConfig.staging.hybrid.base_path + "/oauth/token/",
112 data: dataToSend,
113 success: function(data) {
114 sessionStorage.setItem("AuthorizationCode", data.access_token);
115 sessionStorage.setItem("AuthorizationRefreshToken", data.refresh_token);
116
117 // Small timeout added so that redirection from rails website doesnt result in a flicker
118 setTimeout(function() {
119 redirectToAJS();
120 }, 1000);
121 },
122 error: function(data) {
123 console.log('Failed', data);
124 }
125 });
126 }
127
128
129 function triggerOAuth2(url) {
130
131 var target = "_blank",
132 options = "location=no,toolbar=no";
133
134 inAppBrowserRef = cordova.InAppBrowser.open(url, target, options);
135
136 with(inAppBrowserRef) {
137 addEventListener('loadstart', loadStartCallBack);
138 addEventListener('loadstop', loadStopCallBack);
139 addEventListener('loaderror', loadErrorCallBack);
140 }
141
142 }
143
144 function loadStartCallBack() {
145 console.log("loadStartCallBack");
146 }
147
148 function loadStopCallBack(event) {
149
150 if (inAppBrowserRef != undefined) {
151 // Split Authorization code from url first
152 // The code may contain 'display_myediary_layout' param and hence need to split the code from the pending code.
153 var access_token = event.url.split("authorize/")[1].split("?display_myediary_layout=true")[0];
154 inAppBrowserRef.show();
155 if (access_token && access_token !== "undefined") {
156 setTimeout(function() {
157 localStorage.setItem("AccessToken", access_token);
158 inAppBrowserRef.close();
159 getAuthorizationCode(access_token);
160 inAppBrowserRef = undefined;
161 }, 100);
162 }
163 }
164
165 }
166
167
168
169 function loadErrorCallBack(params) {
170 var scriptErrorMesssage = "alert('Sorry we cannot open that page. Message from the server is : " + params.message + "');"
171
172 inAppBrowserRef.executeScript({
173 code: scriptErrorMesssage
174 }, executeScriptCallBack);
175
176 inAppBrowserRef.close();
177 inAppBrowserRef = undefined;
178
179 }
180
181 function executeScriptCallBack(params) {
182 if (params[0] == null) {
183 $('#status-message').text("Sorry we couldn't open that page. Message from the server is : '" + params.message + "'");
184 }
185 }
186
187 function redirectToAJS(){
188 window.location = "ajs.html";
189 }
190
191 triggerOAuth2(login_url);
192
193 }, false);
194
195 } else {
196 subdomain = window.location.hostname.split('.').shift();
197 origin = window.location.origin;
198 appConfig.basePath = origin;
199
200 dataToSend = {
201 client_id: appConfig.clientId,
202 redirect_uri: origin + '/diary_ajs/authorize.html',
203 response_type: "code",
204 scope: 'manage'
205 }
206
207 login_url = appConfig.basePath + '/oauth/authorize' + '?' + $.param(dataToSend);
208
209 setTimeout(function() {
210 window.location = login_url;
211 }, 1000);
212
213 }
214
215 sessionStorage.setItem("School", subdomain);
216
217
218
219 /********************** Google Analytics script starts here *****************/
220
221 /********************** Google Analytics script starts here *****************/
222
223</script>
224
225</body>
226</html>