summaryrefslogtreecommitdiffstats
path: root/dozentenmodul/src/main/java/org/openslx/dozmod/authentication/BrowserAuthenticator.java
diff options
context:
space:
mode:
Diffstat (limited to 'dozentenmodul/src/main/java/org/openslx/dozmod/authentication/BrowserAuthenticator.java')
-rw-r--r--dozentenmodul/src/main/java/org/openslx/dozmod/authentication/BrowserAuthenticator.java99
1 files changed, 99 insertions, 0 deletions
diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/authentication/BrowserAuthenticator.java b/dozentenmodul/src/main/java/org/openslx/dozmod/authentication/BrowserAuthenticator.java
new file mode 100644
index 00000000..f6587edf
--- /dev/null
+++ b/dozentenmodul/src/main/java/org/openslx/dozmod/authentication/BrowserAuthenticator.java
@@ -0,0 +1,99 @@
+package org.openslx.dozmod.authentication;
+
+import java.io.IOException;
+import java.net.MalformedURLException;
+import java.net.URI;
+import java.net.URISyntaxException;
+
+import org.apache.hc.client5.http.ClientProtocolException;
+import org.apache.hc.core5.http.ParseException;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+import org.apache.thrift.TException;
+import org.openslx.bwlp.thrift.iface.ClientSessionData;
+import org.openslx.bwlp.thrift.iface.TAuthorizationException;
+import org.openslx.bwlp.thrift.iface.TNotFoundException;
+import org.openslx.dozmod.authentication.ShibbolethEcp.ReturnCode;
+import org.openslx.dozmod.gui.control.QLabel;
+import org.openslx.dozmod.gui.helper.I18n;
+import org.openslx.dozmod.util.DesktopEnvironment;
+import org.openslx.thrifthelper.ThriftManager;
+import org.openslx.util.Util;
+
+import com.google.gson.JsonSyntaxException;
+
+/**
+ * Authenticator that relies on opening an external browser/website which should
+ * eventually lead to the provided token yielding a valid session.
+ */
+public class BrowserAuthenticator implements Authenticator {
+
+ /**
+ * Logger instance for this class
+ */
+ private final static Logger LOGGER = LogManager.getLogger(BrowserAuthenticator.class);
+
+ private static final String[] ANIMATION = { " -", " \\", " |", " /" };
+
+ private volatile boolean cancelled = false;
+
+ private final QLabel lblError;
+
+ public BrowserAuthenticator(QLabel lblError) {
+ this.lblError = lblError;
+ }
+
+ @Override
+ public void login(String authUrl, String accessToken, AuthenticatorCallback callback)
+ throws JsonSyntaxException, ClientProtocolException, ParseException,
+ MalformedURLException, URISyntaxException, IOException {
+ // try to login
+ DesktopEnvironment.openWebpageUri(new URI(authUrl));
+ Exception errEx = null;
+ ReturnCode ret = ReturnCode.GENERIC_ERROR;
+ AuthenticationData data = null;
+ String prefix = I18n.WINDOW.getString("Login.Label.error.continueBrowser");
+
+ // Try for ~10 minutes
+ for (int i = 1; i < 600 && !cancelled; ++i) {
+ Util.sleep(600 + i * 5);
+ lblError.setText(prefix + ANIMATION[i % ANIMATION.length]);
+ try {
+ ClientSessionData sess = ThriftManager.getMasterClient()
+ .getSessionFromAccessCode(accessToken);
+ if (sess != null) {
+ data = new AuthenticationData(sess.authToken, sess.sessionId, sess.satellites);
+ ret = ReturnCode.NO_ERROR;
+ break;
+ }
+
+ } catch (TNotFoundException nf) {
+ continue; // Keep waiting
+ } catch (TAuthorizationException ex) {
+ errEx = ex;
+ break;
+ } catch (TException e) {
+ LOGGER.warn("Cannot get login state for browser-based login", e);
+ }
+ }
+ if (cancelled)
+ return;
+ if (data == null && errEx == null) {
+ // Loop timed out
+ errEx = new TException("Timeout, try again");
+ }
+
+ callback.postLogin(ret, data, errEx);
+ }
+
+ @Override
+ public void cancel() {
+ cancelled = true;
+ }
+
+ @Override
+ protected void finalize() throws Throwable {
+ super.finalize();
+ cancel();
+ }
+}