Skip to content

Commit b95a83a

Browse files
authored
Fix the problem of not being able to create PAT for OAuth2 user (#6870)
#### What type of PR is this? /kind bug /area core /milestone 2.20.x #### What this PR does / why we need it: This PR refactors check of whether the current user is a real user to fix the problem of not being able to create PAT for OAuth2 user. #### Does this PR introduce a user-facing change? ```release-note 修复通过 OAuth2 登录之后无法正常创建和恢复个人令牌的问题 ```
1 parent c3020d6 commit b95a83a

File tree

3 files changed

+9
-26
lines changed

3 files changed

+9
-26
lines changed

application/src/main/java/run/halo/app/security/authentication/pat/impl/UserScopedPatHandlerImpl.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
import java.util.List;
1212
import java.util.Objects;
1313
import java.util.function.Predicate;
14+
import org.springframework.security.authentication.AuthenticationTrustResolver;
15+
import org.springframework.security.authentication.AuthenticationTrustResolverImpl;
1416
import org.springframework.security.core.Authentication;
1517
import org.springframework.security.core.GrantedAuthority;
1618
import org.springframework.security.core.context.ReactiveSecurityContextHolder;
@@ -64,6 +66,9 @@ public class UserScopedPatHandlerImpl implements UserScopedPatHandler {
6466

6567
private Clock clock;
6668

69+
private final AuthenticationTrustResolver authTrustResolver =
70+
new AuthenticationTrustResolverImpl();
71+
6772
public UserScopedPatHandlerImpl(ReactiveExtensionClient client,
6873
CryptoService cryptoService,
6974
ExternalUrlSupplier externalUrl,
@@ -84,8 +89,8 @@ public void setClock(Clock clock) {
8489
this.clock = clock;
8590
}
8691

87-
private static Mono<Authentication> mustBeRealUser(Mono<Authentication> authentication) {
88-
return authentication.filter(AuthorityUtils::isRealUser)
92+
private Mono<Authentication> mustBeAuthenticated(Mono<Authentication> authentication) {
93+
return authentication.filter(authTrustResolver::isAuthenticated)
8994
// Non-username-password authentication could not access the API at any time.
9095
.switchIfEmpty(Mono.error(AccessDeniedException::new));
9196
}
@@ -94,7 +99,7 @@ private static Mono<Authentication> mustBeRealUser(Mono<Authentication> authenti
9499
public Mono<ServerResponse> create(ServerRequest request) {
95100
return ReactiveSecurityContextHolder.getContext()
96101
.map(SecurityContext::getAuthentication)
97-
.transform(UserScopedPatHandlerImpl::mustBeRealUser)
102+
.transform(this::mustBeAuthenticated)
98103
.flatMap(auth -> request.bodyToMono(PersonalAccessToken.class)
99104
.switchIfEmpty(
100105
Mono.error(() -> new ServerWebInputException("Missing request body.")))
@@ -222,7 +227,7 @@ public Mono<ServerResponse> delete(ServerRequest request) {
222227
public Mono<ServerResponse> restore(ServerRequest request) {
223228
var restoredPat = ReactiveSecurityContextHolder.getContext()
224229
.map(SecurityContext::getAuthentication)
225-
.transform(UserScopedPatHandlerImpl::mustBeRealUser)
230+
.transform(this::mustBeAuthenticated)
226231
.flatMap(auth -> {
227232
var name = request.pathVariable("name");
228233
return getPat(name, auth.getName());

application/src/main/java/run/halo/app/security/authorization/AuthorityUtils.java

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44
import java.util.Set;
55
import java.util.stream.Collectors;
66
import org.apache.commons.lang3.StringUtils;
7-
import org.springframework.security.authentication.RememberMeAuthenticationToken;
8-
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
9-
import org.springframework.security.core.Authentication;
107
import org.springframework.security.core.GrantedAuthority;
118

129
/**
@@ -51,14 +48,4 @@ public static boolean containsSuperRole(Collection<String> roles) {
5148
return roles.contains(SUPER_ROLE_NAME);
5249
}
5350

54-
/**
55-
* Check if the authentication is a real user.
56-
*
57-
* @param authentication current authentication
58-
* @return true if the authentication is a real user; false otherwise
59-
*/
60-
public static boolean isRealUser(Authentication authentication) {
61-
return authentication instanceof UsernamePasswordAuthenticationToken
62-
|| authentication instanceof RememberMeAuthenticationToken;
63-
}
6451
}

application/src/test/java/run/halo/app/security/authorization/AuthorityUtilsTest.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,12 @@
33
import static org.junit.jupiter.api.Assertions.assertEquals;
44
import static org.junit.jupiter.api.Assertions.assertFalse;
55
import static org.junit.jupiter.api.Assertions.assertTrue;
6-
import static org.mockito.Mockito.mock;
76
import static run.halo.app.security.authorization.AuthorityUtils.authoritiesToRoles;
87
import static run.halo.app.security.authorization.AuthorityUtils.containsSuperRole;
9-
import static run.halo.app.security.authorization.AuthorityUtils.isRealUser;
108

119
import java.util.List;
1210
import java.util.Set;
1311
import org.junit.jupiter.api.Test;
14-
import org.springframework.security.authentication.RememberMeAuthenticationToken;
15-
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
1612
import org.springframework.security.core.authority.SimpleGrantedAuthority;
1713

1814
class AuthorityUtilsTest {
@@ -39,9 +35,4 @@ void containsSuperRoleTest() {
3935
assertFalse(containsSuperRole(Set.of("admin")));
4036
}
4137

42-
@Test
43-
void shouldReturnTrueWhenAuthenticationIsRealUser() {
44-
assertTrue(isRealUser(mock(UsernamePasswordAuthenticationToken.class)));
45-
assertTrue(isRealUser(mock(RememberMeAuthenticationToken.class)));
46-
}
4738
}

0 commit comments

Comments
 (0)