|  | 
|  | 1 | +/* | 
|  | 2 | + * Copyright 2004-present the original author or authors. | 
|  | 3 | + * | 
|  | 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 5 | + * you may not use this file except in compliance with the License. | 
|  | 6 | + * You may obtain a copy of the License at | 
|  | 7 | + * | 
|  | 8 | + *      https://www.apache.org/licenses/LICENSE-2.0 | 
|  | 9 | + * | 
|  | 10 | + * Unless required by applicable law or agreed to in writing, software | 
|  | 11 | + * distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 13 | + * See the License for the specific language governing permissions and | 
|  | 14 | + * limitations under the License. | 
|  | 15 | + */ | 
|  | 16 | + | 
|  | 17 | +package org.springframework.security.authorization; | 
|  | 18 | + | 
|  | 19 | +import java.util.Arrays; | 
|  | 20 | +import java.util.List; | 
|  | 21 | +import java.util.Map; | 
|  | 22 | +import java.util.function.Supplier; | 
|  | 23 | + | 
|  | 24 | +import javax.sql.DataSource; | 
|  | 25 | + | 
|  | 26 | +import org.jspecify.annotations.Nullable; | 
|  | 27 | + | 
|  | 28 | +import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations; | 
|  | 29 | +import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; | 
|  | 30 | +import org.springframework.security.core.Authentication; | 
|  | 31 | +import org.springframework.util.Assert; | 
|  | 32 | + | 
|  | 33 | +/** | 
|  | 34 | + * An {@link AuthorizationManager} that can lookup authorities using a configured SQL | 
|  | 35 | + * statement | 
|  | 36 | + * | 
|  | 37 | + * @author Andrey Litvitski | 
|  | 38 | + * @since 7.0.0 | 
|  | 39 | + */ | 
|  | 40 | +public final class SqlAllAuthoritiesAuthorizationManager<T> implements AuthorizationManager<T> { | 
|  | 41 | + | 
|  | 42 | +	private final NamedParameterJdbcOperations jdbc; | 
|  | 43 | + | 
|  | 44 | +	private final @Nullable List<String> additionalAuthorities; | 
|  | 45 | + | 
|  | 46 | +	private final String sql; | 
|  | 47 | + | 
|  | 48 | +	private final boolean whenTrueMode; | 
|  | 49 | + | 
|  | 50 | +	private SqlAllAuthoritiesAuthorizationManager(NamedParameterJdbcOperations jdbc, String sql, | 
|  | 51 | +			@Nullable List<String> additionalAuthorities, boolean whenTrueMode) { | 
|  | 52 | +		this.jdbc = jdbc; | 
|  | 53 | +		this.sql = sql; | 
|  | 54 | +		this.additionalAuthorities = additionalAuthorities; | 
|  | 55 | +		this.whenTrueMode = whenTrueMode; | 
|  | 56 | +	} | 
|  | 57 | + | 
|  | 58 | +	@Override | 
|  | 59 | +	public AuthorizationResult authorize(Supplier<? extends @Nullable Authentication> authentication, T object) { | 
|  | 60 | +		List<String> additionalAuthorities = findAdditionalAuthorities(authentication.get().getName()); | 
|  | 61 | +		if (additionalAuthorities.isEmpty()) { | 
|  | 62 | +			return new AuthorizationDecision(true); | 
|  | 63 | +		} | 
|  | 64 | +		else { | 
|  | 65 | +			return AllAuthoritiesAuthorizationManager.hasAllAuthorities(additionalAuthorities) | 
|  | 66 | +				.authorize(authentication, object); | 
|  | 67 | +		} | 
|  | 68 | +	} | 
|  | 69 | + | 
|  | 70 | +	private List<String> findAdditionalAuthorities(String authenticationName) { | 
|  | 71 | +		Map<String, Object> params = Map.of("username", authenticationName); | 
|  | 72 | +		if (this.whenTrueMode) { | 
|  | 73 | +			List<Map<String, Object>> rows = this.jdbc.queryForList(this.sql, params); | 
|  | 74 | +			if (rows.isEmpty()) { | 
|  | 75 | +				return List.of(); | 
|  | 76 | +			} | 
|  | 77 | +			return (this.additionalAuthorities == null) ? List.of() : List.copyOf(this.additionalAuthorities); | 
|  | 78 | +		} | 
|  | 79 | +		else { | 
|  | 80 | +			return this.jdbc.query(this.sql, params, (rs, rowNum) -> rs.getString(1)); | 
|  | 81 | +		} | 
|  | 82 | +	} | 
|  | 83 | + | 
|  | 84 | +	public static final class Builder<T> { | 
|  | 85 | + | 
|  | 86 | +		@Nullable private NamedParameterJdbcOperations jdbc; | 
|  | 87 | + | 
|  | 88 | +		@Nullable private List<String> additionalAuthorities; | 
|  | 89 | + | 
|  | 90 | +		private boolean whenTrueMode; | 
|  | 91 | + | 
|  | 92 | +		@Nullable private String sql; | 
|  | 93 | + | 
|  | 94 | +		public Builder<T> whenTrue(String sql) { | 
|  | 95 | +			this.whenTrueMode = true; | 
|  | 96 | +			this.sql = sql; | 
|  | 97 | +			return this; | 
|  | 98 | +		} | 
|  | 99 | + | 
|  | 100 | +		public Builder<T> selectAuthorities(String sql) { | 
|  | 101 | +			this.whenTrueMode = false; | 
|  | 102 | +			this.sql = sql; | 
|  | 103 | +			return this; | 
|  | 104 | +		} | 
|  | 105 | + | 
|  | 106 | +		public Builder<T> additionalAuthorities(String... authorities) { | 
|  | 107 | +			this.additionalAuthorities = Arrays.asList(authorities); | 
|  | 108 | +			return this; | 
|  | 109 | +		} | 
|  | 110 | + | 
|  | 111 | +		public Builder<T> dataSource(DataSource dataSource) { | 
|  | 112 | +			this.jdbc = new NamedParameterJdbcTemplate(dataSource); | 
|  | 113 | +			return this; | 
|  | 114 | +		} | 
|  | 115 | + | 
|  | 116 | +		public SqlAllAuthoritiesAuthorizationManager<T> build() { | 
|  | 117 | +			Assert.notNull(this.jdbc, "jdbc cannot be null"); | 
|  | 118 | +			Assert.notNull(this.sql, "sql cannot be null"); | 
|  | 119 | +			return new SqlAllAuthoritiesAuthorizationManager<>(this.jdbc, this.sql, this.additionalAuthorities, | 
|  | 120 | +					this.whenTrueMode); | 
|  | 121 | +		} | 
|  | 122 | + | 
|  | 123 | +	} | 
|  | 124 | + | 
|  | 125 | +} | 
0 commit comments