-
-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
90 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 64 additions & 46 deletions
110
...rs/client-2/templates/src/main/webapp/app/blocks/interceptor/_auth-expired.interceptor.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
} | ||
}<% } %> | ||
<% } %> | ||
|
||
} | ||
|