Skip to content

Commit e6cbdfa

Browse files
author
Henri Manson
committed
use realm from sasl, login dialog called before Sasl.createSaslClient
1 parent a65eeeb commit e6cbdfa

2 files changed

Lines changed: 31 additions & 15 deletions

File tree

app/sasl_win.bat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
@echo off
22

3-
goto browser-client
3+
REM goto browser-client
44

55
:java-client
66
echo starting SaslJavaClient > plugin.log

app/src/main/java/nl/mansoft/sasl/Client.java

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
*/
66
package nl.mansoft.sasl;
77

8-
import java.io.BufferedWriter;
9-
import java.io.FileWriter;
108
import java.io.IOException;
119
import java.io.PrintWriter;
1210
import java.util.Base64;
@@ -47,19 +45,13 @@
4745

4846
public class Client extends Application {
4947
private Dialog<Pair<String, String>> dialog;
48+
private String authorizationId;
5049
private String password;
5150

5251
public void processCallbacks(Callback[] callbacks) {
5352
for (Callback callback : callbacks) {
5453
if (callback instanceof NameCallback) {
5554
NameCallback nameCallback = (NameCallback) callback;
56-
Optional<Pair<String, String>> result = dialog.showAndWait();
57-
result.ifPresent(usernamePassword -> {
58-
//System.out.println("Username=" + usernamePassword.getKey() + ", Password=" + usernamePassword.getValue());
59-
nameCallback.setName(usernamePassword.getKey());
60-
password = usernamePassword.getValue();
61-
});
62-
6355
nameCallback.setName(nameCallback.getDefaultName());
6456
} else if (callback instanceof PasswordCallback) {
6557
PasswordCallback passwordCallback = (PasswordCallback) callback;
@@ -76,6 +68,11 @@ public static String jsonGetString(JsonObject jsonObject, String name) {
7668
return jsonString == null ? null : jsonString.getString();
7769
}
7870

71+
public static JsonObject jsonGetObject(JsonObject jsonObject, String name) {
72+
JsonObject jsonObjectOut = jsonObject.getJsonObject(name);
73+
return jsonObjectOut;
74+
}
75+
7976
public static boolean addString(JsonObjectBuilder builder, JsonObject jsonObject, String name) {
8077
String value = jsonGetString(jsonObject, name);
8178
boolean result = value != null;
@@ -85,6 +82,15 @@ public static boolean addString(JsonObjectBuilder builder, JsonObject jsonObject
8582
return result;
8683
}
8784

85+
public static boolean addObject(JsonObjectBuilder builder, JsonObject jsonObject, String name) {
86+
JsonObject value = jsonGetObject(jsonObject, name);
87+
boolean result = value != null;
88+
if (result) {
89+
builder.add(name, value);
90+
}
91+
return result;
92+
}
93+
8894
public static void printArgs(PrintWriter logwriter, String[] args) {
8995
if (args.length == 0) {
9096
System.err.println("No arguments");
@@ -103,7 +109,6 @@ private void createDialog() {
103109
// Create the custom dialog.
104110
dialog = new Dialog<>();
105111
dialog.setTitle("Login Dialog");
106-
dialog.setHeaderText("Look, a Custom Login Dialog");
107112

108113
// Set the icon (must be included in the project).
109114
//dialog.setGraphic(new ImageView(this.getClass().getResource("login.png").toString()));
@@ -154,22 +159,31 @@ private void createDialog() {
154159
public void start(Stage primaryStage) {
155160
try {
156161
System.err.println("STARTING");
157-
//printArgs(logwriter, args);
158162
createDialog();
159-
160163
JsonObject inputJson;
161164
String mechanism = "DIGEST-MD5";
162165
SaslClient sc = null;
163166
NativeMessaging nativeMessaging = new NativeMessaging();
164167
while ((inputJson = nativeMessaging.readMessage()) != null) {
165168
System.err.println(inputJson);
169+
String realm = jsonGetString(inputJson, "realm");
166170
if (!inputJson.containsKey("s2s")) {
167171
System.err.println("no realm, creating SASL client");
168172
if (sc != null) {
169173
System.err.println("disposing previous SASL client");
170174
sc.dispose();
171175
}
172-
sc = Sasl.createSaslClient(new String[] { mechanism }, "henri", "http", "test-realm.nl", null, new CallbackHandler() {
176+
177+
dialog.setHeaderText(realm);
178+
179+
Optional<Pair<String, String>> result = dialog.showAndWait();
180+
result.ifPresent(usernamePassword -> {
181+
System.err.println("Username=" + usernamePassword.getKey() + ", Password=" + usernamePassword.getValue());
182+
authorizationId = usernamePassword.getKey();
183+
password = usernamePassword.getValue();
184+
});
185+
186+
sc = Sasl.createSaslClient(new String[] { mechanism }, authorizationId, "http", realm, null, new CallbackHandler() {
173187
@Override
174188
public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
175189
processCallbacks(callbacks);
@@ -179,7 +193,9 @@ public void handle(Callback[] callbacks) throws IOException, UnsupportedCallback
179193
JsonBuilderFactory factory = Json.createBuilderFactory(null);
180194
JsonObjectBuilder outputJsonBuilder = factory.createObjectBuilder()
181195
.add("mech", "DIGEST-MD5");
182-
addString(outputJsonBuilder, inputJson, "realm");
196+
outputJsonBuilder.add("realm", realm);
197+
addString(outputJsonBuilder, inputJson, "requestId");
198+
addObject(outputJsonBuilder, inputJson, "extraInfoSpec");
183199
addString(outputJsonBuilder, inputJson, "s2s");
184200
String s2cBase64 = jsonGetString(inputJson, "s2c");
185201
if (s2cBase64 != null) {

0 commit comments

Comments
 (0)