|
6 | 6 | import com.google.common.annotations.VisibleForTesting;
|
7 | 7 | import com.provectus.kafka.ui.config.ClustersProperties;
|
8 | 8 | import com.provectus.kafka.ui.config.auth.AuthenticatedUser;
|
| 9 | +import com.provectus.kafka.ui.config.auth.RbacUser; |
9 | 10 | import com.provectus.kafka.ui.model.KafkaCluster;
|
10 | 11 | import com.provectus.kafka.ui.model.rbac.AccessContext;
|
11 | 12 | import com.provectus.kafka.ui.service.AdminClientService;
|
|
30 | 31 | import org.springframework.beans.factory.annotation.Autowired;
|
31 | 32 | import org.springframework.security.core.GrantedAuthority;
|
32 | 33 | import org.springframework.security.core.context.SecurityContext;
|
| 34 | +import org.springframework.security.core.userdetails.UserDetails; |
33 | 35 | import org.springframework.security.oauth2.core.oidc.user.OidcUser;
|
34 | 36 | import org.springframework.stereotype.Service;
|
35 | 37 | import reactor.core.publisher.Mono;
|
@@ -196,18 +198,30 @@ private Mono<AuthenticatedUser> extractUser(Signal<?> sig) {
|
196 | 198 | Object key = SecurityContext.class;
|
197 | 199 | if (sig.getContextView().hasKey(key)) {
|
198 | 200 | return sig.getContextView().<Mono<SecurityContext>>get(key)
|
199 |
| - .map(context -> context.getAuthentication().getPrincipal()) |
200 |
| - .cast(OidcUser.class) |
201 |
| - .map(user -> { |
202 |
| - var roles = user.getAuthorities().stream().map(GrantedAuthority::getAuthority).collect(Collectors.toSet()); |
203 |
| - return new AuthenticatedUser(user.getName(), roles); |
204 |
| - }) |
| 201 | + .map(AuditService::createAuthenticatedUser) |
205 | 202 | .switchIfEmpty(NO_AUTH_USER);
|
206 | 203 | } else {
|
207 | 204 | return NO_AUTH_USER;
|
208 | 205 | }
|
209 | 206 | }
|
210 | 207 |
|
| 208 | + private static AuthenticatedUser createAuthenticatedUser(SecurityContext context) { |
| 209 | + var principal = context.getAuthentication().getPrincipal(); |
| 210 | + if (principal instanceof RbacUser user) { |
| 211 | + return new AuthenticatedUser(user.name(), user.groups()); |
| 212 | + } else if (principal instanceof OidcUser user) { |
| 213 | + return new AuthenticatedUser( |
| 214 | + user.getName(), |
| 215 | + user.getAuthorities().stream().map(GrantedAuthority::getAuthority).collect(Collectors.toSet())); |
| 216 | + } else if (principal instanceof UserDetails user) { |
| 217 | + return new AuthenticatedUser( |
| 218 | + user.getUsername(), |
| 219 | + user.getAuthorities().stream().map(GrantedAuthority::getAuthority).collect(Collectors.toSet())); |
| 220 | + } else { |
| 221 | + return null; |
| 222 | + } |
| 223 | + } |
| 224 | + |
211 | 225 | private void sendAuditRecord(AccessContext ctx, AuthenticatedUser user) {
|
212 | 226 | sendAuditRecord(ctx, user, null);
|
213 | 227 | }
|
|
0 commit comments