blob: 2e644ee673d30e6cf135abe1ca882b9025fbf24e (
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
|
package com.ufund.api.ufundapi.service;
import com.ufund.api.ufundapi.model.UserAuth;
import com.ufund.api.ufundapi.persistence.UserAuthDAO;
import org.springframework.stereotype.Component;
import java.io.IOException;
@Component
public class AuthService {
private final UserAuthDAO userAuthDAO;
private final UserService userService;
public AuthService(UserAuthDAO userAuthDAO, UserService userService) {
this.userAuthDAO = userAuthDAO;
this.userService = userService;
}
public UserAuth getUserAuth(String key) {
return userAuthDAO.getUserAuth(key);
}
public void authenticate(String username, String key) throws IllegalAccessException {
var userAuth = getUserAuth(key);
if (userAuth == null || !userAuth.getUsername().equals(username)) {
throw new IllegalAccessException("Unauthorized");
}
}
public String login(String username, String password) throws IllegalAccessException, IOException {
var usr = userService.getUser(username);
if (usr == null || !usr.verifyPassword(password)) {
throw new IllegalAccessException("Unauthorized");
}
var userAuth = UserAuth.generate(username);
userAuthDAO.addUserAuth(userAuth);
return userAuth.getKey();
}
}
|