You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

83 lines
2.5 KiB

  1. <!doctype html>
  2. <html lang="en-US">
  3. <body onload="run()">
  4. </body>
  5. </html>
  6. <script>
  7. 'use strict';
  8. function run () {
  9. var oauth2 = window.opener.swaggerUIRedirectOauth2;
  10. var sentState = oauth2.state;
  11. var isValid, qp;
  12. qp = (window.location.hash || location.search).substring(1);
  13. qp = qp ? JSON.parse('{"' + qp.replace(/&/g, '","').replace(/=/g, '":"') + '"}',
  14. function (key, value) {
  15. return key === "" ? value : decodeURIComponent(value)
  16. }
  17. ) : {}
  18. isValid = qp.state === sentState
  19. if (oauth2.auth.schema.get("flow") === "accessCode" && !oauth2.auth.code) {
  20. if (!isValid) {
  21. oauth2.errCb({
  22. authId: oauth2.auth.name,
  23. source: "auth",
  24. level: "warning",
  25. message: "Authorization may be unsafe, passed state was changed in server Passed state wasn't returned from auth server"
  26. });
  27. }
  28. if (qp.code) {
  29. delete oauth2.state;
  30. oauth2.auth.code = qp.code;
  31. createForm(oauth2.auth, qp).submit();
  32. } else {
  33. oauth2.errCb({
  34. authId: oauth2.auth.name,
  35. source: "auth",
  36. level: "error",
  37. message: "Authorization failed: no accessCode came from the server"
  38. });
  39. window.close();
  40. }
  41. } else {
  42. oauth2.callback({auth: oauth2.auth, token: qp, isValid: isValid});
  43. window.close();
  44. }
  45. }
  46. function createForm(auth, qp) {
  47. var form = document.createElement("form");
  48. var schema = auth.schema;
  49. var action = schema.get("tokenUrl");
  50. var name, input;
  51. var fields = {
  52. code: qp.code,
  53. "redirect_uri": location.protocol + "//" + location.host + location.pathname,
  54. "grant_type": "authorization_code",
  55. "client_secret": auth.clientSecret,
  56. "client_id": auth.clientId
  57. }
  58. for ( name in fields ) {
  59. input = document.createElement("input");
  60. input.name = name;
  61. input.value = fields[name];
  62. input.type = "hidden";
  63. form.appendChild(input);
  64. }
  65. form.method = "POST";
  66. form.action = action;
  67. document.body.appendChild(form);
  68. return form;
  69. }
  70. </script>