Merge pull request #1385 from scm-manager/bugfix/logging_error_reading_api_token

Reduce logging of ApiTokenRealm
This commit is contained in:
René Pfeuffer
2020-10-23 08:15:22 +02:00
committed by GitHub
4 changed files with 21 additions and 5 deletions

View File

@@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Source code fullscreen view ([#1376](https://github.com/scm-manager/scm-manager/pull/1376))
### Changed
- Reduce logging of ApiTokenRealm ([#1385](https://github.com/scm-manager/scm-manager/pull/1385))
- Centralise syntax highlighting ([#1382](https://github.com/scm-manager/scm-manager/pull/1382))
### Fixed

View File

@@ -24,6 +24,7 @@
package sonia.scm.security;
import com.google.common.io.BaseEncoding;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.UsernamePasswordToken;
@@ -61,13 +62,14 @@ public class ApiKeyRealm extends AuthenticatingRealm {
}
@Override
@SuppressWarnings("java:S4738") // java.util.Base64 has no canDecode method
public boolean supports(AuthenticationToken token) {
if (token instanceof UsernamePasswordToken || token instanceof BearerToken) {
boolean containsDot = getPassword(token).contains(".");
if (containsDot) {
LOG.debug("Ignoring token with at least one dot ('.'); this is probably a JWT token");
boolean isBase64 = BaseEncoding.base64().canDecode(getPassword(token));
if (!isBase64) {
LOG.debug("Ignoring non base 64 token; this is probably a JWT token or a normal password");
}
return !containsDot;
return isBase64;
}
return false;
}

View File

@@ -63,7 +63,10 @@ class ApiKeyTokenHandler {
try {
return of(OBJECT_MAPPER.readValue(decoder.decode(token), Token.class));
} catch (IOException | DecodingException e) {
LOG.warn("error reading api token", e);
LOG.debug("failed to read api token, perhaps it is a jwt token or a normal password");
if (LOG.isTraceEnabled()) {
LOG.trace("failed to parse token", e);
}
return empty();
}
}

View File

@@ -25,6 +25,7 @@
package sonia.scm.security;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.AuthorizationException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@@ -105,6 +106,15 @@ class ApiKeyRealmTest {
assertThat(supports).isFalse();
}
@Test
void shouldIgnoreNonBase64Tokens() {
UsernamePasswordToken token = new UsernamePasswordToken("trillian", "My&SecretPassword");
boolean supports = realm.supports(token);
assertThat(supports).isFalse();
}
void verifyScopeSet(String... permissions) {
verify(authenticationInfoBuilder).withScope(argThat(scope -> {
assertThat(scope).containsExactly(permissions);