Skip to content

Commit f8a5dc2

Browse files
authored
Oauth url auth params fix (#180)
* Add url auth params to oauth methods + test fixes descope/etc#10021 * Add missing import * Fix url builder fix javadocs * Fix indentation
1 parent dd46951 commit f8a5dc2

5 files changed

Lines changed: 31 additions & 12 deletions

File tree

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<groupId>com.descope</groupId>
55
<artifactId>java-sdk</artifactId>
66
<modelVersion>4.0.0</modelVersion>
7-
<version>1.0.35</version>
7+
<version>1.0.36</version>
88
<name>${project.groupId}:${project.artifactId}</name>
99
<description>Java library used to integrate with Descope.</description>
1010
<url>https://github.com/descope/descope-java</url>

src/main/java/com/descope/sdk/SdkServicesBase.java

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import lombok.SneakyThrows;
1616
import org.apache.commons.lang3.StringUtils;
1717

18-
1918
public abstract class SdkServicesBase {
2019
protected final Client client;
2120

@@ -40,9 +39,7 @@ protected URI getQueryParamUri(String path, Map<String, String> params) {
4039
if (sb.length() > 1) {
4140
sb.append('&');
4241
}
43-
sb.append(
44-
URLEncoder.encode(e.getKey(), "UTF-8")).append('=').append(
45-
URLEncoder.encode(e.getValue(), "UTF-8"));
42+
sb.append(URLEncoder.encode(e.getKey(), "UTF-8")).append('=').append(URLEncoder.encode(e.getValue(), "UTF-8"));
4643
}
4744
return UriUtils.getUri(client.getUri(), path.concat(sb.toString()));
4845
}
@@ -56,4 +53,26 @@ protected Token validateAndCreateToken(String jwt) {
5653
return JwtUtils.getToken(jwt, client);
5754
}
5855

56+
@SneakyThrows
57+
protected String appendQueryParams(String url, Map<String, String> params) {
58+
if (isNotEmpty(params)) {
59+
URI oldUri = new URI(url);
60+
61+
String newQuery = oldUri.getQuery();
62+
StringBuilder sb = new StringBuilder("");
63+
if (newQuery != null) {
64+
sb.append(newQuery);
65+
}
66+
for (Entry<String, String> e : params.entrySet()) {
67+
if (sb.length() > 0) {
68+
sb.append("&");
69+
}
70+
sb.append(URLEncoder.encode(e.getKey(), "UTF-8")).append('=').append(URLEncoder.encode(e.getValue(), "UTF-8"));
71+
}
72+
73+
return new URI(oldUri.getScheme(), oldUri.getAuthority(), oldUri.getPath(), sb.toString(), oldUri.getFragment())
74+
.toString();
75+
}
76+
return url;
77+
}
5978
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ String start(String provider, String returnURL, LoginOptions loginOptions)
2424
* @param provider - provider
2525
* @param returnURL - return url
2626
* @param loginOptions - {@link LoginOptions loginOptions}
27-
* @param authParams - additional query params to append to the return URL
27+
* @param authParams - additional query params to append to the authorize URL
2828
* @return a string represent the redirect URL
2929
*/
3030
String start(String provider, String returnURL, LoginOptions loginOptions, Map<String, String> authParams)
@@ -47,7 +47,7 @@ String startSignIn(String provider, String returnURL, LoginOptions loginOptions)
4747
* @param provider - provider
4848
* @param returnURL - return url
4949
* @param loginOptions - {@link LoginOptions loginOptions}
50-
* @param authParams - additional query params to append to the return URL
50+
* @param authParams - additional query params to append to the authorize URL
5151
* @return a string represent the redirect URL
5252
*/
5353
String startSignIn(String provider, String returnURL, LoginOptions loginOptions, Map<String, String> authParams)
@@ -70,7 +70,7 @@ String startSignUp(String provider, String returnURL, LoginOptions loginOptions)
7070
* @param provider - provider
7171
* @param returnURL - return url
7272
* @param loginOptions - {@link LoginOptions loginOptions}
73-
* @param authParams - additional query params to append to the return URL
73+
* @param authParams - additional query params to append to the authorize URL
7474
* @return a string represent the redirect URL
7575
*/
7676
String startSignUp(String provider, String returnURL, LoginOptions loginOptions, Map<String, String> authParams)
@@ -83,4 +83,4 @@ String startSignUp(String provider, String returnURL, LoginOptions loginOptions,
8383
* @return {@link AuthenticationInfo}
8484
*/
8585
AuthenticationInfo exchangeToken(String code) throws DescopeException;
86-
}
86+
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ protected String startWithUrl(String url, String provider, String redirectURL, L
3232
ApiProxy apiProxy = getApiProxy();
3333
OAuthResponse res = apiProxy.post(oauthURL, loginOptions, OAuthResponse.class);
3434
url = res.getUrl();
35-
URI resUrl = getQueryParamUri(url, authParams);
36-
return resUrl.toString();
35+
String resUrl = appendQueryParams(url, authParams);
36+
return resUrl;
3737
}
3838

3939
@Override

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ void testStart() {
5959
@Test
6060
void testStartWithAuthParams() {
6161
ApiProxy apiProxy = mock(ApiProxy.class);
62-
doReturn(new OAuthResponse(MOCK_URL)).when(apiProxy).post(any(), any(), any());
62+
doReturn(new OAuthResponse(MOCK_URL + "?q=t")).when(apiProxy).post(any(), any(), any());
6363
try (MockedStatic<ApiProxyBuilder> mockedApiProxyBuilder = mockStatic(ApiProxyBuilder.class)) {
6464
mockedApiProxyBuilder.when(() -> ApiProxyBuilder.buildProxy(any(), any())).thenReturn(apiProxy);
6565
Map<String, String> params = mapOf("aa", "val1");

0 commit comments

Comments
 (0)