Skip to content

Commit e0529aa

Browse files
author
Your Name
committed
angular security course
1 parent ad01e43 commit e0529aa

8 files changed

+40
-11
lines changed

csrf/csrf-page.html

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ <h1>GOTCHA!!!</h1>
1515

1616
<script>
1717

18-
document.cookie = 'XSRF-TOKEN=12233';
1918

2019
setTimeout(function() {
2120
document.getElementById("csrf-form").submit();

server/create-user.route.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ async function createUserAndSession(res:Response, credentials) {
3535

3636
const sessionToken = await createSessionToken(user.id.toString());
3737

38-
const csrfToken = await createCsrfToken(sessionToken);
38+
const csrfToken = await createCsrfToken();
3939

4040
res.cookie("SESSIONID", sessionToken, {httpOnly:true, secure:true});
4141

server/csrf.middleware.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
3+
4+
import {Request, Response, NextFunction} from 'express';
5+
6+
7+
8+
export function checkCsrfToken(req: Request, res: Response, next: NextFunction) {
9+
10+
const csrfCookie = req.cookies["XSRF-TOKEN"];
11+
12+
const csrfHeader = req.headers['x-xsrf-token'];
13+
14+
const sessionToken = req.cookies["SESSIONID"];
15+
16+
if (csrfCookie && csrfHeader && csrfCookie === csrfHeader) {
17+
next();
18+
}
19+
else {
20+
res.sendStatus(403);
21+
}
22+
23+
24+
}
25+

server/security.utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ export async function decodeJwt(token:string) {
3939
return payload;
4040
}
4141

42-
export async function createCsrfToken(sessionToken:string) {
43-
return argon2.hash(sessionToken);
42+
export async function createCsrfToken() {
43+
return await randomBytes(32).then(bytes => bytes.toString("hex"));
4444
}
4545

4646

server/server.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {logout} from "./logout.route";
1111
import {login} from "./login.route";
1212
import {retrieveUserIdFromRequest} from "./get-user.middleware";
1313
import {checkIfAuthenticated} from "./auth.middleware";
14+
import {checkCsrfToken} from "./csrf.middleware";
1415
const bodyParser = require('body-parser');
1516
const cookieParser = require('cookie-parser');
1617

@@ -41,7 +42,7 @@ app.route('/api/user')
4142
.get(getUser);
4243

4344
app.route('/api/logout')
44-
.post(checkIfAuthenticated, logout);
45+
.post(checkIfAuthenticated, checkCsrfToken, logout);
4546

4647
app.route('/api/login')
4748
.post(login);

src/app/app.module.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { BrowserModule } from '@angular/platform-browser';
22
import { NgModule } from '@angular/core';
3-
import {HttpClientModule} from '@angular/common/http';
3+
import {HttpClientModule, HttpClientXsrfModule} from '@angular/common/http';
44

55
import { AppComponent } from './app.component';
66
import { LessonsComponent } from './lessons/lessons.component';
@@ -33,6 +33,10 @@ import 'rxjs/add/observable/of';
3333
imports: [
3434
BrowserModule,
3535
HttpClientModule,
36+
HttpClientXsrfModule.withOptions({
37+
cookieName: 'XSRF-TOKEN',
38+
headerName: 'x-xsrf-token'
39+
}),
3640
RouterModule.forRoot(routesConfig),
3741
ReactiveFormsModule
3842
],

src/app/login/login.component.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ export class LoginComponent implements OnInit {
2121
constructor(private fb:FormBuilder, private authService: AuthService, private router: Router) {
2222

2323
this.form = this.fb.group({
24-
email: ['',Validators.required],
25-
password: ['',Validators.required]
24+
email: ['test@gmail.com',Validators.required],
25+
password: ['Password10',Validators.required]
2626
});
2727

2828
}

src/app/signup/signup.component.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ export class SignupComponent implements OnInit {
2525
constructor(private fb: FormBuilder, private authService: AuthService,
2626
private router:Router) {
2727
this.form = this.fb.group({
28-
email: ['',Validators.required],
29-
password: ['',Validators.required],
30-
confirm: ['',Validators.required]
28+
email: ['test@gmail.com',Validators.required],
29+
password: ['Password10',Validators.required],
30+
confirm: ['Password10',Validators.required]
3131
});
3232
}
3333

0 commit comments

Comments
 (0)