Skip to content

Commit d87a8c9

Browse files
committed
Add implementation using login callback.
1 parent 4662486 commit d87a8c9

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* Copyright (c) 2025 shift7 GmbH. All rights reserved.
3+
*/
4+
5+
package cloud.katta.core;
6+
7+
import ch.cyberduck.core.Credentials;
8+
import ch.cyberduck.core.Host;
9+
import ch.cyberduck.core.LocaleFactory;
10+
import ch.cyberduck.core.LoginCallback;
11+
import ch.cyberduck.core.LoginOptions;
12+
import ch.cyberduck.core.StringAppender;
13+
import ch.cyberduck.core.exception.LoginCanceledException;
14+
15+
import cloud.katta.model.AccountKeyAndDeviceName;
16+
import cloud.katta.workflows.exceptions.AccessException;
17+
18+
public class DefaultDeviceSetupCallback implements DeviceSetupCallback {
19+
20+
private final LoginCallback prompt;
21+
22+
public DefaultDeviceSetupCallback(final LoginCallback prompt) {
23+
this.prompt = prompt;
24+
}
25+
26+
@Override
27+
public AccountKeyAndDeviceName displayAccountKeyAndAskDeviceName(final Host bookmark, final AccountKeyAndDeviceName accountKeyAndDeviceName) throws AccessException {
28+
try {
29+
final Credentials input = prompt.prompt(bookmark, accountKeyAndDeviceName.accountKey(),
30+
LocaleFactory.localizedString("Account Key", "Hub"),
31+
new StringAppender()
32+
.append(LocaleFactory.localizedString("On first login, every user gets a unique Account Key", "Hub"))
33+
.append(LocaleFactory.localizedString("Your Account Key is required to login from other apps or browsers", "Hub"))
34+
.append(LocaleFactory.localizedString("You can see a list of authorized apps on your profile page", "Hub")).toString(),
35+
new LoginOptions()
36+
.usernamePlaceholder(LocaleFactory.localizedString("Account Key", "Hub"))
37+
// Account key not editable
38+
.user(false)
39+
.passwordPlaceholder(accountKeyAndDeviceName.deviceName())
40+
// Input device name
41+
.password(true)
42+
.keychain(true)
43+
);
44+
return new AccountKeyAndDeviceName()
45+
.withAddToKeychain(input.isSaved())
46+
.withDeviceName(input.getUsername())
47+
.withAccountKey(input.getPassword());
48+
}
49+
catch(LoginCanceledException e) {
50+
throw new AccessException(e);
51+
}
52+
}
53+
54+
@Override
55+
public AccountKeyAndDeviceName askForAccountKeyAndDeviceName(final Host bookmark, final String initialDeviceName) throws AccessException {
56+
try {
57+
final Credentials input = prompt.prompt(bookmark, initialDeviceName,
58+
LocaleFactory.localizedString("Authorization Required", "Hub"),
59+
new StringAppender()
60+
.append(LocaleFactory.localizedString("This is your first login on this device.", "Hub"))
61+
.append(LocaleFactory.localizedString("Your Account Key is required to link this browser to your account.", "Hub")).toString(),
62+
new LoginOptions()
63+
.usernamePlaceholder(LocaleFactory.localizedString("Device Name", "Hub"))
64+
// Customize device name
65+
.user(true)
66+
.passwordPlaceholder(LocaleFactory.localizedString("Account Key", "Hub"))
67+
// Input account key
68+
.password(true)
69+
.keychain(true)
70+
);
71+
return new AccountKeyAndDeviceName()
72+
.withAddToKeychain(input.isSaved())
73+
.withDeviceName(input.getUsername())
74+
.withAccountKey(input.getPassword());
75+
}
76+
catch(LoginCanceledException e) {
77+
throw new AccessException(e);
78+
}
79+
}
80+
}

0 commit comments

Comments
 (0)