-
-
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.
- Loading branch information
Showing
34 changed files
with
1,219 additions
and
8 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
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 | ||
} | ||
} |
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
20 changes: 20 additions & 0 deletions
20
...oot/mvc/security/oauth2/account/main/application/AccountsApplicationService.java.mustache
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,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(); | ||
} | ||
} |
154 changes: 154 additions & 0 deletions
154
...generator/server/springboot/mvc/security/oauth2/account/main/domain/Account.java.mustache
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,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(); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
...erver/springboot/mvc/security/oauth2/account/main/domain/AccountsRepository.java.mustache
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,7 @@ | ||
package {{packageName}}.account.domain; | ||
|
||
import java.util.Optional; | ||
|
||
public interface AccountsRepository { | ||
Optional<Account> authenticatedUserAccount(); | ||
} |
36 changes: 36 additions & 0 deletions
36
...ot/mvc/security/oauth2/account/main/infrastructure/primary/AccountsResource.java.mustache
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,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)); | ||
} | ||
} |
Oops, something went wrong.