2020-03-23 15:35:58 +01:00
|
|
|
/*
|
|
|
|
|
* 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.
|
2017-01-17 14:40:50 +01:00
|
|
|
*/
|
2020-10-23 14:42:56 +02:00
|
|
|
|
2017-01-17 14:40:50 +01:00
|
|
|
package sonia.scm.security;
|
|
|
|
|
|
|
|
|
|
import com.google.common.collect.Sets;
|
|
|
|
|
import io.jsonwebtoken.Claims;
|
|
|
|
|
import io.jsonwebtoken.Jwts;
|
2020-10-23 14:42:56 +02:00
|
|
|
import org.apache.shiro.authz.AuthorizationException;
|
|
|
|
|
import org.apache.shiro.subject.PrincipalCollection;
|
|
|
|
|
import org.apache.shiro.subject.Subject;
|
2018-08-29 14:30:23 +02:00
|
|
|
import org.apache.shiro.util.ThreadContext;
|
2020-10-23 14:42:56 +02:00
|
|
|
import org.junit.jupiter.api.AfterEach;
|
|
|
|
|
import org.junit.jupiter.api.BeforeEach;
|
|
|
|
|
import org.junit.jupiter.api.Nested;
|
|
|
|
|
import org.junit.jupiter.api.Test;
|
|
|
|
|
import org.junit.jupiter.api.extension.ExtendWith;
|
2017-01-17 14:40:50 +01:00
|
|
|
import org.mockito.Mock;
|
2020-10-23 14:42:56 +02:00
|
|
|
import org.mockito.junit.jupiter.MockitoExtension;
|
2017-01-17 14:40:50 +01:00
|
|
|
|
2020-11-03 11:42:44 +01:00
|
|
|
import java.util.Collection;
|
2018-08-30 11:48:58 +02:00
|
|
|
import java.util.Set;
|
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
2020-10-23 14:42:56 +02:00
|
|
|
import static java.util.Collections.singleton;
|
|
|
|
|
import static org.assertj.core.api.Assertions.assertThat;
|
|
|
|
|
import static org.junit.Assert.assertThrows;
|
2019-08-02 09:32:44 +02:00
|
|
|
import static org.mockito.Mockito.anyString;
|
2020-10-23 14:42:56 +02:00
|
|
|
import static org.mockito.Mockito.lenient;
|
2018-11-30 11:26:23 +01:00
|
|
|
import static sonia.scm.security.SecureKeyTestUtil.createSecureKey;
|
2018-08-30 11:48:58 +02:00
|
|
|
|
2017-01-17 14:40:50 +01:00
|
|
|
/**
|
|
|
|
|
* Unit test for {@link JwtAccessTokenBuilder}.
|
2020-10-23 14:42:56 +02:00
|
|
|
*
|
2017-01-17 14:40:50 +01:00
|
|
|
* @author Sebastian Sdorra
|
|
|
|
|
*/
|
2020-10-23 14:42:56 +02:00
|
|
|
@ExtendWith(MockitoExtension.class)
|
|
|
|
|
class JwtAccessTokenBuilderTest {
|
2018-08-29 14:30:23 +02:00
|
|
|
|
2017-01-17 14:40:50 +01:00
|
|
|
@Mock
|
|
|
|
|
private KeyGenerator keyGenerator;
|
2020-10-23 14:42:56 +02:00
|
|
|
|
2017-01-17 14:40:50 +01:00
|
|
|
@Mock
|
|
|
|
|
private SecureKeyResolver secureKeyResolver;
|
2020-10-23 14:42:56 +02:00
|
|
|
|
2017-01-17 15:33:19 +01:00
|
|
|
private Set<AccessTokenEnricher> enrichers;
|
2020-10-23 14:42:56 +02:00
|
|
|
|
2017-01-17 15:33:19 +01:00
|
|
|
private JwtAccessTokenBuilderFactory factory;
|
2020-10-23 14:42:56 +02:00
|
|
|
|
|
|
|
|
@Mock
|
|
|
|
|
private Subject subject;
|
|
|
|
|
@Mock
|
|
|
|
|
private PrincipalCollection principalCollection;
|
|
|
|
|
|
|
|
|
|
@BeforeEach
|
|
|
|
|
void bindSubject() {
|
|
|
|
|
lenient().when(subject.getPrincipal()).thenReturn("trillian");
|
|
|
|
|
lenient().when(subject.getPrincipals()).thenReturn(principalCollection);
|
|
|
|
|
ThreadContext.bind(subject);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@AfterEach
|
|
|
|
|
void unbindSubject() {
|
|
|
|
|
ThreadContext.unbindSubject();
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-17 14:40:50 +01:00
|
|
|
/**
|
|
|
|
|
* Prepare mocks and set up object under test.
|
|
|
|
|
*/
|
2020-10-23 14:42:56 +02:00
|
|
|
@BeforeEach
|
|
|
|
|
void setUpObjectUnderTest() {
|
|
|
|
|
lenient().when(keyGenerator.createKey()).thenReturn("42");
|
|
|
|
|
lenient().when(secureKeyResolver.getSecureKey(anyString())).thenReturn(createSecureKey());
|
2017-01-17 14:40:50 +01:00
|
|
|
enrichers = Sets.newHashSet();
|
2017-01-17 15:33:19 +01:00
|
|
|
factory = new JwtAccessTokenBuilderFactory(keyGenerator, secureKeyResolver, enrichers);
|
|
|
|
|
}
|
2020-10-23 14:42:56 +02:00
|
|
|
|
2017-01-17 14:40:50 +01:00
|
|
|
/**
|
|
|
|
|
* Tests {@link JwtAccessTokenBuilder#build()}.
|
|
|
|
|
*/
|
|
|
|
|
@Test
|
2020-10-23 14:42:56 +02:00
|
|
|
void testBuild() {
|
2017-01-17 15:33:19 +01:00
|
|
|
JwtAccessToken token = factory.create().subject("dent")
|
2017-01-17 14:40:50 +01:00
|
|
|
.issuer("https://www.scm-manager.org")
|
|
|
|
|
.expiresIn(5, TimeUnit.SECONDS)
|
|
|
|
|
.custom("a", "b")
|
|
|
|
|
.scope(Scope.valueOf("repo:*"))
|
|
|
|
|
.build();
|
2020-10-23 14:42:56 +02:00
|
|
|
|
2017-01-17 14:40:50 +01:00
|
|
|
// assert claims
|
|
|
|
|
assertClaims(token);
|
2020-10-23 14:42:56 +02:00
|
|
|
|
2017-01-17 14:40:50 +01:00
|
|
|
// reparse and assert again
|
|
|
|
|
String compact = token.compact();
|
2020-10-23 14:42:56 +02:00
|
|
|
assertThat(compact).isNotEmpty();
|
2017-01-17 14:40:50 +01:00
|
|
|
Claims claims = Jwts.parser()
|
|
|
|
|
.setSigningKey(secureKeyResolver.getSecureKey("dent").getBytes())
|
|
|
|
|
.parseClaimsJws(compact)
|
|
|
|
|
.getBody();
|
|
|
|
|
assertClaims(new JwtAccessToken(claims, compact));
|
|
|
|
|
}
|
2019-02-15 10:23:46 +01:00
|
|
|
|
2020-10-23 14:42:56 +02:00
|
|
|
private void assertClaims(JwtAccessToken token) {
|
|
|
|
|
assertThat(token.getId()).isNotEmpty();
|
|
|
|
|
assertThat(token.getIssuedAt()).isNotNull();
|
|
|
|
|
assertThat(token.getExpiration()).isNotNull();
|
|
|
|
|
assertThat(token.getExpiration().getTime() > token.getIssuedAt().getTime()).isTrue();
|
|
|
|
|
assertThat(token.getSubject()).isEqualTo("dent");
|
|
|
|
|
assertThat(token.getIssuer()).isNotEmpty();
|
|
|
|
|
assertThat(token.getIssuer()).get().isEqualTo("https://www.scm-manager.org");
|
|
|
|
|
assertThat(token.getCustom("a")).get().isEqualTo("b");
|
|
|
|
|
assertThat(token.getScope()).hasToString("[\"repo:*\"]");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Nested
|
|
|
|
|
class FromApiKeyRealm {
|
|
|
|
|
|
2020-11-03 11:42:44 +01:00
|
|
|
private Scope scope;
|
|
|
|
|
|
2020-10-23 14:42:56 +02:00
|
|
|
@BeforeEach
|
|
|
|
|
void mockApiKeyRealm() {
|
2020-11-03 11:42:44 +01:00
|
|
|
scope = Scope.valueOf("dummy:scope:*");
|
2020-10-23 14:42:56 +02:00
|
|
|
lenient().when(principalCollection.getRealmNames()).thenReturn(singleton("ApiTokenRealm"));
|
2020-11-03 11:42:44 +01:00
|
|
|
lenient().when(principalCollection.oneByType(Scope.class)).thenReturn(scope);
|
2020-10-23 14:42:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
2020-11-04 09:38:29 +01:00
|
|
|
void shouldCreateJwtAndUsePreviousScope() {
|
2020-10-23 14:42:56 +02:00
|
|
|
JwtAccessTokenBuilder builder = factory.create().subject("dent");
|
2020-11-03 11:42:44 +01:00
|
|
|
final JwtAccessToken accessToken = builder.build();
|
|
|
|
|
assertThat(accessToken).isNotNull();
|
|
|
|
|
assertThat(accessToken.getSubject()).isEqualTo("dent");
|
|
|
|
|
assertThat((Collection<String>) accessToken.getCustom("scope").get()).containsExactly("dummy:scope:*");
|
2020-10-23 14:42:56 +02:00
|
|
|
}
|
2020-11-04 09:38:29 +01:00
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
void shouldThrowExceptionWhenScopeAlreadyDefinedInBuilder() {
|
|
|
|
|
JwtAccessTokenBuilder builder = factory.create().scope(Scope.valueOf("an:incompatible:scope")).subject("dent");
|
|
|
|
|
assertThrows(AuthorizationException.class, builder::build);
|
|
|
|
|
}
|
2020-10-23 14:42:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Nested
|
|
|
|
|
class FromDefaultRealm {
|
|
|
|
|
|
|
|
|
|
@BeforeEach
|
|
|
|
|
void mockDefaultRealm() {
|
|
|
|
|
lenient().when(principalCollection.getRealmNames()).thenReturn(singleton("DefaultRealm"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Tests {@link JwtAccessTokenBuilder#build()} with subject from shiro context.
|
|
|
|
|
*/
|
|
|
|
|
@Test
|
|
|
|
|
void testBuildWithoutSubject() {
|
|
|
|
|
JwtAccessToken token = factory.create().build();
|
|
|
|
|
assertThat(token.getSubject()).isEqualTo("trillian");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Tests {@link JwtAccessTokenBuilder#build()} with explicit subject.
|
|
|
|
|
*/
|
|
|
|
|
@Test
|
|
|
|
|
void testBuildWithSubject() {
|
|
|
|
|
JwtAccessToken token = factory.create().subject("dent").build();
|
|
|
|
|
assertThat(token.getSubject()).isEqualTo("dent");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Tests {@link JwtAccessTokenBuilder#build()} with enricher.
|
|
|
|
|
*/
|
|
|
|
|
@Test
|
|
|
|
|
void testBuildWithEnricher() {
|
|
|
|
|
enrichers.add((b) -> b.custom("c", "d"));
|
|
|
|
|
JwtAccessToken token = factory.create().subject("dent").build();
|
|
|
|
|
assertThat(token.getCustom("c")).get().isEqualTo("d");
|
|
|
|
|
}
|
2017-01-17 14:40:50 +01:00
|
|
|
}
|
2018-08-08 09:20:08 +02:00
|
|
|
}
|