forked from apache/syncope
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AccessTokenDataBinderImpl.java
183 lines (145 loc) · 6.8 KB
/
AccessTokenDataBinderImpl.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.syncope.core.provisioning.java.data;
import com.fasterxml.uuid.Generators;
import com.fasterxml.uuid.impl.RandomBasedGenerator;
import java.util.Date;
import java.util.Map;
import javax.annotation.Resource;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.commons.lang3.tuple.Triple;
import org.apache.cxf.rs.security.jose.common.JoseType;
import org.apache.cxf.rs.security.jose.jws.JwsHeaders;
import org.apache.cxf.rs.security.jose.jws.JwsJwtCompactConsumer;
import org.apache.cxf.rs.security.jose.jws.JwsJwtCompactProducer;
import org.apache.cxf.rs.security.jose.jws.JwsSignatureProvider;
import org.apache.cxf.rs.security.jose.jwt.JwtClaims;
import org.apache.cxf.rs.security.jose.jwt.JwtToken;
import org.apache.syncope.common.lib.to.AccessTokenTO;
import org.apache.syncope.core.persistence.api.dao.AccessTokenDAO;
import org.apache.syncope.core.persistence.api.dao.ConfDAO;
import org.apache.syncope.core.persistence.api.entity.AccessToken;
import org.apache.syncope.core.persistence.api.entity.EntityFactory;
import org.apache.syncope.core.provisioning.api.data.AccessTokenDataBinder;
import org.apache.syncope.core.spring.BeanUtils;
import org.apache.syncope.core.spring.security.DefaultCredentialChecker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class AccessTokenDataBinderImpl implements AccessTokenDataBinder {
private static final String[] IGNORE_PROPERTIES = { "owner" };
private static final RandomBasedGenerator UUID_GENERATOR = Generators.randomBasedGenerator();
@Resource(name = "adminUser")
private String adminUser;
@Resource(name = "jwtIssuer")
private String jwtIssuer;
@Autowired
private JwsSignatureProvider jwsSignatureProvider;
@Autowired
private AccessTokenDAO accessTokenDAO;
@Autowired
private ConfDAO confDAO;
@Autowired
private EntityFactory entityFactory;
@Autowired
private DefaultCredentialChecker credentialChecker;
@Override
public Triple<String, String, Date> generateJWT(
final String subject, final long duration, final Map<String, Object> claims) {
credentialChecker.checkIsDefaultJWSKeyInUse();
long currentTime = new Date().getTime() / 1000L;
long expiryTime = currentTime + 60L * duration;
JwtClaims jwtClaims = new JwtClaims();
jwtClaims.setTokenId(UUID_GENERATOR.generate().toString());
jwtClaims.setSubject(subject);
jwtClaims.setIssuedAt(currentTime);
jwtClaims.setIssuer(jwtIssuer);
jwtClaims.setExpiryTime(expiryTime);
jwtClaims.setNotBefore(currentTime);
for (Map.Entry<String, Object> entry : claims.entrySet()) {
jwtClaims.setClaim(entry.getKey(), entry.getValue());
}
JwsHeaders jwsHeaders = new JwsHeaders(JoseType.JWT, jwsSignatureProvider.getAlgorithm());
JwtToken token = new JwtToken(jwsHeaders, jwtClaims);
JwsJwtCompactProducer producer = new JwsJwtCompactProducer(token);
String signed = producer.signWith(jwsSignatureProvider);
return Triple.of(jwtClaims.getTokenId(), signed, new Date(expiryTime * 1000L));
}
@Override
public Pair<String, Date> create(
final String subject,
final Map<String, Object> claims,
final byte[] authorities,
final boolean replaceExisting) {
String body = null;
Date expiryTime = null;
AccessToken existing = accessTokenDAO.findByOwner(subject);
if (existing != null) {
body = existing.getBody();
expiryTime = existing.getExpiryTime();
}
if (replaceExisting || body == null) {
Triple<String, String, Date> created = generateJWT(
subject,
confDAO.find("jwt.lifetime.minutes", 120L),
claims);
body = created.getMiddle();
expiryTime = created.getRight();
AccessToken accessToken = entityFactory.newEntity(AccessToken.class);
accessToken.setKey(created.getLeft());
accessToken.setBody(body);
accessToken.setExpiryTime(expiryTime);
accessToken.setOwner(subject);
if (!adminUser.equals(accessToken.getOwner())) {
accessToken.setAuthorities(authorities);
}
accessTokenDAO.replace(replaceExisting, accessToken);
}
return Pair.of(body, expiryTime);
}
@Override
public Pair<String, Date> update(final AccessToken accessToken, final byte[] authorities) {
JwsJwtCompactConsumer consumer = new JwsJwtCompactConsumer(accessToken.getBody());
credentialChecker.checkIsDefaultJWSKeyInUse();
long duration = confDAO.find("jwt.lifetime.minutes", 120L);
long currentTime = new Date().getTime() / 1000L;
long expiry = currentTime + 60L * duration;
consumer.getJwtClaims().setExpiryTime(expiry);
Date expiryDate = new Date(expiry * 1000L);
JwsHeaders jwsHeaders = new JwsHeaders(JoseType.JWT, jwsSignatureProvider.getAlgorithm());
JwtToken token = new JwtToken(jwsHeaders, consumer.getJwtClaims());
JwsJwtCompactProducer producer = new JwsJwtCompactProducer(token);
String body = producer.signWith(jwsSignatureProvider);
accessToken.setBody(body);
// AccessToken stores expiry time in milliseconds, as opposed to seconds for the JWT tokens.
accessToken.setExpiryTime(expiryDate);
if (!adminUser.equals(accessToken.getOwner())) {
accessToken.setAuthorities(authorities);
}
accessTokenDAO.save(accessToken);
return Pair.of(body, expiryDate);
}
@Override
public AccessTokenTO getAccessTokenTO(final AccessToken accessToken) {
AccessTokenTO accessTokenTO = new AccessTokenTO();
BeanUtils.copyProperties(accessToken, accessTokenTO, IGNORE_PROPERTIES);
accessTokenTO.setOwner(accessToken.getOwner());
return accessTokenTO;
}
}