/
JdbcQueryableClientDetailsService.java
142 lines (124 loc) · 5.79 KB
/
JdbcQueryableClientDetailsService.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*******************************************************************************
* Cloud Foundry
* Copyright (c) [2009-2016] Pivotal Software, Inc. All Rights Reserved.
*
* This product is licensed to you under the Apache License, Version 2.0 (the "License").
* You may not use this product except in compliance with the License.
*
* This product includes a number of subcomponents with
* separate copyright notices and license terms. Your use of these
* subcomponents is subject to the terms and conditions of the
* subcomponent's license, as noted in the LICENSE file.
*******************************************************************************/
package org.cloudfoundry.identity.uaa.client;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.cloudfoundry.identity.uaa.resources.QueryableResourceManager;
import org.cloudfoundry.identity.uaa.resources.jdbc.AbstractQueryable;
import org.cloudfoundry.identity.uaa.resources.jdbc.JdbcPagingListFactory;
import org.cloudfoundry.identity.uaa.util.JsonUtils;
import org.cloudfoundry.identity.uaa.zone.IdentityZoneHolder;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.security.oauth2.provider.ClientDetails;
import org.springframework.security.oauth2.provider.client.BaseClientDetails;
import org.springframework.security.oauth2.provider.client.JdbcClientDetailsService;
import org.springframework.util.StringUtils;
public class JdbcQueryableClientDetailsService extends AbstractQueryable<ClientDetails> implements
QueryableResourceManager<ClientDetails> {
private static final Log logger = LogFactory.getLog(JdbcQueryableClientDetailsService.class);
private JdbcClientDetailsService delegate;
private static final String CLIENT_FIELDS = "client_id, client_secret, resource_ids, scope, "
+ "authorized_grant_types, web_server_redirect_uri, authorities, access_token_validity, "
+ "refresh_token_validity, additional_information, autoapprove, lastmodified";
public static final String CLIENT_DETAILS_TABLE = "oauth_client_details";
private static final String BASE_FIND_STATEMENT = "select " + CLIENT_FIELDS
+ " from " + CLIENT_DETAILS_TABLE;
public JdbcQueryableClientDetailsService(JdbcClientDetailsService delegate, JdbcTemplate jdbcTemplate,
JdbcPagingListFactory pagingListFactory) {
super(jdbcTemplate, pagingListFactory, new ClientDetailsRowMapper());
this.delegate = delegate;
}
@Override
protected String getBaseSqlQuery() {
return BASE_FIND_STATEMENT;
}
@Override
protected String getTableName() {
return CLIENT_DETAILS_TABLE;
}
@Override
public List<ClientDetails> query(String filter, String sortBy, boolean ascending) {
//validate syntax
getQueryConverter().convert(filter, sortBy, ascending);
if (StringUtils.hasText(filter)) {
filter = "(" + filter + ") and ";
}
filter += " identity_zone_id eq \""+IdentityZoneHolder.get().getId()+"\"";
return super.query(filter, sortBy, ascending);
}
@Override
public List<ClientDetails> retrieveAll() {
return delegate.listClientDetails();
}
@Override
public ClientDetails retrieve(String id) {
return delegate.loadClientByClientId(id);
}
@Override
public ClientDetails create(ClientDetails resource) {
delegate.addClientDetails(resource);
return delegate.loadClientByClientId(resource.getClientId());
}
@Override
public ClientDetails update(String id, ClientDetails resource) {
delegate.updateClientDetails(resource);
return delegate.loadClientByClientId(id);
}
@Override
public ClientDetails delete(String id, int version) {
ClientDetails client = delegate.loadClientByClientId(id);
delegate.removeClientDetails(id);
return client;
}
@Override
protected void validateOrderBy(String orderBy) throws IllegalArgumentException {
super.validateOrderBy(orderBy, CLIENT_FIELDS);
}
private static class ClientDetailsRowMapper implements RowMapper<ClientDetails> {
@Override
public ClientDetails mapRow(ResultSet rs, int rowNum) throws SQLException {
BaseClientDetails details = new BaseClientDetails(rs.getString(1), rs.getString(3), rs.getString(4),
rs.getString(5), rs.getString(7), rs.getString(6));
details.setClientSecret(rs.getString(2));
if (rs.getObject(8) != null) {
details.setAccessTokenValiditySeconds(rs.getInt(8));
}
if (rs.getObject(9) != null) {
details.setRefreshTokenValiditySeconds(rs.getInt(9));
}
String json = rs.getString(10);
if (json != null) {
try {
@SuppressWarnings("unchecked")
Map<String, Object> additionalInformation = JsonUtils.readValue(json, Map.class);
details.setAdditionalInformation(additionalInformation);
} catch (Exception e) {
logger.warn("Could not decode JSON for additional information: " + details, e);
}
}
String scopes = rs.getString(11);
if (scopes != null) {
details.setAutoApproveScopes(StringUtils.commaDelimitedListToSet(scopes));
}
if (rs.getTimestamp(12) != null) {
details.addAdditionalInformation("lastModified", rs.getTimestamp(12));
}
return details;
}
}
}