Skip to content

Commit e99fed8

Browse files
authored
chore: add spotless with google-java-format (#178)
Adopts the same formatting setup as testcontainers-ory-hydra: spotless with google-java-format (plus formatAnnotations, removeUnusedImports, whitespace hygiene) for Java and ktlint for the Gradle Kotlin scripts, enforced through the existing CI build via spotlessCheck. Includes the one-time whole-codebase reformat.
1 parent 99d2809 commit e99fed8

24 files changed

Lines changed: 768 additions & 810 deletions

gradle/libs.versions.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
[versions]
22
foojayResolver = "0.9.0"
3+
googleJavaFormat = "1.34.1"
34
freefairLombok = "9.5.0"
45
hydraClient = "26.2.0"
56
javaJwt = "4.5.2"
67
playwright = "1.61.0"
78
springBoot = "4.1.0"
9+
spotless = "8.7.0"
810
springDependencyManagement = "1.1.7"
911
# Default pin. CI overrides this via -PtestcontainersOryHydraVersion (see settings.gradle.kts);
1012
# do not inline this version back into build.gradle.kts.
@@ -21,4 +23,5 @@ spring-dependency-management-plugin = { module = "io.spring.gradle:dependency-ma
2123
testcontainers-ory-hydra = { module = "com.ardetrick.testcontainers:testcontainers-ory-hydra", version.ref = "testcontainersOryHydra" }
2224

2325
[plugins]
26+
spotless = { id = "com.diffplug.spotless", version.ref = "spotless" }
2427
spring-boot = { id = "org.springframework.boot", version.ref = "springBoot" }

reference-app/build.gradle.kts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,22 @@
11
plugins {
22
alias(libs.plugins.spring.boot)
3+
alias(libs.plugins.spotless)
34
id("project.java-conventions")
45
}
56

7+
spotless {
8+
java {
9+
googleJavaFormat(libs.versions.googleJavaFormat.get())
10+
formatAnnotations()
11+
removeUnusedImports()
12+
trimTrailingWhitespace()
13+
endWithNewline()
14+
}
15+
kotlinGradle {
16+
ktlint()
17+
}
18+
}
19+
620
dependencies {
721
implementation("org.springframework.boot:spring-boot-starter-freemarker")
822
implementation("org.springframework.boot:spring-boot-starter-jersey")

reference-app/src/main/java/com/ardetrick/oryhydrareference/OryHydraReferenceApplication.java

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,15 @@
1010
@SpringBootApplication
1111
public class OryHydraReferenceApplication {
1212

13-
public static void main(String[] args) {
14-
SpringApplication.run(OryHydraReferenceApplication.class, args);
15-
}
16-
17-
@Bean
18-
SecurityFilterChain securityFilterChain(HttpSecurity http) {
19-
// Don't do this in production. This was removed to simplify this reference implementation.
20-
http
21-
.csrf(AbstractHttpConfigurer::disable)
22-
.authorizeHttpRequests(auth -> auth.anyRequest()
23-
.permitAll());
24-
return http.build();
25-
}
13+
public static void main(String[] args) {
14+
SpringApplication.run(OryHydraReferenceApplication.class, args);
15+
}
2616

17+
@Bean
18+
SecurityFilterChain securityFilterChain(HttpSecurity http) {
19+
// Don't do this in production. This was removed to simplify this reference implementation.
20+
http.csrf(AbstractHttpConfigurer::disable)
21+
.authorizeHttpRequests(auth -> auth.anyRequest().permitAll());
22+
return http.build();
23+
}
2724
}

reference-app/src/main/java/com/ardetrick/oryhydrareference/consent/ConsentController.java

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,35 @@
1515
@RequiredArgsConstructor
1616
public class ConsentController {
1717

18-
@NonNull ConsentService consentService;
19-
20-
/**
21-
* The Consent Endpoint (set by urls.consent) is an application written by you. You can find an exemplary Node.js reference implementation on our GitHub.
22-
* The Consent Endpoint uses the consent_challenge value in the URL to fetch information about the consent request by making a HTTP GET request to:
23-
* http(s)://<HYDRA_ADMIN_URL>/oauth2/auth/requests/consent?consent_challenge=<challenge>
24-
* The response (see below in "Consent Challenge Response" tab) contains information about the consent request. The body contains a skip value. If the value is false, the user interface must be shown. If skip is true, you shouldn't show the user interface but instead just accept or reject the consent request! For more details about the implementation check the "Implementing the Consent Endpoint" Guide.
25-
* <p>
26-
* <a href="https://www.ory.sh/docs/hydra/guides/login">...</a>
27-
* <a href="https://www.ory.sh/docs/hydra/concepts/consent">...</a>
28-
*/
29-
@GetMapping
30-
public ModelAndView consentEndpoint(@RequestParam("consent_challenge") final String consentChallenge) {
31-
val response = consentService.processInitialConsentRequest(consentChallenge);
32-
33-
return ConsentModelAndViewMapper.map(response);
34-
}
35-
36-
@PostMapping
37-
public ModelAndView submitConsentForm(final ConsentForm consentForm) {
38-
val consentResponse = consentService.processConsentForm(consentForm);
39-
40-
return ConsentModelAndViewMapper.map(consentResponse);
41-
}
42-
18+
@NonNull ConsentService consentService;
19+
20+
/**
21+
* The Consent Endpoint (set by urls.consent) is an application written by you. You can find an
22+
* exemplary Node.js reference implementation on our GitHub. The Consent Endpoint uses the
23+
* consent_challenge value in the URL to fetch information about the consent request by making a
24+
* HTTP GET request to:
25+
* http(s)://<HYDRA_ADMIN_URL>/oauth2/auth/requests/consent?consent_challenge=<challenge> The
26+
* response (see below in "Consent Challenge Response" tab) contains information about the consent
27+
* request. The body contains a skip value. If the value is false, the user interface must be
28+
* shown. If skip is true, you shouldn't show the user interface but instead just accept or reject
29+
* the consent request! For more details about the implementation check the "Implementing the
30+
* Consent Endpoint" Guide.
31+
*
32+
* <p><a href="https://www.ory.sh/docs/hydra/guides/login">...</a> <a
33+
* href="https://www.ory.sh/docs/hydra/concepts/consent">...</a>
34+
*/
35+
@GetMapping
36+
public ModelAndView consentEndpoint(
37+
@RequestParam("consent_challenge") final String consentChallenge) {
38+
val response = consentService.processInitialConsentRequest(consentChallenge);
39+
40+
return ConsentModelAndViewMapper.map(response);
41+
}
42+
43+
@PostMapping
44+
public ModelAndView submitConsentForm(final ConsentForm consentForm) {
45+
val consentResponse = consentService.processConsentForm(consentForm);
46+
47+
return ConsentModelAndViewMapper.map(consentResponse);
48+
}
4349
}

reference-app/src/main/java/com/ardetrick/oryhydrareference/consent/ConsentForm.java

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,18 @@
22

33
import java.util.List;
44

5-
/**
6-
* The Java object representing the data submitted by the form in consent.ftlh.
7-
*/
5+
/** The Java object representing the data submitted by the form in consent.ftlh. */
86
public record ConsentForm(
9-
String submit,
10-
String consentChallenge,
11-
Boolean remember,
12-
List<String> scopes
13-
) {
7+
String submit, String consentChallenge, Boolean remember, List<String> scopes) {
148

15-
/**
16-
* This field is implemented in HTML as a checkbox. When a checkbox element is not checked in a form
17-
* it is not submitted in the form at all. This provides a useful null safe getter.
18-
*/
19-
public boolean isRemember() {
20-
if (remember == null) {
21-
return false;
22-
}
23-
return remember;
9+
/**
10+
* This field is implemented in HTML as a checkbox. When a checkbox element is not checked in a
11+
* form it is not submitted in the form at all. This provides a useful null safe getter.
12+
*/
13+
public boolean isRemember() {
14+
if (remember == null) {
15+
return false;
2416
}
25-
17+
return remember;
18+
}
2619
}

reference-app/src/main/java/com/ardetrick/oryhydrareference/consent/ConsentModelAndViewMapper.java

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,25 @@
99

1010
public class ConsentModelAndViewMapper {
1111

12-
public static ModelAndView map(@NonNull final ConsentResponse response) {
13-
return switch (response) {
14-
case Skip r -> handleSkip(r);
15-
case DisplayUI r -> handleDisplayUI(r);
16-
case Accepted r -> handleAccepted(r);
17-
};
18-
}
12+
public static ModelAndView map(@NonNull final ConsentResponse response) {
13+
return switch (response) {
14+
case Skip r -> handleSkip(r);
15+
case DisplayUI r -> handleDisplayUI(r);
16+
case Accepted r -> handleAccepted(r);
17+
};
18+
}
1919

20-
private static ModelAndView handleSkip(Skip consentResponseSkip) {
21-
return ModelAndViewUtils.redirectToDifferentContext(consentResponseSkip.redirectTo());
22-
}
20+
private static ModelAndView handleSkip(Skip consentResponseSkip) {
21+
return ModelAndViewUtils.redirectToDifferentContext(consentResponseSkip.redirectTo());
22+
}
2323

24-
private static ModelAndView handleDisplayUI(DisplayUI displayUI) {
25-
return new ModelAndView("consent")
26-
.addObject("consentChallenge", displayUI.consentChallenge())
27-
.addObject("scopes", displayUI.requestedScopes());
28-
}
29-
30-
private static ModelAndView handleAccepted(Accepted accepted) {
31-
return ModelAndViewUtils.redirectToDifferentContext(accepted.redirectTo());
32-
}
24+
private static ModelAndView handleDisplayUI(DisplayUI displayUI) {
25+
return new ModelAndView("consent")
26+
.addObject("consentChallenge", displayUI.consentChallenge())
27+
.addObject("scopes", displayUI.requestedScopes());
28+
}
3329

30+
private static ModelAndView handleAccepted(Accepted accepted) {
31+
return ModelAndViewUtils.redirectToDifferentContext(accepted.redirectTo());
32+
}
3433
}

reference-app/src/main/java/com/ardetrick/oryhydrareference/consent/ConsentResponse.java

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,13 @@
22

33
import java.util.List;
44

5-
public sealed interface ConsentResponse permits
6-
ConsentResponse.Accepted,
7-
ConsentResponse.DisplayUI,
8-
ConsentResponse.Skip {
5+
public sealed interface ConsentResponse
6+
permits ConsentResponse.Accepted, ConsentResponse.DisplayUI, ConsentResponse.Skip {
97

10-
record Skip(String redirectTo) implements ConsentResponse {}
8+
record Skip(String redirectTo) implements ConsentResponse {}
119

12-
record DisplayUI(List<String> requestedScopes,
13-
String consentChallenge) implements ConsentResponse { }
14-
15-
record Accepted(String redirectTo) implements ConsentResponse { }
10+
record DisplayUI(List<String> requestedScopes, String consentChallenge)
11+
implements ConsentResponse {}
1612

13+
record Accepted(String redirectTo) implements ConsentResponse {}
1714
}

reference-app/src/main/java/com/ardetrick/oryhydrareference/consent/ConsentService.java

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -17,40 +17,41 @@
1717
@FieldDefaults(level = AccessLevel.PRIVATE, makeFinal = true)
1818
public class ConsentService {
1919

20-
@NonNull HydraAdminClient hydraAdminClient;
21-
22-
public ConsentResponse processInitialConsentRequest(@NonNull final String consentChallenge) {
23-
val consentRequest = hydraAdminClient.getConsentRequest(consentChallenge);
24-
25-
if (Boolean.TRUE.equals(consentRequest.getSkip())) {
26-
val acceptConsentRequest = AcceptConsentRequest.builder()
27-
.consentChallenge(consentChallenge)
28-
.remember(true)
29-
.grantAccessTokenAudience(consentRequest.getRequestedAccessTokenAudience())
30-
.scopes(consentRequest.getRequestedScope())
31-
.build();
32-
val acceptConsentResponse = hydraAdminClient.acceptConsentRequest(acceptConsentRequest);
33-
return new Skip(acceptConsentResponse.getRedirectTo());
34-
}
35-
36-
return new DisplayUI(consentRequest.getRequestedScope(), consentChallenge);
20+
@NonNull HydraAdminClient hydraAdminClient;
21+
22+
public ConsentResponse processInitialConsentRequest(@NonNull final String consentChallenge) {
23+
val consentRequest = hydraAdminClient.getConsentRequest(consentChallenge);
24+
25+
if (Boolean.TRUE.equals(consentRequest.getSkip())) {
26+
val acceptConsentRequest =
27+
AcceptConsentRequest.builder()
28+
.consentChallenge(consentChallenge)
29+
.remember(true)
30+
.grantAccessTokenAudience(consentRequest.getRequestedAccessTokenAudience())
31+
.scopes(consentRequest.getRequestedScope())
32+
.build();
33+
val acceptConsentResponse = hydraAdminClient.acceptConsentRequest(acceptConsentRequest);
34+
return new Skip(acceptConsentResponse.getRedirectTo());
3735
}
3836

39-
public ConsentResponse processConsentForm(@NonNull final ConsentForm consentForm) {
40-
val consentChallenge = consentForm.consentChallenge();
37+
return new DisplayUI(consentRequest.getRequestedScope(), consentChallenge);
38+
}
4139

42-
val consentRequest = hydraAdminClient.getConsentRequest(consentChallenge);
40+
public ConsentResponse processConsentForm(@NonNull final ConsentForm consentForm) {
41+
val consentChallenge = consentForm.consentChallenge();
4342

44-
val acceptConsentRequest = AcceptConsentRequest.builder()
45-
.consentChallenge(consentChallenge)
46-
.remember(consentForm.isRemember())
47-
.grantAccessTokenAudience(consentRequest.getRequestedAccessTokenAudience())
48-
.scopes(consentForm.scopes())
49-
.build();
43+
val consentRequest = hydraAdminClient.getConsentRequest(consentChallenge);
5044

51-
val oauth2RedirectTo = hydraAdminClient.acceptConsentRequest(acceptConsentRequest);
45+
val acceptConsentRequest =
46+
AcceptConsentRequest.builder()
47+
.consentChallenge(consentChallenge)
48+
.remember(consentForm.isRemember())
49+
.grantAccessTokenAudience(consentRequest.getRequestedAccessTokenAudience())
50+
.scopes(consentForm.scopes())
51+
.build();
5252

53-
return new Accepted(oauth2RedirectTo.getRedirectTo());
54-
}
53+
val oauth2RedirectTo = hydraAdminClient.acceptConsentRequest(acceptConsentRequest);
5554

55+
return new Accepted(oauth2RedirectTo.getRedirectTo());
56+
}
5657
}

reference-app/src/main/java/com/ardetrick/oryhydrareference/demo/DemoController.java

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@
1515
import org.springframework.web.servlet.ModelAndView;
1616

1717
/**
18-
* This controller is for demonstration purposes only to make it easier to run through the OAuth2 flow and interact with
19-
* Hydra clients. It is not required nor expected to be used for any production use cases.
18+
* This controller is for demonstration purposes only to make it easier to run through the OAuth2
19+
* flow and interact with Hydra clients. It is not required nor expected to be used for any
20+
* production use cases.
2021
*/
2122
@Slf4j
2223
@RequiredArgsConstructor
@@ -25,18 +26,19 @@
2526
@FieldDefaults(level = AccessLevel.PRIVATE, makeFinal = true)
2627
public class DemoController {
2728

28-
@NonNull HydraAdminClient hydraAdminClient;
29+
@NonNull HydraAdminClient hydraAdminClient;
2930

30-
@GetMapping(produces = {MediaType.TEXT_HTML_VALUE})
31-
@ResponseBody
32-
public ModelAndView test() {
33-
val clients = hydraAdminClient.listOAuth2Clients()
34-
.stream()
35-
.map(client -> new MvcClient(client.getClientName(), client.getClientId(), client.getRedirectUris()))
36-
.toList();
37-
38-
return new ModelAndView("demo")
39-
.addObject("clients", clients);
40-
}
31+
@GetMapping(produces = {MediaType.TEXT_HTML_VALUE})
32+
@ResponseBody
33+
public ModelAndView test() {
34+
val clients =
35+
hydraAdminClient.listOAuth2Clients().stream()
36+
.map(
37+
client ->
38+
new MvcClient(
39+
client.getClientName(), client.getClientId(), client.getRedirectUris()))
40+
.toList();
4141

42+
return new ModelAndView("demo").addObject("clients", clients);
43+
}
4244
}
Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
package com.ardetrick.oryhydrareference.hydra;
22

3+
import java.util.List;
34
import lombok.Builder;
45
import lombok.NonNull;
56

6-
import java.util.List;
7-
87
@Builder
9-
public record AcceptConsentRequest(@NonNull String consentChallenge,
10-
boolean remember,
11-
List<String> grantAccessTokenAudience,
12-
List<String> scopes
13-
) {}
8+
public record AcceptConsentRequest(
9+
@NonNull String consentChallenge,
10+
boolean remember,
11+
List<String> grantAccessTokenAudience,
12+
List<String> scopes) {}

0 commit comments

Comments
 (0)