|
| 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