AuthService.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /* ========================================================================
  2. * Author : Lith
  3. * Version : 1.3
  4. * Date : 2023-06-18
  5. * Email : serset@yeah.net
  6. * ======================================================================== */
  7. /* AuthService.js
  8. <script src="./AuthService.js" makeSureLogin="true"></script>
  9. <script type="text/javascript" >
  10. let token = authService.accessToken.access_token;
  11. authService.logoff();
  12. authService.makeSureLogin();
  13. </script>
  14. */
  15. ; ((window) => {
  16. window.authStore = new AuthStore();
  17. window.authService = new AuthService();
  18. function AuthStore() {
  19. // getQueryString('name') getQueryString("aaa.html?a=1&amp;b=2",'name')
  20. this.getQueryString = function (key, src) {
  21. if (!src) src = location.search;
  22. var v = (src.match(new RegExp("(?:\\?|&)" + key + "=(.*?)(?=&|$)")) || ['', null])[1];
  23. return v && decodeURIComponent(v);
  24. };
  25. this.clearToken = function () {
  26. localStorage.removeItem('jwt_token');
  27. localStorage.removeItem('jwt_token_expires_time');
  28. };
  29. this.cacheToken = function (accessToken) {
  30. localStorage.setItem('jwt_token', accessToken.access_token);
  31. let expires_time = parseInt(accessToken.expires_time);
  32. if (!expires_time && accessToken.expires_in) {
  33. let expires_in = accessToken.expires_in - 10;
  34. expires_time = accessToken.expires_time = new Date().getTime() + expires_in * 1000;
  35. }
  36. if (!expires_time || expires_time <= new Date().getTime()) {
  37. return null;
  38. }
  39. localStorage.setItem('jwt_token_expires_time', expires_time);
  40. return {
  41. access_token: localStorage.getItem('jwt_token'),
  42. expires_time: localStorage.getItem('jwt_token_expires_time')
  43. };
  44. };
  45. //{access_token:'xx',expires_time:'1686857267655'}
  46. this.getToken = function () {
  47. var expires_time = parseInt(localStorage.getItem('jwt_token_expires_time'));
  48. if (expires_time) {
  49. if (expires_time <= new Date().getTime()) {
  50. localStorage.removeItem('jwt_token');
  51. localStorage.removeItem('jwt_token_expires_time');
  52. }
  53. else {
  54. return {
  55. access_token: localStorage.getItem('jwt_token'),
  56. expires_time: localStorage.getItem('jwt_token_expires_time')
  57. };
  58. }
  59. }
  60. return null;
  61. };
  62. }
  63. function AuthService() {
  64. this.audience;
  65. this.loginUrl = '/login.html';
  66. this.indexUrl = '/';
  67. this.ssoBaseUrl = 'https://sso.lith.cloud';
  68. this.onLoginSuccess = null;
  69. this.accessToken = null;
  70. let self = this;
  71. function jumpToSsoLogin() {
  72. //'https://sso.vit.com.cn/connect/authorize?client_id=Vit.SSO.Example&redirect_uri=http://localhost:5000&response_type=token&scope=openid profile email phone&state=123&nonce=456&audience=common';
  73. let url = self.ssoBaseUrl + '/login.html?redirect_uri=' + encodeURIComponent(location.href);
  74. if (self.audience) url += '&audience=' + encodeURIComponent(self.audience);
  75. window.location.href = url;
  76. }
  77. function jumpToPrevUrl() {
  78. let redirect_uri = localStorage.getItem('jwt_redirect_uri');
  79. if (redirect_uri) {
  80. localStorage.removeItem('jwt_redirect_uri');
  81. window.location.href = redirect_uri;
  82. } else {
  83. window.location.href = self.indexUrl;
  84. }
  85. };
  86. // authClient.login();
  87. this.login = function () {
  88. let access_token = authStore.getQueryString('access_token');
  89. if (access_token) {
  90. var expires_time = authStore.getQueryString('expires_time');
  91. this.accessToken = authStore.cacheToken({ access_token, expires_time });
  92. if (this.accessToken) {
  93. try {
  94. if (this.onLoginSuccess) this.onLoginSuccess(this.accessToken);
  95. } catch (e) {
  96. console.log(e);
  97. }
  98. jumpToPrevUrl();
  99. return this.accessToken;
  100. }
  101. }
  102. this.accessToken = authStore.getToken();
  103. if (this.accessToken) {
  104. jumpToPrevUrl();
  105. return this.accessToken;
  106. }
  107. jumpToSsoLogin();
  108. return false;
  109. };
  110. this.logoff = function (redirect_uri) {
  111. this.accessToken = null;
  112. authStore.clearToken();
  113. if (!redirect_uri) redirect_uri = new URL(this.indexUrl, location.origin).toString();
  114. window.location.href = self.ssoBaseUrl + '/logoff.html?redirect_uri=' + encodeURIComponent(redirect_uri);
  115. };
  116. // authService.makeSureLogin();
  117. this.makeSureLogin = function () {
  118. this.accessToken = authStore.getToken();
  119. if (this.accessToken) {
  120. return this.accessToken;
  121. }
  122. localStorage.setItem('jwt_redirect_uri', location.href);
  123. window.location.href = this.loginUrl;
  124. return false;
  125. };
  126. }
  127. try {
  128. let config = (typeof (AuthService_Config) == 'object') ? AuthService_Config : {};
  129. if (config.loginUrl) authService.loginUrl = config.loginUrl;
  130. if (config.indexUrl) authService.indexUrl = config.indexUrl;
  131. if (config.onLoginSuccess) authService.onLoginSuccess = config.onLoginSuccess;
  132. if (config.ssoBaseUrl) authService.ssoBaseUrl = config.ssoBaseUrl;
  133. if (config.audience) authService.audience = config.audience;
  134. if ('true' == document.currentScript.getAttribute('makeSureLogin')) {
  135. if (config.autoLogin !== false)
  136. authService.makeSureLogin();
  137. } else if ('true' == document.currentScript.getAttribute('login')) {
  138. authService.login();
  139. }
  140. } catch (e) {
  141. console.log(e);
  142. }
  143. })(window);