Skip to content

Commit

Permalink
Merge pull request #2087 from DamnClin/rework-oauth2-module
Browse files Browse the repository at this point in the history
Rework OAuth2 module
  • Loading branch information
pascalgrimaud authored Jun 15, 2022
2 parents cee407f + 48e76e9 commit 5b48d4c
Show file tree
Hide file tree
Showing 107 changed files with 2,599 additions and 2,532 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ private Function<Entry<PropertyKey, PropertyValue>, SpringProperty> toProperty(
return property -> builderFactory.get().key(property.getKey()).value(property.getValue()).profile(properties.getKey()).build();
}

public static JHipsterModuleBuilder moduleForProject(JHipsterModuleProperties properties) {
public static JHipsterModuleBuilder moduleBuilder(JHipsterModuleProperties properties) {
return new JHipsterModuleBuilder(properties);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ public JHipsterSource template(String file) {
return file(file + MUSTACHE_EXTENSION);
}

public JHipsterSource append(String element) {
return file(element);
}

public JHipsterSource file(String file) {
return new JHipsterSource(source.resolve(file));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,6 @@ private GeneratorAction() {}
public static final String USER_AND_AUTHORITY_ENTITIES_MYSQL = "user-and-authority-entities-mysql";
public static final String USER_AND_AUTHORITY_ENTITIES_MARIADB = "user-and-authority-entities-mariadb";

public static final String SPRINGBOOT_OAUTH2 = "springboot-oauth2";
public static final String SPRINGBOOT_OAUTH2_ACCOUNT = "springboot-oauth2-account";

public static final String SONAR_JAVA_BACKEND = "sonar-java-backend";
public static final String SONAR_JAVA_BACKEND_AND_FRONTEND = "sonar-java-backend-and-frontend";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public JHipsterModule buildModule(JHipsterModuleProperties properties) {
JHipsterDestination mainDestination = toSrcMainJava().append(packagePath);

//@formatter:off
return moduleForProject(properties)
return moduleBuilder(properties)
.context()
.packageName(properties.basePackage())
.put("collectionClass", baseClassName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import tech.jhipster.lite.generator.server.javatool.base.application.JavaBaseApplicationService;

@Configuration
class JavaBaseResourceConfiguration {
class JavaBaseConfiguration {

@Bean
JHipsterModuleResource javaBaseModule(JavaBaseApplicationService javaBase) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public JHipsterModule buildModuleBannerIppon(JHipsterModuleProperties properties
private JHipsterModule buildModuleBanner(JHipsterModuleProperties properties, String file) {
// @formatter:off
return JHipsterModule
.moduleForProject(properties)
.moduleBuilder(properties)
.files()
.add(source().file(file), destination())
.and()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public JHipsterModule buildModule(JHipsterModuleProperties properties) {
JHipsterSource source = from("server/springboot/cucumber");

//@formatter:off
return moduleForProject(properties)
return moduleBuilder(properties)
.context()
.packageName(properties.basePackage())
.put("applicationName", applicationName)
Expand Down
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);
}
}
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
}
}
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();
}
}
Loading

0 comments on commit 5b48d4c

Please sign in to comment.