Skip to content

Commit bc81500

Browse files
Create LoginController1.java
1 parent f917b3d commit bc81500

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

src/main/LoginController1.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.scalesec.vulnado;
2+
3+
import org.springframework.boot.*;
4+
import org.springframework.http.HttpStatus;
5+
import org.springframework.web.bind.annotation.*;
6+
import org.springframework.boot.autoconfigure.*;
7+
import org.springframework.stereotype.*;
8+
import org.springframework.beans.factory.annotation.*;
9+
import java.io.Serializable;
10+
11+
@RestController
12+
@EnableAutoConfiguration
13+
public class LoginController {
14+
@Value("${app.secret}")
15+
private String secret;
16+
17+
@CrossOrigin(origins = "*")
18+
@RequestMapping(value = "/login", method = RequestMethod.POST, produces = "application/json", consumes = "application/json")
19+
LoginResponse login(@RequestBody LoginRequest input) {
20+
User user = User.fetch(input.username);
21+
if (Postgres.md5(input.password).equals(user.hashedPassword)) {
22+
return new LoginResponse(user.token(secret));
23+
} else {
24+
throw new Unauthorized("Access Denied");
25+
}
26+
}
27+
}
28+
29+
class LoginRequest implements Serializable {
30+
public String username;
31+
public String password;
32+
}
33+
34+
class LoginResponse implements Serializable {
35+
public String token;
36+
public LoginResponse(String msg) { this.token = msg; }
37+
}
38+
39+
@ResponseStatus(HttpStatus.UNAUTHORIZED)
40+
class Unauthorized extends RuntimeException {
41+
public Unauthorized(String exception) {
42+
super(exception);
43+
}
44+
}

0 commit comments

Comments
 (0)