Skip to content

Commit 1e5536a

Browse files
msamusenkajgrandja
authored andcommitted
JwkSetConverter excludes enc keys
skip unsupported public key use (enc) without discarding the entire set Fixes spring-atticgh-1470
1 parent ec78c7b commit 1e5536a

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
lines changed

spring-security-oauth2/src/main/java/org/springframework/security/oauth2/provider/token/store/jwk/JwkSetConverter.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2017 the original author or authors.
2+
* Copyright 2012-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -83,6 +83,7 @@ public Set<JwkDefinition> convert(InputStream jwkSetSource) {
8383
Map<String, String> attributes = new HashMap<String, String>();
8484

8585
while (parser.nextToken() == JsonToken.START_OBJECT) {
86+
attributes.clear();
8687
while (parser.nextToken() == JsonToken.FIELD_NAME) {
8788
String attributeName = parser.getCurrentName();
8889
// gh-1082 - skip arrays such as x5c as we can't deal with them yet
@@ -94,6 +95,13 @@ public Set<JwkDefinition> convert(InputStream jwkSetSource) {
9495
}
9596
}
9697

98+
// gh-1470 - skip unsupported public key use (enc) without discarding the entire set
99+
JwkDefinition.PublicKeyUse publicKeyUse =
100+
JwkDefinition.PublicKeyUse.fromValue(attributes.get(PUBLIC_KEY_USE));
101+
if (JwkDefinition.PublicKeyUse.ENC.equals(publicKeyUse)) {
102+
continue;
103+
}
104+
97105
JwkDefinition jwkDefinition = null;
98106
JwkDefinition.KeyType keyType =
99107
JwkDefinition.KeyType.fromValue(attributes.get(KEY_TYPE));
@@ -108,7 +116,6 @@ public Set<JwkDefinition> convert(InputStream jwkSetSource) {
108116
jwkDefinition.getKeyId() + " (" + KEY_ID + ")");
109117
}
110118
}
111-
attributes.clear();
112119
}
113120

114121
} catch (IOException ex) {

spring-security-oauth2/src/test/java/org/springframework/security/oauth2/provider/token/store/jwk/JwkSetConverterTest.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2017 the original author or authors.
2+
* Copyright 2012-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -138,13 +138,12 @@ public void convertWhenJwkSetStreamHasRSAJwkElementWithMissingPublicKeyUseAttrib
138138
}
139139

140140
@Test
141-
public void convertWhenJwkSetStreamHasRSAJwkElementWithENCPublicKeyUseAttributeThenThrowJwkException() throws Exception {
142-
this.thrown.expect(JwkException.class);
143-
this.thrown.expectMessage("enc (use) is currently not supported.");
141+
public void convertWhenJwkSetStreamHasRSAJwkElementWithENCPublicKeyUseAttributeThenReturnEmptyJwkSet() throws Exception {
144142
Map<String, Object> jwkSetObject = new HashMap<String, Object>();
145143
Map<String, Object> jwkObject = this.createJwkObject(JwkDefinition.KeyType.RSA, "key-id-1", JwkDefinition.PublicKeyUse.ENC);
146144
jwkSetObject.put(JwkAttributes.KEYS, new Map[] {jwkObject});
147-
this.converter.convert(this.asInputStream(jwkSetObject));
145+
Set<JwkDefinition> jwkSet = this.converter.convert(this.asInputStream(jwkSetObject));
146+
assertTrue("JWK Set NOT empty", jwkSet.isEmpty());
148147
}
149148

150149
@Test
@@ -190,13 +189,12 @@ public void convertWhenJwkSetStreamHasECJwkElementWithMissingPublicKeyUseAttribu
190189
}
191190

192191
@Test
193-
public void convertWhenJwkSetStreamHasECJwkElementWithENCPublicKeyUseAttributeThenThrowJwkException() throws Exception {
194-
this.thrown.expect(JwkException.class);
195-
this.thrown.expectMessage("enc (use) is currently not supported.");
192+
public void convertWhenJwkSetStreamHasECJwkElementWithENCPublicKeyUseAttributeThenReturnEmptyJwkSet() throws Exception {
196193
Map<String, Object> jwkSetObject = new HashMap<String, Object>();
197194
Map<String, Object> jwkObject = this.createEllipticCurveJwkObject("key-id-1", JwkDefinition.PublicKeyUse.ENC, null);
198195
jwkSetObject.put(JwkAttributes.KEYS, new Map[] {jwkObject});
199-
this.converter.convert(this.asInputStream(jwkSetObject));
196+
Set<JwkDefinition> jwkSet = this.converter.convert(this.asInputStream(jwkSetObject));
197+
assertTrue("JWK Set NOT empty", jwkSet.isEmpty());
200198
}
201199

202200
@Test

0 commit comments

Comments
 (0)