|
| 1 | +/* |
| 2 | + * Copyright 2002-2024 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.authentication.ott; |
| 18 | + |
| 19 | +import java.sql.ResultSet; |
| 20 | +import java.sql.SQLException; |
| 21 | +import java.sql.Timestamp; |
| 22 | +import java.sql.Types; |
| 23 | +import java.time.Instant; |
| 24 | +import java.util.ArrayList; |
| 25 | +import java.util.List; |
| 26 | +import java.util.function.Function; |
| 27 | + |
| 28 | +import org.springframework.jdbc.core.ArgumentPreparedStatementSetter; |
| 29 | +import org.springframework.jdbc.core.JdbcOperations; |
| 30 | +import org.springframework.jdbc.core.PreparedStatementSetter; |
| 31 | +import org.springframework.jdbc.core.RowMapper; |
| 32 | +import org.springframework.jdbc.core.SqlParameterValue; |
| 33 | +import org.springframework.util.Assert; |
| 34 | +import org.springframework.util.CollectionUtils; |
| 35 | + |
| 36 | +/** |
| 37 | + * |
| 38 | + * A JDBC implementation of an {@link OneTimeTokenService} that uses a |
| 39 | + * {@link JdbcOperations} for {@link OneTimeToken} persistence. |
| 40 | + * |
| 41 | + * <p> |
| 42 | + * <b>NOTE:</b> This {@code JdbcOneTimeTokenService} depends on the table definition |
| 43 | + * described in "classpath:org/springframework/security/core/ott/jdbc/one-time-tokens.sql" |
| 44 | + * and therefore MUST be defined in the database schema. |
| 45 | + * |
| 46 | + * @author Max Batischev |
| 47 | + * @since 6.4 |
| 48 | + */ |
| 49 | +public final class JdbcOneTimeTokenService implements OneTimeTokenService { |
| 50 | + |
| 51 | + private final JdbcOperations jdbcOperations; |
| 52 | + |
| 53 | + private Function<OneTimeToken, List<SqlParameterValue>> oneTimeTokenParametersMapper = new OneTimeTokenParametersMapper(); |
| 54 | + |
| 55 | + private RowMapper<OneTimeToken> oneTimeTokenRowMapper = new OneTimeTokenRowMapper(); |
| 56 | + |
| 57 | + private static final String TABLE_NAME = "one_time_tokens"; |
| 58 | + |
| 59 | + // @formatter:off |
| 60 | + private static final String COLUMN_NAMES = "token_value, " |
| 61 | + + "username, " |
| 62 | + + "expires_at"; |
| 63 | + // @formatter:on |
| 64 | + |
| 65 | + // @formatter:off |
| 66 | + private static final String SAVE_AUTHORIZED_CLIENT_SQL = "INSERT INTO " + TABLE_NAME |
| 67 | + + " (" + COLUMN_NAMES + ") VALUES (?, ?, ?)"; |
| 68 | + // @formatter:on |
| 69 | + |
| 70 | + private static final String FILTER = "token_value = ?"; |
| 71 | + |
| 72 | + private static final String DELETE_ONE_TIME_TOKEN_SQL = "DELETE FROM " + TABLE_NAME + " WHERE " + FILTER; |
| 73 | + |
| 74 | + // @formatter:off |
| 75 | + private static final String SELECT_ONE_TIME_TOKEN_SQL = "SELECT " + COLUMN_NAMES |
| 76 | + + " FROM " + TABLE_NAME |
| 77 | + + " WHERE " + FILTER; |
| 78 | + // @formatter:on |
| 79 | + |
| 80 | + /** |
| 81 | + * Constructs a {@code JdbcOneTimeTokenService} using the provide parameters. |
| 82 | + * @param jdbcOperations the JDBC operations |
| 83 | + */ |
| 84 | + public JdbcOneTimeTokenService(JdbcOperations jdbcOperations) { |
| 85 | + Assert.notNull(jdbcOperations, "jdbcOperations cannot be null"); |
| 86 | + this.jdbcOperations = jdbcOperations; |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public OneTimeToken generate(GenerateOneTimeTokenRequest request) { |
| 91 | + Assert.notNull(request, "generateOneTimeTokenRequest cannot be null"); |
| 92 | + |
| 93 | + OneTimeToken oneTimeToken = OneTimeTokenUtils.generateOneTimeToken(request, |
| 94 | + OneTimeTokenUtils.DEFAULT_ONE_TIME_TOKEN_TIME_TO_LIVE); |
| 95 | + insertOneTimeToken(oneTimeToken); |
| 96 | + return oneTimeToken; |
| 97 | + } |
| 98 | + |
| 99 | + private void insertOneTimeToken(OneTimeToken oneTimeToken) { |
| 100 | + List<SqlParameterValue> parameters = this.oneTimeTokenParametersMapper.apply(oneTimeToken); |
| 101 | + PreparedStatementSetter pss = new ArgumentPreparedStatementSetter(parameters.toArray()); |
| 102 | + this.jdbcOperations.update(SAVE_AUTHORIZED_CLIENT_SQL, pss); |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + public OneTimeToken consume(OneTimeTokenAuthenticationToken authenticationToken) { |
| 107 | + Assert.notNull(authenticationToken, "authenticationToken cannot be null"); |
| 108 | + |
| 109 | + List<OneTimeToken> tokens = selectOneTimeToken(authenticationToken); |
| 110 | + if (CollectionUtils.isEmpty(tokens)) { |
| 111 | + return null; |
| 112 | + } |
| 113 | + OneTimeToken token = tokens.get(0); |
| 114 | + deleteOneTimeToken(token); |
| 115 | + if (OneTimeTokenUtils.isExpired(token)) { |
| 116 | + return null; |
| 117 | + } |
| 118 | + return token; |
| 119 | + } |
| 120 | + |
| 121 | + private List<OneTimeToken> selectOneTimeToken(OneTimeTokenAuthenticationToken authenticationToken) { |
| 122 | + List<SqlParameterValue> parameters = List |
| 123 | + .of(new SqlParameterValue(Types.VARCHAR, authenticationToken.getTokenValue())); |
| 124 | + PreparedStatementSetter pss = new ArgumentPreparedStatementSetter(parameters.toArray()); |
| 125 | + return this.jdbcOperations.query(SELECT_ONE_TIME_TOKEN_SQL, pss, this.oneTimeTokenRowMapper); |
| 126 | + } |
| 127 | + |
| 128 | + private void deleteOneTimeToken(OneTimeToken oneTimeToken) { |
| 129 | + List<SqlParameterValue> parameters = List |
| 130 | + .of(new SqlParameterValue(Types.VARCHAR, oneTimeToken.getTokenValue())); |
| 131 | + PreparedStatementSetter pss = new ArgumentPreparedStatementSetter(parameters.toArray()); |
| 132 | + this.jdbcOperations.update(DELETE_ONE_TIME_TOKEN_SQL, pss); |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Sets the {@code Function} used for mapping {@link OneTimeToken} to a {@code List} |
| 137 | + * of {@link SqlParameterValue}. The default is {@link OneTimeTokenParametersMapper}. |
| 138 | + * @param oneTimeTokenParametersMapper the {@code Function} used for mapping |
| 139 | + * {@link OneTimeToken} to a {@code List} of {@link SqlParameterValue} |
| 140 | + */ |
| 141 | + public void setOneTimeTokenParametersMapper( |
| 142 | + Function<OneTimeToken, List<SqlParameterValue>> oneTimeTokenParametersMapper) { |
| 143 | + Assert.notNull(oneTimeTokenParametersMapper, "oneTimeTokenParametersMapper cannot be null"); |
| 144 | + this.oneTimeTokenParametersMapper = oneTimeTokenParametersMapper; |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * Sets the {@link RowMapper} used for mapping the current row in |
| 149 | + * {@code java.sql.ResultSet} to {@link OneTimeToken}. The default is |
| 150 | + * {@link OneTimeTokenRowMapper}. |
| 151 | + * @param oneTimeTokenRowMapper the {@link RowMapper} used for mapping the current row |
| 152 | + * in {@code java.sql.ResultSet} to {@link OneTimeToken} |
| 153 | + */ |
| 154 | + public void setOneTimeTokenRowMapper(RowMapper<OneTimeToken> oneTimeTokenRowMapper) { |
| 155 | + Assert.notNull(oneTimeTokenRowMapper, "oneTimeTokenRowMapper cannot be null"); |
| 156 | + this.oneTimeTokenRowMapper = oneTimeTokenRowMapper; |
| 157 | + } |
| 158 | + |
| 159 | + /** |
| 160 | + * The default {@code Function} that maps {@link OneTimeToken} to a {@code List} of |
| 161 | + * {@link SqlParameterValue}. |
| 162 | + * |
| 163 | + * @author Max Batischev |
| 164 | + * @since 6.4 |
| 165 | + */ |
| 166 | + public static class OneTimeTokenParametersMapper implements Function<OneTimeToken, List<SqlParameterValue>> { |
| 167 | + |
| 168 | + @Override |
| 169 | + public List<SqlParameterValue> apply(OneTimeToken oneTimeToken) { |
| 170 | + List<SqlParameterValue> parameters = new ArrayList<>(); |
| 171 | + parameters.add(new SqlParameterValue(Types.VARCHAR, oneTimeToken.getTokenValue())); |
| 172 | + parameters.add(new SqlParameterValue(Types.VARCHAR, oneTimeToken.getUsername())); |
| 173 | + parameters.add(new SqlParameterValue(Types.TIMESTAMP, Timestamp.from(oneTimeToken.getExpiresAt()))); |
| 174 | + return parameters; |
| 175 | + } |
| 176 | + |
| 177 | + } |
| 178 | + |
| 179 | + /** |
| 180 | + * The default {@link RowMapper} that maps the current row in |
| 181 | + * {@code java.sql.ResultSet} to {@link OneTimeToken}. |
| 182 | + * |
| 183 | + * @author Max Batischev |
| 184 | + * @since 6.4 |
| 185 | + */ |
| 186 | + public static class OneTimeTokenRowMapper implements RowMapper<OneTimeToken> { |
| 187 | + |
| 188 | + @Override |
| 189 | + public OneTimeToken mapRow(ResultSet rs, int rowNum) throws SQLException { |
| 190 | + String tokenValue = rs.getString("token_value"); |
| 191 | + String userName = rs.getString("username"); |
| 192 | + Instant expiresAt = rs.getTimestamp("expires_at").toInstant(); |
| 193 | + return new DefaultOneTimeToken(tokenValue, userName, expiresAt); |
| 194 | + } |
| 195 | + |
| 196 | + } |
| 197 | + |
| 198 | +} |
0 commit comments