Skip to content

Commit

Permalink
Fix saml start (#110)
Browse files Browse the repository at this point in the history
  • Loading branch information
orius123 committed Apr 18, 2024
1 parent 0b5dc60 commit 720ad60
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 16 deletions.
2 changes: 1 addition & 1 deletion examples/management-cli/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<dependency>
<groupId>com.descope</groupId>
<artifactId>java-sdk</artifactId>
<version>1.0.17</version>
<version>1.0.19</version>
</dependency>
<dependency>
<groupId>info.picocli</groupId>
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<groupId>com.descope</groupId>
<artifactId>java-sdk</artifactId>
<modelVersion>4.0.0</modelVersion>
<version>1.0.18</version>
<version>1.0.19</version>
<name>${project.groupId}:${project.artifactId}</name>
<description>Java library used to integrate with Descope.</description>
<url>https://github.com/descope/descope-java</url>
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/com/descope/model/auth/SAMLResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.descope.model.auth;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class SAMLResponse {
private String url;
}
14 changes: 6 additions & 8 deletions src/main/java/com/descope/sdk/auth/impl/SAMLServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import com.descope.exception.DescopeException;
import com.descope.model.auth.AuthenticationInfo;
import com.descope.model.auth.SAMLResponse;
import com.descope.model.client.Client;
import com.descope.model.magiclink.LoginOptions;
import com.descope.proxy.ApiProxy;
Expand All @@ -23,24 +24,21 @@ class SAMLServiceImpl extends AuthenticationServiceImpl implements SAMLService {
@Override
public String start(String tenant, String returnURL, LoginOptions loginOptions)
throws DescopeException {
Map<String, String> request = mapOf("tenant", tenant);
Map<String, String> params = mapOf("tenant", tenant);
if (StringUtils.isNotBlank(returnURL)) {
request.put("redirectURL", returnURL);
params.put("redirectURL", returnURL);
}
URI samlStartURLAML = composeSAMLStartURL();
URI samlStartURL = getQueryParamUri(COMPOSE_SAML_START_LINK, params);
ApiProxy apiProxy = getApiProxy();
return apiProxy.post(samlStartURLAML, request, String.class);
SAMLResponse response = apiProxy.post(samlStartURL, loginOptions, SAMLResponse.class);
return response.getUrl();
}

@Override
public AuthenticationInfo exchangeToken(String code) throws DescopeException {
return exchangeToken(code, composeOAuthExchangeTokenURL());
}

private URI composeSAMLStartURL() {
return getUri(COMPOSE_SAML_START_LINK);
}

private URI composeOAuthExchangeTokenURL() {
return getUri(EXCHANGE_SAML_LINK);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import static org.mockito.Mockito.mockStatic;

import com.descope.model.auth.AuthenticationInfo;
import com.descope.model.auth.SAMLResponse;
import com.descope.model.client.Client;
import com.descope.model.jwt.Token;
import com.descope.model.jwt.response.SigningKeysResponse;
Expand All @@ -36,17 +37,16 @@ public class SamlLinkServiceImplTest {
@BeforeEach
void setUp() {
Client client = TestUtils.getClient();
this.samlService =
AuthenticationServiceBuilder.buildServices(client).getSamlService();
this.samlService = AuthenticationServiceBuilder.buildServices(client).getSamlService();
}

@Test
void testStart() {
ApiProxy apiProxy = mock(ApiProxy.class);
doReturn(MOCK_URL).when(apiProxy).post(any(), any(), any());
doReturn(new SAMLResponse(MOCK_URL)).when(apiProxy).post(any(), any(), any());
try (MockedStatic<ApiProxyBuilder> mockedApiProxyBuilder = mockStatic(ApiProxyBuilder.class)) {
mockedApiProxyBuilder.when(
() -> ApiProxyBuilder.buildProxy(any(), any())).thenReturn(apiProxy);
() -> ApiProxyBuilder.buildProxy(any(), any())).thenReturn(apiProxy);
String start = samlService.start("tenant", "returnurl", new LoginOptions());
Assertions.assertThat(start).isNotBlank().contains(MOCK_URL);
}
Expand All @@ -57,12 +57,12 @@ void testExchangeToken() {
ApiProxy apiProxy = mock(ApiProxy.class);
doReturn(MOCK_JWT_RESPONSE).when(apiProxy).post(any(), any(), any());
doReturn(new SigningKeysResponse(Arrays.asList(MOCK_SIGNING_KEY)))
.when(apiProxy).get(any(), eq(SigningKeysResponse.class));
.when(apiProxy).get(any(), eq(SigningKeysResponse.class));

AuthenticationInfo authenticationInfo;
try (MockedStatic<ApiProxyBuilder> mockedApiProxyBuilder = mockStatic(ApiProxyBuilder.class)) {
mockedApiProxyBuilder.when(
() -> ApiProxyBuilder.buildProxy(any(), any())).thenReturn(apiProxy);
() -> ApiProxyBuilder.buildProxy(any(), any())).thenReturn(apiProxy);

try (MockedStatic<JwtUtils> mockedJwtUtils = mockStatic(JwtUtils.class)) {
mockedJwtUtils.when(() -> JwtUtils.getToken(anyString(), any())).thenReturn(MOCK_TOKEN);
Expand Down

0 comments on commit 720ad60

Please sign in to comment.