Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

01_CD_MP #12

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
84 changes: 84 additions & 0 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions pw/pw-jwt-oauth/client/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion pw/pw-jwt-oauth/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"@angular/platform-browser": "~13.0.0",
"@angular/platform-browser-dynamic": "~13.0.0",
"@angular/router": "~13.0.0",
"maven": "^5.0.0",
"ngx-webstorage": "^9.0.0",
"rxjs": "~7.4.0",
"tslib": "^2.3.0",
Expand All @@ -37,4 +38,4 @@
"karma-jasmine-html-reporter": "~1.7.0",
"typescript": "~4.4.3"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,24 @@ export class JwtInterceptor implements HttpInterceptor {
private ng2localStorage: LocalStorageService,
private ng2sessionStorage: SessionStorageService,
private router: Router
) {}
) {
}

intercept(
req: HttpRequest<any>,
next: HttpHandler
): Observable<HttpEvent<any>> {
// retrieve jwt token from client storage (local or session) with the key 'authenticationToken'
// let token = this.ng2localStorage.retrieve(....) || this.ng2sessionStorage.retrieve(....);
// verify token is present
// if (...){
// set authorization header in the request with the token : 'Authorization: Bearer __token__'
// req = req.clone({
// setHeaders: {
// Authorization: ....
// }
// });
let token = this.ng2localStorage.retrieve('authenticationToken') || this.ng2sessionStorage.retrieve('authenticationToken');
// get the JWT token from the client's storage
if (token) {
// check if the token is present
req = req.clone({
setHeaders: {
Authorization: `Bearer ${token}`
// set authorization header
}
});
}

return next.handle(req).pipe(
tap(
Expand Down
28 changes: 15 additions & 13 deletions pw/pw-jwt-oauth/client/src/app/services/auth/auth-jwt.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,22 @@ export class AuthServerProvider {
map((resp) => this.authenticateSuccess(resp, credentials.rememberMe))
);
}

// TODO PW-JWT-auth
// In case of authentication success , get the JWT from the response and store it in client storage

// Get JWT in case of authentication success and store it in client storage
authenticateSuccess(resp: HttpResponse<any>, rememberMe: boolean) {
// BearerToken to retrieve from Authorization Header ( use resp.headers.get )
// let bearerToken = resp.headers.get('....');
// Verify the content of the authorization header is indeed a bearer token and not null ( use slice on string)
// if (bearerToken && ...)
// retrieve the token by removing the "Bearer" string with slice function
// let jwt = bearerToken.slice(.....);
// store the jwt in the credentials ( use storeAuthenticationToken )
// this.storeAuthenticationToken(.....);
// return the jwt
return resp;
let bearerToken = resp.headers.get("Authorization");
let jwt;

// Verify the bearerToken
if(bearerToken?.slice(0,6) == "Bearer"){

// Retrieve the token from bearerToken
jwt = bearerToken.slice(7);

// Store the jwt in the credentials
this.storeAuthenticationToken(jwt, rememberMe)
}
return jwt;
}

loginWithToken(jwt: string, rememberMe: boolean) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,10 @@ protected void configure(HttpSecurity http) throws Exception {
.antMatchers("/management/health").permitAll()
.antMatchers("/management/**").hasAuthority(AuthoritiesConstants.ADMIN)
.antMatchers("/v2/api-docs/**").hasAuthority(AuthoritiesConstants.ADMIN)
// .and()
// .apply(securityConfigurerAdapter())
.and()
.apply(securityConfigurerAdapter())
;
// TODO uncomment this line to activate JWT filter
// Uncomment line 103 & 104 to activate the JWT configurer

}

Expand Down