Skip to content

Commit

Permalink
Fix OAuth start (#72)
Browse files Browse the repository at this point in the history
* Fix OAuth start

* Fix test
  • Loading branch information
slavikm authored Oct 25, 2023
1 parent e6a7524 commit d5a49c8
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 16 deletions.
4 changes: 2 additions & 2 deletions 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.8</version>
<version>1.0.9</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 Expand Up @@ -154,8 +154,8 @@
<version>2.15.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<version>2.15.2</version>
</dependency>
</dependencies>
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/descope/literals/Routes.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public static class AuthEndPoints {

// OAuth
public static final String COMPOSE_OAUTH_LINK = "/v1/auth/oauth/authorize";
public static final String COMPOSE_OAUTH_LINK_SIGN_IN = "/v1/auth/oauth/authorize/signin";
public static final String COMPOSE_OAUTH_LINK_SIGN_UP = "/v1/auth/oauth/authorize/signup";
public static final String EXCHANGE_OAUTH_LINK = "/v1/auth/oauth/exchange";

// SAML
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/com/descope/model/auth/OAuthResponse.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 OAuthResponse {
private String url;
}
24 changes: 23 additions & 1 deletion src/main/java/com/descope/sdk/auth/OAuthService.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
public interface OAuthService {

/**
* Use to start an OAuth authentication using the given OAuthProvider.
* Use to start an OAuth authentication using the given OAuthProvider with sign up or in and options.
*
* @param provider - provider
* @param returnURL - return url
Expand All @@ -17,6 +17,28 @@ public interface OAuthService {
String start(String provider, String returnURL, LoginOptions loginOptions)
throws DescopeException;

/**
* Use to start an OAuth authentication using the given OAuthProvider with sign in and options.
*
* @param provider - provider
* @param returnURL - return url
* @param loginOptions - {@link LoginOptions loginOptions}
* @return a string represent the redirect URL
*/
String startSignIn(String provider, String returnURL, LoginOptions loginOptions)
throws DescopeException;

/**
* Use to start an OAuth authentication using the given OAuthProvider with sign up and options.
*
* @param provider - provider
* @param returnURL - return url
* @param loginOptions - {@link LoginOptions loginOptions}
* @return a string represent the redirect URL
*/
String startSignUp(String provider, String returnURL, LoginOptions loginOptions)
throws DescopeException;

/**
* Use to exchange the OAuth code with actual {@link AuthenticationInfo}.
*
Expand Down
39 changes: 28 additions & 11 deletions src/main/java/com/descope/sdk/auth/impl/OAuthServiceImpl.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package com.descope.sdk.auth.impl;

import static com.descope.literals.Routes.AuthEndPoints.COMPOSE_OAUTH_LINK;
import static com.descope.literals.Routes.AuthEndPoints.COMPOSE_OAUTH_LINK_SIGN_IN;
import static com.descope.literals.Routes.AuthEndPoints.COMPOSE_OAUTH_LINK_SIGN_UP;
import static com.descope.literals.Routes.AuthEndPoints.EXCHANGE_OAUTH_LINK;

import com.descope.exception.DescopeException;
import com.descope.model.auth.AuthParams;
import com.descope.model.auth.AuthenticationInfo;
import com.descope.model.auth.OAuthResponse;
import com.descope.model.client.Client;
import com.descope.model.magiclink.LoginOptions;
import com.descope.sdk.auth.OAuthService;
Expand All @@ -19,26 +22,40 @@ class OAuthServiceImpl extends AuthenticationServiceImpl implements OAuthService
super(client, authParams);
}

@Override
public String start(String provider, String redirectURL, LoginOptions loginOptions)
protected String startWithUrl(String url, String provider, String redirectURL, LoginOptions loginOptions)
throws DescopeException {
Map<String, String> request = new HashMap<>();
request.put("provider", provider);
Map<String, String> params = new HashMap<>();
params.put("provider", provider);
if (StringUtils.isNotBlank(redirectURL)) {
request.put("redirectURL", redirectURL);
params.put("redirectURL", redirectURL);
}
URI oauthURL = composeOAuthURL();
URI oauthURL = getQueryParamUri(url, params);
var apiProxy = getApiProxy();
return apiProxy.post(oauthURL, request, String.class);
var res = apiProxy.post(oauthURL, loginOptions, OAuthResponse.class);
return res.getUrl();
}

@Override
public AuthenticationInfo exchangeToken(String code) throws DescopeException {
return exchangeToken(code, composeOAuthExchangeTokenURL());
public String start(String provider, String redirectURL, LoginOptions loginOptions)
throws DescopeException {
return startWithUrl(COMPOSE_OAUTH_LINK, provider, redirectURL, loginOptions);
}

private URI composeOAuthURL() {
return getUri(COMPOSE_OAUTH_LINK);
@Override
public String startSignIn(String provider, String redirectURL, LoginOptions loginOptions)
throws DescopeException {
return startWithUrl(COMPOSE_OAUTH_LINK_SIGN_IN, provider, redirectURL, loginOptions);
}

@Override
public String startSignUp(String provider, String redirectURL, LoginOptions loginOptions)
throws DescopeException {
return startWithUrl(COMPOSE_OAUTH_LINK_SIGN_UP, provider, redirectURL, loginOptions);
}

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

private URI composeOAuthExchangeTokenURL() {
Expand Down
17 changes: 15 additions & 2 deletions src/test/java/com/descope/sdk/auth/impl/OAuthServiceImplTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import static com.descope.sdk.TestUtils.MOCK_TOKEN;
import static com.descope.sdk.TestUtils.MOCK_URL;
import static com.descope.sdk.TestUtils.PROJECT_ID;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
Expand All @@ -14,6 +15,7 @@
import static org.mockito.Mockito.when;

import com.descope.model.auth.AuthenticationInfo;
import com.descope.model.auth.OAuthResponse;
import com.descope.model.jwt.Provider;
import com.descope.model.jwt.Token;
import com.descope.model.jwt.response.SigningKeysResponse;
Expand All @@ -24,6 +26,7 @@
import com.descope.sdk.TestUtils;
import com.descope.sdk.auth.OAuthService;
import com.descope.utils.JwtUtils;
import java.net.URLDecoder;
import java.security.Key;
import java.util.List;
import org.assertj.core.api.Assertions;
Expand All @@ -34,7 +37,7 @@
public class OAuthServiceImplTest {

private OAuthService oauthService;

@BeforeEach
void setUp() {
var authParams = TestUtils.getAuthParams();
Expand All @@ -46,7 +49,7 @@ void setUp() {
@Test
void testStart() {
var apiProxy = mock(ApiProxy.class);
doReturn(MOCK_URL).when(apiProxy).post(any(), any(), any());
doReturn(new OAuthResponse(MOCK_URL)).when(apiProxy).post(any(), any(), any());
try (MockedStatic<ApiProxyBuilder> mockedApiProxyBuilder = mockStatic(ApiProxyBuilder.class)) {
mockedApiProxyBuilder.when(
() -> ApiProxyBuilder.buildProxy(any(), any())).thenReturn(apiProxy);
Expand Down Expand Up @@ -95,4 +98,14 @@ void testExchangeToken() {
Assertions.assertThat(user.getUserId()).isNotBlank();
Assertions.assertThat(user.getLoginIds()).isNotEmpty();
}

void testExampleRequireBrowser() throws Exception {
System.out.println(oauthService.start("google", "https://localhost/kuku", null));
String encodedCode = "";
var code = URLDecoder.decode(encodedCode, "UTF-8");
var authInfo = oauthService.exchangeToken(code);
var user = authInfo.getUser();
assertNotNull(user);
}

}

0 comments on commit d5a49c8

Please sign in to comment.