com.microsoft.aad.msal4j.AuthorizationResponseHandler Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of com.liferay.mail.outlook.auth.connector.provider
Show all versions of com.liferay.mail.outlook.auth.connector.provider
Liferay Mail Outlook Auth Connector Provider
// Generated by delombok at Mon Apr 17 18:26:07 UTC 2023
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package com.microsoft.aad.msal4j;
import com.sun.net.httpserver.Headers;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.concurrent.BlockingQueue;
import java.util.stream.Collectors;
class AuthorizationResponseHandler implements HttpHandler {
private static final Logger LOG = LoggerFactory.getLogger(AuthorizationResponseHandler.class);
private static final String DEFAULT_SUCCESS_MESSAGE = "Authentication Complete Authentication complete. You can close the browser and return to the application. ";
private static final String DEFAULT_FAILURE_MESSAGE = "Authentication Failed Authentication failed. You can return to the application. Feel free to close this browser tab. Error details: error {0} error_description: {1} ";
private BlockingQueue authorizationResultQueue;
private SystemBrowserOptions systemBrowserOptions;
AuthorizationResponseHandler(BlockingQueue authorizationResultQueue, SystemBrowserOptions systemBrowserOptions) {
this.authorizationResultQueue = authorizationResultQueue;
this.systemBrowserOptions = systemBrowserOptions;
}
@Override
public void handle(HttpExchange httpExchange) throws IOException {
try {
if (!httpExchange.getRequestURI().getPath().equalsIgnoreCase("/")) {
httpExchange.sendResponseHeaders(200, 0);
return;
}
String responseBody = new BufferedReader(new InputStreamReader(httpExchange.getRequestBody())).lines().collect(Collectors.joining("\n"));
AuthorizationResult result = AuthorizationResult.fromResponseBody(responseBody);
sendResponse(httpExchange, result);
authorizationResultQueue.put(result);
} catch (InterruptedException ex) {
LOG.error("Error reading response from socket: " + ex.getMessage());
throw new MsalClientException(ex);
} finally {
httpExchange.close();
}
}
private void sendResponse(HttpExchange httpExchange, AuthorizationResult result) throws IOException {
switch (result.status()) {
case Success:
sendSuccessResponse(httpExchange, getSuccessfulResponseMessage());
break;
case ProtocolError:
case UnknownError:
sendErrorResponse(httpExchange, getErrorResponseMessage());
break;
}
}
private void sendSuccessResponse(HttpExchange httpExchange, String response) throws IOException {
if (systemBrowserOptions == null || systemBrowserOptions.browserRedirectSuccess() == null) {
send200Response(httpExchange, response);
} else {
send302Response(httpExchange, systemBrowserOptions().browserRedirectSuccess().toString());
}
}
private void sendErrorResponse(HttpExchange httpExchange, String response) throws IOException {
if (systemBrowserOptions == null || systemBrowserOptions.browserRedirectError() == null) {
send200Response(httpExchange, response);
} else {
send302Response(httpExchange, systemBrowserOptions().browserRedirectError().toString());
}
}
private void send302Response(HttpExchange httpExchange, String redirectUri) throws IOException {
Headers responseHeaders = httpExchange.getResponseHeaders();
responseHeaders.set("Location", redirectUri);
httpExchange.sendResponseHeaders(302, 0);
}
private void send200Response(HttpExchange httpExchange, String response) throws IOException {
byte[] responseBytes = response.getBytes("UTF-8");
httpExchange.getResponseHeaders().set("Content-Type", "text/html; charset=UTF-8");
httpExchange.sendResponseHeaders(200, responseBytes.length);
OutputStream os = httpExchange.getResponseBody();
os.write(responseBytes);
os.close();
}
private String getSuccessfulResponseMessage() {
if (systemBrowserOptions == null || systemBrowserOptions.htmlMessageSuccess() == null) {
return DEFAULT_SUCCESS_MESSAGE;
}
return systemBrowserOptions().htmlMessageSuccess();
}
private String getErrorResponseMessage() {
if (systemBrowserOptions == null || systemBrowserOptions.htmlMessageError() == null) {
return DEFAULT_FAILURE_MESSAGE;
}
return systemBrowserOptions().htmlMessageError();
}
@java.lang.SuppressWarnings("all")
public BlockingQueue authorizationResultQueue() {
return this.authorizationResultQueue;
}
@java.lang.SuppressWarnings("all")
public SystemBrowserOptions systemBrowserOptions() {
return this.systemBrowserOptions;
}
}