Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fhanik committed Nov 18, 2016
1 parent fe45dfd commit b60ef39
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 7 deletions.
Expand Up @@ -28,9 +28,14 @@
import java.security.spec.RSAPublicKeySpec; import java.security.spec.RSAPublicKeySpec;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;


import static org.cloudfoundry.identity.uaa.oauth.jwk.JsonWebKey.KeyType.MAC;
import static org.cloudfoundry.identity.uaa.oauth.jwk.JsonWebKey.KeyType.RSA; import static org.cloudfoundry.identity.uaa.oauth.jwk.JsonWebKey.KeyType.RSA;
import static org.springframework.util.StringUtils.hasText;


/** /**
* See https://tools.ietf.org/html/rfc7517 * See https://tools.ietf.org/html/rfc7517
Expand Down Expand Up @@ -90,7 +95,12 @@ public JsonWebKey setKid(String kid) {
} }


public final KeyUse getUse() { public final KeyUse getUse() {
return KeyUse.valueOf((String) getKeyProperties().get("use")); String use = (String) getKeyProperties().get("use");
KeyUse result = null;
if (hasText(use)) {
result = KeyUse.valueOf(use);
}
return result;
} }


@Override @Override
Expand All @@ -117,13 +127,26 @@ public String getAlgorithm() {


public String getValue() { public String getValue() {
String result = (String) getKeyProperties().get("value"); String result = (String) getKeyProperties().get("value");
if (result == null && RSA.equals(getKty())) { if (result == null) {
result = pemEncodePublicKey(getRsaPublicKey(this)); if (RSA.equals(getKty())) {
this.json.put("value", result); result = pemEncodePublicKey(getRsaPublicKey(this));
this.json.put("value", result);
} else if (MAC.equals(getKty())) {
result = (String) getKeyProperties().get("k");
this.json.put("value", result);
}
} }
return result; return result;
} }


public Set<KeyOperation> getKeyOps() {
List<String> result = (List<String>) getKeyProperties().get("key_ops");
if (result==null) {
result = Collections.emptyList();
}
return result.stream().map(o -> KeyOperation.valueOf(o)).collect(Collectors.toSet());
}

public static String pemEncodePublicKey(PublicKey publicKey) { public static String pemEncodePublicKey(PublicKey publicKey) {
String begin = "-----BEGIN PUBLIC KEY-----\n"; String begin = "-----BEGIN PUBLIC KEY-----\n";
String end = "\n-----END PUBLIC KEY-----"; String end = "\n-----END PUBLIC KEY-----";
Expand Down
Expand Up @@ -17,9 +17,13 @@


import org.junit.Test; import org.junit.Test;


import java.util.Arrays;
import java.util.LinkedHashSet;

import static org.cloudfoundry.identity.uaa.oauth.jwk.JsonWebKey.KeyUse.sig; import static org.cloudfoundry.identity.uaa.oauth.jwk.JsonWebKey.KeyUse.sig;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;


public class JsonWebKeySetTests { public class JsonWebKeySetTests {


Expand Down Expand Up @@ -52,33 +56,56 @@ public class JsonWebKeySetTests {
" \"n\": \"AMcWv4ogKaz625PU5cnCEJSZHZ0pXLumxrzHMSVLLOrHugnJ8nUlnI7NOiP1PlJ9Mirf3pqBsclZV9imE1qG9n_u4xeofF_5kf0EvWCT1jqQKdszlHrSB_CPJbX91A-M7Of03f3jN3YUmgUfB2r1CzTAG6CylQtlU1HGru96r9_P\",\n" + " \"n\": \"AMcWv4ogKaz625PU5cnCEJSZHZ0pXLumxrzHMSVLLOrHugnJ8nUlnI7NOiP1PlJ9Mirf3pqBsclZV9imE1qG9n_u4xeofF_5kf0EvWCT1jqQKdszlHrSB_CPJbX91A-M7Of03f3jN3YUmgUfB2r1CzTAG6CylQtlU1HGru96r9_P\",\n" +
" \"use\": \"sig\",\n" + " \"use\": \"sig\",\n" +
" \"value\": \"-----BEGIN PUBLIC KEY-----\\nMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDHFr+KICms+tuT1OXJwhCUmR2d\\nKVy7psa8xzElSyzqx7oJyfJ1JZyOzToj9T5SfTIq396agbHJWVfYphNahvZ/7uMX\\nqHxf+ZH9BL1gk9Y6kCnbM5R60gfwjyW1/dQPjOzn9N394zd2FJoFHwdq9Qs0wBug\\nspULZVNRxq7veq/fzwIDAQAB\\n-----END PUBLIC KEY-----\"\n" + " \"value\": \"-----BEGIN PUBLIC KEY-----\\nMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDHFr+KICms+tuT1OXJwhCUmR2d\\nKVy7psa8xzElSyzqx7oJyfJ1JZyOzToj9T5SfTIq396agbHJWVfYphNahvZ/7uMX\\nqHxf+ZH9BL1gk9Y6kCnbM5R60gfwjyW1/dQPjOzn9N394zd2FJoFHwdq9Qs0wBug\\nspULZVNRxq7veq/fzwIDAQAB\\n-----END PUBLIC KEY-----\"\n" +
" },\n" +
" {\n" +
" \"alg\": \"HMACSHA256\",\n" +
" \"k\": \"test-mac-key\",\n" +
" \"kid\": \"mac-id\",\n" +
" \"kty\": \"MAC\",\n" +
" \"key_ops\": [\"sign\",\"verify\"]\n" +
" }\n" + " }\n" +
" ]\n" + " ]\n" +
"}"; "}";




@Test @Test
public void test_multi_key() { public void test_multi_key() {
test_key(multiKeyJson); JsonWebKeySet<JsonWebKey> keys = test_key(multiKeyJson);
assertEquals(2, keys.getKeys().size());
JsonWebKey key = keys.getKeys().get(1);
assertEquals("HMACSHA256", key.getAlgorithm());

assertEquals(
"test-mac-key",
key.getValue()
);

assertEquals(
"test-mac-key",
key.getKeyProperties().get("k")
);

assertNull(key.getUse());
assertEquals(new LinkedHashSet<>(Arrays.asList(JsonWebKey.KeyOperation.sign, JsonWebKey.KeyOperation.verify)), key.getKeyOps());
} }


@Test @Test
public void test_single_key() { public void test_single_key() {
test_key(singleKeyJson); test_key(singleKeyJson);
} }


public void test_key(String json) { public JsonWebKeySet<JsonWebKey> test_key(String json) {
JsonWebKeySet<JsonWebKey> keys = JsonWebKeyHelper.deserialize(json); JsonWebKeySet<JsonWebKey> keys = JsonWebKeyHelper.deserialize(json);
assertNotNull(keys); assertNotNull(keys);
assertNotNull(keys.getKeys()); assertNotNull(keys.getKeys());
assertEquals(1, keys.getKeys().size());
JsonWebKey key = keys.getKeys().get(0); JsonWebKey key = keys.getKeys().get(0);
assertEquals("SHA256withRSA", key.getAlgorithm()); assertEquals("SHA256withRSA", key.getAlgorithm());
assertEquals( assertEquals(
"-----BEGIN PUBLIC KEY-----\nMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDHFr+KICms+tuT1OXJwhCUmR2d\nKVy7psa8xzElSyzqx7oJyfJ1JZyOzToj9T5SfTIq396agbHJWVfYphNahvZ/7uMX\nqHxf+ZH9BL1gk9Y6kCnbM5R60gfwjyW1/dQPjOzn9N394zd2FJoFHwdq9Qs0wBug\nspULZVNRxq7veq/fzwIDAQAB\n-----END PUBLIC KEY-----", "-----BEGIN PUBLIC KEY-----\nMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDHFr+KICms+tuT1OXJwhCUmR2d\nKVy7psa8xzElSyzqx7oJyfJ1JZyOzToj9T5SfTIq396agbHJWVfYphNahvZ/7uMX\nqHxf+ZH9BL1gk9Y6kCnbM5R60gfwjyW1/dQPjOzn9N394zd2FJoFHwdq9Qs0wBug\nspULZVNRxq7veq/fzwIDAQAB\n-----END PUBLIC KEY-----",
key.getValue() key.getValue()
); );
assertEquals(sig, key.getUse()); assertEquals(sig, key.getUse());
return keys;
} }


} }
Expand Up @@ -123,6 +123,16 @@ public void deleteProvider() throws Exception {
@Test @Test
public void successfulLoginWithOIDCProvider() throws Exception { public void successfulLoginWithOIDCProvider() throws Exception {
createOIDCProviderWithRequestedScopes(); createOIDCProviderWithRequestedScopes();
validateSuccessfulOIDCLogin();
}

@Test
public void successfulLoginWithOIDCProvider_MultiKeys() throws Exception {
createOIDCProviderWithRequestedScopes(null,"https://oidc10.identity.cf-app.com", "https://oidc10.identity.cf-app.com/token_keys");
validateSuccessfulOIDCLogin();
}

public void validateSuccessfulOIDCLogin() {
webDriver.get(baseUrl + "/login"); webDriver.get(baseUrl + "/login");
webDriver.findElement(By.linkText("My OIDC Provider")).click(); webDriver.findElement(By.linkText("My OIDC Provider")).click();
Assert.assertThat(webDriver.getCurrentUrl(), Matchers.containsString("oidc10.identity.cf-app.com")); Assert.assertThat(webDriver.getCurrentUrl(), Matchers.containsString("oidc10.identity.cf-app.com"));
Expand Down

0 comments on commit b60ef39

Please sign in to comment.