Skip to content

Commit dfb1fee

Browse files
committed
verification for field of type Key added
1 parent 92200f2 commit dfb1fee

3 files changed

Lines changed: 110 additions & 1 deletion

File tree

src/main/java/org/oidc/msg/ParameterVerification.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.oidc.msg.validator.DateClaimValidator;
2222
import org.oidc.msg.validator.IntClaimValidator;
2323
import org.oidc.msg.validator.JWTClaimValidator;
24+
import org.oidc.msg.validator.KeyClaimValidator;
2425
import org.oidc.msg.validator.ListClaimValidator;
2526
import org.oidc.msg.validator.MapClaimValidator;
2627
import org.oidc.msg.validator.MessageClaimValidator;
@@ -50,7 +51,9 @@ public enum ParameterVerification {
5051
SINGLE_REQUIRED_MESSAGE(new ParameterVerificationDefinition(new MessageClaimValidator(), true)),
5152
SINGLE_OPTIONAL_MESSAGE(new ParameterVerificationDefinition(new MessageClaimValidator(), false)),
5253
SINGLE_REQUIRED_JWT(new ParameterVerificationDefinition(new JWTClaimValidator(), true)),
53-
SINGLE_OPTIONAL_JWT(new ParameterVerificationDefinition(new JWTClaimValidator(), false));
54+
SINGLE_OPTIONAL_JWT(new ParameterVerificationDefinition(new JWTClaimValidator(), false)),
55+
SINGLE_REQUIRED_KEY(new ParameterVerificationDefinition(new KeyClaimValidator(), true)),
56+
SINGLE_OPTIONAL_KEY(new ParameterVerificationDefinition(new KeyClaimValidator(), false));
5457

5558

5659
/** Verification definition. */
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright (C) 2018 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.oidc.msg.validator;
18+
19+
import com.auth0.msg.Key;
20+
import org.oidc.msg.InvalidClaimException;
21+
22+
/**
23+
* A {@link ClaimValidator} for {@link Key}s.
24+
*/
25+
public class KeyClaimValidator implements ClaimValidator<Key> {
26+
27+
@Override
28+
public Key validate(Object value) throws InvalidClaimException {
29+
if (!(value instanceof Key)) {
30+
throw new InvalidClaimException(
31+
String.format("Parameter '%s' is not of expected type", value));
32+
}
33+
return (Key) value;
34+
}
35+
36+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright (C) 2018 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.oidc.msg.validator;
18+
19+
import com.auth0.jwt.exceptions.oicmsg_exceptions.DeserializationNotPossible;
20+
import com.auth0.jwt.exceptions.oicmsg_exceptions.SerializationNotPossible;
21+
import com.auth0.jwt.exceptions.oicmsg_exceptions.ValueError;
22+
import com.auth0.msg.Key;
23+
import java.util.Map;
24+
import org.junit.Assert;
25+
import org.junit.Before;
26+
import org.junit.Test;
27+
import org.oidc.msg.InvalidClaimException;
28+
29+
/**
30+
* Unit tests for {@link KeyClaimValidator}.
31+
*/
32+
public class KeyClaimValidatorTest extends BaseClaimValidatorTest<KeyClaimValidator> {
33+
34+
@Before
35+
public void setup() {
36+
validator = new KeyClaimValidator();
37+
}
38+
39+
@Test
40+
public void testValidValue() throws InvalidClaimException {
41+
Assert.assertTrue(validator.validate(new MockKey()) instanceof Key);
42+
}
43+
44+
@Test(expected = InvalidClaimException.class)
45+
public void testInvalidValue() throws InvalidClaimException {
46+
validator.validate("somethingelse");
47+
}
48+
49+
class MockKey extends Key {
50+
51+
@Override
52+
public void deserialize() throws DeserializationNotPossible {
53+
}
54+
55+
@Override
56+
public java.security.Key getKey(Boolean arg0) throws ValueError {
57+
return null;
58+
}
59+
60+
@Override
61+
public boolean isPrivateKey() {
62+
return false;
63+
}
64+
65+
@Override
66+
public Map<String, Object> serialize(boolean arg0) throws SerializationNotPossible {
67+
return null;
68+
}
69+
}
70+
}

0 commit comments

Comments
 (0)