/
StoredCredentialApp.java
106 lines (79 loc) · 3.23 KB
/
StoredCredentialApp.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See License.txt in the project root.
package com.microsoft.credentialstorage.sample;
import com.microsoft.credentialstorage.model.StoredCredential;
import com.microsoft.credentialstorage.SecretStore;
import com.microsoft.credentialstorage.StorageProvider;
import com.microsoft.credentialstorage.StorageProvider.SecureOption;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Arrays;
public class StoredCredentialApp {
private static final Logger log = LoggerFactory.getLogger(StoredCredentialApp.class);
private static final String CREDENTIALS_KEY = "TestCredentials";
private SecretStore<StoredCredential> credentialStorage;
public static void main(final String[] args) {
final StoredCredentialApp app = new StoredCredentialApp();
app.run();
}
private void run() {
// Get a secure store instance.
credentialStorage = StorageProvider.getCredentialStorage(true, SecureOption.REQUIRED);
if (credentialStorage == null) {
log.error("No secure credential storage available.");
return;
}
registerUser();
userLogin();
unregisterUser();
}
private void registerUser() {
log.info("Registering a new user:");
final StoredCredential credential = enterCredentials();
try {
// 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);
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();
}
}
}
private void unregisterUser() {
// Remove credentials from the store.
credentialStorage.delete(CREDENTIALS_KEY);
log.info("User deleted.");
}
private StoredCredential enterCredentials() {
// Request user name from user.
final String userName = System.console().readLine("Enter user name: ");
// 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;
}
}