Skip to content

Commit

Permalink
Format code
Browse files Browse the repository at this point in the history
  • Loading branch information
ch4mpy committed May 16, 2023
1 parent a881cd9 commit 1104a02
Show file tree
Hide file tree
Showing 136 changed files with 4,051 additions and 4,576 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,18 @@
@PreAuthorize("isAuthenticated()")
public class GreetingController {

@GetMapping("/greet")
public MessageDto getGreeting(OAuthentication<OpenidClaimSet> auth) {
return new MessageDto("Hi %s! You are granted with: %s and your email is %s."
.formatted(auth.getName(), auth.getAuthorities(), auth.getClaims().getEmail()));
}
@GetMapping("/greet")
public MessageDto getGreeting(OAuthentication<OpenidClaimSet> auth) {
return new MessageDto(
"Hi %s! You are granted with: %s and your email is %s.".formatted(auth.getName(), auth.getAuthorities(), auth.getClaims().getEmail()));
}

@GetMapping("/nice")
@PreAuthorize("hasAuthority('NICE')")
public MessageDto getNiceGreeting(OAuthentication<OpenidClaimSet> auth) {
return new MessageDto("Dear %s! You are granted with: %s."
.formatted(auth.getName(), auth.getAuthorities()));
}
@GetMapping("/nice")
@PreAuthorize("hasAuthority('NICE')")
public MessageDto getNiceGreeting(OAuthentication<OpenidClaimSet> auth) {
return new MessageDto("Dear %s! You are granted with: %s.".formatted(auth.getName(), auth.getAuthorities()));
}

static record MessageDto(String body) {
}
static record MessageDto(String body) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,19 @@
import io.swagger.v3.oas.annotations.security.OAuthScope;
import io.swagger.v3.oas.annotations.security.SecurityScheme;

@SecurityScheme(name = "authorization-code", type = SecuritySchemeType.OAUTH2, flows = @OAuthFlows(authorizationCode = @OAuthFlow(authorizationUrl = "https://localhost:8443/realms/master/protocol/openid-connect/auth", tokenUrl = "https://localhost:8443/realms/master/protocol/openid-connect/token", scopes = {
@OAuthScope(name = "openid"), @OAuthScope(name = "profile") })))
@SecurityScheme(
name = "authorization-code",
type = SecuritySchemeType.OAUTH2,
flows = @OAuthFlows(
authorizationCode = @OAuthFlow(
authorizationUrl = "https://localhost:8443/realms/master/protocol/openid-connect/auth",
tokenUrl = "https://localhost:8443/realms/master/protocol/openid-connect/token",
scopes = { @OAuthScope(name = "openid"), @OAuthScope(name = "profile") })))
@SpringBootApplication
public class ResourceServerMultitenantDynamicApplication {

public static void main(String[] args) {
SpringApplication.run(ResourceServerMultitenantDynamicApplication.class, args);
}
public static void main(String[] args) {
SpringApplication.run(ResourceServerMultitenantDynamicApplication.class, args);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,41 +19,40 @@
@Import(WebSecurityConfig.class)
class GreetingControllerTest {

@Autowired
MockMvcSupport api;

@Test
@OpenId(authorities = {
"AUTHOR" }, claims = @OpenIdClaims(usernameClaim = StandardClaimNames.PREFERRED_USERNAME, preferredUsername = "Tonton Pirate", email = "[email protected]"))
void givenUserIsAuthenticated_whenGreet_thenOk() throws Exception {
api.get("/greet").andExpect(status().isOk())
.andExpect(jsonPath("$.body").value(
"Hi Tonton Pirate! You are granted with: [AUTHOR] and your email is [email protected]."));
}

@Test
void givenRequestIsAnonymous_whenGreet_thenUnauthorized() throws Exception {
api.get("/greet").andExpect(status().isUnauthorized());
}

@Test
@OpenId(authorities = { "NICE",
"AUTHOR" }, claims = @OpenIdClaims(usernameClaim = StandardClaimNames.PREFERRED_USERNAME, preferredUsername = "Tonton Pirate", email = "[email protected]"))
void givenUserIsGrantedWithNice_whenGetNice_thenOk() throws Exception {
api.get("/nice").andExpect(status().isOk())
.andExpect(jsonPath("$.body").value(
"Dear Tonton Pirate! You are granted with: [NICE, AUTHOR]."));
}

@Test
@OpenId(authorities = { "AUTHOR" }, claims = @OpenIdClaims(preferredUsername = "Tonton Pirate"))
void givenUserIsNotGrantedWithNice_whenGetNice_thenForbidden() throws Exception {
api.get("/nice").andExpect(status().isForbidden());
}

@Test
void givenRequestIsAnonymous_whenGetNice_thenUnauthorized() throws Exception {
api.get("/nice").andExpect(status().isUnauthorized());
}
@Autowired
MockMvcSupport api;

@Test
@OpenId(
authorities = { "AUTHOR" },
claims = @OpenIdClaims(usernameClaim = StandardClaimNames.PREFERRED_USERNAME, preferredUsername = "Tonton Pirate", email = "[email protected]"))
void givenUserIsAuthenticated_whenGreet_thenOk() throws Exception {
api.get("/greet").andExpect(status().isOk())
.andExpect(jsonPath("$.body").value("Hi Tonton Pirate! You are granted with: [AUTHOR] and your email is [email protected]."));
}

@Test
void givenRequestIsAnonymous_whenGreet_thenUnauthorized() throws Exception {
api.get("/greet").andExpect(status().isUnauthorized());
}

@Test
@OpenId(
authorities = { "NICE", "AUTHOR" },
claims = @OpenIdClaims(usernameClaim = StandardClaimNames.PREFERRED_USERNAME, preferredUsername = "Tonton Pirate", email = "[email protected]"))
void givenUserIsGrantedWithNice_whenGetNice_thenOk() throws Exception {
api.get("/nice").andExpect(status().isOk()).andExpect(jsonPath("$.body").value("Dear Tonton Pirate! You are granted with: [NICE, AUTHOR]."));
}

@Test
@OpenId(authorities = { "AUTHOR" }, claims = @OpenIdClaims(preferredUsername = "Tonton Pirate"))
void givenUserIsNotGrantedWithNice_whenGetNice_thenForbidden() throws Exception {
api.get("/nice").andExpect(status().isForbidden());
}

@Test
void givenRequestIsAnonymous_whenGetNice_thenUnauthorized() throws Exception {
api.get("/nice").andExpect(status().isUnauthorized());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,12 @@
@RequestMapping("/greet")
public class GreetingController {

@GetMapping()
@PreAuthorize("hasAuthority('NICE')")
public MessageDto getGreeting(Authentication auth) {
return new MessageDto("Hi %s! You are granted with: %s.".formatted(
auth.getName(),
auth.getAuthorities()));
}
@GetMapping()
@PreAuthorize("hasAuthority('NICE')")
public MessageDto getGreeting(Authentication auth) {
return new MessageDto("Hi %s! You are granted with: %s.".formatted(auth.getName(), auth.getAuthorities()));
}

static record MessageDto(String body) {
}
static record MessageDto(String body) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
@SpringBootApplication
public class ResourceServerWithOAuthenticationApplication {

public static void main(String[] args) {
SpringApplication.run(ResourceServerWithOAuthenticationApplication.class, args);
}
public static void main(String[] args) {
SpringApplication.run(ResourceServerWithOAuthenticationApplication.class, args);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,41 +30,38 @@
@EnableMethodSecurity
public class WebSecurityConfig {

@Bean
@Profile("oauthentication")
// This bean is optional as a default one is provided (building a
// BearerAuthenticationToken)
OpaqueTokenAuthenticationConverter introspectionAuthenticationConverter(
Converter<Map<String, Object>, Collection<? extends GrantedAuthority>> authoritiesConverter) {
return (String introspectedToken,
OAuth2AuthenticatedPrincipal authenticatedPrincipal) -> new OAuthentication<>(
new OpenidClaimSet(authenticatedPrincipal.getAttributes()),
authoritiesConverter.convert(authenticatedPrincipal.getAttributes()),
introspectedToken);
}
@Bean
@Profile("oauthentication")
// This bean is optional as a default one is provided (building a
// BearerAuthenticationToken)
OpaqueTokenAuthenticationConverter
introspectionAuthenticationConverter(Converter<Map<String, Object>, Collection<? extends GrantedAuthority>> authoritiesConverter) {
return (String introspectedToken, OAuth2AuthenticatedPrincipal authenticatedPrincipal) -> new OAuthentication<>(
new OpenidClaimSet(authenticatedPrincipal.getAttributes()),
authoritiesConverter.convert(authenticatedPrincipal.getAttributes()),
introspectedToken);
}

@Component
@Profile("auth0 | cognito")
public static class UserEndpointOpaqueTokenIntrospector implements OpaqueTokenIntrospector {
private final URI userinfoUri;
private final RestTemplate restClient = new RestTemplate();
@Component
@Profile("auth0 | cognito")
public static class UserEndpointOpaqueTokenIntrospector implements OpaqueTokenIntrospector {
private final URI userinfoUri;
private final RestTemplate restClient = new RestTemplate();

public UserEndpointOpaqueTokenIntrospector(OAuth2ResourceServerProperties oauth2Properties)
throws IOException {
userinfoUri = URI.create(oauth2Properties.getOpaquetoken().getIntrospectionUri());
}
public UserEndpointOpaqueTokenIntrospector(OAuth2ResourceServerProperties oauth2Properties) throws IOException {
userinfoUri = URI.create(oauth2Properties.getOpaquetoken().getIntrospectionUri());
}

@Override
@SuppressWarnings("unchecked")
public OAuth2AuthenticatedPrincipal introspect(String token) {
HttpHeaders headers = new HttpHeaders();
headers.setBearerAuth(token);
final var claims = new OpenidClaimSet(restClient
.exchange(userinfoUri, HttpMethod.GET, new HttpEntity<>(headers), Map.class).getBody());
// No need to map authorities there, it is done later by
// OpaqueTokenAuthenticationConverter
return new OAuth2IntrospectionAuthenticatedPrincipal(claims, List.of());
}
@Override
@SuppressWarnings("unchecked")
public OAuth2AuthenticatedPrincipal introspect(String token) {
HttpHeaders headers = new HttpHeaders();
headers.setBearerAuth(token);
final var claims = new OpenidClaimSet(restClient.exchange(userinfoUri, HttpMethod.GET, new HttpEntity<>(headers), Map.class).getBody());
// No need to map authorities there, it is done later by
// OpaqueTokenAuthenticationConverter
return new OAuth2IntrospectionAuthenticatedPrincipal(claims, List.of());
}

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
@Import(WebSecurityConfig.class)
class GreetingControllerTest {

@Autowired
MockMvcSupport mockMvc;
@Autowired
MockMvcSupport mockMvc;

// @formatter:off
// @formatter:off
@Test
@OpenId(
authorities = { "NICE", "AUTHOR" },
Expand All @@ -34,15 +34,15 @@ void givenUserIsGrantedWithNice_whenGreet_thenOk() throws Exception {
}
// @formatter:on

@Test
@OpenId(authorities = "AUTHOR", claims = @OpenIdClaims(preferredUsername = "Tonton Pirate"))
void givenUserIsNotGrantedWithNice_whenGreet_thenForbidden() throws Exception {
mockMvc.get("/greet").andExpect(status().isForbidden());
}
@Test
@OpenId(authorities = "AUTHOR", claims = @OpenIdClaims(preferredUsername = "Tonton Pirate"))
void givenUserIsNotGrantedWithNice_whenGreet_thenForbidden() throws Exception {
mockMvc.get("/greet").andExpect(status().isForbidden());
}

@Test
void givenRequestIsAnonymous_whenGreet_thenUnauthorized() throws Exception {
mockMvc.get("/greet").andExpect(status().isUnauthorized());
}
@Test
void givenRequestIsAnonymous_whenGreet_thenUnauthorized() throws Exception {
mockMvc.get("/greet").andExpect(status().isUnauthorized());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,21 @@
@AutoConfigureMockMvc
@ImportAutoConfiguration({ AddonsWebmvcTestConf.class })
class ResourceServerWithOAuthenticationApplicationTests {
@Autowired
MockMvcSupport api;
@Autowired
MockMvcSupport api;

@Test
void givenRequestIsAnonymous_whenGreet_thenUnauthorized() throws Exception {
api.get("/greet").andExpect(status().isUnauthorized());
}
@Test
void givenRequestIsAnonymous_whenGreet_thenUnauthorized() throws Exception {
api.get("/greet").andExpect(status().isUnauthorized());
}

@Test
@OpenId()
void givenUserIsNotGrantedWithNice_whenGreet_thenForbidden() throws Exception {
api.get("/greet").andExpect(status().isForbidden());
}
@Test
@OpenId()
void givenUserIsNotGrantedWithNice_whenGreet_thenForbidden() throws Exception {
api.get("/greet").andExpect(status().isForbidden());
}

// @formatter:off
// @formatter:off
@Test
@OpenId(
authorities = { "NICE", "AUTHOR" },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,18 @@
@PreAuthorize("isAuthenticated()")
public class GreetingController {

@GetMapping("/greet")
public MessageDto getGreeting(OAuthentication<OpenidClaimSet> auth) {
return new MessageDto("Hi %s! You are granted with: %s and your email is %s."
.formatted(auth.getName(), auth.getAuthorities(), auth.getClaims().getEmail()));
}
@GetMapping("/greet")
public MessageDto getGreeting(OAuthentication<OpenidClaimSet> auth) {
return new MessageDto(
"Hi %s! You are granted with: %s and your email is %s.".formatted(auth.getName(), auth.getAuthorities(), auth.getClaims().getEmail()));
}

@GetMapping("/nice")
@PreAuthorize("hasAuthority('NICE')")
public MessageDto getNiceGreeting(OAuthentication<OpenidClaimSet> auth) {
return new MessageDto("Dear %s! You are granted with: %s."
.formatted(auth.getName(), auth.getAuthorities()));
}
@GetMapping("/nice")
@PreAuthorize("hasAuthority('NICE')")
public MessageDto getNiceGreeting(OAuthentication<OpenidClaimSet> auth) {
return new MessageDto("Dear %s! You are granted with: %s.".formatted(auth.getName(), auth.getAuthorities()));
}

static record MessageDto(String body) {
}
static record MessageDto(String body) {
}
}
Loading

0 comments on commit 1104a02

Please sign in to comment.