-
Notifications
You must be signed in to change notification settings - Fork 30
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
Rework auth to use jwts #2087
Merged
Merged
Rework auth to use jwts #2087
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
3130846
Extract auth server functionality into separate module. Make it use JWTs
evansdianga d7d1697
Remove unnecessary require
evansdianga 7c73976
Refactor code into modules and remove from main express file.
evansdianga 021866a
Refactor isauthenticated middleware to use JTWs to verify authenticat…
evansdianga c1c6384
Add jsonwebtoken library to handle JWTs
evansdianga 3553535
Add rxjs-compat libray to enable use of Observable of in Angular
evansdianga 8051bdc
Update appcomponnet html to correspond to auth status
evansdianga c94dc1b
Add Http interceptors to work with JWTs
evansdianga 17e5e03
Add http interceptors to module
evansdianga f4728bb
Rework authentication to use JWTs on client side
evansdianga e4a4a7c
Extract method for getting translations to service
evansdianga 487e566
Allow user to extend session before auto expiry
evansdianga 4b890ec
Remove unused reference to httpclient and move up the clearinterval
evansdianga ee22a25
Generate JWT secret key using cryptoto generate 32byte key. Ensure th…
evansdianga bfeb2ad
Update token expiry to 1hr
evansdianga File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
28 changes: 28 additions & 0 deletions
28
editor/src/app/core/http/interceptors/auth-token-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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/** | ||
* Adapted from https://stackoverflow.com/questions/45735655/how-do-i-set-the-baseurl-for-angular-httpclient | ||
*/ | ||
import {Injectable} from '@angular/core'; | ||
import {HttpEvent, HttpInterceptor, HttpHandler, HttpRequest, HttpHeaders, HttpErrorResponse} from '@angular/common/http'; | ||
import {Observable} from 'rxjs/Observable'; | ||
import { AuthenticationService } from '../../auth/_services/authentication.service'; | ||
import { tap } from 'rxjs/operators'; | ||
|
||
@Injectable() | ||
export class APIInterceptor implements HttpInterceptor { | ||
constructor(private authenticationService: AuthenticationService) {} | ||
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { | ||
const token = localStorage.getItem('token'); | ||
const apiReq = token | ||
? req.clone({ url: `${req.url}` , headers: req.headers.set('Authorization', token) }) | ||
: req.clone({ url: `${req.url}` }); | ||
return next.handle(apiReq).pipe( tap(() => {}, | ||
(err: any) => { | ||
if (err instanceof HttpErrorResponse) { | ||
if (err.status !== 401) { | ||
return; | ||
} | ||
this.authenticationService.logout(); | ||
} | ||
})); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { HTTP_INTERCEPTORS } from '@angular/common/http'; | ||
import { APIInterceptor } from './auth-token-interceptor'; | ||
|
||
/** Http interceptor providers in outside-in order */ | ||
/** | ||
* Gotten from https://angular.io/guide/http#advanced-usage | ||
*/ | ||
export const httpInterceptorProviders = [ | ||
{ provide: HTTP_INTERCEPTORS, useClass: APIInterceptor, multi: true }, | ||
]; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
const jwt = require('jsonwebtoken'); | ||
const expiresIn ='1h'; | ||
const issuer = 'Tangerine'; | ||
const jwtTokenSecret = require('crypto').randomBytes(256).toString('base64'); | ||
|
||
const createLoginJWT = ({ username }) => { | ||
const signingOptions = { | ||
expiresIn, | ||
issuer, | ||
subject: username, | ||
}; | ||
return jwt.sign({ username }, jwtTokenSecret, signingOptions); | ||
}; | ||
|
||
const verifyJWT = (token) => { | ||
try { | ||
const jwtPayload = jwt.verify(token, jwtTokenSecret, { issuer }); | ||
return !!jwtPayload; | ||
} catch (error) { | ||
return false; | ||
} | ||
}; | ||
|
||
const decodeJWT = (token) => { | ||
try { | ||
const jwtPayload = jwt.verify(token, jwtTokenSecret, { issuer }); | ||
return jwtPayload; | ||
} catch (error) { | ||
return undefined; | ||
} | ||
}; | ||
|
||
module.exports = { | ||
createLoginJWT, | ||
decodeJWT, | ||
verifyJWT, | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@evansdianga This looks similar to the approach laid out here. Was there another article that you found that talked about this approach?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @rjsteinert , most approaches coalesced around using a 256 bit cryptographically secure hashes. There is a link in the PR description which links to a discussion on the
hapi.js
repo as they used a similar approach. The discussion there also lists quite a number of links and gives further info on the approach.