Skip to content

Commit

Permalink
added new auth expired interceptor
Browse files Browse the repository at this point in the history
  • Loading branch information
xetys committed Nov 13, 2016
1 parent 84592b7 commit fe9d896
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import { PaginationConfig } from './blocks/config/uib-pagination.config';
import { HomeComponent } from './home';
import { <%=jhiPrefixCapitalized%>MainComponent, NavbarComponent, FooterComponent, PageRibbonComponent } from './layouts';

import { AuthExpiredInterceptor } from './blocks/interceptor/auth-expired.interceptor';
import { ErrorHandlerInterceptor } from './blocks/interceptor/errorhandler.interceptor';
import { NotificationInterceptor } from './blocks/interceptor/notification.interceptor';

Expand Down Expand Up @@ -51,7 +50,6 @@ angular
.directive('navbar', <angular.IDirectiveFactory> upgradeAdapter.downgradeNg2Component(NavbarComponent))
.directive('footer', <angular.IDirectiveFactory> upgradeAdapter.downgradeNg2Component(FooterComponent))
.directive('<%=jhiPrefix%>Main', <angular.IDirectiveFactory> upgradeAdapter.downgradeNg2Component(<%=jhiPrefixCapitalized%>MainComponent))
.factory('AuthExpiredInterceptor', AuthExpiredInterceptor)
.factory('ErrorHandlerInterceptor', ErrorHandlerInterceptor)
.factory('NotificationInterceptor', NotificationInterceptor)<% if (enableTranslation) { %>
.factory('TranslationStorageProvider', TranslationStorageProvider)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
<%_ if(authenticationType === 'uaa') { _%>
import { AuthInterceptor } from './blocks/interceptor/auth.interceptor';
<%_ } %>
import { NgModule } from '@angular/core';
import { NgModule, Injector } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { UIRouterModule, RootModule } from 'ui-router-ng2';
<%_ if (authenticationType === 'oauth2' || authenticationType === 'jwt' || authenticationType === 'uaa') { _%>
import { Ng2Webstorage, LocalStorageService, SessionStorageService } from 'ng2-webstorage';
<%_ } if(authenticationType === 'session') { _%>
import { Ng2Webstorage } from 'ng2-webstorage';
<% } %>

import { <%=angular2AppName%>SharedModule } from './shared';
import { <%=angular2AppName%>AdminModule } from './admin/admin.ng2module'; //TODO these couldnt be used from barrels due to an error
Expand All @@ -28,6 +32,13 @@ import {
} from './layouts';
import { localStorageConfig } from './blocks/config/localstorage.config';
import { HttpInterceptor } from './blocks/interceptor/http.interceptor';
import {AuthExpiredInterceptor} from "./blocks/interceptor/auth-expired.interceptor";
<%_ if (authenticationType === 'oauth2' || authenticationType === 'jwt' || authenticationType === 'uaa') { _%>
import {Http, XHRBackend, RequestOptions} from "@angular/http";
<%_ } if(authenticationType === 'session') { _%>
import { StateStorageService } from "./shared/auth/state-storage.service";
<% } %>



localStorageConfig();
Expand Down Expand Up @@ -72,26 +83,35 @@ let routerConfig = {
useFactory: (
backend: XHRBackend,
defaultOptions: RequestOptions,
<%_ if(authenticationType === 'uaa') { _%>
<%_ if (authenticationType === 'oauth2' || authenticationType === 'jwt' || authenticationType === 'uaa') { _%>
localStorage : LocalStorageService,
sessionStorage : SessionStorageService
sessionStorage : SessionStorageService,
injector
<%_ } if (authenticationType === 'session') { _%>
injector
<%_ } _%>
) => new HttpInterceptor(
backend,
defaultOptions,
[
<%_ if(authenticationType === 'uaa') { _%>
new AuthInterceptor(localStorage, sessionStorage)
<%_ if (authenticationType === 'oauth2' || authenticationType === 'jwt' || authenticationType === 'uaa') { _%>
new AuthInterceptor(localStorage, sessionStorage),
new AuthExpiredInterceptor(injector)
<%_ } if (authenticationType === 'session') { _%>
new AuthExpiredInterceptor(injector, injector.get("$rootScope"), stateStorageService)
<%_ } _%>
//other intecetpors can be added here
]
),
deps: [
XHRBackend,
RequestOptions,
<%_ if(authenticationType === 'uaa') { _%>
Injector,
<%_ if (authenticationType === 'oauth2' || authenticationType === 'jwt' || authenticationType === 'uaa') { _%>
LocalStorageService,
SessionStorageService
<%_ } if (authenticationType === 'session') { _%>
StateStorageService
<%_ } _%>
]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ export function HttpConfig($httpProvider, httpRequestInterceptorCacheBusterProvi
httpRequestInterceptorCacheBusterProvider.setMatchlist([/.*api.*/, /.*protected.*/], true);

$httpProvider.interceptors.push('ErrorHandlerInterceptor');
$httpProvider.interceptors.push('AuthExpiredInterceptor');
$httpProvider.interceptors.push('NotificationInterceptor');
// jhipster-needle-angularjs-add-interceptor JHipster will add new application http interceptor here
}
Original file line number Diff line number Diff line change
@@ -1,52 +1,70 @@
import * as angular from 'angular';
import {HttpInterceptable} from "./http.interceptable";
import {RequestOptionsArgs, Response} from "@angular/http";
import {Observable} from "rxjs/Observable";
import {Injector} from "@angular/core";
<%_ if (authenticationType === 'oauth2' || authenticationType === 'jwt' || authenticationType === 'uaa') { _%>
import {AuthService} from "../../shared/auth/auth.service";
import {Principal} from "../../shared/auth/principal.service";
<%_ } if (authenticationType === 'session') { _%>
import {AuthServerProvider} from "../../shared/auth/auth-jwt.service";
import {StateStorageService} from "../../shared/auth/state-storage.service";
<% } %>


export class AuthExpiredInterceptor extends HttpInterceptable {

<%_ if (authenticationType === 'oauth2' || authenticationType === 'jwt' || authenticationType === 'uaa') { _%>
constructor(private injector : Injector) {
super();
}
<%_ } if (authenticationType === 'session') { _%>
constructor(private injector : Injector, private $rootScope, private stateStorageService : StateStorageService) {
super();
}
<% } %>
requestIntercept(options?: RequestOptionsArgs): RequestOptionsArgs {
return options;
}

<%_ if (authenticationType === 'oauth2' || authenticationType === 'jwt' || authenticationType === 'uaa') { _%>
AuthExpiredInterceptor.$inject = ['$rootScope', '$q', '$injector'/*, '$localStorage', '$sessionStorage'*/];

export function AuthExpiredInterceptor($rootScope, $q, $injector/*, $localStorage, $sessionStorage*/) {
var service = {
responseError: responseError
};

return service;

function responseError(response) {
if (response.status === 401) {
//delete $localStorage.authenticationToken;
//delete $sessionStorage.authenticationToken;
var Principal = $injector.get('Principal');
if (Principal.isAuthenticated()) {
var Auth = $injector.get('Auth');
Auth.authorize(true);
responseIntercept(observable: Observable<Response>): Observable<Response> {
let self = this;

return <Observable<Response>> observable.catch((error, source) => {
if(error.status === 401) {
let principal : Principal = self.injector.get(Principal);

if(principal.isAuthenticated()) {
let auth : AuthService = self.injector.get(AuthService);
auth.authorize(true);
}

}
}
return $q.reject(response);
return Observable.throw(error);
});
}
}
<%_ } if (authenticationType === 'session') { _%>
AuthExpiredInterceptor.$inject = ['$rootScope', '$q', '$injector', '$document'];

export function AuthExpiredInterceptor($rootScope, $q, $injector, $document) {
var service = {
responseError: responseError
};

return service;

function responseError(response) {
// If we have an unauthorized request we redirect to the login page
// Don't do this check on the account API to avoid infinite loop
if (response.status === 401 && angular.isDefined(response.data.path) && response.data.path.indexOf('/api/account') === -1) {
var Auth = $injector.get('Auth');
var to = $rootScope.toState;
var params = $rootScope.toStateParams;
Auth.logout();
if (to.name !== 'accessdenied') {
Auth.storePreviousState(to.name, params);

<%_ } if (authenticationType === 'session') { _%>
responseIntercept(observable: Observable<Response>): Observable<Response> {
let self = this;

return <Observable<Response>> observable.catch((error) => {
//todo: this is ng1 way...the ng2 would be more like someRouterService.subscribe(url).forEach..... but I don't know how to do this bow
if(error.status === 401 && !!error.data.path && error.data.path.indexOf("/api/account") === -1) {
let authServerProvider = self.injector.get(AuthServerProvider);
let to = self.$rootScope.toState;
let toParams = self.$rootScope.toStateParams;
authServerProvider.logout();

if(to.name === 'accessdenied') {
self.stateStorageService.storePreviousState(to.name, toParams);
}

return Observable.throw(error);
}
//var LoginService = $injector.get('LoginService');
//LoginService.open();
}
return $q.reject(response);
});
}
}<% } %>
<% } %>

}

0 comments on commit fe9d896

Please sign in to comment.