diff options
author | Daniel <djr2468@gmail.com> | 2023-04-05 21:45:37 +0100 |
---|---|---|
committer | Daniel <djr2468@gmail.com> | 2023-04-05 21:45:37 +0100 |
commit | a710ddd5ad336cd45d3a0e69f13985db840763dc (patch) | |
tree | 4420bfcc613afbdd578735ff44858d910c4343b3 | |
parent | 1ecd7efca41344f29f8caa42fc6caaed8c7294a1 (diff) | |
download | trackermap-server-a710ddd5ad336cd45d3a0e69f13985db840763dc.tar.gz trackermap-server-a710ddd5ad336cd45d3a0e69f13985db840763dc.tar.bz2 trackermap-server-a710ddd5ad336cd45d3a0e69f13985db840763dc.zip |
Review changes 2
-rw-r--r-- | src/main/java/org/traccar/MainModule.java | 12 | ||||
-rw-r--r-- | src/main/java/org/traccar/database/OpenIdProvider.java | 67 |
2 files changed, 35 insertions, 44 deletions
diff --git a/src/main/java/org/traccar/MainModule.java b/src/main/java/org/traccar/MainModule.java index 220798767..4db6e0e32 100644 --- a/src/main/java/org/traccar/MainModule.java +++ b/src/main/java/org/traccar/MainModule.java @@ -96,6 +96,7 @@ import javax.ws.rs.client.Client; import javax.ws.rs.client.ClientBuilder; import java.io.IOException; import java.net.InetAddress; +import java.net.URISyntaxException; import java.net.UnknownHostException; import java.net.http.HttpClient; import java.util.Properties; @@ -176,11 +177,12 @@ public class MainModule extends AbstractModule { @Singleton @Provides public static OpenIdProvider provideOpenIDProvider( - Config config, LoginService loginService, ObjectMapper objectMapper) throws InterruptedException, IOException { - if (config.hasKey(Keys.OPENID_CLIENT_ID)) { - return new OpenIdProvider(config, loginService, HttpClient.newHttpClient(), objectMapper); - } - return null; + Config config, LoginService loginService, ObjectMapper objectMapper + ) throws InterruptedException, IOException, URISyntaxException { + if (config.hasKey(Keys.OPENID_CLIENT_ID)) { + return new OpenIdProvider(config, loginService, HttpClient.newHttpClient(), objectMapper); + } + return null; } @Provides diff --git a/src/main/java/org/traccar/database/OpenIdProvider.java b/src/main/java/org/traccar/database/OpenIdProvider.java index 8b93feea7..941d0e587 100644 --- a/src/main/java/org/traccar/database/OpenIdProvider.java +++ b/src/main/java/org/traccar/database/OpenIdProvider.java @@ -38,8 +38,6 @@ import javax.servlet.http.HttpServletRequest; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; import com.google.inject.Inject; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import com.nimbusds.oauth2.sdk.http.HTTPResponse; import com.nimbusds.oauth2.sdk.AuthorizationCode; @@ -62,12 +60,9 @@ import com.nimbusds.openid.connect.sdk.OIDCTokenResponseParser; import com.nimbusds.openid.connect.sdk.UserInfoResponse; import com.nimbusds.openid.connect.sdk.UserInfoRequest; import com.nimbusds.openid.connect.sdk.AuthenticationRequest; - import com.nimbusds.openid.connect.sdk.claims.UserInfo; public class OpenIdProvider { - private static final Logger LOGGER = LoggerFactory.getLogger(OpenIdProvider.class); - private final Boolean force; private final ClientID clientId; private final ClientAuthentication clientAuth; @@ -84,40 +79,34 @@ public class OpenIdProvider { @Inject public OpenIdProvider( Config config, LoginService loginService, HttpClient httpClient, ObjectMapper objectMapper - ) throws InterruptedException, IOException { - this.loginService = loginService; - - force = config.getBoolean(Keys.OPENID_FORCE); - clientId = new ClientID(config.getString(Keys.OPENID_CLIENT_ID)); - clientAuth = new ClientSecretBasic(clientId, new Secret(config.getString(Keys.OPENID_CLIENT_SECRET))); - - try { - callbackUrl = new URI(config.getString(Keys.WEB_URL, "") + "/api/session/openid/callback"); - baseUrl = new URI(config.getString(Keys.WEB_URL, "")); - - if (config.hasKey(Keys.OPENID_ISSUER_URL)) { - HttpRequest httpRequest = HttpRequest.newBuilder( - URI.create(config.getString(Keys.OPENID_ISSUER_URL) + "/.well-known/openid-configuration")) - .header("Accept", "application/json") - .build(); - - String httpResponse = httpClient.send(httpRequest, BodyHandlers.ofString()).body(); - - Map<String, Object> discoveryMap = objectMapper.readValue( - httpResponse, new TypeReference<Map<String, Object>>() { }); - - authUrl = new URI((String) discoveryMap.get("authorization_endpoint")); - tokenUrl = new URI((String) discoveryMap.get("token_endpoint")); - userInfoUrl = new URI((String) discoveryMap.get("userinfo_endpoint")); - - LOGGER.info("OpenID Connect auto discovery successful"); - } else { - authUrl = new URI(config.getString(Keys.OPENID_AUTH_URL)); - tokenUrl = new URI(config.getString(Keys.OPENID_TOKEN_URL)); - userInfoUrl = new URI(config.getString(Keys.OPENID_USERINFO_URL)); - } - } catch (URISyntaxException error) { - LOGGER.error("Invalid URIs provided in OpenID configuration"); + ) throws InterruptedException, IOException, URISyntaxException { + this.loginService = loginService; + + force = config.getBoolean(Keys.OPENID_FORCE); + clientId = new ClientID(config.getString(Keys.OPENID_CLIENT_ID)); + clientAuth = new ClientSecretBasic(clientId, new Secret(config.getString(Keys.OPENID_CLIENT_SECRET))); + + callbackUrl = new URI(config.getString(Keys.WEB_URL, "") + "/api/session/openid/callback"); + baseUrl = new URI(config.getString(Keys.WEB_URL, "")); + + if (config.hasKey(Keys.OPENID_ISSUER_URL)) { + HttpRequest httpRequest = HttpRequest.newBuilder( + URI.create(config.getString(Keys.OPENID_ISSUER_URL) + "/.well-known/openid-configuration")) + .header("Accept", "application/json") + .build(); + + String httpResponse = httpClient.send(httpRequest, BodyHandlers.ofString()).body(); + + Map<String, Object> discoveryMap = objectMapper.readValue( + httpResponse, new TypeReference<Map<String, Object>>() { }); + + authUrl = new URI((String) discoveryMap.get("authorization_endpoint")); + tokenUrl = new URI((String) discoveryMap.get("token_endpoint")); + userInfoUrl = new URI((String) discoveryMap.get("userinfo_endpoint")); + } else { + authUrl = new URI(config.getString(Keys.OPENID_AUTH_URL)); + tokenUrl = new URI(config.getString(Keys.OPENID_TOKEN_URL)); + userInfoUrl = new URI(config.getString(Keys.OPENID_USERINFO_URL)); } adminGroup = config.getString(Keys.OPENID_ADMIN_GROUP); |