-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ : add strategies to manage oauth2 clients and move them in specific…
… package
- Loading branch information
1 parent
cf25f37
commit 0cbcd07
Showing
15 changed files
with
344 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
src/main/java/io/codeka/gaia/registries/RegistryOAuth2Provider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package io.codeka.gaia.registries; | ||
|
||
import io.codeka.gaia.teams.OAuth2User; | ||
import org.apache.commons.lang.StringUtils; | ||
import org.springframework.security.oauth2.client.OAuth2AuthorizedClient; | ||
import org.springframework.security.oauth2.core.user.DefaultOAuth2User; | ||
|
||
public interface RegistryOAuth2Provider { | ||
/** | ||
* Return the name of the provider for the current strategy | ||
*/ | ||
String getProvider(); | ||
|
||
/** | ||
* Determines if the strategy is matching the provider | ||
*/ | ||
default boolean isAssignableFor(String provider) { | ||
return StringUtils.equals(getProvider(), provider); | ||
} | ||
|
||
/** | ||
* Returns the data from the authorized client | ||
*/ | ||
default OAuth2User getOAuth2User(DefaultOAuth2User user, OAuth2AuthorizedClient client) { | ||
return new OAuth2User( | ||
client.getClientRegistration().getRegistrationId(), | ||
client.getAccessToken().getTokenValue(), | ||
user.getAttributes()); | ||
} | ||
|
||
/** | ||
* Returns the http(s) url filled with oauth2 token | ||
*/ | ||
default String getOAuth2Url(String url, String token) { | ||
return url.replaceAll("^(http[s]?://)(.*)$", "$1oauth2:" + token + "@$2"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
src/main/java/io/codeka/gaia/registries/config/RegistryConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package io.codeka.gaia.registries.config; | ||
|
||
import io.codeka.gaia.registries.RegistryOAuth2Provider; | ||
import io.codeka.gaia.registries.RegistryRawContent; | ||
import io.codeka.gaia.registries.github.GitHubOAuth2Provider; | ||
import io.codeka.gaia.registries.github.GitHubRawContent; | ||
import io.codeka.gaia.registries.gitlab.GitLabOAuth2Provider; | ||
import io.codeka.gaia.registries.gitlab.GitLabRawContent; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* Config for registry management | ||
*/ | ||
@Configuration | ||
public class RegistryConfig { | ||
@Bean | ||
List<RegistryRawContent> registryRawContents() { | ||
return List.of(new GitHubRawContent(), new GitLabRawContent()); | ||
} | ||
|
||
@Bean | ||
List<RegistryOAuth2Provider> registryOAuth2Providers() { | ||
return List.of(new GitHubOAuth2Provider(), new GitLabOAuth2Provider()); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/io/codeka/gaia/registries/github/GitHubOAuth2Provider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package io.codeka.gaia.registries.github; | ||
|
||
import io.codeka.gaia.registries.RegistryOAuth2Provider; | ||
|
||
public class GitHubOAuth2Provider implements RegistryOAuth2Provider { | ||
@Override | ||
public String getProvider() { | ||
return "github"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
src/main/java/io/codeka/gaia/registries/gitlab/GitLabOAuth2Provider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package io.codeka.gaia.registries.gitlab; | ||
|
||
import io.codeka.gaia.registries.RegistryOAuth2Provider; | ||
|
||
public class GitLabOAuth2Provider implements RegistryOAuth2Provider { | ||
@Override | ||
public String getProvider() { | ||
return "gitlab"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
src/test/java/io/codeka/gaia/registries/config/RegistryConfigIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package io.codeka.gaia.registries.config; | ||
|
||
import io.codeka.gaia.registries.RegistryOAuth2Provider; | ||
import io.codeka.gaia.registries.RegistryRawContent; | ||
import io.codeka.gaia.registries.github.GitHubOAuth2Provider; | ||
import io.codeka.gaia.registries.github.GitHubRawContent; | ||
import io.codeka.gaia.registries.gitlab.GitLabOAuth2Provider; | ||
import io.codeka.gaia.registries.gitlab.GitLabRawContent; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
|
||
import java.util.List; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
@SpringBootTest(classes = RegistryConfig.class) | ||
class RegistryConfigIT { | ||
@Test | ||
void registryRawContents_shouldBeInstantiated(@Autowired(required = false) List<RegistryRawContent> registryRawContents) { | ||
assertThat(registryRawContents).isNotNull() | ||
.hasSize(2) | ||
.extracting("class") | ||
.contains(GitHubRawContent.class, GitLabRawContent.class); | ||
} | ||
|
||
@Test | ||
void registryOAuth2Providers_shouldBeInstantiated(@Autowired(required = false) List<RegistryOAuth2Provider> registryOAuth2Providers) { | ||
assertThat(registryOAuth2Providers).isNotNull() | ||
.hasSize(2) | ||
.extracting("class") | ||
.contains(GitHubOAuth2Provider.class, GitLabOAuth2Provider.class); | ||
} | ||
} |
84 changes: 84 additions & 0 deletions
84
src/test/java/io/codeka/gaia/registries/github/GitHubOAuth2ProviderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package io.codeka.gaia.registries.github; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.security.oauth2.client.OAuth2AuthorizedClient; | ||
import org.springframework.security.oauth2.client.registration.ClientRegistration; | ||
import org.springframework.security.oauth2.core.AuthorizationGrantType; | ||
import org.springframework.security.oauth2.core.OAuth2AccessToken; | ||
import org.springframework.security.oauth2.core.user.DefaultOAuth2User; | ||
|
||
import java.util.HashMap; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.junit.jupiter.api.Assertions.*; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
class GitHubOAuth2ProviderTest { | ||
|
||
private GitHubOAuth2Provider gitHubOAuth2Provider; | ||
|
||
@BeforeEach | ||
void setup() { | ||
gitHubOAuth2Provider = new GitHubOAuth2Provider(); | ||
} | ||
|
||
@Test | ||
void getProvider_shouldReturnProvider() { | ||
assertNotNull(gitHubOAuth2Provider.getProvider()); | ||
} | ||
|
||
@Test | ||
void isAssignableFor_shouldReturnTrue_forValidProvider() { | ||
assertTrue(gitHubOAuth2Provider.isAssignableFor("github")); | ||
} | ||
|
||
@Test | ||
void isAssignableFor_shouldReturnFalse_forInvalidProvider() { | ||
assertFalse(gitHubOAuth2Provider.isAssignableFor("gitlab")); | ||
} | ||
|
||
@Test | ||
void getOAuth2User_shouldReturnANewOAuthUser() { | ||
// given | ||
var attributes = new HashMap<String, Object>(); | ||
var user = mock(DefaultOAuth2User.class); | ||
var client = mock(OAuth2AuthorizedClient.class); | ||
var registration = ClientRegistration | ||
.withRegistrationId("test_registration_id") | ||
.authorizationGrantType(AuthorizationGrantType.AUTHORIZATION_CODE) | ||
.clientId("test_client_id") | ||
.redirectUriTemplate("test_uri_template") | ||
.authorizationUri("test_authorization_uri") | ||
.tokenUri("test_token_uri") | ||
.build(); | ||
var accessToken = mock(OAuth2AccessToken.class); | ||
|
||
// when | ||
when(user.getAttributes()).thenReturn(attributes); | ||
when(client.getClientRegistration()).thenReturn(registration); | ||
when(client.getAccessToken()).thenReturn(accessToken); | ||
when(accessToken.getTokenValue()).thenReturn("test_token"); | ||
var result = gitHubOAuth2Provider.getOAuth2User(user, client); | ||
|
||
// then | ||
assertThat(result).isNotNull() | ||
.hasFieldOrPropertyWithValue("provider", "test_registration_id") | ||
.hasFieldOrPropertyWithValue("token", "test_token") | ||
.hasFieldOrPropertyWithValue("attributes", attributes); | ||
} | ||
|
||
@Test | ||
void getOAuth2Url_shouldReturnUrl_withToken() { | ||
// given | ||
var url = "https://github.com/CodeKaio/gaia.git"; | ||
var token = "test_token"; | ||
|
||
// when | ||
var result = gitHubOAuth2Provider.getOAuth2Url(url, token); | ||
|
||
// then | ||
assertThat(result).isNotNull().isEqualTo("https://oauth2:[email protected]/CodeKaio/gaia.git"); | ||
} | ||
} |
Oops, something went wrong.