blob: b9c8ed38baf875c943f9961172793c5a16062373 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
package com.ufund.api.ufundapi.controller;
import com.ufund.api.ufundapi.model.UserAuth;
import com.ufund.api.ufundapi.persistence.UserAuthDAO;
import com.ufund.api.ufundapi.persistence.UserDAO;
import com.ufund.api.ufundapi.service.AuthService;
import com.ufund.api.ufundapi.service.UserService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import javax.net.ssl.HttpsURLConnection;
import java.io.IOException;
import java.util.Map;
@RestController
@RequestMapping("auth")
public class AuthController {
private final UserService userService;
private final AuthService authService;
public AuthController(UserService userService, AuthService authService) {
this.userService = userService;
this.authService = authService;
}
/**
* Attempts to log in as a user
* @param params A map/json object in the format {username: string, password: string}
* @return An api key if the auth was successful, null otherwise
*/
@PostMapping("")
public ResponseEntity<String> login(@RequestBody Map<String, String> params) {
String username = params.get("username");
String password = params.get("password");
try {
String key = authService.login(username, password);
return new ResponseEntity<>(key, HttpStatus.OK);
} catch (IOException ex) {
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
} catch (IllegalAccessException e) {
return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
}
}
/**
* TODO
* @return
*/
@DeleteMapping("")
public ResponseEntity<Object> logout() {
return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
}
}
|