Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable OAuth2 refresh token. #15424

Merged
merged 7 commits into from
Jun 28, 2021
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions generators/app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,11 @@ module.exports = class JHipsterAppGenerator extends BaseBlueprintGenerator {
type: Boolean,
});

this.option('reactive', {
desc: 'Generate a reactive backend',
type: Boolean,
});

// Just constructing help, stop here
if (this.options.help) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,15 @@ describe('Service Tests', () => {
});

describe('identity', () => {
it('should call /account only once if last call have not returned', () => {
// When I call
service.identity().subscribe();
// Once more
service.identity().subscribe();
// Then there is only request
httpMock.expectOne({ method: 'GET' });
});

it('should call /account only once if not logged out after first authentication and should call /account again if user has logged out', () => {
// Given the user is authenticated
service.identity().subscribe();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import { TrackerService } from '../tracker/tracker.service';
export class AccountService {
private userIdentity: Account | null = null;
private authenticationState = new ReplaySubject<Account | null>(1);
private accountCache$?: Observable<Account | null>;
private accountCache$?: Observable<Account> | null;

constructor(
<%_ if (enableTranslation) { _%>
Expand All @@ -62,6 +62,9 @@ export class AccountService {
authenticate(identity: Account | null): void {
this.userIdentity = identity;
this.authenticationState.next(this.userIdentity);
if (!identity) {
this.accountCache$ = null;
}
<%_ if (communicationSpringWebsocket) { _%>
if (identity) {
this.trackerService.connect();
Expand All @@ -82,29 +85,26 @@ export class AccountService {
}

identity(force?: boolean): Observable<Account | null> {
if (!this.accountCache$ || force || !this.isAuthenticated()) {
if (!this.accountCache$ || force) {
this.accountCache$ = this.fetch().pipe(
catchError(() => of(null)),
tap((account: Account | null) => {
tap((account: Account) => {
this.authenticate(account);

<%_ if (enableTranslation) { _%>
// After retrieve the account info, the language will be changed to
// the user's preferred language configured in the account setting
// unless user have choosed other language in the current session
if (!this.sessionStorageService.retrieve('locale') && account) {
if (!this.sessionStorageService.retrieve('locale')) {
this.translateService.use(account.langKey);
}
<%_ } _%>

if (account) {
this.navigateToStoredUrl();
}
this.navigateToStoredUrl();
}),
shareReplay()
);
}
return this.accountCache$;
return this.accountCache$.pipe(catchError(() => of(null)));
}

isAuthenticated(): boolean {
Expand Down
4 changes: 4 additions & 0 deletions generators/generator-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -2422,6 +2422,10 @@ templates: ${JSON.stringify(existingTemplates, null, 2)}`;
this.jhipsterConfig.microfrontend = options.microfrontend;
}

if (options.reactive !== undefined) {
this.jhipsterConfig.reactive = options.reactive;
}

if (options.clientPackageManager) {
this.jhipsterConfig.clientPackageManager = options.clientPackageManager;
}
Expand Down
29 changes: 29 additions & 0 deletions generators/server/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -1165,6 +1165,35 @@ const serverFiles = {
templates: ['META-INF/services/reactor.blockhound.integration.BlockHoundIntegration'],
},
],
springBootOauth2: [
{
condition: generator => generator.authenticationTypeOauth2 && generator.applicationTypeMonolith,
path: SERVER_MAIN_SRC_DIR,
templates: [
{
file: 'package/config/OAuth2Configuration.java',
renameTo: generator => `${generator.javaDir}config/OAuth2Configuration.java`,
},
],
},
{
condition: generator => generator.authenticationTypeOauth2 && !generator.applicationTypeMicroservice,
path: SERVER_MAIN_SRC_DIR,
templates: [
{
file: generator => `package/web/filter/OAuth2${generator.reactive ? 'Reactive' : ''}RefreshTokensWebFilter.java`,
renameTo: generator => `${generator.javaDir}web/filter/OAuth2${generator.reactive ? 'Reactive' : ''}RefreshTokensWebFilter.java`,
},
],
},
{
condition: generator => generator.authenticationTypeOauth2 && !generator.applicationTypeMicroservice,
path: SERVER_TEST_SRC_DIR,
templates: [
{ file: 'package/test/util/OAuth2TestUtil.java', renameTo: generator => `${generator.testDir}test/util/OAuth2TestUtil.java` },
],
},
],
serverTestFw: [
{
condition: generator => generator.databaseType === 'cassandra',
Expand Down
4 changes: 4 additions & 0 deletions generators/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,8 +251,11 @@ module.exports = class JHipsterServerGenerator extends BaseBlueprintGenerator {
return {
loadSharedConfig() {
this.loadAppConfig();
this.loadDerivedAppConfig();
this.loadClientConfig();
this.loadDerivedClientConfig();
this.loadServerConfig();
this.loadDerivedServerConfig();
this.loadTranslationConfig();
},
};
Expand Down Expand Up @@ -461,6 +464,7 @@ module.exports = class JHipsterServerGenerator extends BaseBlueprintGenerator {
'java:docker': './mvnw -ntp verify -DskipTests jib:dockerBuild',
'backend:unit:test': `./mvnw -ntp -P-webapp verify --batch-mode ${javaCommonLog} ${javaTestLog}`,
'backend:build-cache': './mvnw dependency:go-offline',
'backend:debug': './mvnw -Dspring-boot.run.jvmArguments="-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:8000"',
});
} else if (buildTool === 'gradle') {
const excludeWebapp = this.jhipsterConfig.skipClient ? '' : '-x webapp';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<%_ const reactivePrefix = reactive ? 'Reactive' : '' %>
package <%= packageName %>.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.oauth2.client.<%= reactivePrefix %>OAuth2AuthorizedClientManager;
import org.springframework.security.oauth2.client.registration.<%= reactivePrefix %>ClientRegistrationRepository;
import org.springframework.security.oauth2.client.web.Default<%= reactivePrefix %>OAuth2AuthorizedClientManager;
import org.springframework.security.oauth2.client.web.<%= reactive ? 'server.Server' : '' %>OAuth2AuthorizedClientRepository;

@Configuration
public class OAuth2Configuration {

@Bean
public <%= reactivePrefix %>OAuth2AuthorizedClientManager authorizedClientManager(
<%= reactivePrefix %>ClientRegistrationRepository clientRegistrationRepository,
<%= reactive ? 'Server' : '' %>OAuth2AuthorizedClientRepository authorizedClientRepository
) {
<%= reactivePrefix %>OAuth2AuthorizedClientManager authorizedClientManager = new Default<%= reactivePrefix %>OAuth2AuthorizedClientManager(
clientRegistrationRepository,
authorizedClientRepository
);
return authorizedClientManager;
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,16 @@ public class WebConfigurer implements <% if (!reactive) { %>ServletContextInitia
this.jHipsterProperties = jHipsterProperties;
<%_ if (devDatabaseTypeH2Any && reactive) { _%>
if (env.acceptsProfiles(Profiles.of(JHipsterConstants.SPRING_PROFILE_DEVELOPMENT))) {
H2ConfigurationHelper.initH2Console();
<%_ if (!applicationTypeMonolith) { _%>
try {
<%_ } _%>
H2ConfigurationHelper.initH2Console();
<%_ if (!applicationTypeMonolith) { _%>
} catch (Exception e) {
// Console may already be running on another app.
e.printStackTrace();
};
<%_ } _%>
}
<%_ } _%>
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -999,7 +999,16 @@ public class UserService {
})
<%_ } _%>
.collect(Collectors.toSet()));
return <% if (databaseType !== 'no' && !reactive) { %>new <%= asDto('AdminUser') %>(syncUserWithIdP(attributes, user))<% } else if (!reactive) { %>user<% } %><% if (databaseType === 'no' && reactive) { %>Mono.just(user)<% } else if (reactive) { %>syncUserWithIdP(attributes, user).flatMap(u -> Mono.just(new <%= asDto('AdminUser') %>(u)))<% } %>;

<%_ if (databaseTypeNo) { _%>
return <% if (reactive) { %>Mono.just(user)<% } else { %>user<% } %>;
<%_ } else { _%>
<%_ if (!reactive) { _%>
return new <%= asDto('AdminUser') %>(syncUserWithIdP(attributes, user));
<%_ } else { _%>
return syncUserWithIdP(attributes, user).flatMap(u -> Mono.just(new <%= asDto('AdminUser') %>(u)));
<%_ } _%>
<%_ } _%>
}

private static <%= databaseType === 'no' ? asDto('AdminUser') : asEntity('User') %> getUser(Map<String, Object> details) {
Expand Down
Loading