Skip to content

Commit 9b0c55e

Browse files
committed
[ALS-5514] Implement Okta authentication support
OktaAuthenticationService has been added to handle interactions with Okta's SDK for user authentication. Session status is fetched and checked to verify if a user is authenticated. The necessary Okta SDK dependency is also included in the pic-sure-auth-services module.
1 parent 9a05aec commit 9b0c55e

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

pic-sure-auth-services/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@
1616
<xml.bind.version>2.3.0</xml.bind.version>
1717
</properties>
1818
<dependencies>
19+
<dependency>
20+
<groupId>com.okta.sdk</groupId>
21+
<artifactId>okta-sdk-api</artifactId>
22+
<version>1.4.0</version>
23+
</dependency>
1924
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
2025
<dependency>
2126
<groupId>edu.harvard.hms.dbmi.avillach</groupId>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package edu.harvard.hms.dbmi.avillach.auth.rest;
2+
3+
import com.okta.sdk.authc.credentials.TokenClientCredentials;
4+
import com.okta.sdk.client.Client;
5+
import com.okta.sdk.client.Clients;
6+
import com.okta.sdk.resource.session.Session;
7+
import edu.harvard.dbmi.avillach.util.response.PICSUREResponse;
8+
import io.swagger.annotations.Api;
9+
import org.apache.commons.lang3.StringUtils;
10+
import org.slf4j.Logger;
11+
import org.slf4j.LoggerFactory;
12+
13+
import javax.ws.rs.Consumes;
14+
import javax.ws.rs.POST;
15+
import javax.ws.rs.Path;
16+
import javax.ws.rs.Produces;
17+
import javax.ws.rs.core.*;
18+
import java.util.Map;
19+
20+
@Api
21+
@Path("/okta")
22+
@Consumes("application/json")
23+
@Produces("application/json")
24+
public class OktaAuthenticationService {
25+
private final Logger logger = LoggerFactory.getLogger(this.getClass());
26+
27+
private final String oktaDomain = System.getenv("okta_client_origin");
28+
private final String apiToken = System.getenv("okta_client_api_token");
29+
30+
@POST
31+
@Path("/authentication")
32+
public Response authenticate(@Context HttpHeaders httpHeaders, @Context UriInfo uriInfo, Map<String, String> authRequest) {
33+
Client client = Clients.builder()
34+
.setOrgUrl(oktaDomain)
35+
.setClientCredentials(new TokenClientCredentials(apiToken))
36+
.build();
37+
38+
Map<String, Cookie> cookies = httpHeaders.getCookies();
39+
40+
// Print all of the cookies in the request
41+
for (Cookie cookie : cookies.values()) {
42+
logger.info("Cookie: " + cookie.getName() + " = " + cookie.getValue());
43+
}
44+
45+
String oktaSessionID = null;
46+
// Look for the SID cookie for OKTA
47+
if (cookies.containsKey("sid")) {
48+
oktaSessionID = cookies.get("sid").getValue();
49+
logger.info("SID: " + oktaSessionID);
50+
}
51+
52+
53+
if (StringUtils.isNotBlank(oktaSessionID)) {
54+
// Check with OKTA if the user is authenticated
55+
Session session = client.getSession(oktaSessionID);
56+
57+
if (session != null && session.getStatus() != null) {
58+
boolean isAuthenticated = session.getStatus().toString().equals("ACTIVE");
59+
60+
return PICSUREResponse.success("Session is active: " + isAuthenticated);
61+
}
62+
}
63+
64+
return PICSUREResponse.error("Session is not active");
65+
}
66+
}

0 commit comments

Comments
 (0)