diff --git a/.github/workflows/maven.yml b/.github/workflows/maven.yml index 619b7d0..2566386 100644 --- a/.github/workflows/maven.yml +++ b/.github/workflows/maven.yml @@ -5,9 +5,9 @@ name: Java CI with Maven on: push: - branches: [ master ] + branches: [ main ] pull_request: - branches: [ master ] + branches: [ main ] jobs: build: diff --git a/sample/src/main/java/com/microsoft/credentialstorage/sample/StoredCredentialApp.java b/sample/src/main/java/com/microsoft/credentialstorage/sample/StoredCredentialApp.java index 539b007..4611a05 100644 --- a/sample/src/main/java/com/microsoft/credentialstorage/sample/StoredCredentialApp.java +++ b/sample/src/main/java/com/microsoft/credentialstorage/sample/StoredCredentialApp.java @@ -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(); - } } } @@ -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); + } + } } } diff --git a/src/main/java/com/microsoft/credentialstorage/model/StoredSecret.java b/src/main/java/com/microsoft/credentialstorage/model/StoredSecret.java index 901af94..1af87e4 100644 --- a/src/main/java/com/microsoft/credentialstorage/model/StoredSecret.java +++ b/src/main/java/com/microsoft/credentialstorage/model/StoredSecret.java @@ -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(); + } }