Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.71 KB | None | 0 0
  1. function OAuthManager() {
  2. this._config = requireConfig('OAuthManager');
  3. iEventEmitter.call(this);
  4.  
  5. var that = this;
  6. if (!OAuthManager._initialized) {
  7. $.ajaxPrefilter(function (opts, currentOpts, xhr) {
  8. if (that._shouldAuthenticate(opts.url)) {
  9. if (that._hasOAuthCookieExpired()) {
  10. xhr.abort();
  11. that.forceLogout();
  12. } else {
  13. var shoudRetry = false;
  14.  
  15. if (!that._hasOAuthTokenExpired()) {
  16. var token = localStorage.getItem(that._config.accessTokenKey);
  17. var authHeader = 'Bearer ' + token;
  18. xhr.setRequestHeader('Authorization', authHeader);
  19. } else {
  20. try {
  21. xhr.abort();
  22. } catch (ex) {
  23. console.log('ex', ex);
  24. }
  25.  
  26. that.refreshToken().then(function () {
  27. var token = localStorage.getItem(that._config.accessTokenKey);
  28. var authHeader = 'Bearer ' + token;
  29. var headers = {};
  30. headers.Authorization = authHeader;
  31. currentOpts.headers = headers;
  32. xhr = $.ajax(currentOpts);
  33. }).catch(function (err) {
  34. console.log(err);
  35. that.forceLogout();
  36. }).done();
  37. }
  38. }
  39. } else {
  40.  
  41. }
  42. });
  43.  
  44. OAuthManager._initialized = true;
  45. }
  46. };
  47.  
  48. (function ($, undefined) {
  49.  
  50. OAuthManager.prototype = Object.create(iEventEmitter.prototype);
  51. OAuthManager.prototype.constructor = OAuthManager;
  52.  
  53. OAuthManager._initialized = false;
  54.  
  55. OAuthManager.prototype.start = function () {
  56. return Q.resolve(true);
  57. };
  58. OAuthManager.prototype.startCheckForceLogout = function () {
  59. var that = this;
  60. var checkForceLogout = function () {
  61. console.log('checkForceLogout');
  62. if (that._hasOAuthCookieExpired()) {
  63. console.log('force logout');
  64. that.forceLogout();
  65. } else {
  66. setTimeout(function () {
  67. checkForceLogout();
  68. }, 15000);
  69. }
  70. };
  71.  
  72. checkForceLogout();
  73. };
  74. OAuthManager.prototype.login = function (username, password) {
  75. var that = this;
  76. return Q.ajax({
  77. type: 'POST',
  78. url: this._config.urls.tokenUrl,
  79. data: {
  80. grant_type: this._config.defaultGrantType,
  81. username: username,
  82. password: password
  83. }
  84. }).then(function (data) {
  85. that._storeOAuthData(data);
  86. return data;
  87. });
  88. };
  89. OAuthManager.prototype.logout = function () {
  90. var that = this;
  91. return Q.ajax({
  92. type: 'POST',
  93. url: this._config.urls.logout
  94. }).then(function () {
  95. that._clearOAuthData();
  96. });
  97. };
  98.  
  99. OAuthManager.prototype.forgottenPassword = function (username) {
  100. return Q.ajax({
  101. url: this._config.urls.forgottenPassword,
  102. type: 'POST',
  103. dataType: 'json',
  104. contentType: 'application/x-www-form-urlencoded',
  105. data: {
  106. '': username
  107. }
  108. });
  109. };
  110.  
  111. OAuthManager.prototype.resetPassword = function (password, passwordConfirm, resetToken) {
  112. return Q.ajax({
  113. url: resetToken ? this._config.urls.resetPassword : this._config.urls.resetPasswordFirstLogin,
  114. type: 'POST',
  115. dataType: 'json',
  116. contentType: 'application/x-www-form-urlencoded',
  117. data: {
  118. NewPassword: password,
  119. ConfirmNewPassword: passwordConfirm,
  120. ResetToken: resetToken
  121. }
  122. });
  123. };
  124.  
  125. OAuthManager.prototype.redirectToLogin = function () {
  126. window.location.href = this._config.urls.loginPage;
  127. }
  128. OAuthManager.prototype.redirectToResetPasswordFirstLogin = function () {
  129. window.location.href = this._config.urls.resetPasswordFirstLoginPage;
  130. }
  131.  
  132. OAuthManager.prototype.refreshToken = function () {
  133. var that = this;
  134. var token = localStorage.getItem(this._config.refreshTokenKey);
  135. var promise;
  136.  
  137. if (token) {
  138. var deferred = Q.defer();
  139. $.ajax({
  140. type: 'POST',
  141. url: this._config.urls.tokenUrl,
  142. data: {
  143. grant_type: 'refresh_token',
  144. refresh_token: token
  145. },
  146. success: function (data) {
  147. deferred.resolve(data);
  148. },
  149. error: function (err) {
  150. deferred.reject(err);
  151. }
  152. });
  153.  
  154. promise = deferred.promise.then(function (data) {
  155. that._storeOAuthData(data);
  156. });
  157. } else {
  158. promise = Q.reject('no refresh token');
  159. }
  160.  
  161. return promise;
  162. };
  163. OAuthManager.prototype.forceLogout = function () {
  164. this._clearOAuthData();
  165. this._clearOAuthCookie();
  166.  
  167. this.trigger('forcelogout');
  168. };
  169.  
  170. OAuthManager.prototype._hasOAuthCookieExpired = function () {
  171. var cookieKey = this._config.oAuthCookieKey;
  172. return !this._hasCookie(cookieKey);
  173. };
  174. OAuthManager.prototype._hasOAuthTokenExpired = function () {
  175. var hasExpired = true;
  176. var expire = localStorage.getItem(this._config.accessTokenExpireKey);
  177. if (expire && !isNaN(expire) && Date.now() < parseInt(expire, 10)) {
  178. hasExpired = false;
  179. }
  180. return hasExpired;
  181. };
  182. OAuthManager.prototype._storeOAuthData = function (data) {
  183. var expireDateMs = Date.now() + (data.expires_in * 1000);
  184.  
  185. localStorage.setItem(this._config.accessTokenKey, data.access_token);
  186. localStorage.setItem(this._config.accessTokenExpireKey, expireDateMs);
  187. localStorage.setItem(this._config.refreshTokenKey, data.refresh_token);
  188. };
  189. OAuthManager.prototype._clearOAuthData = function () {
  190. localStorage.removeItem(this._config.accessTokenKey);
  191. localStorage.removeItem(this._config.accessTokenExpireKey);
  192. localStorage.removeItem(this._config.refreshTokenKey);
  193. };
  194. OAuthManager.prototype._clearOAuthCookie = function () {
  195. this._clearCookie(this._config.oAuthCookieKey);
  196. };
  197. OAuthManager.prototype._clearCookie = function (name) {
  198. document.cookie = name + '=; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;';
  199. };
  200. OAuthManager.prototype._hasCookie = function (cookieName) {
  201. return (document.cookie && document.cookie.indexOf(cookieName + '=') != -1);
  202. };
  203. OAuthManager.prototype._shouldAuthenticate = function (url) {
  204. var result = true;
  205. var urls = this._getUnauthenticatedUrls();
  206. for (var i = 0, len = urls.length; i < len; i++) {
  207. var u = urls[i];
  208. if (url.endsWith(u)) {
  209. result = false;
  210. break;
  211. }
  212. }
  213. return result;
  214. };
  215. OAuthManager.prototype._getUnauthenticatedUrls = function () {
  216. return [
  217. this._config.urls.tokenUrl,
  218. this._config.urls.forgottenPassword,
  219. this._config.urls.resetPassword
  220. ];
  221. };
  222.  
  223. })(jQuery);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement