Skip to content

Commit b43cda3

Browse files
authored
Add url auth params to oauth methods (#178)
+ test fixes descope/etc#10021
1 parent 86de1e7 commit b43cda3

File tree

3 files changed

+91
-20
lines changed

3 files changed

+91
-20
lines changed

src/main/java/com/descope/sdk/auth/OAuthService.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.descope.exception.DescopeException;
44
import com.descope.model.auth.AuthenticationInfo;
55
import com.descope.model.magiclink.LoginOptions;
6+
import java.util.Map;
67

78
public interface OAuthService {
89

@@ -17,6 +18,18 @@ public interface OAuthService {
1718
String start(String provider, String returnURL, LoginOptions loginOptions)
1819
throws DescopeException;
1920

21+
/**
22+
* Use to start an OAuth authentication using the given OAuthProvider with sign up or in and options.
23+
*
24+
* @param provider - provider
25+
* @param returnURL - return url
26+
* @param loginOptions - {@link LoginOptions loginOptions}
27+
* @param authParams - additional query params to append to the return URL
28+
* @return a string represent the redirect URL
29+
*/
30+
String start(String provider, String returnURL, LoginOptions loginOptions, Map<String, String> authParams)
31+
throws DescopeException;
32+
2033
/**
2134
* Use to start an OAuth authentication using the given OAuthProvider with sign in and options.
2235
*
@@ -28,6 +41,18 @@ String start(String provider, String returnURL, LoginOptions loginOptions)
2841
String startSignIn(String provider, String returnURL, LoginOptions loginOptions)
2942
throws DescopeException;
3043

44+
/**
45+
* Use to start an OAuth authentication using the given OAuthProvider with sign in and options.
46+
*
47+
* @param provider - provider
48+
* @param returnURL - return url
49+
* @param loginOptions - {@link LoginOptions loginOptions}
50+
* @param authParams - additional query params to append to the return URL
51+
* @return a string represent the redirect URL
52+
*/
53+
String startSignIn(String provider, String returnURL, LoginOptions loginOptions, Map<String, String> authParams)
54+
throws DescopeException;
55+
3156
/**
3257
* Use to start an OAuth authentication using the given OAuthProvider with sign up and options.
3358
*
@@ -39,6 +64,18 @@ String startSignIn(String provider, String returnURL, LoginOptions loginOptions)
3964
String startSignUp(String provider, String returnURL, LoginOptions loginOptions)
4065
throws DescopeException;
4166

67+
/**
68+
* Use to start an OAuth authentication using the given OAuthProvider with sign up and options.
69+
*
70+
* @param provider - provider
71+
* @param returnURL - return url
72+
* @param loginOptions - {@link LoginOptions loginOptions}
73+
* @param authParams - additional query params to append to the return URL
74+
* @return a string represent the redirect URL
75+
*/
76+
String startSignUp(String provider, String returnURL, LoginOptions loginOptions, Map<String, String> authParams)
77+
throws DescopeException;
78+
4279
/**
4380
* Use to exchange the OAuth code with actual {@link AuthenticationInfo}.
4481
*

src/main/java/com/descope/sdk/auth/impl/OAuthServiceImpl.java

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,34 +22,51 @@ class OAuthServiceImpl extends AuthenticationServiceImpl implements OAuthService
2222
super(client);
2323
}
2424

25-
protected String startWithUrl(String url, String provider, String redirectURL, LoginOptions loginOptions)
26-
throws DescopeException {
25+
protected String startWithUrl(String url, String provider, String redirectURL, LoginOptions loginOptions,
26+
Map<String, String> authParams) throws DescopeException {
2727
Map<String, String> params = mapOf("provider", provider);
2828
if (StringUtils.isNotBlank(redirectURL)) {
2929
params.put("redirectURL", redirectURL);
3030
}
3131
URI oauthURL = getQueryParamUri(url, params);
3232
ApiProxy apiProxy = getApiProxy();
3333
OAuthResponse res = apiProxy.post(oauthURL, loginOptions, OAuthResponse.class);
34-
return res.getUrl();
34+
url = res.getUrl();
35+
URI resUrl = getQueryParamUri(url, authParams);
36+
return resUrl.toString();
3537
}
3638

3739
@Override
38-
public String start(String provider, String redirectURL, LoginOptions loginOptions)
39-
throws DescopeException {
40-
return startWithUrl(COMPOSE_OAUTH_LINK, provider, redirectURL, loginOptions);
40+
public String start(String provider, String redirectURL, LoginOptions loginOptions) throws DescopeException {
41+
return startWithUrl(COMPOSE_OAUTH_LINK, provider, redirectURL, loginOptions, null);
4142
}
4243

4344
@Override
44-
public String startSignIn(String provider, String redirectURL, LoginOptions loginOptions)
45+
public String start(String provider, String redirectURL, LoginOptions loginOptions, Map<String, String> authParams)
4546
throws DescopeException {
46-
return startWithUrl(COMPOSE_OAUTH_LINK_SIGN_IN, provider, redirectURL, loginOptions);
47+
return startWithUrl(COMPOSE_OAUTH_LINK, provider, redirectURL, loginOptions, authParams);
4748
}
4849

4950
@Override
50-
public String startSignUp(String provider, String redirectURL, LoginOptions loginOptions)
51-
throws DescopeException {
52-
return startWithUrl(COMPOSE_OAUTH_LINK_SIGN_UP, provider, redirectURL, loginOptions);
51+
public String startSignIn(String provider, String redirectURL, LoginOptions loginOptions) throws DescopeException {
52+
return startWithUrl(COMPOSE_OAUTH_LINK_SIGN_IN, provider, redirectURL, loginOptions, null);
53+
}
54+
55+
@Override
56+
public String startSignIn(String provider, String redirectURL, LoginOptions loginOptions,
57+
Map<String, String> authParams) throws DescopeException {
58+
return startWithUrl(COMPOSE_OAUTH_LINK_SIGN_IN, provider, redirectURL, loginOptions, authParams);
59+
}
60+
61+
@Override
62+
public String startSignUp(String provider, String redirectURL, LoginOptions loginOptions) throws DescopeException {
63+
return startWithUrl(COMPOSE_OAUTH_LINK_SIGN_UP, provider, redirectURL, loginOptions, null);
64+
}
65+
66+
@Override
67+
public String startSignUp(String provider, String redirectURL, LoginOptions loginOptions,
68+
Map<String, String> authParams) throws DescopeException {
69+
return startWithUrl(COMPOSE_OAUTH_LINK_SIGN_UP, provider, redirectURL, loginOptions, authParams);
5370
}
5471

5572
@Override

src/test/java/com/descope/sdk/auth/impl/OAuthServiceImplTest.java

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import static com.descope.sdk.TestUtils.MOCK_TOKEN;
77
import static com.descope.sdk.TestUtils.MOCK_URL;
88
import static com.descope.sdk.TestUtils.PROJECT_ID;
9+
import static com.descope.utils.CollectionUtils.mapOf;
910
import static org.junit.jupiter.api.Assertions.assertNotNull;
1011
import static org.mockito.ArgumentMatchers.any;
1112
import static org.mockito.ArgumentMatchers.anyString;
@@ -28,6 +29,7 @@
2829
import com.descope.utils.JwtUtils;
2930
import java.net.URLDecoder;
3031
import java.util.Arrays;
32+
import java.util.Map;
3133
import org.assertj.core.api.Assertions;
3234
import org.junit.jupiter.api.BeforeEach;
3335
import org.junit.jupiter.api.Test;
@@ -36,37 +38,52 @@
3638
public class OAuthServiceImplTest {
3739

3840
private OAuthService oauthService;
39-
41+
4042
@BeforeEach
4143
void setUp() {
4244
Client client = TestUtils.getClient();
43-
this.oauthService =
44-
AuthenticationServiceBuilder.buildServices(client).getOauthService();
45+
this.oauthService = AuthenticationServiceBuilder.buildServices(client).getOauthService();
4546
}
4647

4748
@Test
4849
void testStart() {
4950
ApiProxy apiProxy = mock(ApiProxy.class);
5051
doReturn(new OAuthResponse(MOCK_URL)).when(apiProxy).post(any(), any(), any());
5152
try (MockedStatic<ApiProxyBuilder> mockedApiProxyBuilder = mockStatic(ApiProxyBuilder.class)) {
52-
mockedApiProxyBuilder.when(
53-
() -> ApiProxyBuilder.buildProxy(any(), any())).thenReturn(apiProxy);
53+
mockedApiProxyBuilder.when(() -> ApiProxyBuilder.buildProxy(any(), any())).thenReturn(apiProxy);
5454
String start = oauthService.start("provider", "returnurl", new LoginOptions());
5555
Assertions.assertThat(start).isNotBlank().contains(MOCK_URL);
5656
}
5757
}
5858

59+
@Test
60+
void testStartWithAuthParams() {
61+
ApiProxy apiProxy = mock(ApiProxy.class);
62+
doReturn(new OAuthResponse(MOCK_URL)).when(apiProxy).post(any(), any(), any());
63+
try (MockedStatic<ApiProxyBuilder> mockedApiProxyBuilder = mockStatic(ApiProxyBuilder.class)) {
64+
mockedApiProxyBuilder.when(() -> ApiProxyBuilder.buildProxy(any(), any())).thenReturn(apiProxy);
65+
Map<String, String> params = mapOf("aa", "val1");
66+
params.put("bb", "val2");
67+
68+
String start = oauthService.start("provider", "returnurl", new LoginOptions(), params);
69+
Assertions.assertThat(start).isNotBlank().contains(MOCK_URL);
70+
Assertions.assertThat(start).isNotBlank().contains("val1");
71+
Assertions.assertThat(start).isNotBlank().contains("val2");
72+
Assertions.assertThat(start).isNotBlank().contains("aa");
73+
Assertions.assertThat(start).isNotBlank().contains("bb");
74+
}
75+
}
76+
5977
@Test
6078
void testExchangeToken() {
6179
ApiProxy apiProxy = mock(ApiProxy.class);
6280
doReturn(MOCK_JWT_RESPONSE).when(apiProxy).post(any(), any(), any());
63-
doReturn(new SigningKeysResponse(Arrays.asList(MOCK_SIGNING_KEY)))
64-
.when(apiProxy).get(any(), eq(SigningKeysResponse.class));
81+
doReturn(new SigningKeysResponse(Arrays.asList(MOCK_SIGNING_KEY))).when(apiProxy).get(any(),
82+
eq(SigningKeysResponse.class));
6583

6684
AuthenticationInfo authenticationInfo;
6785
try (MockedStatic<ApiProxyBuilder> mockedApiProxyBuilder = mockStatic(ApiProxyBuilder.class)) {
68-
mockedApiProxyBuilder.when(
69-
() -> ApiProxyBuilder.buildProxy(any(), any())).thenReturn(apiProxy);
86+
mockedApiProxyBuilder.when(() -> ApiProxyBuilder.buildProxy(any(), any())).thenReturn(apiProxy);
7087

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

0 commit comments

Comments
 (0)