oauth2-redirect.html 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <!doctype html>
  2. <html lang="en-US">
  3. <head>
  4. <title>Swagger UI: OAuth2 Redirect</title>
  5. </head>
  6. <body>
  7. <script>
  8. 'use strict';
  9. function run () {
  10. var oauth2 = window.opener.swaggerUIRedirectOauth2;
  11. var sentState = oauth2.state;
  12. var redirectUrl = oauth2.redirectUrl;
  13. var isValid, qp, arr;
  14. if (/code|token|error/.test(window.location.hash)) {
  15. qp = window.location.hash.substring(1);
  16. } else {
  17. qp = location.search.substring(1);
  18. }
  19. arr = qp.split("&");
  20. arr.forEach(function (v,i,_arr) { _arr[i] = '"' + v.replace('=', '":"') + '"';});
  21. qp = qp ? JSON.parse('{' + arr.join() + '}',
  22. function (key, value) {
  23. return key === "" ? value : decodeURIComponent(value);
  24. }
  25. ) : {};
  26. isValid = qp.state === sentState;
  27. if ((
  28. oauth2.auth.schema.get("flow") === "accessCode" ||
  29. oauth2.auth.schema.get("flow") === "authorizationCode" ||
  30. oauth2.auth.schema.get("flow") === "authorization_code"
  31. ) && !oauth2.auth.code) {
  32. if (!isValid) {
  33. oauth2.errCb({
  34. authId: oauth2.auth.name,
  35. source: "auth",
  36. level: "warning",
  37. message: "Authorization may be unsafe, passed state was changed in server Passed state wasn't returned from auth server"
  38. });
  39. }
  40. if (qp.code) {
  41. delete oauth2.state;
  42. oauth2.auth.code = qp.code;
  43. oauth2.callback({auth: oauth2.auth, redirectUrl: redirectUrl});
  44. } else {
  45. let oauthErrorMsg;
  46. if (qp.error) {
  47. oauthErrorMsg = "["+qp.error+"]: " +
  48. (qp.error_description ? qp.error_description+ ". " : "no accessCode received from the server. ") +
  49. (qp.error_uri ? "More info: "+qp.error_uri : "");
  50. }
  51. oauth2.errCb({
  52. authId: oauth2.auth.name,
  53. source: "auth",
  54. level: "error",
  55. message: oauthErrorMsg || "[Authorization failed]: no accessCode received from the server"
  56. });
  57. }
  58. } else {
  59. oauth2.callback({auth: oauth2.auth, token: qp, isValid: isValid, redirectUrl: redirectUrl});
  60. }
  61. window.close();
  62. }
  63. window.addEventListener('DOMContentLoaded', function () {
  64. run();
  65. });
  66. </script>
  67. </body>
  68. </html>