forked from oktadev/okta-aws-cli-assume-role
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BrowserAuthentication.java
162 lines (138 loc) · 6.31 KB
/
BrowserAuthentication.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package com.okta.tools.authentication;
import com.okta.tools.OktaAwsCliEnvironment;
import com.okta.tools.helpers.CookieHelper;
import com.okta.tools.io.SubresourceIntegrityStrippingHack;
import com.okta.tools.util.NodeListIterable;
import com.sun.javafx.webkit.WebConsoleListener;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.ScrollPane;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import javax.annotation.Nonnull;
import java.io.IOException;
import java.net.URI;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicReference;
public final class BrowserAuthentication extends Application {
// Trade-off: JavaFX app model makes interacting with UI state challenging
// Experienced JavaFX devs welcomed to suggest solutions to this
private static final CountDownLatch USER_AUTH_COMPLETE = new CountDownLatch(1);
// Trade-off: JavaFX app model makes passing parameters to UI challenging
// Experienced JavaFX devs welcomed to suggest solutions to this
private static OktaAwsCliEnvironment ENVIRONMENT;
private static CookieHelper cookieHelper;
// The value of samlResponse is only valid if USER_AUTH_COMPLETE has counted down to zero
private static final AtomicReference<String> samlResponse = new AtomicReference<>();
public static String login(OktaAwsCliEnvironment environment) throws InterruptedException {
ENVIRONMENT = environment;
cookieHelper = new CookieHelper(ENVIRONMENT);
launch();
USER_AUTH_COMPLETE.await();
return samlResponse.get();
}
@Override
public void start(final Stage stage) throws IOException {
stage.setWidth(802);
stage.setHeight(650);
stage.setOnCloseRequest(event -> System.exit(1));
Scene scene = new Scene(new Group());
final WebView browser = new WebView();
final WebEngine webEngine = browser.getEngine();
ScrollPane scrollPane = new ScrollPane();
scrollPane.setContent(browser);
URI uri = URI.create(ENVIRONMENT.oktaAwsAppUrl);
initializeCookies(uri);
SubresourceIntegrityStrippingHack.overrideHttpsProtocolHandler(ENVIRONMENT);
webEngine.getLoadWorker().stateProperty()
.addListener((ov, oldState, newState) -> {
if (webEngine.getDocument() != null) {
checkForAwsSamlSignon(stage, webEngine);
stage.setTitle(webEngine.getLocation());
}
});
webEngine.getLoadWorker().exceptionProperty()
.addListener((ov, oldState, newState) -> {
System.out.format("exception(%s => %s)\n%s\n", oldState, newState, webEngine.getLoadWorker().getException());
});
WebConsoleListener.setDefaultListener((webView, message, lineNumber, sourceId) -> {
System.out.println("WebConsoleListener: " + message + "[" + webEngine.getLocation() + ":" + lineNumber + "]");
});
webEngine.load(uri.toASCIIString());
scene.setRoot(scrollPane);
stage.setScene(scene);
stage.show();
}
private void initializeCookies(URI uri) throws IOException {
Map<String, List<String>> headers = cookieHelper.loadCookieHeaders();
java.net.CookieHandler.setDefault(new CookieManager(cookieHelper));
java.net.CookieHandler.getDefault().put(uri, headers);
}
private void checkForAwsSamlSignon(Stage stage, WebEngine webEngine) {
String samlResponseForAws = getSamlResponseForAws(webEngine.getDocument());
if (samlResponseForAws != null) {
finishAuthentication(stage, samlResponseForAws);
}
}
private String getSamlResponseForAws(Document document) {
Node awsStsSamlForm = getAwsStsSamlForm(document);
if (awsStsSamlForm == null) return null;
return getSamlResponseFromForm(awsStsSamlForm);
}
private Node getAwsStsSamlForm(Document document) {
NodeList formNodes = document.getElementsByTagName("form");
for (Node form : new NodeListIterable(formNodes)) {
NamedNodeMap formAttributes = form.getAttributes();
if (formAttributes == null) continue;
Node formActionAttribute = formAttributes.getNamedItem("action");
if (formActionAttribute == null) continue;
String formAction = formActionAttribute.getTextContent();
if ("https://signin.aws.amazon.com/saml".equals(formAction)) {
return form;
}
}
return null;
}
private String getSamlResponseFromForm(@Nonnull Node awsStsSamlForm) {
Node samlResponseInput = getSamlResponseInput(awsStsSamlForm);
if (samlResponseInput == null)
throw new IllegalStateException("Request to AWS STS SAML endpoint missing SAMLResponse");
NamedNodeMap attributes = samlResponseInput.getAttributes();
Node value = attributes.getNamedItem("value");
return value.getTextContent();
}
private Node getSamlResponseInput(@Nonnull Node parent) {
for (Node child : new NodeListIterable(parent.getChildNodes())) {
if (isSamlResponseInput(child)) {
return child;
} else {
Node samlResponseInput = getSamlResponseInput(child);
if (samlResponseInput != null) return samlResponseInput;
}
}
return null;
}
private boolean isSamlResponseInput(@Nonnull Node child) {
boolean isInput = "input".equals(child.getLocalName());
if (!isInput) return false;
NamedNodeMap attributes = child.getAttributes();
if (attributes == null) return false;
Node nameAttribute = attributes.getNamedItem("name");
if (nameAttribute == null) return false;
String name = nameAttribute.getTextContent();
return "SAMLResponse".equals(name);
}
private void finishAuthentication(Stage stage, String samlResponseForAws) {
samlResponse.set(samlResponseForAws);
stage.close();
USER_AUTH_COMPLETE.countDown();
}
}