mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-17 10:41:06 +01:00
token enricher should use new access token api
This commit is contained in:
@@ -63,9 +63,9 @@ public class JwtAccessTokenBuilderTest {
|
||||
@Mock
|
||||
private SecureKeyResolver secureKeyResolver;
|
||||
|
||||
private Set<TokenClaimsEnricher> enrichers;
|
||||
private Set<AccessTokenEnricher> enrichers;
|
||||
|
||||
private JwtAccessTokenBuilder builder;
|
||||
private JwtAccessTokenBuilderFactory factory;
|
||||
|
||||
@Rule
|
||||
public ShiroRule shiro = new ShiroRule();
|
||||
@@ -78,9 +78,8 @@ public class JwtAccessTokenBuilderTest {
|
||||
when(keyGenerator.createKey()).thenReturn("42");
|
||||
when(secureKeyResolver.getSecureKey(anyString())).thenReturn(createSecureKey());
|
||||
enrichers = Sets.newHashSet();
|
||||
JwtAccessTokenBuilderFactory factory = new JwtAccessTokenBuilderFactory(keyGenerator, secureKeyResolver, enrichers);
|
||||
builder = factory.create();
|
||||
}
|
||||
factory = new JwtAccessTokenBuilderFactory(keyGenerator, secureKeyResolver, enrichers);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests {@link JwtAccessTokenBuilder#build()} with subject from shiro context.
|
||||
@@ -92,7 +91,7 @@ public class JwtAccessTokenBuilderTest {
|
||||
password = "secret"
|
||||
)
|
||||
public void testBuildWithoutSubject() {
|
||||
JwtAccessToken token = builder.build();
|
||||
JwtAccessToken token = factory.create().build();
|
||||
assertEquals("trillian", token.getSubject());
|
||||
}
|
||||
|
||||
@@ -101,7 +100,7 @@ public class JwtAccessTokenBuilderTest {
|
||||
*/
|
||||
@Test
|
||||
public void testBuildWithSubject() {
|
||||
JwtAccessToken token = builder.subject("dent").build();
|
||||
JwtAccessToken token = factory.create().subject("dent").build();
|
||||
assertEquals("dent", token.getSubject());
|
||||
}
|
||||
|
||||
@@ -110,8 +109,8 @@ public class JwtAccessTokenBuilderTest {
|
||||
*/
|
||||
@Test
|
||||
public void testBuildWithEnricher() {
|
||||
enrichers.add((claims) -> claims.put("c", "d"));
|
||||
JwtAccessToken token = builder.subject("dent").build();
|
||||
enrichers.add((b) -> b.custom("c", "d"));
|
||||
JwtAccessToken token = factory.create().subject("dent").build();
|
||||
assertEquals("d", token.getCustom("c").get());
|
||||
}
|
||||
|
||||
@@ -120,7 +119,7 @@ public class JwtAccessTokenBuilderTest {
|
||||
*/
|
||||
@Test
|
||||
public void testBuild(){
|
||||
JwtAccessToken token = builder.subject("dent")
|
||||
JwtAccessToken token = factory.create().subject("dent")
|
||||
.issuer("https://www.scm-manager.org")
|
||||
.expiresIn(5, TimeUnit.SECONDS)
|
||||
.custom("a", "b")
|
||||
|
||||
@@ -31,13 +31,8 @@
|
||||
|
||||
package sonia.scm.security;
|
||||
|
||||
import com.google.common.collect.Maps;
|
||||
import java.util.Map;
|
||||
import javax.inject.Provider;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import org.junit.Test;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import org.junit.Before;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
@@ -47,19 +42,22 @@ import sonia.scm.config.ScmConfiguration;
|
||||
import sonia.scm.util.HttpUtil;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link XsrfTokenClaimsEnricher}.
|
||||
* Unit tests for {@link XsrfAccessTokenEnricher}.
|
||||
*
|
||||
* @author Sebastian Sdorra
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class XsrfTokenClaimsEnricherTest {
|
||||
public class XsrfAccessTokenEnricherTest {
|
||||
|
||||
@Mock
|
||||
private HttpServletRequest request;
|
||||
|
||||
@Mock
|
||||
private AccessTokenBuilder builder;
|
||||
|
||||
private ScmConfiguration configuration;
|
||||
|
||||
private XsrfTokenClaimsEnricher enricher;
|
||||
private XsrfAccessTokenEnricher enricher;
|
||||
|
||||
/**
|
||||
* Prepare object under test.
|
||||
@@ -67,11 +65,16 @@ public class XsrfTokenClaimsEnricherTest {
|
||||
@Before
|
||||
public void prepareObjectUnderTest() {
|
||||
configuration = new ScmConfiguration();
|
||||
enricher = new XsrfTokenClaimsEnricher(configuration, () -> request);
|
||||
enricher = new XsrfAccessTokenEnricher(configuration, () -> request) {
|
||||
@Override
|
||||
String createToken() {
|
||||
return "42";
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests {@link XsrfTokenClaimsEnricher#enrich(java.util.Map)}.
|
||||
* Tests {@link XsrfAccessTokenEnricher#enrich(java.util.Map)}.
|
||||
*/
|
||||
@Test
|
||||
public void testEnrich() {
|
||||
@@ -80,15 +83,14 @@ public class XsrfTokenClaimsEnricherTest {
|
||||
when(request.getHeader(HttpUtil.HEADER_SCM_CLIENT)).thenReturn(HttpUtil.SCM_CLIENT_WUI);
|
||||
|
||||
// execute
|
||||
Map<String,Object> claims = Maps.newHashMap();
|
||||
enricher.enrich(claims);
|
||||
enricher.enrich(builder);
|
||||
|
||||
// assert
|
||||
assertNotNull(claims.get(Xsrf.CLAIMS_KEY));
|
||||
verify(builder).custom(Xsrf.TOKEN_KEY, "42");
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests {@link XsrfTokenClaimsEnricher#enrich(java.util.Map)} with disabled xsrf protection.
|
||||
* Tests {@link XsrfAccessTokenEnricher#enrich(java.util.Map)} with disabled xsrf protection.
|
||||
*/
|
||||
@Test
|
||||
public void testEnrichWithDisabledXsrf() {
|
||||
@@ -97,15 +99,14 @@ public class XsrfTokenClaimsEnricherTest {
|
||||
when(request.getHeader(HttpUtil.HEADER_SCM_CLIENT)).thenReturn(HttpUtil.SCM_CLIENT_WUI);
|
||||
|
||||
// execute
|
||||
Map<String,Object> claims = Maps.newHashMap();
|
||||
enricher.enrich(claims);
|
||||
enricher.enrich(builder);
|
||||
|
||||
// assert
|
||||
assertNull(claims.get(Xsrf.CLAIMS_KEY));
|
||||
verify(builder, never()).custom(Xsrf.TOKEN_KEY, "42");
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests {@link XsrfTokenClaimsEnricher#enrich(java.util.Map)} with disabled xsrf protection.
|
||||
* Tests {@link XsrfAccessTokenEnricher#enrich(java.util.Map)} with disabled xsrf protection.
|
||||
*/
|
||||
@Test
|
||||
public void testEnrichWithNonWuiClient() {
|
||||
@@ -113,11 +114,10 @@ public class XsrfTokenClaimsEnricherTest {
|
||||
configuration.setEnabledXsrfProtection(true);
|
||||
|
||||
// execute
|
||||
Map<String,Object> claims = Maps.newHashMap();
|
||||
enricher.enrich(claims);
|
||||
enricher.enrich(builder);
|
||||
|
||||
// assert
|
||||
assertNull(claims.get(Xsrf.CLAIMS_KEY));
|
||||
verify(builder, never()).custom(Xsrf.TOKEN_KEY, "42");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -70,7 +70,7 @@ public class XsrfTokenClaimsValidatorTest {
|
||||
public void testValidate() {
|
||||
// prepare
|
||||
Map<String, Object> claims = Maps.newHashMap();
|
||||
claims.put(Xsrf.CLAIMS_KEY, "abc");
|
||||
claims.put(Xsrf.TOKEN_KEY, "abc");
|
||||
when(request.getHeader(Xsrf.HEADER_KEY)).thenReturn("abc");
|
||||
|
||||
// execute and assert
|
||||
@@ -84,7 +84,7 @@ public class XsrfTokenClaimsValidatorTest {
|
||||
public void testValidateWithWrongHeader() {
|
||||
// prepare
|
||||
Map<String, Object> claims = Maps.newHashMap();
|
||||
claims.put(Xsrf.CLAIMS_KEY, "abc");
|
||||
claims.put(Xsrf.TOKEN_KEY, "abc");
|
||||
when(request.getHeader(Xsrf.HEADER_KEY)).thenReturn("123");
|
||||
|
||||
// execute and assert
|
||||
@@ -98,7 +98,7 @@ public class XsrfTokenClaimsValidatorTest {
|
||||
public void testValidateWithoutHeader() {
|
||||
// prepare
|
||||
Map<String, Object> claims = Maps.newHashMap();
|
||||
claims.put(Xsrf.CLAIMS_KEY, "abc");
|
||||
claims.put(Xsrf.TOKEN_KEY, "abc");
|
||||
|
||||
// execute and assert
|
||||
assertFalse(validator.validate(claims));
|
||||
|
||||
Reference in New Issue
Block a user