2020-03-23 15:35:58 +01:00
|
|
|
/*
|
|
|
|
|
* MIT License
|
2017-01-17 14:40:50 +01:00
|
|
|
*
|
2020-03-23 15:35:58 +01:00
|
|
|
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
|
2017-01-17 14:40:50 +01:00
|
|
|
*
|
2020-03-23 15:35:58 +01:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
* furnished to do so, subject to the following conditions:
|
2017-01-17 14:40:50 +01:00
|
|
|
*
|
2020-03-23 15:35:58 +01:00
|
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
|
|
|
* copies or substantial portions of the Software.
|
2017-01-17 14:40:50 +01:00
|
|
|
*
|
2020-03-23 15:35:58 +01:00
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
|
* SOFTWARE.
|
2017-01-17 14:40:50 +01:00
|
|
|
*/
|
2020-03-23 15:35:58 +01:00
|
|
|
|
2017-01-17 14:40:50 +01:00
|
|
|
package sonia.scm.security;
|
|
|
|
|
|
2019-01-21 14:27:14 +01:00
|
|
|
import com.google.common.collect.ImmutableSet;
|
2017-01-17 14:40:50 +01:00
|
|
|
import io.jsonwebtoken.Claims;
|
2018-11-29 08:01:25 +01:00
|
|
|
|
|
|
|
|
import java.util.Collections;
|
2017-01-17 14:40:50 +01:00
|
|
|
import java.util.Date;
|
2018-11-29 08:01:25 +01:00
|
|
|
import java.util.Map;
|
2017-01-17 14:40:50 +01:00
|
|
|
import java.util.Optional;
|
2019-01-21 14:27:14 +01:00
|
|
|
import java.util.Set;
|
2017-01-17 14:40:50 +01:00
|
|
|
|
2018-11-30 09:22:02 +01:00
|
|
|
import static java.util.Optional.ofNullable;
|
|
|
|
|
|
2017-01-17 14:40:50 +01:00
|
|
|
/**
|
|
|
|
|
* Jwt implementation of {@link AccessToken}.
|
|
|
|
|
*
|
|
|
|
|
* @author Sebastian Sdorra
|
|
|
|
|
* @since 2.0.0
|
|
|
|
|
*/
|
|
|
|
|
public final class JwtAccessToken implements AccessToken {
|
2018-11-30 09:22:02 +01:00
|
|
|
|
2018-11-30 10:15:12 +01:00
|
|
|
public static final String REFRESHABLE_UNTIL_CLAIM_KEY = "scm-manager.refreshExpiration";
|
2018-11-30 09:22:02 +01:00
|
|
|
public static final String PARENT_TOKEN_ID_CLAIM_KEY = "scm-manager.parentTokenId";
|
2019-01-21 14:27:14 +01:00
|
|
|
public static final String GROUPS_CLAIM_KEY = "scm-manager.groups";
|
|
|
|
|
|
2017-01-17 14:40:50 +01:00
|
|
|
private final Claims claims;
|
|
|
|
|
private final String compact;
|
|
|
|
|
|
|
|
|
|
JwtAccessToken(Claims claims, String compact) {
|
|
|
|
|
this.claims = claims;
|
|
|
|
|
this.compact = compact;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public String getId() {
|
|
|
|
|
return claims.getId();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public String getSubject() {
|
|
|
|
|
return claims.getSubject();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public Optional<String> getIssuer() {
|
|
|
|
|
return Optional.ofNullable(claims.getIssuer());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public Date getIssuedAt() {
|
|
|
|
|
return claims.getIssuedAt();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public Date getExpiration() {
|
|
|
|
|
return claims.getExpiration();
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-29 08:01:25 +01:00
|
|
|
@Override
|
2018-11-30 09:22:02 +01:00
|
|
|
public Optional<Date> getRefreshExpiration() {
|
|
|
|
|
return ofNullable(claims.get(REFRESHABLE_UNTIL_CLAIM_KEY, Date.class));
|
2018-11-29 08:01:25 +01:00
|
|
|
}
|
|
|
|
|
|
2018-12-17 13:06:11 +01:00
|
|
|
@Override
|
2018-11-30 09:43:13 +01:00
|
|
|
public Optional<String> getParentKey() {
|
|
|
|
|
return ofNullable(claims.get(PARENT_TOKEN_ID_CLAIM_KEY).toString());
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-17 14:40:50 +01:00
|
|
|
@Override
|
|
|
|
|
public Scope getScope() {
|
|
|
|
|
return Scopes.fromClaims(claims);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2017-01-17 15:33:19 +01:00
|
|
|
@SuppressWarnings("unchecked")
|
2017-01-17 14:40:50 +01:00
|
|
|
public Optional<Object> getCustom(String key) {
|
|
|
|
|
return Optional.ofNullable(claims.get(key));
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-21 14:27:14 +01:00
|
|
|
@Override
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
|
public Set<String> getGroups() {
|
|
|
|
|
Iterable<String> groups = claims.get(GROUPS_CLAIM_KEY, Iterable.class);
|
|
|
|
|
if (groups != null) {
|
|
|
|
|
return ImmutableSet.copyOf(groups);
|
|
|
|
|
}
|
|
|
|
|
return ImmutableSet.of();
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-17 14:40:50 +01:00
|
|
|
@Override
|
|
|
|
|
public String compact() {
|
|
|
|
|
return compact;
|
|
|
|
|
}
|
2018-11-29 08:01:25 +01:00
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public Map<String, Object> getClaims() {
|
|
|
|
|
return Collections.unmodifiableMap(claims);
|
|
|
|
|
}
|
2017-01-17 14:40:50 +01:00
|
|
|
}
|