Skip to content

Commit

Permalink
adjust /authenticate api
Browse files Browse the repository at this point in the history
  • Loading branch information
mshima committed Aug 23, 2024
1 parent eeb62e5 commit feaa9b8
Show file tree
Hide file tree
Showing 14 changed files with 90 additions and 102 deletions.
3 changes: 3 additions & 0 deletions .github/labeler.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,10 @@
- changed-files:
- any-glob-to-any-file:
- generators/common/**
- generators/java/**/*
- generators/liquibase/**/*
- generators/server/**/*
- generators/spring-*/**/*

'theme: upgrade':
- changed-files:
Expand Down
9 changes: 9 additions & 0 deletions generators/app/__snapshots__/generator.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,7 @@ exports[`generator - app with default config should match snapshot 1`] = `
],
"liquibaseAddH2Properties": false,
"liquibaseDefaultSchemaName": "",
"listOrFlux": "List",
"lowercaseBaseName": "jhipster",
"mainClass": "JhipsterApp",
"mainJavaDir": "src/main/java/",
Expand Down Expand Up @@ -682,6 +683,8 @@ exports[`generator - app with default config should match snapshot 1`] = `
},
"nodePackageManager": "npm",
"nodeVersion": "NODE_VERSION",
"optionalOrMono": "Optional",
"optionalOrMonoOfNullable": "Optional.ofNullable",
"packageFolder": "com/mycompany/myapp/",
"packageInfoJavadocs": [
{
Expand Down Expand Up @@ -1197,6 +1200,7 @@ exports[`generator - app with gateway should match snapshot 1`] = `
],
"liquibaseAddH2Properties": false,
"liquibaseDefaultSchemaName": "",
"listOrFlux": "Flux",
"lowercaseBaseName": "jhipster",
"mainClass": "JhipsterApp",
"mainJavaDir": "src/main/java/",
Expand Down Expand Up @@ -1294,6 +1298,8 @@ exports[`generator - app with gateway should match snapshot 1`] = `
},
"nodePackageManager": "npm",
"nodeVersion": "NODE_VERSION",
"optionalOrMono": "Mono",
"optionalOrMonoOfNullable": "justOrEmpty",
"packageFolder": "com/mycompany/myapp/",
"packageInfoJavadocs": [
{
Expand Down Expand Up @@ -1815,6 +1821,7 @@ exports[`generator - app with microservice should match snapshot 1`] = `
],
"liquibaseAddH2Properties": false,
"liquibaseDefaultSchemaName": "",
"listOrFlux": "List",
"lowercaseBaseName": "jhipster",
"mainClass": "JhipsterApp",
"mainJavaDir": "src/main/java/",
Expand Down Expand Up @@ -1853,6 +1860,8 @@ exports[`generator - app with microservice should match snapshot 1`] = `
},
"nodePackageManager": "npm",
"nodeVersion": "NODE_VERSION",
"optionalOrMono": "Optional",
"optionalOrMonoOfNullable": "Optional.ofNullable",
"packageFolder": "com/mycompany/myapp/",
"packageInfoJavadocs": [
{
Expand Down
5 changes: 5 additions & 0 deletions generators/base-application/types/entity.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,11 @@ type Entity = Required<BaseEntity> &
*/
anyFieldHasFileBasedContentType: boolean;

/**
* Any relationship is required or id
*/
anyRelationshipIsRequired: boolean;

dtoMapstruct: boolean;
};

Expand Down
3 changes: 3 additions & 0 deletions generators/bootstrap-application-base/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,9 @@ export default class BootstrapApplicationBase extends BaseApplicationGenerator {

get postPreparingEachEntity() {
return this.asPostPreparingEachEntityTaskGroup({
hasRequiredRelationship({ entity }) {
entity.anyRelationshipIsRequired = entity.relationships.some(rel => rel.relationshipRequired || rel.id);
},
checkForCircularRelationships({ entity }) {
const detectCyclicRequiredRelationship = (entity, relatedEntities) => {
if (relatedEntities.has(entity)) return true;
Expand Down
3 changes: 3 additions & 0 deletions generators/bootstrap-application/generator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ describe(`generator - ${generator}`, () => {
"anyFieldIsUUID": true,
"anyFieldIsZonedDateTime": false,
"anyPropertyHasValidation": true,
"anyRelationshipIsRequired": false,
"applicationType": "monolith",
"auditableEntity": true,
"authenticationType": "jwt",
Expand Down Expand Up @@ -1089,6 +1090,7 @@ describe(`generator - ${generator}`, () => {
"anyFieldIsUUID": true,
"anyFieldIsZonedDateTime": false,
"anyPropertyHasValidation": false,
"anyRelationshipIsRequired": false,
"applicationType": "monolith",
"authenticationType": "jwt",
"baseName": "jhipster",
Expand Down Expand Up @@ -1432,6 +1434,7 @@ describe(`generator - ${generator}`, () => {
"anyFieldIsUUID": true,
"anyFieldIsZonedDateTime": false,
"anyPropertyHasValidation": false,
"anyRelationshipIsRequired": false,
"applicationType": "monolith",
"authenticationType": "jwt",
"baseName": "jhipster",
Expand Down
7 changes: 7 additions & 0 deletions generators/java/generators/bootstrap/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,13 @@ export default class BootstrapGenerator extends BaseApplicationGenerator {
...editFileCallback,
);
},
imperativeOrReactive({ applicationDefaults }) {
applicationDefaults({
optionalOrMono: ({ reactive }) => (reactive ? 'Mono' : 'Optional'),
optionalOrMonoOfNullable: ({ reactive }) => (reactive ? 'justOrEmpty' : 'Optional.ofNullable'),
listOrFlux: ({ reactive }) => (reactive ? 'Flux' : 'List'),
});
},
});
}

Expand Down
3 changes: 3 additions & 0 deletions generators/server/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ export type SpringBootApplication = JavaApplication &
skipCheckLengthOfIdentifier: boolean;

imperativeOrReactive: string;
optionalOrMono: string;
optionalOrMonoOfNullable: string;
listOrFlux: string;
generateAuthenticationApi?: boolean;
generateInMemoryUserCredentials?: boolean;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@
-%>
package <%= packageName %>.web.rest;

<%_ if (authenticationTypeSession && !reactive) { _%>
<%_ if (authenticationTypeSession && !reactive) { _%>
import <%= packageName %>.domain.PersistentToken;
import <%= packageName %>.repository.PersistentTokenRepository;
<%_ } _%>
<%_ if (!reactive) { _%>
<%_ } _%>
<%_ if (!reactive) { _%>
import <%= user.entityAbsoluteClass %>;
<%_ } _%>
<%_ } _%>
import <%= packageName %>.repository.UserRepository;
import <%= packageName %>.security.SecurityUtils;
import <%= packageName %>.service.MailService;
Expand All @@ -35,31 +35,23 @@ import <%= packageName %>.web.rest.errors.*;
import <%= packageName %>.web.rest.vm.KeyAndPasswordVM;
import <%= packageName %>.web.rest.vm.ManagedUserVM;

import java.security.Principal;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
<%_ if (reactive) { _%>
import org.springframework.web.server.ServerWebExchange;
<%_ if (reactive) { _%>
import java.util.Objects;
import reactor.core.publisher.Mono;
<%_ } _%>

<%_ if (!reactive && !authenticationTypeJwt) { _%>
import jakarta.servlet.http.HttpServletRequest;
<%_ } _%>
<%_ } else { _%>
import java.util.*;
<%_ } _%>
import jakarta.validation.Valid;
<%_ if (authenticationTypeSession && !reactive) { _%>
<%_ if (authenticationTypeSession && !reactive) { _%>
import java.nio.charset.StandardCharsets;
import java.net.URLDecoder;
<%_ } _%>
<%_ if (reactive) { _%>
import java.security.Principal;
import java.util.Objects;
<%_ } _%>
<%_ if (!reactive) { _%>
import java.util.*;
<%_ } _%>
<%_ } _%>

/**
* REST controller for managing the current user's account.
Expand Down Expand Up @@ -144,19 +136,13 @@ public class AccountResource {
/**
* {@code GET /authenticate} : check if the user is authenticated, and return its login.
*
* @param request the HTTP request.
* @param principal the authentication principal.
* @return the login if the user is authenticated.
*/
@GetMapping("/authenticate")
<%_ if (reactive) { _%>
public Mono<String> isAuthenticated(ServerWebExchange request) {
public <%- optionalOrMono %><String> isAuthenticated(Principal principal) {
LOG.debug("REST request to check if the current user is authenticated");
return request.getPrincipal().map(Principal::getName);
<%_ } else { _%>
public String isAuthenticated(HttpServletRequest request) {
LOG.debug("REST request to check if the current user is authenticated");
return request.getRemoteUser();
<%_ } _%>
return <%- optionalOrMonoOfNullable %>(principal).map(Principal::getName);
}

<%_ } _%>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,19 @@ import <%= packageName %>.security.SecurityUtils;
import <%= packageName %>.service.UserService;
import <%= packageName %>.service.dto.<%= user.adminUserDto %>;

import java.security.Principal;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.authentication.AbstractAuthenticationToken;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
<%_ if (reactive) { _%>
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
<%_ } else { _%>
import jakarta.servlet.http.HttpServletRequest;
import java.util.Optional;
<%_ } _%>

import java.security.Principal;

/**
* REST controller for managing the current user's account.
*/
Expand Down Expand Up @@ -82,18 +80,12 @@ public class AccountResource {
/**
* {@code GET /authenticate} : check if the user is authenticated, and return its login.
*
* @param request the HTTP request.
* @param principal the authentication principal.
* @return the login if the user is authenticated.
*/
@GetMapping("/authenticate")
<%_ if (reactive) { _%>
public Mono<String> isAuthenticated(ServerWebExchange request) {
public <%- optionalOrMono %><String> isAuthenticated(Principal principal) {
LOG.debug("REST request to check if the current user is authenticated");
return request.getPrincipal().map(Principal::getName);
<%_ } else { _%>
public String isAuthenticated(HttpServletRequest request) {
LOG.debug("REST request to check if the current user is authenticated");
return request.getRemoteUser();
<%_ } _%>
return <%- optionalOrMonoOfNullable %>(principal).map(Principal::getName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,21 @@ import <%= packageName %>.security.SecurityUtils;
import <%= packageName %>.domain.Authority;

<%_ } _%>

import java.security.Principal;
<%_ if (reactive) { _%>
import reactor.core.publisher.Mono;
<%_ } else { _%>
import jakarta.servlet.http.HttpServletRequest;
import java.util.Optional;
<%_ } _%>

<%_ if (authenticationTypeOauth2) { _%>
import org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken;
<%_ if (reactive) { _%>
import org.springframework.web.server.ServerWebExchange;
<%_ } _%>
<%_ } _%>
<%_ if (authenticationTypeOauth2 || authenticationTypeJwt) { _%>
import java.security.Principal;
import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationToken;
import org.springframework.security.authentication.AbstractAuthenticationToken;
<%_ } else { _%>
<%_ if (reactive) { _%>
import java.security.Principal;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.security.core.context.ReactiveSecurityContextHolder;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.userdetails.UserDetails;
Expand Down Expand Up @@ -147,19 +142,13 @@ public class AccountResource {
/**
* {@code GET /authenticate} : check if the user is authenticated, and return its login.
*
* @param request the HTTP request.
* @param principal the authentication principal.
* @return the login if the user is authenticated.
*/
@GetMapping("/authenticate")
<%_ if (reactive) { _%>
public Mono<String> isAuthenticated(ServerWebExchange request) {
public <%- optionalOrMono %><String> isAuthenticated(Principal principal) {
LOG.debug("REST request to check if the current user is authenticated");
return request.getPrincipal().map(Principal::getName);
<%_ } else { _%>
public String isAuthenticated(HttpServletRequest request) {
LOG.debug("REST request to check if the current user is authenticated");
return request.getRemoteUser();
<%_ } _%>
return <%- optionalOrMonoOfNullable %>(principal).map(Principal::getName);
}
<%_ } _%>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,12 @@ import org.springframework.security.oauth2.jwt.JwtClaimsSet;
import org.springframework.security.oauth2.jwt.JwtEncoder;
import org.springframework.security.oauth2.jwt.JwtEncoderParameters;
import org.springframework.web.bind.annotation.*;
<%_ if (reactive) { _%>
import java.security.Principal;
<%_ if (reactive) { _%>
import reactor.core.publisher.Mono;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.security.authentication.ReactiveAuthenticationManager;
<%_ } else { _%>
import jakarta.servlet.http.HttpServletRequest;
import java.util.Optional;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.core.context.SecurityContextHolder;
<%_ } _%>
Expand Down Expand Up @@ -120,19 +119,13 @@ public class AuthenticateController {
/**
* {@code GET /authenticate} : check if the user is authenticated, and return its login.
*
* @param request the HTTP request.
* @param principal the authentication principal.
* @return the login if the user is authenticated.
*/
@GetMapping("/authenticate")
<%_ if (reactive) { _%>
public Mono<String> isAuthenticated(ServerWebExchange request) {
public <%- optionalOrMono %><String> isAuthenticated(Principal principal) {
LOG.debug("REST request to check if the current user is authenticated");
return request.getPrincipal().map(Principal::getName);
<%_ } else { _%>
public String isAuthenticated(HttpServletRequest request) {
LOG.debug("REST request to check if the current user is authenticated");
return request.getRemoteUser();
<%_ } _%>
return <%- optionalOrMonoOfNullable %>(principal).map(Principal::getName);
}

public String createToken(Authentication authentication, boolean rememberMe) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,6 @@ import org.springframework.transaction.annotation.Transactional;
import reactor.core.publisher.Mono;
<%_ } _%>

<%_ if (databaseTypeSql && !reactive) { _%>
import jakarta.persistence.EntityManager;
<%_ } _%>
<%_ if (databaseTypeCassandra || databaseTypeCouchbase) { _%>
import java.util.stream.Stream;
<%_ } _%>
Expand Down Expand Up @@ -130,11 +127,6 @@ class PublicUserResourceIT {
@Autowired
private UserSearchRepository mockUserSearchRepository;
<%_ } _%>
<%_ if (databaseTypeSql) { _%>

@Autowired
private EntityManager em;
<%_ } _%>
<%_ if (cacheProviderAny) { _%>

@Autowired
Expand Down Expand Up @@ -168,7 +160,7 @@ class PublicUserResourceIT {
<%_ } _%>
@BeforeEach
public void initTest() {
user = UserResourceIT.initTestUser(<% if (databaseTypeSql) { %>em<% } %>);
user = UserResourceIT.initTestUser();
}

@AfterEach
Expand Down
Loading

0 comments on commit feaa9b8

Please sign in to comment.