-
-
Notifications
You must be signed in to change notification settings - Fork 217
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2087 from DamnClin/rework-oauth2-module
Rework OAuth2 module
- Loading branch information
Showing
107 changed files
with
2,599 additions
and
2,532 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
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
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
23 changes: 14 additions & 9 deletions
23
...r/server/springboot/mvc/security/oauth2/application/OAuth2SecurityApplicationService.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 |
---|---|---|
@@ -1,23 +1,28 @@ | ||
package tech.jhipster.lite.generator.server.springboot.mvc.security.oauth2.application; | ||
|
||
import org.springframework.stereotype.Service; | ||
import tech.jhipster.lite.generator.project.domain.Project; | ||
import tech.jhipster.lite.generator.server.springboot.mvc.security.oauth2.domain.OAuth2SecurityService; | ||
import tech.jhipster.lite.generator.docker.domain.DockerImages; | ||
import tech.jhipster.lite.generator.module.domain.JHipsterModule; | ||
import tech.jhipster.lite.generator.module.domain.properties.JHipsterModuleProperties; | ||
import tech.jhipster.lite.generator.server.springboot.mvc.security.oauth2.domain.OAuth2AccountModuleFactory; | ||
import tech.jhipster.lite.generator.server.springboot.mvc.security.oauth2.domain.OAuth2ModuleFactory; | ||
|
||
@Service | ||
public class OAuth2SecurityApplicationService { | ||
|
||
private final OAuth2SecurityService oauth2SecurityService; | ||
private final OAuth2ModuleFactory oAuth2factory; | ||
private final OAuth2AccountModuleFactory acountsFactory; | ||
|
||
public OAuth2SecurityApplicationService(OAuth2SecurityService oauth2SecurityService) { | ||
this.oauth2SecurityService = oauth2SecurityService; | ||
public OAuth2SecurityApplicationService(DockerImages dockerImages) { | ||
oAuth2factory = new OAuth2ModuleFactory(dockerImages); | ||
acountsFactory = new OAuth2AccountModuleFactory(); | ||
} | ||
|
||
public void addOAuth2(Project project) { | ||
oauth2SecurityService.addOAuth2(project); | ||
public JHipsterModule buildOAuth2Module(JHipsterModuleProperties properties) { | ||
return oAuth2factory.buildModule(properties); | ||
} | ||
|
||
public void addAccountContext(Project project) { | ||
oauth2SecurityService.addAccountContext(project); | ||
public JHipsterModule buildOAuth2AccountModule(JHipsterModuleProperties properties) { | ||
return acountsFactory.buildModule(properties); | ||
} | ||
} |
82 changes: 82 additions & 0 deletions
82
...te/generator/server/springboot/mvc/security/oauth2/domain/OAuth2AccountModuleFactory.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,82 @@ | ||
package tech.jhipster.lite.generator.server.springboot.mvc.security.oauth2.domain; | ||
|
||
import static tech.jhipster.lite.generator.module.domain.JHipsterModule.*; | ||
|
||
import tech.jhipster.lite.generator.module.domain.JHipsterDestination; | ||
import tech.jhipster.lite.generator.module.domain.JHipsterModule; | ||
import tech.jhipster.lite.generator.module.domain.JHipsterSource; | ||
import tech.jhipster.lite.generator.module.domain.properties.JHipsterModuleProperties; | ||
|
||
public class OAuth2AccountModuleFactory { | ||
|
||
private static final String PACKAGE_INFO = "package-info.java"; | ||
private static final String APPLICATION = "application"; | ||
private static final String DOMAIN = "domain"; | ||
private static final String INFRASTRUCTURE = "infrastructure"; | ||
private static final String PRIMARY = INFRASTRUCTURE + "/primary"; | ||
private static final String SECONDARY = INFRASTRUCTURE + "/secondary"; | ||
|
||
private static final JHipsterSource ACCOUNT_SOURCE = from("server/springboot/mvc/security/oauth2/account"); | ||
private static final JHipsterSource ACCOUNT_MAIN_SOURCE = ACCOUNT_SOURCE.append("main"); | ||
private static final JHipsterSource ACCOUNT_TEST_SOURCE = ACCOUNT_SOURCE.append("test"); | ||
|
||
private static final JHipsterSource USER_IDENTITY_SOURCE = from("server/springboot/mvc/security/oauth2/useridentity"); | ||
private static final JHipsterSource USER_IDENTITY_MAIN_SOURCE = USER_IDENTITY_SOURCE.append("main"); | ||
private static final JHipsterSource USER_IDENTITY_TEST_SOURCE = USER_IDENTITY_SOURCE.append("test"); | ||
|
||
public JHipsterModule buildModule(JHipsterModuleProperties properties) { | ||
String packagePath = properties.basePackage().path(); | ||
JHipsterDestination accountMainDestination = toSrcMainJava().append(packagePath).append("account"); | ||
JHipsterDestination accountTestDestination = toSrcTestJava().append(packagePath).append("account"); | ||
|
||
JHipsterDestination userIdentityMainDestination = toSrcMainJava().append(packagePath).append("useridentity"); | ||
JHipsterDestination userIdentityTestDestination = toSrcTestJava().append(packagePath).append("useridentity"); | ||
|
||
//@formatter:off | ||
return moduleBuilder(properties) | ||
.context() | ||
.packageName(properties.basePackage()) | ||
.and() | ||
.files() | ||
.add(ACCOUNT_MAIN_SOURCE.append(APPLICATION).template("AccountsApplicationService.java"), accountMainDestination.append(APPLICATION).append("AccountsApplicationService.java")) | ||
.batch(ACCOUNT_MAIN_SOURCE.append(DOMAIN), accountMainDestination.append(DOMAIN)) | ||
.add("Account.java") | ||
.add("AccountsRepository.java") | ||
.and() | ||
.batch(ACCOUNT_MAIN_SOURCE.append(PRIMARY), accountMainDestination.append(PRIMARY)) | ||
.add("RestAccount.java") | ||
.add("AccountsResource.java") | ||
.and() | ||
.batch(ACCOUNT_MAIN_SOURCE.append(SECONDARY), accountMainDestination.append(SECONDARY)) | ||
.add("OAuth2AccountsRepository.java") | ||
.add("OAuth2AuthenticationReader.java") | ||
.add("UnknownAuthenticationSchemeException.java") | ||
.and() | ||
.add(ACCOUNT_MAIN_SOURCE.template(PACKAGE_INFO), accountMainDestination.append(PACKAGE_INFO)) | ||
.add(ACCOUNT_TEST_SOURCE.append(DOMAIN).template("AccountsFixture.java"), accountTestDestination.append(DOMAIN).append("AccountsFixture.java")) | ||
.batch(ACCOUNT_TEST_SOURCE.append(PRIMARY), accountTestDestination.append(PRIMARY)) | ||
.add("RestAccountTest.java") | ||
.add("AccountsResourceIntTest.java") | ||
.add("AccountsResourceTest.java") | ||
.and() | ||
.add(ACCOUNT_TEST_SOURCE.append(SECONDARY).template("OAuth2AuthenticationReaderTest.java"), accountTestDestination.append(SECONDARY).append("OAuth2AuthenticationReaderTest.java")) | ||
.add(ACCOUNT_TEST_SOURCE.append(INFRASTRUCTURE).template("OAuth2TokenFixture.java"), accountTestDestination.append(INFRASTRUCTURE).append("OAuth2TokenFixture.java")) | ||
.batch(USER_IDENTITY_MAIN_SOURCE.append(DOMAIN), userIdentityMainDestination.append(DOMAIN)) | ||
.add("Email.java") | ||
.add("Firstname.java") | ||
.add("Lastname.java") | ||
.add("Name.java") | ||
.and() | ||
.add(USER_IDENTITY_MAIN_SOURCE.template(PACKAGE_INFO), userIdentityMainDestination.append(PACKAGE_INFO)) | ||
.batch(USER_IDENTITY_TEST_SOURCE.append(DOMAIN), userIdentityTestDestination.append(DOMAIN)) | ||
.add("EmailTest.java") | ||
.add("FirstnameTest.java") | ||
.add("LastnameTest.java") | ||
.add("NameTest.java") | ||
.add("UsersIdentitiesFixture.java") | ||
.and() | ||
.and() | ||
.build(); | ||
//@formatter:on | ||
} | ||
} |
192 changes: 192 additions & 0 deletions
192
...ster/lite/generator/server/springboot/mvc/security/oauth2/domain/OAuth2ModuleFactory.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,192 @@ | ||
package tech.jhipster.lite.generator.server.springboot.mvc.security.oauth2.domain; | ||
|
||
import static tech.jhipster.lite.generator.module.domain.JHipsterModule.*; | ||
|
||
import tech.jhipster.lite.error.domain.Assert; | ||
import tech.jhipster.lite.generator.docker.domain.DockerImage; | ||
import tech.jhipster.lite.generator.docker.domain.DockerImages; | ||
import tech.jhipster.lite.generator.module.domain.JHipsterDestination; | ||
import tech.jhipster.lite.generator.module.domain.JHipsterModule; | ||
import tech.jhipster.lite.generator.module.domain.JHipsterModule.JHipsterModuleBuilder; | ||
import tech.jhipster.lite.generator.module.domain.JHipsterSource; | ||
import tech.jhipster.lite.generator.module.domain.javadependency.GroupId; | ||
import tech.jhipster.lite.generator.module.domain.javadependency.JavaDependency; | ||
import tech.jhipster.lite.generator.module.domain.javadependency.JavaDependencyScope; | ||
import tech.jhipster.lite.generator.module.domain.properties.JHipsterModuleProperties; | ||
import tech.jhipster.lite.generator.module.domain.replacement.TextMatcher; | ||
|
||
public class OAuth2ModuleFactory { | ||
|
||
private static final String TARGET_ANNOTATION = "@Target(ElementType.TYPE)"; | ||
private static final String SPRING_BOOT_IMPORT = "import org.springframework.boot.test.context.SpringBootTest;"; | ||
private static final GroupId SPRING_GROUP = groupId("org.springframework.boot"); | ||
private static final String BREAK = "\n"; | ||
private static final String DOMAIN = "domain"; | ||
private static final String PRIMARY = "infrastructure/primary"; | ||
|
||
private static final JHipsterSource SOURCE = from("server/springboot/mvc/security/oauth2"); | ||
private static final JHipsterSource MAIN_SOURCE = SOURCE.append("main"); | ||
private static final JHipsterSource TEST_SOURCE = SOURCE.append("test"); | ||
private static final JHipsterSource DOCKER_SOURCE = SOURCE.append("docker"); | ||
private static final JHipsterDestination DOCKER_DESTINATION = to("src/main/docker"); | ||
|
||
private final DockerImages dockerImages; | ||
|
||
public OAuth2ModuleFactory(DockerImages dockerImages) { | ||
Assert.notNull("dockerImages", dockerImages); | ||
|
||
this.dockerImages = dockerImages; | ||
} | ||
|
||
public JHipsterModule buildModule(JHipsterModuleProperties properties) { | ||
Assert.notNull("properties", properties); | ||
|
||
//@formatter:off | ||
JHipsterModuleBuilder builder = moduleBuilder(properties) | ||
.context() | ||
.packageName(properties.basePackage()) | ||
.put("applicationName", properties.projectBaseName() | ||
.capitalized()) | ||
.and(); | ||
//@formatter:on | ||
|
||
appendKeycloak(builder); | ||
appendJavaFiles(builder, properties); | ||
appendDependencies(builder); | ||
appendSpringProperties(builder); | ||
appendIntegrationTestAnnotationUpdates(builder, properties); | ||
|
||
return builder.build(); | ||
} | ||
|
||
private void appendKeycloak(JHipsterModuleBuilder builder) { | ||
DockerImage keycloakImage = dockerImages.get("jboss/keycloak"); | ||
|
||
builder.context().put("dockerKeycloakVersion", keycloakImage.version()).put("dockerKeycloakImage", keycloakImage.fullName()); | ||
|
||
builder | ||
.files() | ||
.add(DOCKER_SOURCE.template("keycloak.yml"), DOCKER_DESTINATION.append("keycloak.yml")) | ||
.batch(DOCKER_SOURCE, DOCKER_DESTINATION.append("keycloak-realm-config")) | ||
.add("jhipster-realm.json") | ||
.add("jhipster-users-0.json"); | ||
} | ||
|
||
private void appendJavaFiles(JHipsterModuleBuilder builder, JHipsterModuleProperties properties) { | ||
String packagePath = properties.basePackage().path(); | ||
JHipsterDestination mainDestination = toSrcMainJava().append(packagePath).append("authentication"); | ||
JHipsterDestination testDestination = toSrcTestJava().append(packagePath).append("authentication"); | ||
|
||
//@formatter:off | ||
builder | ||
.files() | ||
.add(MAIN_SOURCE.template("package-info.java"), mainDestination.append("package-info.java")) | ||
.batch(MAIN_SOURCE.append(DOMAIN), mainDestination.append(DOMAIN)) | ||
.add("Role.java") | ||
.add("Roles.java") | ||
.add("Username.java") | ||
.and() | ||
.batch(MAIN_SOURCE.append(PRIMARY), mainDestination.append(PRIMARY)) | ||
.add("ApplicationSecurityProperties.java") | ||
.add("AudienceValidator.java") | ||
.add("AuthenticatedUser.java") | ||
.add("AuthenticationException.java") | ||
.add("AuthenticationExceptionAdvice.java") | ||
.add("Claims.java") | ||
.add("CustomClaimConverter.java") | ||
.add("JwtGrantedAuthorityConverter.java") | ||
.add("NotAuthenticatedUserException.java") | ||
.add("OAuth2Configuration.java") | ||
.add("SecurityConfiguration.java") | ||
.add("UnknownAuthenticationException.java") | ||
.and() | ||
.batch(TEST_SOURCE.append(DOMAIN), testDestination.append(DOMAIN)) | ||
.add("RolesTest.java") | ||
.add("RoleTest.java") | ||
.add("UsernameTest.java") | ||
.and() | ||
.batch(TEST_SOURCE.append(PRIMARY), testDestination.append(PRIMARY)) | ||
.add("AccountExceptionResource.java") | ||
.add("ApplicationSecurityPropertiesTest.java") | ||
.add("AudienceValidatorTest.java") | ||
.add("AuthenticatedUserTest.java") | ||
.add("AuthenticationExceptionAdviceIT.java") | ||
.add("ClaimsTest.java") | ||
.add("CustomClaimConverterIT.java") | ||
.add("FakeRequestAttributes.java") | ||
.add("JwtGrantedAuthorityConverterTest.java") | ||
.add("SecurityConfigurationIT.java") | ||
.add("SecurityConfigurationTest.java") | ||
.add("TestSecurityConfiguration.java") | ||
.add("WithUnauthenticatedMockUser.java"); | ||
//@formatter:on | ||
} | ||
|
||
private void appendDependencies(JHipsterModuleBuilder builder) { | ||
builder | ||
.javaDependencies() | ||
.add(SPRING_GROUP, artifactId("spring-boot-starter-security")) | ||
.add(SPRING_GROUP, artifactId("spring-boot-starter-oauth2-client")) | ||
.add(SPRING_GROUP, artifactId("spring-boot-starter-oauth2-resource-server")) | ||
.add(springSecurityTest()); | ||
} | ||
|
||
private void appendSpringProperties(JHipsterModuleBuilder builder) { | ||
builder | ||
.springMainProperties() | ||
.set( | ||
propertyKey("spring.security.oauth2.client.provider.oidc.issuer-uri"), | ||
propertyValue("http://localhost:9080/auth/realms/jhipster") | ||
) | ||
.set(propertyKey("spring.security.oauth2.client.registration.oidc.client-id"), propertyValue("web_app")) | ||
.set(propertyKey("spring.security.oauth2.client.registration.oidc.client-secret"), propertyValue("web_app")) | ||
.set(propertyKey("spring.security.oauth2.client.registration.oidc.scope"), propertyValue("openid,profile,email")) | ||
.set(propertyKey("application.security.oauth2.audience"), propertyValue("account,api://default")); | ||
|
||
builder | ||
.springTestProperties() | ||
.set(propertyKey("spring.main.allow-bean-definition-overriding"), propertyValue("true")) | ||
.set( | ||
propertyKey("spring.security.oauth2.client.provider.oidc.issuer-uri"), | ||
propertyValue("http://DO_NOT_CALL:9080/auth/realms/jhipster") | ||
); | ||
} | ||
|
||
private void appendIntegrationTestAnnotationUpdates(JHipsterModuleBuilder builder, JHipsterModuleProperties properties) { | ||
String baseClass = properties.projectBaseName().capitalized() + "App.class"; | ||
TextMatcher importNeedle = text(SPRING_BOOT_IMPORT); | ||
|
||
String integrationtTestFile = "src/test/java/" + properties.basePackage().path() + "/IntegrationTest.java"; | ||
|
||
builder | ||
.mandatoryReplacements() | ||
.in(integrationtTestFile) | ||
.add(importNeedle, testSecurityConfigurationImport(properties)) | ||
.add(text(baseClass), baseClass + ", TestSecurityConfiguration.class") | ||
.add(importNeedle, withMockUserImport()) | ||
.add(text(TARGET_ANNOTATION), TARGET_ANNOTATION + BREAK + "@WithMockUser"); | ||
} | ||
|
||
private String testSecurityConfigurationImport(JHipsterModuleProperties properties) { | ||
return new StringBuilder() | ||
.append(SPRING_BOOT_IMPORT) | ||
.append(BREAK) | ||
.append("import ") | ||
.append(properties.basePackage().get()) | ||
.append(".authentication.infrastructure.primary.TestSecurityConfiguration;") | ||
.toString(); | ||
} | ||
|
||
private String withMockUserImport() { | ||
return SPRING_BOOT_IMPORT + BREAK + "import org.springframework.security.test.context.support.WithMockUser;"; | ||
} | ||
|
||
private JavaDependency springSecurityTest() { | ||
return JavaDependency | ||
.builder() | ||
.groupId("org.springframework.security") | ||
.artifactId("spring-security-test") | ||
.scope(JavaDependencyScope.TEST) | ||
.build(); | ||
} | ||
} |
Oops, something went wrong.