Skip to content

Commit

Permalink
Add oauth2 account
Browse files Browse the repository at this point in the history
  • Loading branch information
DamnClin committed Jun 15, 2022
1 parent 86141d4 commit 48e76e9
Show file tree
Hide file tree
Showing 34 changed files with 1,219 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,25 @@
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 OAuth2ModuleFactory factory;
private final OAuth2ModuleFactory oAuth2factory;
private final OAuth2AccountModuleFactory acountsFactory;

public OAuth2SecurityApplicationService(DockerImages dockerImages) {
factory = new OAuth2ModuleFactory(dockerImages);
oAuth2factory = new OAuth2ModuleFactory(dockerImages);
acountsFactory = new OAuth2AccountModuleFactory();
}

public JHipsterModule buildOAuth2Module(JHipsterModuleProperties properties) {
return factory.buildOAuth2Module(properties);
return oAuth2factory.buildModule(properties);
}

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
Expand Up @@ -38,7 +38,7 @@ public OAuth2ModuleFactory(DockerImages dockerImages) {
this.dockerImages = dockerImages;
}

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

//@formatter:off
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,15 @@ JHipsterModuleResource oAuth2Module(OAuth2SecurityApplicationService oAuth2) {
)
.factory(oAuth2::buildOAuth2Module);
}
//TODO: account management module

@Bean
JHipsterModuleResource oAuth2AccountModule(OAuth2SecurityApplicationService oAuth2) {
return JHipsterModuleResource
.builder()
.legacyUrl("/api/servers/spring-boot/security-systems/oauth2/account")
.slug("springboot-oauth2-account")
.propertiesDefinition(JHipsterModulePropertiesDefinition.builder().addBasePackage().build())
.apiDoc(new JHipsterModuleApiDoc("Spring Boot - MVC - Security", "Add a account context for OAuth 2.0 / OIDC Authentication"))
.factory(oAuth2::buildOAuth2AccountModule);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package {{packageName}}.account.application;

import {{packageName}}.account.domain.Account;
import {{packageName}}.account.domain.AccountsRepository;
import java.util.Optional;
import org.springframework.stereotype.Service;

@Service
public class AccountsApplicationService {
private final AccountsRepository accounts;
public AccountsApplicationService(AccountsRepository accounts) {
this.accounts = accounts;
}

public Optional<Account> authenticatedUserAccount() {
return accounts.authenticatedUserAccount();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
package {{packageName}}.account.domain;

import java.util.Collection;
import java.util.stream.Collectors;

import {{packageName}}.authentication.domain.Role;
import {{packageName}}.authentication.domain.Roles;
import {{packageName}}.authentication.domain.Username;
import {{packageName}}.error.domain.Assert;
import {{packageName}}.useridentity.domain.Email;
import {{packageName}}.useridentity.domain.Firstname;
import {{packageName}}.useridentity.domain.Lastname;
import {{packageName}}.useridentity.domain.Name;

public class Account {
private final Username username;
private final Name name;
private final Email email;
private final Roles roles;
private Account(UserBuilder builder) {
Assert.notNull("username", builder.username);
Assert.notNull("firstname", builder.firstname);
Assert.notNull("lastname", builder.lastname);
Assert.notNull("roles", builder.roles);
username = builder.username;
name = new Name(builder.firstname, builder.lastname);
email = builder.email;
roles = builder.roles;
}

public static AccountUsernameBuilder builder() {
return new UserBuilder();
}

public Username username() {
return username;
}

public Name name() {
return name;
}

public Email email() {
return email;
}

public Roles roles() {
return roles;
}

public static class UserBuilder implements AccountUsernameBuilder, AccountFirstnameBuilder, AccountLastnameBuilder,
AccountEmailBuilder, AccountRolesBuilder, AccountOptionalFieldBuilder {
private Username username;
private Firstname firstname;
private Lastname lastname;
private Email email;
private Roles roles;
private UserBuilder() {
}

@Override
public AccountFirstnameBuilder username(Username username) {
this.username = username;
return this;
}

@Override
public AccountLastnameBuilder firstname(Firstname firstname) {
this.firstname = firstname;
return this;
}

@Override
public AccountEmailBuilder lastname(Lastname lastname) {
this.lastname = lastname;
return this;
}

@Override
public AccountRolesBuilder email(Email email) {
this.email = email;
return this;
}

@Override
public AccountOptionalFieldBuilder roles(Roles roles) {
this.roles = roles;
return this;
}

@Override
public Account build() {
return new Account(this);
}
}

public interface AccountUsernameBuilder {
AccountFirstnameBuilder username(Username username);
default AccountFirstnameBuilder username(String username) {
return username(new Username(username));
}
}

public interface AccountFirstnameBuilder {
AccountLastnameBuilder firstname(Firstname firstname);
default AccountLastnameBuilder firstname(String firstname) {
return firstname(new Firstname(firstname));
}
}

public interface AccountLastnameBuilder {
AccountEmailBuilder lastname(Lastname lastname);
default AccountEmailBuilder lastname(String lastname) {
return lastname(new Lastname(lastname));
}
}

public interface AccountEmailBuilder {
AccountRolesBuilder email(Email email);
default AccountRolesBuilder email(String email) {
return email(new Email(email));
}
}

public interface AccountRolesBuilder {
AccountOptionalFieldBuilder roles(Roles roles);
default AccountOptionalFieldBuilder roles(Collection<String> roles) {
Assert.notNull("roles", roles);
return roles(new Roles(roles.stream()
.map(Role::from)
.collect(Collectors.toUnmodifiableSet())));
}
}

public interface AccountOptionalFieldBuilder {
Account build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package {{packageName}}.account.domain;

import java.util.Optional;

public interface AccountsRepository {
Optional<Account> authenticatedUserAccount();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package {{packageName}}.account.infrastructure.primary;

import {{packageName}}.account.application.AccountsApplicationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Tag(name = "Accounts")
@RequestMapping("/api")
class AccountsResource {
private final AccountsApplicationService accounts;
public AccountsResource(AccountsApplicationService accounts) {
this.accounts = accounts;
}

@GetMapping("authenticated-user-account")
@Operation(summary = "Get authenticated user account")
@ApiResponse(responseCode = "200", description = "Account for the current user")
@ApiResponse(responseCode = "401", description = "The user is not authenticated")
ResponseEntity<RestAccount> getAuthenticatedUserAccount() {
return accounts
.authenticatedUserAccount()
.map(RestAccount::from)
.map(ResponseEntity::ok)
.orElseGet(() -> new ResponseEntity<>(HttpStatus.UNAUTHORIZED));
}
}
Loading

0 comments on commit 48e76e9

Please sign in to comment.