diff options
author | Hayden Hartman <haydenhartman10@gmail.com> | 2025-03-15 23:59:47 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-03-15 23:59:47 -0400 |
commit | 9baaa0590fbc38c06d530786a1de804ee9edd7db (patch) | |
tree | 7c94dc98f9b1978f8ccf3c38bb3777237bf0788a /ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserAuthFIleDAO.java | |
parent | e4e6ae9a3d142fc78f31ee19464ec5e54bfb516f (diff) | |
parent | a3150b8a8e17c8a71f617745bb8588b397a75f47 (diff) | |
download | JellySolutions-9baaa0590fbc38c06d530786a1de804ee9edd7db.tar.gz JellySolutions-9baaa0590fbc38c06d530786a1de804ee9edd7db.tar.bz2 JellySolutions-9baaa0590fbc38c06d530786a1de804ee9edd7db.zip |
Merge pull request #8 from RIT-SWEN-261-02/api-auth
First attempt at an authentication system.
Diffstat (limited to 'ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserAuthFIleDAO.java')
-rw-r--r-- | ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserAuthFIleDAO.java | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserAuthFIleDAO.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserAuthFIleDAO.java new file mode 100644 index 0000000..1fc1e92 --- /dev/null +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserAuthFIleDAO.java @@ -0,0 +1,73 @@ +package com.ufund.api.ufundapi.persistence; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.ufund.api.ufundapi.model.UserAuth; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; + +import java.io.File; +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; + +@Component +public class UserAuthFIleDAO implements UserAuthDAO { + + private final Map<String, UserAuth> userAuthMap; + private final ObjectMapper objectMapper; + private final String filename; + + public UserAuthFIleDAO(ObjectMapper objectMapper, @Value("${authKeys.file}") String filename) throws IOException { + this.userAuthMap = new HashMap<>(); + this.objectMapper = objectMapper; + this.filename = filename; + load(); + } + + /** + * Loads the data from the file into the map + * + * @throws IOException Thrown if there was an issue reading the file + */ + private void load() throws IOException { + userAuthMap.clear(); + + UserAuth[] userAuthKeysArray = objectMapper.readValue(new File(filename), UserAuth[].class); + + for (UserAuth userAuth : userAuthKeysArray) { + userAuthMap.put(userAuth.getKey(), userAuth); + } + } + + /** + * Saves the data from the map into the json file + * + * @throws IOException Thrown on any problem writing the file + */ + private void save() throws IOException { + objectMapper.writeValue(new File(filename), userAuthMap.values()); + } + + @Override + public UserAuth getUserAuth(String key) { + synchronized (userAuthMap) { + return userAuthMap.get(key); + } + } + + @Override + public void addUserAuth(UserAuth userAuth) throws IOException { + synchronized (userAuthMap) { + userAuthMap.put(userAuth.getKey(), userAuth); + save(); + } + } + + @Override + public void removeUserAuth(String key) throws IOException { + synchronized (userAuthMap) { + userAuthMap.remove(key); + save(); + } + } +} |