Skip to content

Commit d67e321

Browse files
authored
Replace registration hook by fallback in CustomOAuth2UserService (#68)
1 parent 10d6505 commit d67e321

7 files changed

Lines changed: 16 additions & 108 deletions

File tree

src/main/java/io/autoinvestor/ServerWebExchangeFactory.java

Lines changed: 0 additions & 23 deletions
This file was deleted.

src/main/java/io/autoinvestor/configuration/CustomOAuth2UserService.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public Mono<OAuth2User> loadUser(OAuth2UserRequest userRequest) {
2525

2626
return delegate.loadUser(userRequest)
2727
.flatMap(oauth2User -> fetchUserId(oauth2User)
28+
.switchIfEmpty(createUser(oauth2User))
2829
.map(userId -> {
2930
Map<String, Object> attributes = new HashMap<>(oauth2User.getAttributes());
3031
attributes.put("userId", userId);
@@ -34,7 +35,6 @@ public Mono<OAuth2User> loadUser(OAuth2UserRequest userRequest) {
3435
"sub"
3536
);
3637
})
37-
.switchIfEmpty(Mono.error(new RuntimeException("User not found")))
3838
);
3939
}
4040

@@ -43,5 +43,11 @@ private Mono<String> fetchUserId(OAuth2User user) {
4343
.getUser(user.getAttribute("email"))
4444
.map(userResponse -> userResponse.userId().toString());
4545
}
46+
47+
private Mono<String> createUser(OAuth2User user) {
48+
return usersClient
49+
.createUser(user.getAttribute("email"))
50+
.then(fetchUserId(user));
51+
}
4652
}
4753

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,18 @@
11
package io.autoinvestor.configuration;
22

33
import lombok.RequiredArgsConstructor;
4-
import org.springframework.beans.factory.annotation.Value;
54
import org.springframework.context.annotation.Bean;
65
import org.springframework.context.annotation.Configuration;
7-
import org.springframework.core.annotation.Order;
8-
import org.springframework.http.HttpStatus;
96
import org.springframework.security.config.Customizer;
10-
import org.springframework.security.config.web.server.SecurityWebFiltersOrder;
117
import org.springframework.security.config.web.server.ServerHttpSecurity;
128
import org.springframework.security.web.server.SecurityWebFilterChain;
139
import org.springframework.security.web.server.util.matcher.ServerWebExchangeMatchers;
14-
import org.springframework.web.server.WebFilter;
1510

1611
@Configuration
1712
@RequiredArgsConstructor
1813
public class SecurityConfiguration {
1914

20-
@Value("${autoinvestor.okta.hookAuthHeaderName}")
21-
private String apiAuthHeaderName;
22-
23-
@Value("${autoinvestor.okta.hookAuthHeaderValue}")
24-
private String apiAuthHeaderValue;
25-
2615
@Bean
27-
@Order(1)
28-
public SecurityWebFilterChain hookSecurityWebFilterChain(ServerHttpSecurity http) {
29-
return http
30-
.securityMatcher(ServerWebExchangeMatchers.pathMatchers("/api/hook/**"))
31-
.csrf(ServerHttpSecurity.CsrfSpec::disable)
32-
.authorizeExchange(exchanges -> exchanges.anyExchange().permitAll())
33-
.addFilterAt(hookAuthenticationWebFilter(), SecurityWebFiltersOrder.AUTHENTICATION)
34-
.build();
35-
}
36-
37-
@Bean
38-
@Order(2)
3916
public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity http) {
4017
return http
4118
.securityMatcher(ServerWebExchangeMatchers.anyExchange())
@@ -47,17 +24,4 @@ public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity http) {
4724
.oauth2Login(Customizer.withDefaults())
4825
.build();
4926
}
50-
51-
private WebFilter hookAuthenticationWebFilter() {
52-
return (exchange, chain) -> {
53-
String headerValue = exchange.getRequest().getHeaders().getFirst(apiAuthHeaderName);
54-
55-
if (apiAuthHeaderValue.equals(headerValue)) {
56-
return chain.filter(exchange);
57-
} else {
58-
exchange.getResponse().setStatusCode(HttpStatus.UNAUTHORIZED);
59-
return exchange.getResponse().setComplete();
60-
}
61-
};
62-
}
6327
}

src/main/java/io/autoinvestor/controller/inlinehook/HookController.java

Lines changed: 0 additions & 24 deletions
This file was deleted.

src/main/java/io/autoinvestor/controller/inlinehook/RegisterHookRequest.java

Lines changed: 0 additions & 17 deletions
This file was deleted.

src/main/java/io/autoinvestor/filters/ClaimToHeaderGatewayFilterFactory.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package io.autoinvestor.filters;
22

3-
import io.autoinvestor.ServerWebExchangeFactory;
43
import lombok.Getter;
54
import lombok.NoArgsConstructor;
65
import lombok.RequiredArgsConstructor;
@@ -12,6 +11,7 @@
1211
import org.springframework.security.core.context.SecurityContext;
1312
import org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken;
1413
import org.springframework.stereotype.Component;
14+
import org.springframework.web.server.ServerWebExchange;
1515

1616
import java.util.List;
1717

@@ -20,20 +20,25 @@
2020
@RequiredArgsConstructor
2121
public class ClaimToHeaderGatewayFilterFactory implements GatewayFilterFactory<ClaimToHeaderGatewayFilterFactory.Config> {
2222

23-
private final ServerWebExchangeFactory serverWebExchangeFactory;
24-
2523
@Override
2624
public GatewayFilter apply(Config config) {
2725
return (exchange, chain) -> ReactiveSecurityContextHolder.getContext()
2826
.map(SecurityContext::getAuthentication)
2927
.filter(authentication -> authentication instanceof OAuth2AuthenticationToken)
3028
.map(authentication -> (OAuth2AuthenticationToken) authentication)
3129
.mapNotNull(authentication -> authentication.getPrincipal().getAttribute(config.getClaim()))
32-
.map(userId -> serverWebExchangeFactory.withHeader(exchange, config.getHeaderName(), userId.toString()))
30+
.filter(userId -> userId instanceof String)
31+
.map(userId -> withHeader(exchange, config.getHeaderName(), (String) userId))
3332
.defaultIfEmpty(exchange)
3433
.flatMap(chain::filter);
3534
}
3635

36+
private static ServerWebExchange withHeader(ServerWebExchange exchange, String headerName, String headerValue) {
37+
return exchange.mutate().request(request -> request.headers(headers ->
38+
headers.add(headerName, headerValue)
39+
)).build();
40+
}
41+
3742
@Override
3843
public Config newConfig() {
3944
return new Config();

src/main/resources/application.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
autoinvestor:
2-
okta:
3-
hookAuthHeaderName: "${OKTA_HOOK_AUTH_HEADER_NAME}"
4-
hookAuthHeaderValue: "${OKTA_HOOK_AUTH_HEADER_VALUE}"
52
client:
63
users:
74
url: "${USERS_BASE_URL}"

0 commit comments

Comments
 (0)