Skip to content
This repository was archived by the owner on Mar 7, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/maven.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ name: Java CI with Maven

on:
push:
branches: [ master ]
branches: [ main ]
pull_request:
branches: [ master ]
branches: [ main ]

jobs:
build:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,40 +44,24 @@ private void run() {
private void registerUser() {
log.info("Registering a new user:");

final StoredCredential credential = enterCredentials();

try {
try (StoredCredential credential = enterCredentials()) {
// Save the credential to the store.
credentialStorage.add(CREDENTIALS_KEY, credential);
log.info("User registered.");
} finally {
// clear password value.
credential.clear();
}
}

private void userLogin() {
log.info("Authenticating a user");

final StoredCredential enteredCredential = enterCredentials();
StoredCredential storedCredential = null;

try {
// Save the credential to the store.
storedCredential = credentialStorage.get(CREDENTIALS_KEY);
try (StoredCredential enteredCredential = enterCredentials();
StoredCredential storedCredential = credentialStorage.get(CREDENTIALS_KEY)) {

if (storedCredential.equals(enteredCredential)) {
log.info("User logged in successfully.");
} else {
log.info("Authentication failed.");
}
} finally {
// clear password value
enteredCredential.clear();

if (storedCredential != null) {
storedCredential.clear();
}
}
}

Expand All @@ -94,13 +78,15 @@ private StoredCredential enterCredentials() {
// Request password from user.
// Using API which returns char[] to avoid creating String
// to minimize memory footprint for secure purposes.
final char[] password = System.console().readPassword("Enter password: ");

final StoredCredential credential = new StoredCredential(userName, password);

// Password value is not needed anymore, clear it now without waiting GC to remove it.
Arrays.fill(password, (char) 0x00);

return credential;
char[] password = null;
try {
password = System.console().readPassword("Enter password: ");
return new StoredCredential(userName, password);
} finally {
// Password value is not needed anymore, clear it now without waiting GC to remove it.
if (password != null) {
Arrays.fill(password, (char) 0x00);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,17 @@
/**
* An interface representing a secret.
*/
public interface StoredSecret {
public interface StoredSecret extends AutoCloseable {
/**
* Clear the secret value.
*/
void clear();

/**
* On close clear the value.
*/
@Override
default void close() {
clear();
}
}