mirror of
https://github.com/scm-manager/scm-manager.git
synced 2026-07-28 14:09:07 +02:00
User jwt sessions can now be endless
Committed-by: Eduard Heimbuch <eduard.heimbuch@cloudogu.com> Co-authored-by: tzerr <thomas.zerr@cloudogu.com> Reviewed-by: Konstantin Schaper <konstantin.schaper@cloudogu.com>
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
|
||||
*
|
||||
* 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:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
package sonia.scm.lifecycle.jwt;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import javax.xml.bind.annotation.XmlAccessType;
|
||||
import javax.xml.bind.annotation.XmlAccessorType;
|
||||
import javax.xml.bind.annotation.XmlRootElement;
|
||||
|
||||
@Data
|
||||
@XmlRootElement(name = "jwtSettings")
|
||||
@XmlAccessorType(XmlAccessType.FIELD)
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class JwtSettings {
|
||||
|
||||
private boolean endlessJwtEnabledLastStartUp = false;
|
||||
private long keysValidAfterTimestampInMs = 0;
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
/*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
|
||||
*
|
||||
* 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:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
package sonia.scm.lifecycle.jwt;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import sonia.scm.lifecycle.PrivilegedStartupAction;
|
||||
import sonia.scm.plugin.Extension;
|
||||
import sonia.scm.security.JwtSystemProperties;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import java.time.Clock;
|
||||
import java.time.Instant;
|
||||
|
||||
@Extension
|
||||
public class JwtSettingsStartupAction implements PrivilegedStartupAction {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(JwtSettingsStartupAction.class);
|
||||
private final JwtSettingsStore store;
|
||||
private final Clock clock;
|
||||
|
||||
@Inject
|
||||
public JwtSettingsStartupAction(JwtSettingsStore store) {
|
||||
this(store, Clock.systemDefaultZone());
|
||||
}
|
||||
|
||||
public JwtSettingsStartupAction(JwtSettingsStore store, Clock clock) {
|
||||
this.store = store;
|
||||
this.clock = clock;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
LOG.debug("Checking JWT Settings");
|
||||
|
||||
JwtSettings settings = store.get();
|
||||
boolean isEndlessJwtEnabledNow = JwtSystemProperties.isEndlessJwtEnabled();
|
||||
|
||||
if(!areSettingsChanged(settings, isEndlessJwtEnabledNow)) {
|
||||
LOG.debug("JWT Settings unchanged");
|
||||
return;
|
||||
}
|
||||
|
||||
JwtSettings updatedSettings = new JwtSettings();
|
||||
updatedSettings.setEndlessJwtEnabledLastStartUp(isEndlessJwtEnabledNow);
|
||||
|
||||
if(areEndlessJwtNeedingInvalidation(settings, isEndlessJwtEnabledNow)) {
|
||||
updatedSettings.setKeysValidAfterTimestampInMs(Instant.now(clock).toEpochMilli());
|
||||
} else {
|
||||
updatedSettings.setKeysValidAfterTimestampInMs(settings.getKeysValidAfterTimestampInMs());
|
||||
}
|
||||
|
||||
store.set(updatedSettings);
|
||||
|
||||
LOG.debug("JWT Settings updated");
|
||||
}
|
||||
|
||||
private boolean areSettingsChanged(JwtSettings settings, boolean isEndlessJwtEnabledNow) {
|
||||
return settings.isEndlessJwtEnabledLastStartUp() != isEndlessJwtEnabledNow;
|
||||
}
|
||||
|
||||
private boolean areEndlessJwtNeedingInvalidation(JwtSettings settings, boolean isEndlessJwtEnabledNow) {
|
||||
return settings.isEndlessJwtEnabledLastStartUp() && !isEndlessJwtEnabledNow;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
|
||||
*
|
||||
* 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:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
package sonia.scm.lifecycle.jwt;
|
||||
|
||||
import sonia.scm.store.ConfigurationStore;
|
||||
import sonia.scm.store.ConfigurationStoreFactory;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import javax.inject.Singleton;
|
||||
|
||||
@Singleton
|
||||
public class JwtSettingsStore {
|
||||
|
||||
static final String STORE_NAME = "jwt-settings";
|
||||
private final ConfigurationStore<JwtSettings> store;
|
||||
|
||||
@Inject
|
||||
public JwtSettingsStore(ConfigurationStoreFactory storeFactory) {
|
||||
store = storeFactory.withType(JwtSettings.class).withName(STORE_NAME).build();
|
||||
}
|
||||
|
||||
public JwtSettings get() {
|
||||
return store.getOptional().orElse(new JwtSettings());
|
||||
}
|
||||
|
||||
public void set(JwtSettings settings) {
|
||||
store.set(settings);
|
||||
}
|
||||
}
|
||||
@@ -45,6 +45,8 @@ import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static sonia.scm.security.JwtSystemProperties.ENDLESS_JWT;
|
||||
|
||||
/**
|
||||
* Jwt implementation of {@link AccessTokenBuilder}.
|
||||
*
|
||||
@@ -184,9 +186,11 @@ public final class JwtAccessTokenBuilder implements AccessTokenBuilder {
|
||||
Claims claims = Jwts.claims(customClaims)
|
||||
.setSubject(sub)
|
||||
.setId(id)
|
||||
.setIssuedAt(Date.from(now))
|
||||
.setExpiration(new Date(now.toEpochMilli() + expiration));
|
||||
.setIssuedAt(Date.from(now));
|
||||
|
||||
if(!JwtSystemProperties.isEndlessJwtEnabled()) {
|
||||
claims.setExpiration(new Date(now.toEpochMilli() + expiration));
|
||||
}
|
||||
|
||||
if (refreshableFor > 0) {
|
||||
long re = refreshableForUnit.toMillis(refreshableFor);
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
|
||||
*
|
||||
* 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:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
package sonia.scm.security;
|
||||
|
||||
public class JwtSystemProperties {
|
||||
|
||||
public static final String ENDLESS_JWT = "scm.endlessJwt";
|
||||
|
||||
public static boolean isEndlessJwtEnabled() {
|
||||
return Boolean.parseBoolean(System.getProperty(ENDLESS_JWT));
|
||||
}
|
||||
}
|
||||
@@ -36,6 +36,8 @@ import io.jsonwebtoken.SigningKeyResolverAdapter;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import sonia.scm.lifecycle.jwt.JwtSettings;
|
||||
import sonia.scm.lifecycle.jwt.JwtSettingsStore;
|
||||
import sonia.scm.store.ConfigurationEntryStore;
|
||||
import sonia.scm.store.ConfigurationEntryStoreFactory;
|
||||
|
||||
@@ -82,16 +84,17 @@ public class SecureKeyResolver extends SigningKeyResolverAdapter
|
||||
*/
|
||||
@Inject
|
||||
@SuppressWarnings("unchecked")
|
||||
public SecureKeyResolver(ConfigurationEntryStoreFactory storeFactory) {
|
||||
this(storeFactory, new SecureRandom());
|
||||
public SecureKeyResolver(ConfigurationEntryStoreFactory storeFactory, JwtSettingsStore jwtSettingsStore) {
|
||||
this(storeFactory, jwtSettingsStore, new SecureRandom());
|
||||
}
|
||||
|
||||
SecureKeyResolver(ConfigurationEntryStoreFactory storeFactory, Random random)
|
||||
SecureKeyResolver(ConfigurationEntryStoreFactory storeFactory, JwtSettingsStore jwtSettingsStore, Random random)
|
||||
{
|
||||
store = storeFactory
|
||||
.withType(SecureKey.class)
|
||||
.withName(STORE_NAME)
|
||||
.build();
|
||||
this.jwtSettingsStore = jwtSettingsStore;
|
||||
this.random = random;
|
||||
}
|
||||
|
||||
@@ -109,13 +112,7 @@ public class SecureKeyResolver extends SigningKeyResolverAdapter
|
||||
|
||||
checkArgument(!Strings.isNullOrEmpty(subject), "subject is required");
|
||||
|
||||
SecureKey key = store.get(subject);
|
||||
|
||||
if (key == null) {
|
||||
return getSecureKey(subject).getBytes();
|
||||
}
|
||||
|
||||
return key.getBytes();
|
||||
return getSecureKey(subject).getBytes();
|
||||
}
|
||||
|
||||
//~--- get methods ----------------------------------------------------------
|
||||
@@ -132,7 +129,7 @@ public class SecureKeyResolver extends SigningKeyResolverAdapter
|
||||
{
|
||||
SecureKey key = store.get(subject);
|
||||
|
||||
if (key == null)
|
||||
if (key == null || isKeyExpired(key))
|
||||
{
|
||||
logger.trace("create new key for subject");
|
||||
key = createNewKey();
|
||||
@@ -142,6 +139,12 @@ public class SecureKeyResolver extends SigningKeyResolverAdapter
|
||||
return key;
|
||||
}
|
||||
|
||||
private boolean isKeyExpired(SecureKey key) {
|
||||
JwtSettings settings = jwtSettingsStore.get();
|
||||
|
||||
return key.getCreationDate() < settings.getKeysValidAfterTimestampInMs();
|
||||
}
|
||||
|
||||
//~--- methods --------------------------------------------------------------
|
||||
|
||||
/**
|
||||
@@ -166,4 +169,6 @@ public class SecureKeyResolver extends SigningKeyResolverAdapter
|
||||
|
||||
/** configuration entry store */
|
||||
private final ConfigurationEntryStore<SecureKey> store;
|
||||
|
||||
private final JwtSettingsStore jwtSettingsStore;
|
||||
}
|
||||
|
||||
@@ -105,7 +105,7 @@ public class TokenRefreshFilter extends HttpFilter {
|
||||
LOG.trace("could not resolve token", e);
|
||||
return;
|
||||
}
|
||||
if (accessToken instanceof JwtAccessToken) {
|
||||
if (accessToken instanceof JwtAccessToken && !isEndlessToken((JwtAccessToken) accessToken)) {
|
||||
refresher.refresh((JwtAccessToken) accessToken)
|
||||
.ifPresent(jwtAccessToken -> refreshJwtToken(request, response, jwtAccessToken));
|
||||
}
|
||||
@@ -116,4 +116,8 @@ public class TokenRefreshFilter extends HttpFilter {
|
||||
LOG.debug("refreshing JWT authentication token");
|
||||
issuer.authenticate(request, response, jwtAccessToken);
|
||||
}
|
||||
|
||||
private boolean isEndlessToken(JwtAccessToken token) {
|
||||
return token.getExpiration() == null;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user