Advertisement
Guest User

Untitled

a guest
Mar 31st, 2016
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.89 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>RAMP Api</title>
  5. <link href='//fonts.googleapis.com/css?family=Droid+Sans:400,700' rel='stylesheet' type='text/css'/>
  6. <link href='css/hightlight.default.css' media='screen' rel='stylesheet' type='text/css'/>
  7. <link href='css/screen.css' media='screen' rel='stylesheet' type='text/css'/>
  8. <script src='lib/jquery-1.8.0.min.js' type='text/javascript'></script>
  9. <script src='lib/jquery.slideto.min.js' type='text/javascript'></script>
  10. <script src='lib/jquery.wiggle.min.js' type='text/javascript'></script>
  11. <script src='lib/jquery.ba-bbq.min.js' type='text/javascript'></script>
  12. <script src='lib/handlebars-1.0.rc.1.js' type='text/javascript'></script>
  13. <script src='lib/underscore-min.js' type='text/javascript'></script>
  14. <script src='lib/backbone-min.js' type='text/javascript'></script>
  15. <script src='lib/swagger.js' type='text/javascript'></script>
  16. <script src='lib/jquery.lightbox_me.js' type='text/javascript'></script>
  17. <script src='swagger-ui.js' type='text/javascript'></script>
  18. <script src='lib/highlight.7.3.pack.js' type='text/javascript'></script>
  19.  
  20.  
  21. <script type="text/javascript">
  22.  
  23. var tokenCheckInterval = null;
  24. var currentToken = null;
  25. var currentTokenSettings = null;
  26. var inRefresh = false;
  27. var origionalPath = window.location.toString().split("swagger")[0];
  28. var authPath = window.location.toString().split("swagger")[0] + "docs";
  29. var currentApiPath = window.location.toString().split("swagger")[0]+ "docs";
  30.  
  31.  
  32. function setToken(token, refreshToken, settings){
  33.  
  34. inRefresh = false;
  35.  
  36. window.swaggerUi.options.api_key = token;
  37. window.swaggerUi.api.api_key = token;
  38. window.swaggerUi.options.api_refresh_key = refreshToken;
  39. window.swaggerUi.api.api_refresh_key = refreshToken;
  40. $("#input_apiKey").val(token);
  41. $("#input_api_refreshKey").val(refreshToken);
  42.  
  43. currentToken = token;
  44. currentTokenSettings = settings;
  45.  
  46. clearInterval(tokenCheckInterval);
  47. tokenCheckInterval = setInterval(checkToken, 1000);
  48. checkToken();
  49. }
  50.  
  51. function checkToken(){
  52. if(currentTokenSettings == null){
  53. return;
  54. }
  55.  
  56. var s = currentTokenSettings;
  57. if(s.tokenExpires){
  58. var remains = s.tokenExpires - new Date().getTime();
  59.  
  60.  
  61. if(remains < 0){
  62. /** Need to refresh it! **/
  63. if(inRefresh != true && s.refresh){
  64.  
  65. inRefresh = true;
  66. refreshToken();
  67. }
  68.  
  69. remains = 0;
  70. }
  71.  
  72.  
  73. var secs = Math.floor(remains / 1000);
  74. var mins = Math.floor(secs / 60);
  75. secs = secs - (mins * 60);
  76.  
  77. $("#refreshBtn").text(paddZero(mins) + ":" + paddZero(secs));
  78. $("#refreshBtn").reload();
  79. }
  80. //currentTokenSettings
  81. }
  82.  
  83. function refreshToken(){
  84.  
  85. var baseUrl = authPath;
  86. var s = currentTokenSettings;
  87. try{
  88. $.ajax({
  89. type: "GET",
  90. url: baseUrl.split("/docs").join("/auth/" + s.sessionToken),
  91. headers : {
  92. 'X-Amp-Auth':currentToken
  93. },
  94. success: function(data){
  95. if(typeof data === 'string'){
  96. data = JSON.parse(data);
  97. }
  98. if(data.status == "success"){
  99. s.sessionToken = data.content.sessionToken;
  100. var clockDrift = new Date().getTime() - data.content.currentServerTime;
  101. s.tokenExpires = data.content.permissionExpirationTime + clockDrift;
  102.  
  103. setToken(data.content.permissionsToken,data.content.sessionToken, s);
  104. }
  105. },
  106. error: function(data){
  107. alert("Unable to refreshToken.");
  108. },
  109. contentType: 'application/json'
  110. });
  111. }catch(e){
  112. console.log(e);
  113. }
  114. }
  115.  
  116.  
  117.  
  118.  
  119. function paddZero(input){
  120. if(input < 10){
  121. return "0" + input;
  122. }
  123. return input.toString();
  124. }
  125.  
  126. function reloadSwaggerDocs(){
  127. var authToken = $("#input_apiKey").val();
  128. var token = this.currentToken;
  129. var docsUrl ;
  130. if (!new RegExp('\/docs$').test(currentApiPath)){
  131. currentApiPath = currentApiPath + '/docs';
  132. }
  133. docsUrl = currentApiPath;
  134. var settings = this.currentTokenSettings;
  135. window.swaggerUi = new SwaggerUi(
  136. {
  137. discoveryUrl: currentApiPath,
  138. apiKey:"",
  139. apiKeyName: "X-Amp-Auth",
  140. dom_id:"swagger-ui-container",
  141. supportHeaderParams: true,
  142. supportedSubmitMethods: ['get', 'post', 'put', 'delete'],
  143. onComplete: function(swaggerApi, swaggerUi){
  144. if(console) {
  145. console.log("Loaded SwaggerUI")
  146. console.log(swaggerApi);
  147. console.log(swaggerUi);
  148. }
  149. $('pre code').each(function(i, e) {hljs.highlightBlock(e)});
  150. },
  151. onFailure: function(data) {
  152. if(console) {
  153. console.log("Unable to Load SwaggerUI");
  154. console.log(data);
  155. }
  156. },
  157. docExpansion: "none"
  158. });
  159.  
  160. // $("#input_apiKey").val(token);
  161. window.swaggerUi.load();
  162.  
  163. this.currentToken = token;
  164. this.currentTokenSettings = settings;
  165. refreshToken();
  166.  
  167. }
  168.  
  169. function doAuth(){
  170. var username = $("#username").val();
  171. var password = $("#passwd").val();
  172. var baseUrl = currentApiPath;
  173.  
  174. try{
  175. $.ajax({
  176. type: "POST",
  177. url: baseUrl.split("/docs").join("/auth"),
  178. data: JSON.stringify({
  179. username: username,
  180. password: password
  181. }),
  182. success: function(data){
  183. if(typeof data === 'string'){
  184. data = JSON.parse(data);
  185. }
  186. if(data.status == "success"){
  187. var settings = {};
  188.  
  189. /** Auto refresh **/
  190. settings.refresh = true;
  191. settings.sessionToken = data.content.sessionToken;
  192.  
  193. //currentServerTime
  194. var clockDrift = new Date().getTime() - data.content.currentServerTime;
  195. settings.tokenExpires = data.content.permissionExpirationTime + clockDrift;
  196. $("#impersonateBtn").text("Impersonate");
  197. setToken(data.content.permissionsToken, data.content.sessionToken, settings);
  198. }
  199. },
  200. error: function(data){
  201. alert("Unable to Get new token.");
  202. },
  203. contentType: 'application/json'
  204. });
  205. }catch(e){
  206. console.log(e);
  207. }
  208.  
  209. $('#loginWindow').trigger('close');
  210. return false;
  211. }
  212.  
  213. function impersonate(){
  214. var header = $("#impersonationType").val();
  215. var impersonationValue = $("#impersonationValue").val();
  216.  
  217. if(impersonationValue == "" || impersonationValue <= 0){
  218. alert("Id must not be empty or a negative value.");
  219. return;
  220. }
  221. var baseUrl = $("#input_baseUrl").val();
  222.  
  223. var headers = {
  224. 'X-Amp-Auth':currentToken
  225. };
  226. headers[header] = impersonationValue;
  227.  
  228. try{
  229. $.ajax({
  230. type: "POST",
  231. url: baseUrl.split("/docs").join("/auth"),
  232. headers : headers,
  233. success: function(data){
  234. if(typeof data === 'string'){
  235. data = JSON.parse(data);
  236. }
  237. if(data.status == "success"){
  238. var settings = {};
  239. /** Auto refresh **/
  240. settings.refresh = true;
  241. settings.sessionToken = data.content.sessionToken;
  242.  
  243. //currentServerTime
  244. var clockDrift = new Date().getTime() - data.content.currentServerTime;
  245. settings.tokenExpires = data.content.permissionExpirationTime + clockDrift;
  246. $("#impersonateBtn").text(header + ":" + impersonationValue);
  247. setToken(data.content.permissionsToken, data.content.sessionToken, settings);
  248. }
  249. },
  250. error: function(data){
  251. alert("Unable to Impersonate.");
  252. },
  253. contentType: 'application/json'
  254. });
  255. }catch(e){
  256. console.log(e);
  257. }
  258.  
  259. $('#impersonationWindow').trigger('close');
  260. return false;
  261. }
  262.  
  263. $(function () {
  264.  
  265. $('#input_apiKey').change(function() {
  266. var key = $('#input_apiKey').val();
  267. if(key && key.trim() != "") {
  268. window.swaggerUi.options.api_key = key;
  269. window.swaggerUi.api.api_key = key;
  270. }
  271. });
  272.  
  273.  
  274. $('#input_api_refreshKey').change(function() {
  275. var key = $('#input_api_refreshKey').val();
  276. if(key && key.trim() != "") {
  277. window.swaggerUi.options.api_refresh_key = key;
  278. window.swaggerUi.api.api_refresh_key = key;
  279. }
  280. });
  281.  
  282. $("#authenticateBtn").click(function(e){
  283.  
  284. $('#loginWindow').lightbox_me({
  285. centered: true,
  286. onLoad: function() {
  287. $('#loginWindow').find('input:first').focus()
  288. }
  289. });
  290. e.preventDefault();
  291. });
  292.  
  293. $("#refreshBtn").click(function(e){
  294. refreshToken();
  295. });
  296.  
  297. $("#impersonateBtn").click(function(e){
  298.  
  299. $('#impersonationWindow').lightbox_me({
  300. centered: true,
  301. onLoad: function() {
  302. $('#impersonationWindow').find('input:first').focus()
  303. }
  304. });
  305. e.preventDefault();
  306. });
  307.  
  308.  
  309.  
  310. var url = window.location.toString().split("swagger")[0] + "docs";
  311.  
  312. window.swaggerUi = new SwaggerUi({
  313. discoveryUrl: url,
  314. apiKey:"",
  315. apiKeyName: "X-Amp-Auth",
  316. dom_id:"swagger-ui-container",
  317. supportHeaderParams: true,
  318. supportedSubmitMethods: ['get', 'post', 'put', 'delete'],
  319. onComplete: function(swaggerApi, swaggerUi){
  320. if(console) {
  321. console.log("Loaded SwaggerUI")
  322. console.log(swaggerApi);
  323. console.log(swaggerUi);
  324. }
  325. $('pre code').each(function(i, e) {hljs.highlightBlock(e)});
  326. },
  327. onFailure: function(data) {
  328. if(console) {
  329. console.log("Unable to Load SwaggerUI");
  330. console.log(data);
  331. }
  332. },
  333. docExpansion: "none"
  334. });
  335. $("#moveAPIBtn").click(function(e){
  336. currentApiPath = $('#input_baseUrl').val();
  337. reloadSwaggerDocs()
  338. e.preventDefault();
  339. });
  340.  
  341. var url = window.location.toString().split("swagger")[0] + "docs";
  342.  
  343. reloadSwaggerDocs()
  344. });
  345.  
  346. </script>
  347. </head>
  348.  
  349. <body>
  350. <div id='header'>
  351. <div class="swagger-ui-wrap">
  352. <a id="logo"></a>
  353.  
  354. <form id='api_selector'>
  355. <div class='input'id="base_url_descriptor">
  356. <input id="input_baseUrl" name="baseUrl" type="text"/>
  357. <p>Docs URL</p>
  358. </div>
  359. <div class='input' id="access_token_descriptor">
  360. <input placeholder="auth_access_token" id="input_apiKey" name="apiKey" type="text"/>
  361. <p>Access token</p>
  362. </div>
  363. <div class='input' id="refresh_token_descriptor">
  364. <input placeholder="auth_refresh_token" id="input_api_refreshKey" name="apiRefreshKey" type="text"/>
  365. <p>Refresh token</p>
  366. </div>
  367.  
  368. <!--<div class='input'><a id="moveAPIBtn" href="#">Switch API</a></div>-->
  369. <div class='input'><a id="authenticateBtn" href="#">Get Token</a></div>
  370. <div class='input'><a id="refreshBtn" href="#">Refresh</a></div>
  371. <div class='input'><a id="impersonateBtn" href="#">Impersonate</a></div>
  372. <!-- <div class='input'><a id="explore" href="#">Explore</a></div> -->
  373. </form>
  374. </div>
  375.  
  376. </div>
  377.  
  378. <div id="message-bar" class="swagger-ui-wrap">
  379. &nbsp;
  380. </div>
  381.  
  382. <div class="swagger-ui-wrap">
  383. <a href="status_codes.html" target="_blank">Status Codes</a>
  384. <br />
  385. <br />
  386. </div>
  387.  
  388.  
  389. <div id="swagger-ui-container" class="swagger-ui-wrap">
  390. </div>
  391.  
  392. <div id="loginWindow">
  393. <div class="modalBody">
  394.  
  395. <form action="#" onsubmit="return doAuth();">
  396.  
  397. <div class="txt-fld">
  398. <label for="username">Username</label>
  399. <input id="username" class="modalInput" name="" type="text" />
  400.  
  401. </div>
  402. <div class="txt-fld">
  403. <label for="passwd">Password</label>
  404. <input id="passwd" class="modalInput" name="" type="password" />
  405. </div>
  406.  
  407. <div class="txt-fld">
  408. <label for="refresh">Refresh Token</label>
  409. <input id="refresh" class="modalInput" name="" type="checkbox" style="width:auto;" checked="checked">
  410. </div>
  411.  
  412. </div>
  413. <div class="modalFooter">
  414. <button type="submit">Login</button>
  415. </div>
  416. </form>
  417. </div>
  418.  
  419.  
  420.  
  421. <div id="impersonationWindow">
  422. <div class="modalBody">
  423.  
  424. <form action="#" onsubmit="return impersonate();">
  425.  
  426. <div class="txt-fld">
  427. <label for="impersonationType" id="impersonationTypeLabel">Impersonate :</label>
  428. <select id="impersonationType">
  429. <option value="X-Amp-User">user</option>
  430. <option value="X-Amp-Company">company</option>
  431. </select>
  432.  
  433. </div>
  434.  
  435. <div class="txt-fld">
  436. <label for="impersonationValue" id="impersonationValueLabel">Id </label>
  437. <input id="impersonationValue" class="modalInput" name="" type="number" />
  438.  
  439. </div>
  440.  
  441. </div>
  442. <div class="modalFooter">
  443. <button type="submit">Login</button>
  444. </div>
  445. </form>
  446. </div>
  447.  
  448. </body>
  449. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement