diff options
author | sowgro <tpoke.ferrari@gmail.com> | 2025-03-06 19:05:37 -0500 |
---|---|---|
committer | sowgro <tpoke.ferrari@gmail.com> | 2025-03-06 19:05:37 -0500 |
commit | eb4edcc7e7e4f9a6a59bed6d3952486f179fc445 (patch) | |
tree | 72d10e5e551791ad0d4931e31cffb9a3b03ad4ab /ufund-api/src/main/java/com/ufund/api/ufundapi/service/AuthService.java | |
parent | 1719047ab97f7773b8b847a10482a8c91b53741d (diff) | |
parent | 1fe3905e9d4354657d22e9dbc1a244108ab55a83 (diff) | |
download | JellySolutions-eb4edcc7e7e4f9a6a59bed6d3952486f179fc445.tar.gz JellySolutions-eb4edcc7e7e4f9a6a59bed6d3952486f179fc445.tar.bz2 JellySolutions-eb4edcc7e7e4f9a6a59bed6d3952486f179fc445.zip |
Merge branch 'refs/heads/service-layer' into api-auth
Diffstat (limited to 'ufund-api/src/main/java/com/ufund/api/ufundapi/service/AuthService.java')
-rw-r--r-- | ufund-api/src/main/java/com/ufund/api/ufundapi/service/AuthService.java | 38 |
1 files changed, 37 insertions, 1 deletions
diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/service/AuthService.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/service/AuthService.java index caf1edd..2e644ee 100644 --- a/ufund-api/src/main/java/com/ufund/api/ufundapi/service/AuthService.java +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/service/AuthService.java @@ -1,5 +1,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(); + } + } |