Skip to content

Commit

Permalink
Merge pull request #10928 from renanfranca/1133-vue-oauth2-keycloak-m…
Browse files Browse the repository at this point in the history
…odule

add vue oauth2 keycloak module
  • Loading branch information
renanfranca authored Sep 24, 2024
2 parents 1375285 + b1aeaa9 commit 0775206
Show file tree
Hide file tree
Showing 23 changed files with 1,341 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package tech.jhipster.lite.generator.client.angular.security.oauth2.domain;

import static tech.jhipster.lite.module.domain.JHipsterModule.*;
import static tech.jhipster.lite.module.domain.npm.JHLiteNpmVersionSource.ANGULAR;
import static tech.jhipster.lite.module.domain.npm.JHLiteNpmVersionSource.*;
import static tech.jhipster.lite.module.domain.replacement.ReplacementCondition.notMatchingRegex;

import java.util.regex.Pattern;
Expand Down Expand Up @@ -114,7 +114,7 @@ public JHipsterModule buildModule(JHipsterModuleProperties properties) {
//@formatter:off
return moduleBuilder(properties)
.packageJson()
.addDependency(packageName("keycloak-js"), ANGULAR)
.addDependency(packageName("keycloak-js"), COMMON)
.and()
.files()
.batch(SOURCE.append("auth"), APP_DESTINATION.append("auth"))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package tech.jhipster.lite.generator.client.vue.security.oauth2_keycloak.application;

import org.springframework.stereotype.Service;
import tech.jhipster.lite.generator.client.vue.security.oauth2_keycloak.domain.VueOAuth2KeycloakModulesFactory;
import tech.jhipster.lite.module.domain.JHipsterModule;
import tech.jhipster.lite.module.domain.properties.JHipsterModuleProperties;

@Service
public class VueOAuth2KeycloakApplicationService {

private final VueOAuth2KeycloakModulesFactory factory;

public VueOAuth2KeycloakApplicationService() {
factory = new VueOAuth2KeycloakModulesFactory();
}

public JHipsterModule buildModule(JHipsterModuleProperties properties) {
return factory.buildModule(properties);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package tech.jhipster.lite.generator.client.vue.security.oauth2_keycloak.domain;

import static tech.jhipster.lite.module.domain.JHipsterModule.*;
import static tech.jhipster.lite.module.domain.npm.JHLiteNpmVersionSource.*;

import tech.jhipster.lite.module.domain.Indentation;
import tech.jhipster.lite.module.domain.JHipsterModule;
import tech.jhipster.lite.module.domain.file.JHipsterDestination;
import tech.jhipster.lite.module.domain.file.JHipsterSource;
import tech.jhipster.lite.module.domain.properties.JHipsterModuleProperties;
import tech.jhipster.lite.shared.error.domain.Assert;

public class VueOAuth2KeycloakModulesFactory {

private static final JHipsterSource SOURCE = from("client/vue");
private static final JHipsterSource APP_SOURCE = from("client/vue/webapp/app");
private static final JHipsterSource DOCUMENTATION_SOURCE = SOURCE.append("documentation");

private static final JHipsterDestination MAIN_DESTINATION = to("src/main/webapp/app");
private static final JHipsterDestination TEST_DESTINATION = to("src/test/webapp");

private static final String MAIN_TS_IMPORT_NEEDLE = "// jhipster-needle-main-ts-import";
private static final String MAIN_TS_PROVIDER_NEEDLE = "// jhipster-needle-main-ts-provider";

private static final String KEYCLOAK_IMPORT =
"""
import { provideForAuth } from '@/auth/application/AuthProvider';
import { KeycloakHttp } from '@/auth/infrastructure/secondary/KeycloakHttp';
import Keycloak from 'keycloak-js';\
""";
private static final String KEYCLOAK_SETUP =
"""
const keycloakHttp = new KeycloakHttp(
%snew Keycloak({
%surl: 'http://localhost:9080',
%srealm: 'jhipster',
%sclientId: 'web_app',
%s}),
);
provideForAuth(keycloakHttp);\
""";

public JHipsterModule buildModule(JHipsterModuleProperties properties) {
Assert.notNull("properties", properties);

Indentation indentation = properties.indentation();

//@formatter:off
return moduleBuilder(properties)
.documentation(documentationTitle("Vue Authentication Components"),
DOCUMENTATION_SOURCE.file("vue-authentication-components.md"))
.packageJson()
.addDependency(packageName("keycloak-js"), COMMON)
.and()
.files()
.batch(APP_SOURCE.append("auth"), MAIN_DESTINATION.append("auth"))
.addTemplate("application/AuthProvider.ts")
.addTemplate("domain/AuthRepository.ts")
.addTemplate("domain/AuthenticatedUser.ts")
.addTemplate("infrastructure/secondary/KeycloakAuthRepository.ts")
.addTemplate("infrastructure/secondary/KeycloakHttp.ts")
.and()
.add(APP_SOURCE.template("test/webapp/unit/auth/application/AuthProvider.spec.ts"), TEST_DESTINATION.append("unit/auth/application/AuthProvider.spec.ts"))
.batch(APP_SOURCE.append("test/webapp/unit/auth/infrastructure/secondary"), TEST_DESTINATION.append("unit/auth/infrastructure/secondary"))
.addTemplate("KeycloakAuthRepository.spec.ts")
.addTemplate("KeycloakHttp.spec.ts")
.addTemplate("KeycloakHttpStub.ts")
.addTemplate("KeycloakStub.ts")
.and()
.and()
.mandatoryReplacements()
.in(path("src/main/webapp/app/main.ts"))
.add(lineBeforeText(MAIN_TS_IMPORT_NEEDLE),
KEYCLOAK_IMPORT
)
.add(lineBeforeText(MAIN_TS_PROVIDER_NEEDLE),
KEYCLOAK_SETUP.formatted(indentation.spaces(),
indentation.times(2),
indentation.times(2),
indentation.times(2),
indentation.spaces())
)
.and()
.and()
.build();
//@formatter:on
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package tech.jhipster.lite.generator.client.vue.security.oauth2_keycloak.infrastructure.primary;

import static tech.jhipster.lite.generator.slug.domain.JHLiteModuleSlug.*;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import tech.jhipster.lite.generator.client.vue.security.oauth2_keycloak.application.VueOAuth2KeycloakApplicationService;
import tech.jhipster.lite.module.domain.resource.JHipsterModuleOrganization;
import tech.jhipster.lite.module.domain.resource.JHipsterModulePropertiesDefinition;
import tech.jhipster.lite.module.domain.resource.JHipsterModuleResource;

@Configuration
class VueOAuth2KeycloakModuleConfiguration {

@Bean
JHipsterModuleResource vueOAuth2KeycloakModule(VueOAuth2KeycloakApplicationService oauth2Keycloak) {
return JHipsterModuleResource.builder()
.slug(VUE_OAUTH2_KEYCLOAK)
.propertiesDefinition(JHipsterModulePropertiesDefinition.builder().addIndentation().build())
.apiDoc("Vue", "Add OAuth2 Keycloak authentication to Vue")
.organization(JHipsterModuleOrganization.builder().addDependency(VUE_CORE).build())
.tags("client", "vue", "auth", "oauth2", "keycloak")
.factory(oauth2Keycloak::buildModule);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@tech.jhipster.lite.BusinessContext
package tech.jhipster.lite.generator.client.vue.security.oauth2_keycloak;
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ public enum JHLiteModuleSlug implements JHipsterModuleSlugFactory {
SVELTE_CORE("svelte-core"),
TYPESCRIPT("typescript"),
VUE_CORE("vue-core"),
VUE_OAUTH2_KEYCLOAK("vue-oauth2-keycloak"),
VUE_PINIA("vue-pinia"),
TS_PAGINATION_DOMAIN("ts-pagination-domain"),
TS_REST_PAGINATION("ts-rest-pagination");
Expand Down
Loading

0 comments on commit 0775206

Please sign in to comment.