Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function OAuthManager() {
- this._config = requireConfig('OAuthManager');
- iEventEmitter.call(this);
- var that = this;
- if (!OAuthManager._initialized) {
- $.ajaxPrefilter(function (opts, currentOpts, xhr) {
- if (that._shouldAuthenticate(opts.url)) {
- if (that._hasOAuthCookieExpired()) {
- xhr.abort();
- that.forceLogout();
- } else {
- var shoudRetry = false;
- if (!that._hasOAuthTokenExpired()) {
- var token = localStorage.getItem(that._config.accessTokenKey);
- var authHeader = 'Bearer ' + token;
- xhr.setRequestHeader('Authorization', authHeader);
- } else {
- try {
- xhr.abort();
- } catch (ex) {
- console.log('ex', ex);
- }
- that.refreshToken().then(function () {
- var token = localStorage.getItem(that._config.accessTokenKey);
- var authHeader = 'Bearer ' + token;
- var headers = {};
- headers.Authorization = authHeader;
- currentOpts.headers = headers;
- xhr = $.ajax(currentOpts);
- }).catch(function (err) {
- console.log(err);
- that.forceLogout();
- }).done();
- }
- }
- } else {
- }
- });
- OAuthManager._initialized = true;
- }
- };
- (function ($, undefined) {
- OAuthManager.prototype = Object.create(iEventEmitter.prototype);
- OAuthManager.prototype.constructor = OAuthManager;
- OAuthManager._initialized = false;
- OAuthManager.prototype.start = function () {
- return Q.resolve(true);
- };
- OAuthManager.prototype.startCheckForceLogout = function () {
- var that = this;
- var checkForceLogout = function () {
- console.log('checkForceLogout');
- if (that._hasOAuthCookieExpired()) {
- console.log('force logout');
- that.forceLogout();
- } else {
- setTimeout(function () {
- checkForceLogout();
- }, 15000);
- }
- };
- checkForceLogout();
- };
- OAuthManager.prototype.login = function (username, password) {
- var that = this;
- return Q.ajax({
- type: 'POST',
- url: this._config.urls.tokenUrl,
- data: {
- grant_type: this._config.defaultGrantType,
- username: username,
- password: password
- }
- }).then(function (data) {
- that._storeOAuthData(data);
- return data;
- });
- };
- OAuthManager.prototype.logout = function () {
- var that = this;
- return Q.ajax({
- type: 'POST',
- url: this._config.urls.logout
- }).then(function () {
- that._clearOAuthData();
- });
- };
- OAuthManager.prototype.forgottenPassword = function (username) {
- return Q.ajax({
- url: this._config.urls.forgottenPassword,
- type: 'POST',
- dataType: 'json',
- contentType: 'application/x-www-form-urlencoded',
- data: {
- '': username
- }
- });
- };
- OAuthManager.prototype.resetPassword = function (password, passwordConfirm, resetToken) {
- return Q.ajax({
- url: resetToken ? this._config.urls.resetPassword : this._config.urls.resetPasswordFirstLogin,
- type: 'POST',
- dataType: 'json',
- contentType: 'application/x-www-form-urlencoded',
- data: {
- NewPassword: password,
- ConfirmNewPassword: passwordConfirm,
- ResetToken: resetToken
- }
- });
- };
- OAuthManager.prototype.redirectToLogin = function () {
- window.location.href = this._config.urls.loginPage;
- }
- OAuthManager.prototype.redirectToResetPasswordFirstLogin = function () {
- window.location.href = this._config.urls.resetPasswordFirstLoginPage;
- }
- OAuthManager.prototype.refreshToken = function () {
- var that = this;
- var token = localStorage.getItem(this._config.refreshTokenKey);
- var promise;
- if (token) {
- var deferred = Q.defer();
- $.ajax({
- type: 'POST',
- url: this._config.urls.tokenUrl,
- data: {
- grant_type: 'refresh_token',
- refresh_token: token
- },
- success: function (data) {
- deferred.resolve(data);
- },
- error: function (err) {
- deferred.reject(err);
- }
- });
- promise = deferred.promise.then(function (data) {
- that._storeOAuthData(data);
- });
- } else {
- promise = Q.reject('no refresh token');
- }
- return promise;
- };
- OAuthManager.prototype.forceLogout = function () {
- this._clearOAuthData();
- this._clearOAuthCookie();
- this.trigger('forcelogout');
- };
- OAuthManager.prototype._hasOAuthCookieExpired = function () {
- var cookieKey = this._config.oAuthCookieKey;
- return !this._hasCookie(cookieKey);
- };
- OAuthManager.prototype._hasOAuthTokenExpired = function () {
- var hasExpired = true;
- var expire = localStorage.getItem(this._config.accessTokenExpireKey);
- if (expire && !isNaN(expire) && Date.now() < parseInt(expire, 10)) {
- hasExpired = false;
- }
- return hasExpired;
- };
- OAuthManager.prototype._storeOAuthData = function (data) {
- var expireDateMs = Date.now() + (data.expires_in * 1000);
- localStorage.setItem(this._config.accessTokenKey, data.access_token);
- localStorage.setItem(this._config.accessTokenExpireKey, expireDateMs);
- localStorage.setItem(this._config.refreshTokenKey, data.refresh_token);
- };
- OAuthManager.prototype._clearOAuthData = function () {
- localStorage.removeItem(this._config.accessTokenKey);
- localStorage.removeItem(this._config.accessTokenExpireKey);
- localStorage.removeItem(this._config.refreshTokenKey);
- };
- OAuthManager.prototype._clearOAuthCookie = function () {
- this._clearCookie(this._config.oAuthCookieKey);
- };
- OAuthManager.prototype._clearCookie = function (name) {
- document.cookie = name + '=; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;';
- };
- OAuthManager.prototype._hasCookie = function (cookieName) {
- return (document.cookie && document.cookie.indexOf(cookieName + '=') != -1);
- };
- OAuthManager.prototype._shouldAuthenticate = function (url) {
- var result = true;
- var urls = this._getUnauthenticatedUrls();
- for (var i = 0, len = urls.length; i < len; i++) {
- var u = urls[i];
- if (url.endsWith(u)) {
- result = false;
- break;
- }
- }
- return result;
- };
- OAuthManager.prototype._getUnauthenticatedUrls = function () {
- return [
- this._config.urls.tokenUrl,
- this._config.urls.forgottenPassword,
- this._config.urls.resetPassword
- ];
- };
- })(jQuery);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement