From e9d5addc7a0b65c426803171471ca5a042b73c93 Mon Sep 17 00:00:00 2001 From: Gunther6070 Date: Thu, 6 Mar 2025 17:24:15 -0500 Subject: Migrated auth controller methods to auth service --- .../ufund/api/ufundapi/service/AuthService.java | 38 +++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) (limited to 'ufund-api/src/main/java/com/ufund/api/ufundapi/service/AuthService.java') 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(); + } + } -- cgit v1.2.3