filechampion4j 0.9.8.3
File validation library for Java
Loading...
Searching...
No Matches
dev.filechampion.filechampion4j.CredentialsManager Class Reference
Collaboration diagram for dev.filechampion.filechampion4j.CredentialsManager:
Collaboration graph

Public Member Functions

 CredentialsManager (Path credsPath, List< String > credsNamesList) throws IllegalArgumentException
 
void setExpirationTime (long expirationTime) throws IllegalArgumentException
 
char[] getCredentials (String credsName) throws IllegalArgumentException, IOException
 

Private Member Functions

char[] getCredFileChars (Path credFilePath) throws IOException
 
void logInfo (StringBuilder message)
 
void logSevere (StringBuilder message)
 
void logFine (StringBuilder message)
 
void generateRandomString ()
 
char[] addSalt (char[] charString)
 
void checkCredExpiration ()
 

Private Attributes

StringBuilder logMessage = new StringBuilder()
 
long credsExpirationTime = 300000
 
Path credsPath
 
List< String > credsNamesList
 
List< char[]> credsList = new LinkedList<>()
 
Map< String, Map< Integer, Long > > credsMap = new HashMap<>()
 
SecureRandom random = new SecureRandom()
 
String randomString = ""
 
int randomStringIndex = 0
 
boolean expirationTimerStarted = false
 

Static Private Attributes

static final Logger LOGGER = Logger.getLogger(CredentialsManager.class.getName())
 

Detailed Description

CredentialsManager class is used to manage any credendials that are needed for plugins, such as API keys, etc.

Definition at line 27 of file CredentialsManager.java.

Constructor & Destructor Documentation

◆ CredentialsManager()

dev.filechampion.filechampion4j.CredentialsManager.CredentialsManager ( Path  credsPath,
List< String >  credsNamesList 
) throws IllegalArgumentException

Constructor for CredentialsManager class.

Parameters
credsPath(Path) - Path to the directory where the credentials files are stored.
credsNamesList(List <String>) - List of the names of the credentials names/files.
Exceptions
IllegalArgumentException- If the credsPath is null or does not exist, or if the credsNamesList is null or empty, or if any of the credentials files are not found.

Definition at line 59 of file CredentialsManager.java.

59 {
60 if (credsPath == null || !Files.exists(credsPath)) {
61 logSevere(logMessage.replace(0, logMessage.length() ,"Defined credentials path was not found."));
62 throw new IllegalArgumentException("Defined credentials path was not found.");
63 }
64 if (credsNamesList == null || credsNamesList.isEmpty()) {
65 logSevere(logMessage.replace(0, logMessage.length() ,"Defined credentials list was empty."));
66 throw new IllegalArgumentException("Defined credentials list was empty.");
67 }
68 this.credsPath = credsPath;
69
70 // Check that all creds files exist
71 for (String credsName : credsNamesList) {
72 Path credFilePath = credsPath.resolve(credsName);
73 if (!Files.exists(credFilePath)) {
74 logSevere(logMessage.replace(0, logMessage.length() ,"Credentials file ").append(credsName).append(" was not found."));
75 throw new IllegalArgumentException("Credentials file " + credsName + " was not found.");
76 }
77 }
78 this.credsNamesList = credsNamesList;
79 logInfo(logMessage.replace(0, logMessage.length() ,"CredentialsManager initialized."));
80 }

References dev.filechampion.filechampion4j.CredentialsManager.credsNamesList, dev.filechampion.filechampion4j.CredentialsManager.credsPath, dev.filechampion.filechampion4j.CredentialsManager.logInfo(), dev.filechampion.filechampion4j.CredentialsManager.logMessage, and dev.filechampion.filechampion4j.CredentialsManager.logSevere().

Here is the call graph for this function:

Member Function Documentation

◆ addSalt()

char[] dev.filechampion.filechampion4j.CredentialsManager.addSalt ( char[]  charString)
private

Add unique salt to a given string.

Parameters
charString(char[]) - The string to add salt to.
Returns
(char[]) - The salted string.

Definition at line 223 of file CredentialsManager.java.

223 {
224 if (this.randomStringIndex + charString.length > this.randomString.length()) {
226 this.randomStringIndex = 0;
227 }
228 char[] saltChars = randomString.substring(randomStringIndex, randomStringIndex + charString.length).toCharArray();
229 char[] saltedString = new char[charString.length * 2];
230 int index = 0;
231 for (int i = 0; i < charString.length; i++) {
232 saltedString[index++] = charString[i];
233 saltedString[index++] = saltChars[i];
234 }
235 this.randomStringIndex += saltChars.length;
236 return saltedString;
237 }

◆ checkCredExpiration()

void dev.filechampion.filechampion4j.CredentialsManager.checkCredExpiration ( )
private

This method is used to check if any credential is expired, and remove them from the list if it is.

Definition at line 242 of file CredentialsManager.java.

242 {
243 logFine(logMessage.replace(0, logMessage.length() ,"Checking for expired credentials."));
244 List<String> expiredKeys = new ArrayList<>();
245 for (Map.Entry<String, Map<Integer, Long>> entry : this.credsMap.entrySet()) {
246 String key = entry.getKey();
247 Integer credPosition = entry.getValue().keySet().iterator().next();
248 long credLastTime = entry.getValue().getOrDefault(2, credsExpirationTime);
249 if (System.currentTimeMillis() - credLastTime > credsExpirationTime) {
250 Arrays.fill(this.credsList.get(credPosition), '0');
251 this.credsList.remove(this.credsList.get(credPosition));
252 expiredKeys.add(key);
253 logFine(logMessage.replace(0, logMessage.length() ,"Credentials ").append(key).append(" removed from list."));
254 }
255 }
256 for (String key : expiredKeys) {
257 this.credsMap.remove(key);
258 logFine(logMessage.replace(0, logMessage.length() ,"Expired credentials metadata ").append(key).append(" removed from map."));
259 }
260 }

◆ generateRandomString()

void dev.filechampion.filechampion4j.CredentialsManager.generateRandomString ( )
private

Generate a random string of a given length

Definition at line 206 of file CredentialsManager.java.

206 {
207 int leftLimit = 48; // numeral '0'
208 int rightLimit = 122; // letter 'z'
209 int targetStringLength = 2048;
210 this.randomString = random.ints(leftLimit, rightLimit + 1)
211 .filter(i -> (i <= 57 || i >= 65) && (i <= 90 || i >= 97))
212 .limit(targetStringLength)
213 .collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append)
214 .toString();
215 logFine(logMessage.replace(0, logMessage.length() ,"Random string generated."));
216 }

◆ getCredentials()

char[] dev.filechampion.filechampion4j.CredentialsManager.getCredentials ( String  credsName) throws IllegalArgumentException, IOException

This method is used to retrieve a credential from the cached credentials list or read it from the file and add it to the list.

Parameters
credsName(String) - Name of the credentials name/file to retrieve.
Returns
(char[]) - The credentials as a char[].
Exceptions
IllegalArgumentException- If the credsName is null or empty, or if the credsName is not found in the credsNamesList.
IOException- If there is an error reading the credentials file.

Definition at line 112 of file CredentialsManager.java.

112 {
113 if (credsName == null || credsName.isEmpty()) {
114 logSevere(logMessage.replace(0, logMessage.length() ,"Credentials name was empty."));
115 throw new IllegalArgumentException("Credentials name was empty.");
116 }
117 if (!this.credsNamesList.contains(credsName)) {
118 logSevere(logMessage.replace(0, logMessage.length() ,"Credentials name was not found."));
119 throw new IllegalArgumentException("Credentials name was not found.");
120 }
124 }
125
126 if (!this.credsMap.containsKey(credsName)) {
127 Path fullCredPath = this.credsPath.resolve(credsName);
128 this.credsList.add(addSalt(this.getCredFileChars(fullCredPath)));
129 logFine(logMessage.replace(0, logMessage.length() ,"Credentials ").append(credsName).append(" with salt added to list."));
130 credsMap.computeIfAbsent(credsName, k -> new HashMap<>()).put(this.credsList.size() - 1, System.currentTimeMillis());
131 logFine(logMessage.replace(0, logMessage.length() ,"Credentials metadata").append(credsName).append(" added to map."));
132 }
133 this.credsMap.get(credsName).put(2, System.currentTimeMillis());
134 logFine(logMessage.replace(0, logMessage.length() ,"Credentials ").append(credsName).append(" timestamp was updated in the list."));
135 return this.credsList.get(this.credsMap.get(credsName).keySet().iterator().next());
136 }

◆ getCredFileChars()

char[] dev.filechampion.filechampion4j.CredentialsManager.getCredFileChars ( Path  credFilePath) throws IOException
private

This method is used to read a single credential file into a char[] for storage in the Map. char[] grows dynamically as the file is read using 64 byte chunks.

Parameters
credFilePath(Path) - Path to the credentials file to read.
Returns
(char[]) - The credentials as a char[].
Exceptions
IOException- If there is an error reading the credentials file.

Definition at line 145 of file CredentialsManager.java.

145 {
146 logFine(logMessage.replace(0, logMessage.length() ,"Reading credentials from file: ").append(credFilePath.toString()));
147 try (BufferedReader br = Files.newBufferedReader(Paths.get(credFilePath.toString()), Charset.defaultCharset())) {
148 char[] tmpCharArray = new char[1];
149 int charRead = 0;
150 int position = 0;
151 while((charRead = br.read()) != -1) {
152 if(position == tmpCharArray.length) {
153 char[] temp = new char[tmpCharArray.length + 1];
154 System.arraycopy(tmpCharArray, 0, temp, 0, position);
155 tmpCharArray = temp;
156 }
157 tmpCharArray[position++] = (char) charRead;
158 }
159 logFine(logMessage.replace(0, logMessage.length() ,"Credentials read from file: ").append(credFilePath.toString()));
160 return tmpCharArray;
161 } catch (Exception e) {
162 logSevere(logMessage.replace(0, logMessage.length() ,"Error reading credentials file: ").append(e.getMessage()));
163 throw new IOException("Error reading credentials file: " + e.getMessage());
164 }
165 }

◆ logFine()

void dev.filechampion.filechampion4j.CredentialsManager.logFine ( StringBuilder  message)
private

LOGGER.fine wrapper

Parameters
message(StringBuilder) - message to log

Definition at line 197 of file CredentialsManager.java.

197 {
198 if (LOGGER.isLoggable(Level.FINE )) {
199 LOGGER.fine(message.toString());
200 }
201 }

◆ logInfo()

void dev.filechampion.filechampion4j.CredentialsManager.logInfo ( StringBuilder  message)
private

LOGGER.info wrapper

Parameters
message(String) - message to log

Definition at line 177 of file CredentialsManager.java.

177 {
178 if (LOGGER.isLoggable(Level.INFO)) {
179 LOGGER.info(message.toString());
180 }
181 }

Referenced by dev.filechampion.filechampion4j.CredentialsManager.CredentialsManager(), and dev.filechampion.filechampion4j.CredentialsManager.setExpirationTime().

Here is the caller graph for this function:

◆ logSevere()

void dev.filechampion.filechampion4j.CredentialsManager.logSevere ( StringBuilder  message)
private

LOGGER.severe wrapper

Parameters
message(String) - message to log

Definition at line 187 of file CredentialsManager.java.

187 {
188 if (LOGGER.isLoggable(Level.SEVERE)) {
189 LOGGER.severe(message.toString());
190 }
191 }

Referenced by dev.filechampion.filechampion4j.CredentialsManager.CredentialsManager(), and dev.filechampion.filechampion4j.CredentialsManager.setExpirationTime().

Here is the caller graph for this function:

◆ setExpirationTime()

void dev.filechampion.filechampion4j.CredentialsManager.setExpirationTime ( long  expirationTime) throws IllegalArgumentException

This method allows the user to set the expiration time for credentials in milliseconds, overriding the default of 300000 (5 minutes).

Parameters
expirationTime(long) - The expiration time in milliseconds.
Exceptions
IllegalArgumentException- If the expirationTime is less than or equal to 0.

Definition at line 87 of file CredentialsManager.java.

87 {
88 if (expirationTime <= 0) {
89 logSevere(logMessage.replace(0, logMessage.length() ,"Expiration time must be greater than 0."));
90 throw new IllegalArgumentException("Expiration time must be greater than 0.");
91 }
92 this.credsExpirationTime = expirationTime;
93 logInfo(logMessage.replace(0, logMessage.length() ,"Expiration time set to ").append(expirationTime).append(" milliseconds."));
94 // add timer to remove expired creds from the list
95 Timer timer = new Timer();
96 timer.schedule(new java.util.TimerTask() {
97 @Override
98 public void run() {
99 checkCredExpiration();
100 }
102 logInfo(logMessage.replace(0, logMessage.length() ,"Scheduled timer to check for expired credentials every ").append(expirationTime).append(" milliseconds."));
103 }

References dev.filechampion.filechampion4j.CredentialsManager.logInfo(), dev.filechampion.filechampion4j.CredentialsManager.logMessage, and dev.filechampion.filechampion4j.CredentialsManager.logSevere().

Here is the call graph for this function:

Member Data Documentation

◆ credsExpirationTime

long dev.filechampion.filechampion4j.CredentialsManager.credsExpirationTime = 300000
private

Definition at line 43 of file CredentialsManager.java.

◆ credsList

List<char[]> dev.filechampion.filechampion4j.CredentialsManager.credsList = new LinkedList<>()
private

Definition at line 46 of file CredentialsManager.java.

◆ credsMap

Map<String, Map<Integer, Long> > dev.filechampion.filechampion4j.CredentialsManager.credsMap = new HashMap<>()
private

Definition at line 47 of file CredentialsManager.java.

◆ credsNamesList

List<String> dev.filechampion.filechampion4j.CredentialsManager.credsNamesList
private

◆ credsPath

Path dev.filechampion.filechampion4j.CredentialsManager.credsPath
private

◆ expirationTimerStarted

boolean dev.filechampion.filechampion4j.CredentialsManager.expirationTimerStarted = false
private

Definition at line 51 of file CredentialsManager.java.

◆ LOGGER

final Logger dev.filechampion.filechampion4j.CredentialsManager.LOGGER = Logger.getLogger(CredentialsManager.class.getName())
staticprivate

Definition at line 41 of file CredentialsManager.java.

◆ logMessage

StringBuilder dev.filechampion.filechampion4j.CredentialsManager.logMessage = new StringBuilder()
private

◆ random

SecureRandom dev.filechampion.filechampion4j.CredentialsManager.random = new SecureRandom()
private

Definition at line 48 of file CredentialsManager.java.

◆ randomString

String dev.filechampion.filechampion4j.CredentialsManager.randomString = ""
private

Definition at line 49 of file CredentialsManager.java.

◆ randomStringIndex

int dev.filechampion.filechampion4j.CredentialsManager.randomStringIndex = 0
private

Definition at line 50 of file CredentialsManager.java.


The documentation for this class was generated from the following file: