Skip to content

Commit c33acc0

Browse files
committed
Polish AuthoritiesGranter
1 parent e641403 commit c33acc0

File tree

3 files changed

+36
-37
lines changed

3 files changed

+36
-37
lines changed

config/src/main/java/org/springframework/security/config/annotation/web/configurers/MfaConfigurer.java

Lines changed: 31 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import java.time.Clock;
2121
import java.time.Duration;
2222
import java.time.Instant;
23-
import java.util.ArrayList;
2423
import java.util.Collection;
2524
import java.util.HashSet;
2625
import java.util.List;
@@ -44,6 +43,7 @@
4443
import org.springframework.security.core.AuthenticationException;
4544
import org.springframework.security.core.AuthenticationResult;
4645
import org.springframework.security.core.GrantedAuthority;
46+
import org.springframework.security.core.authority.AuthorityUtils;
4747
import org.springframework.security.core.authority.ExpirableGrantedAuthority;
4848
import org.springframework.security.core.authority.SimpleGrantedAuthority;
4949
import org.springframework.security.core.context.SecurityContextHolder;
@@ -118,11 +118,9 @@ public void configure(B builder) throws Exception {
118118

119119
interface AuthoritiesGranter {
120120

121-
AuthenticationResult grantAuthorities(AuthenticationResult authentication);
121+
Collection<GrantedAuthority> grantableAuthorities(AuthorizationRequest request);
122122

123-
default Collection<String> grantableAuthorities() {
124-
return List.of();
125-
}
123+
Collection<GrantedAuthority> grantableAuthorities(Authentication result);
126124

127125
}
128126

@@ -135,12 +133,17 @@ static final class PreAuthenticatedAuthoritiesGranter implements AuthoritiesGran
135133
}
136134

137135
@Override
138-
public AuthenticationResult grantAuthorities(AuthenticationResult authentication) {
136+
public Collection<GrantedAuthority> grantableAuthorities(AuthorizationRequest request) {
137+
return List.of();
138+
}
139+
140+
@Override
141+
public Collection<GrantedAuthority> grantableAuthorities(Authentication result) {
139142
Authentication current = this.strategy.getContext().getAuthentication();
140143
if (current == null || !current.isAuthenticated()) {
141-
return authentication;
144+
return List.of();
142145
}
143-
return authentication.withGrantedAuthorities((a) -> a.addAll(current.getAuthorities()));
146+
return new HashSet<>(current.getAuthorities());
144147
}
145148

146149
}
@@ -153,26 +156,22 @@ static final class CompositeAuthoritiesGranter implements AuthoritiesGranter {
153156
this.authoritiesGranters = List.of(authorities);
154157
}
155158

156-
CompositeAuthoritiesGranter(Collection<AuthoritiesGranter> authorities) {
157-
this.authoritiesGranters = new ArrayList<>(authorities);
158-
}
159-
160159
@Override
161-
public Collection<String> grantableAuthorities() {
162-
Collection<String> grantable = new ArrayList<>();
160+
public Collection<GrantedAuthority> grantableAuthorities(AuthorizationRequest request) {
161+
Collection<GrantedAuthority> authorities = new HashSet<>();
163162
for (AuthoritiesGranter granter : this.authoritiesGranters) {
164-
grantable.addAll(granter.grantableAuthorities());
163+
authorities.addAll(granter.grantableAuthorities(request));
165164
}
166-
return grantable;
165+
return authorities;
167166
}
168167

169168
@Override
170-
public AuthenticationResult grantAuthorities(AuthenticationResult authentication) {
171-
AuthenticationResult granted = authentication;
169+
public Collection<GrantedAuthority> grantableAuthorities(Authentication result) {
170+
Collection<GrantedAuthority> authorities = new HashSet<>();
172171
for (AuthoritiesGranter granter : this.authoritiesGranters) {
173-
granted = granter.grantAuthorities(granted);
172+
authorities.addAll(granter.grantableAuthorities(result));
174173
}
175-
return granted;
174+
return authorities;
176175
}
177176

178177
}
@@ -197,12 +196,15 @@ static final class SimpleAuthoritiesGranter implements AuthoritiesGranter {
197196
}
198197

199198
@Override
200-
public Collection<String> grantableAuthorities() {
201-
return this.authorities;
199+
public Collection<GrantedAuthority> grantableAuthorities(AuthorizationRequest request) {
200+
Collection<GrantedAuthority> grantable = AuthorityUtils.createAuthorityList(this.authorities);
201+
Collection<GrantedAuthority> requested = request.getAuthorities();
202+
grantable.retainAll(requested);
203+
return grantable;
202204
}
203205

204206
@Override
205-
public AuthenticationResult grantAuthorities(AuthenticationResult authentication) {
207+
public Collection<GrantedAuthority> grantableAuthorities(Authentication result) {
206208
Collection<GrantedAuthority> toGrant = new HashSet<>();
207209
for (String authority : this.authorities) {
208210
if (this.grantingTime == null) {
@@ -213,9 +215,8 @@ public AuthenticationResult grantAuthorities(AuthenticationResult authentication
213215
toGrant.add(new ExpirableGrantedAuthority(authority, expiresAt));
214216
}
215217
}
216-
Collection<GrantedAuthority> current = new HashSet<>(authentication.getAuthorities());
217-
toGrant.addAll(current);
218-
return authentication.withGrantedAuthorities(toGrant);
218+
toGrant.removeAll(result.getAuthorities());
219+
return toGrant;
219220
}
220221

221222
void setClock(Clock clock) {
@@ -240,7 +241,8 @@ static final class AuthoritiesGranterAuthenticationManager implements Authentica
240241
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
241242
Authentication result = this.authenticationManager.authenticate(authentication);
242243
Assert.isInstanceOf(AuthenticationResult.class, result, "must be of type AuthenticationResult");
243-
return this.authoritiesGranter.grantAuthorities((AuthenticationResult) result);
244+
Collection<GrantedAuthority> authorities = this.authoritiesGranter.grantableAuthorities(result);
245+
return ((AuthenticationResult) result).withGrantedAuthorities((a) -> a.addAll(authorities));
244246
}
245247

246248
}
@@ -258,14 +260,8 @@ static final class SimpleAuthorizationEntryPoint implements AuthorizationEntryPo
258260
}
259261

260262
@Override
261-
public boolean authorizes(AuthorizationRequest authorizationRequest) {
262-
Collection<String> grantable = this.authoritiesGranter.grantableAuthorities();
263-
for (GrantedAuthority needed : authorizationRequest.getAuthorities()) {
264-
if (grantable.contains(needed.getAuthority())) {
265-
return true;
266-
}
267-
}
268-
return false;
263+
public Collection<GrantedAuthority> grantableAuthorities(AuthorizationRequest request) {
264+
return this.authoritiesGranter.grantableAuthorities(request);
269265
}
270266

271267
@Override

web/src/main/java/org/springframework/security/web/AuthorizationEntryPoint.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,13 @@
1616

1717
package org.springframework.security.web;
1818

19+
import java.util.Collection;
20+
1921
import org.springframework.security.authorization.AuthorizationRequest;
22+
import org.springframework.security.core.GrantedAuthority;
2023

2124
public interface AuthorizationEntryPoint extends AuthenticationEntryPoint {
2225

23-
boolean authorizes(AuthorizationRequest authorizationRequest);
26+
Collection<GrantedAuthority> grantableAuthorities(AuthorizationRequest authorizationRequest);
2427

2528
}

web/src/main/java/org/springframework/security/web/AuthorizationRequestingAccessDeniedHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public void handle(HttpServletRequest request, HttpServletResponse response, Acc
5151
return;
5252
}
5353
for (AuthorizationEntryPoint entry : this.entries) {
54-
if (entry.authorizes(authorizationRequest)) {
54+
if (!entry.grantableAuthorities(authorizationRequest).isEmpty()) {
5555
AuthenticationException iae = new InsufficientAuthenticationException("access denied", access);
5656
entry.commence(request, response, iae);
5757
return;

0 commit comments

Comments
 (0)