Skip to content

Commit 4662486

Browse files
committed
Save account key in keychain.
1 parent b127c33 commit 4662486

File tree

5 files changed

+35
-16
lines changed

5 files changed

+35
-16
lines changed

hub/src/main/java/cloud/katta/core/DeviceSetupCallback.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ public interface DeviceSetupCallback {
1717
/**
1818
* Prompt user for device name
1919
*
20-
* @return Device name
20+
* @return Account key and device name
2121
* @throws AccessException Canceled prompt by user
2222
*/
23-
String displayAccountKeyAndAskDeviceName(Host bookmark, AccountKeyAndDeviceName accountKeyAndDeviceName) throws AccessException;
23+
AccountKeyAndDeviceName displayAccountKeyAndAskDeviceName(Host bookmark, AccountKeyAndDeviceName accountKeyAndDeviceName) throws AccessException;
2424

2525
/**
2626
* Prompt user for existing account key
@@ -50,7 +50,7 @@ default UserKeys generateUserKeys() {
5050

5151
DeviceSetupCallback disabled = new DeviceSetupCallback() {
5252
@Override
53-
public String displayAccountKeyAndAskDeviceName(final Host bookmark, final AccountKeyAndDeviceName accountKeyAndDeviceName) throws AccessException {
53+
public AccountKeyAndDeviceName displayAccountKeyAndAskDeviceName(final Host bookmark, final AccountKeyAndDeviceName accountKeyAndDeviceName) throws AccessException {
5454
throw new AccessException("Disabled");
5555
}
5656

hub/src/main/java/cloud/katta/workflows/UserKeysServiceImpl.java

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
package cloud.katta.workflows;
66

77
import ch.cyberduck.core.Host;
8+
import ch.cyberduck.core.PasswordStoreFactory;
9+
import ch.cyberduck.core.exception.LocalAccessDeniedException;
810

911
import org.apache.logging.log4j.LogManager;
1012
import org.apache.logging.log4j.Logger;
@@ -77,7 +79,11 @@ public UserKeys getOrCreateUserKeys(final Host hub, final UserDto me, final Devi
7779
case 404:
7880
log.warn("Device keys from keychain not present in hub. Setting up existing device w/ Account Key for existing user keys.");
7981
// Setup existing device w/ Account Key (e.g. same device for multiple hubs)
80-
return this.recover(me, deviceKeyPair, prompt.askForAccountKeyAndDeviceName(hub, COMPUTER_NAME));
82+
final AccountKeyAndDeviceName input = prompt.askForAccountKeyAndDeviceName(hub, COMPUTER_NAME);
83+
if(input.addToKeychain()) {
84+
this.save(hub, me, input.accountKey());
85+
}
86+
return this.recover(me, deviceKeyPair, input);
8187
default:
8288
throw e;
8389
}
@@ -94,14 +100,25 @@ else if(validate(me)) {
94100
// TODO https://github.com/shift7-ch/katta-server/issues/27
95101
// private key generated with P384KeyPair causes "Unexpected Error: Data provided to an operation does not meet requirements" in `UserKeys.recover`: `const privateKey = await crypto.subtle.importKey('pkcs8', decodedPrivateKey, UserKeys.KEY_DESIGNATION, false, UserKeys.KEY_USAGES);`
96102
final String accountKey = prompt.generateAccountKey();
97-
final String deviceName = prompt.displayAccountKeyAndAskDeviceName(hub,
103+
final AccountKeyAndDeviceName input = prompt.displayAccountKeyAndAskDeviceName(hub,
98104
new AccountKeyAndDeviceName().withAccountKey(accountKey).withDeviceName(COMPUTER_NAME));
99-
100-
return this.uploadDeviceKeys(deviceName,
105+
if(input.addToKeychain()) {
106+
this.save(hub, me, accountKey);
107+
}
108+
return this.uploadDeviceKeys(input.deviceName(),
101109
this.uploadUserKeys(me, prompt.generateUserKeys(), accountKey), deviceKeyPair);
102110
}
103111
}
104112

113+
private void save(final Host hub, final UserDto me, final String accountKey) {
114+
try {
115+
PasswordStoreFactory.get().addPassword(hub.getNickname(), me.getEmail(), accountKey);
116+
}
117+
catch(LocalAccessDeniedException ex) {
118+
log.warn("Failure saving account key", ex);
119+
}
120+
}
121+
105122
private UserKeys recover(final UserDto me, final DeviceKeys deviceKeyPair, final AccountKeyAndDeviceName accountKeyAndDeviceName) throws
106123
ApiException, SecurityFailure {
107124
try {

hub/src/test/java/cloud/katta/core/util/MockableDeviceSetupCallback.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public static void setProxy(final DeviceSetupCallback proxy) {
1818
private static DeviceSetupCallback proxy = null;
1919

2020
@Override
21-
public String displayAccountKeyAndAskDeviceName(final Host bookmark, final AccountKeyAndDeviceName accountKeyAndDeviceName) throws AccessException {
21+
public AccountKeyAndDeviceName displayAccountKeyAndAskDeviceName(final Host bookmark, final AccountKeyAndDeviceName accountKeyAndDeviceName) throws AccessException {
2222
return proxy.displayAccountKeyAndAskDeviceName(bookmark, accountKeyAndDeviceName);
2323
}
2424

hub/src/test/java/cloud/katta/testsetup/AbstractHubTest.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -204,24 +204,26 @@ protected static HubSession setupConnection(final HubTestConfig.Setup setup) thr
204204
}
205205

206206
protected static @NotNull DeviceSetupCallback deviceSetupCallback(HubTestConfig.Setup setup) {
207-
final DeviceSetupCallback proxy = new DeviceSetupCallback() {
207+
return new DeviceSetupCallback() {
208208
@Override
209-
public String displayAccountKeyAndAskDeviceName(final Host bookmark, final AccountKeyAndDeviceName accountKeyAndDeviceName) {
210-
return "firstLoginMockSetup";
209+
public AccountKeyAndDeviceName displayAccountKeyAndAskDeviceName(final Host bookmark, final AccountKeyAndDeviceName accountKeyAndDeviceName) {
210+
return new AccountKeyAndDeviceName().withAccountKey(setup.userConfig.setupCode).withDeviceName(
211+
String.format("%s %s", accountKeyAndDeviceName.deviceName(), DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL)
212+
.format(ZonedDateTime.now(ZoneId.of("Europe/Zurich")))));
211213
}
212214

213215
@Override
214216
public AccountKeyAndDeviceName askForAccountKeyAndDeviceName(final Host bookmark, final String initialDeviceName) {
215-
return new AccountKeyAndDeviceName().withAccountKey(setup.userConfig.setupCode).withDeviceName(String.format("firstLoginMockSetup %s", DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL)
216-
.format(ZonedDateTime.now(ZoneId.of("Europe/Zurich")))));
217+
return new AccountKeyAndDeviceName().withAccountKey(setup.userConfig.setupCode).withDeviceName(
218+
String.format("%s %s", initialDeviceName, DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL)
219+
.format(ZonedDateTime.now(ZoneId.of("Europe/Zurich")))));
217220
}
218221

219222
@Override
220223
public String generateAccountKey() {
221224
return staticSetupCode();
222225
}
223226
};
224-
return proxy;
225227
}
226228
}
227229

osx/src/main/java/cloud/katta/controller/PromptDeviceSetupCallback.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public PromptDeviceSetupCallback(final ProxyController controller) {
2727
}
2828

2929
@Override
30-
public String displayAccountKeyAndAskDeviceName(final Host bookmark, final AccountKeyAndDeviceName accountKeyAndDeviceName) throws AccessException {
30+
public AccountKeyAndDeviceName displayAccountKeyAndAskDeviceName(final Host bookmark, final AccountKeyAndDeviceName accountKeyAndDeviceName) throws AccessException {
3131
if(log.isDebugEnabled()) {
3232
log.debug(String.format("Display Account Key for %s", bookmark));
3333
}
@@ -37,7 +37,7 @@ public String displayAccountKeyAndAskDeviceName(final Host bookmark, final Accou
3737
case SheetCallback.ALTERNATE_OPTION:
3838
throw new AccessException(new ConnectionCanceledException());
3939
}
40-
return accountKeyAndDeviceName.deviceName();
40+
return accountKeyAndDeviceName;
4141
}
4242

4343
@Override

0 commit comments

Comments
 (0)