Skip to content
This repository was archived by the owner on May 31, 2022. It is now read-only.

Commit 799fb1a

Browse files
msamusenkajgrandja
authored andcommitted
JwkSetConverter excludes enc keys
skip unsupported public key use (enc) without discarding the entire set Fixes gh-1470
1 parent 7118946 commit 799fb1a

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

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

+10-2
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.
@@ -82,6 +82,7 @@ public Set<JwkDefinition> convert(InputStream jwkSetSource) {
8282
Map<String, String> attributes = new HashMap<String, String>();
8383

8484
while (parser.nextToken() == JsonToken.START_OBJECT) {
85+
attributes.clear();
8586
while (parser.nextToken() == JsonToken.FIELD_NAME) {
8687
String attributeName = parser.getCurrentName();
8788
// gh-1082 - skip arrays such as x5c as we can't deal with them yet
@@ -92,6 +93,14 @@ public Set<JwkDefinition> convert(InputStream jwkSetSource) {
9293
attributes.put(attributeName, parser.getValueAsString());
9394
}
9495
}
96+
97+
// gh-1470 - skip unsupported public key use (enc) without discarding the entire set
98+
JwkDefinition.PublicKeyUse publicKeyUse =
99+
JwkDefinition.PublicKeyUse.fromValue(attributes.get(PUBLIC_KEY_USE));
100+
if (JwkDefinition.PublicKeyUse.ENC.equals(publicKeyUse)) {
101+
continue;
102+
}
103+
95104
JwkDefinition.KeyType keyType =
96105
JwkDefinition.KeyType.fromValue(attributes.get(KEY_TYPE));
97106
if (JwkDefinition.KeyType.RSA.equals(keyType)) {
@@ -101,7 +110,6 @@ public Set<JwkDefinition> convert(InputStream jwkSetSource) {
101110
jwkDefinition.getKeyId() + " (" + KEY_ID + ")");
102111
}
103112
}
104-
attributes.clear();
105113
}
106114

107115
} catch (IOException ex) {

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

+4-5
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.
@@ -147,13 +147,12 @@ public void convertWhenJwkSetStreamHasJwkElementWithMissingPublicKeyUseAttribute
147147
}
148148

149149
@Test
150-
public void convertWhenJwkSetStreamHasJwkElementWithENCPublicKeyUseAttributeThenThrowJwkException() throws Exception {
151-
this.thrown.expect(JwkException.class);
152-
this.thrown.expectMessage("enc (use) is currently not supported.");
150+
public void convertWhenJwkSetStreamHasRSAJwkElementWithENCPublicKeyUseAttributeThenReturnEmptyJwkSet() throws Exception {
153151
Map<String, Object> jwkSetObject = new HashMap<String, Object>();
154152
Map<String, Object> jwkObject = this.createJwkObject(JwkDefinition.KeyType.RSA, "key-id-1", JwkDefinition.PublicKeyUse.ENC);
155153
jwkSetObject.put(JwkAttributes.KEYS, new Map[] {jwkObject});
156-
this.converter.convert(this.asInputStream(jwkSetObject));
154+
Set<JwkDefinition> jwkSet = this.converter.convert(this.asInputStream(jwkSetObject));
155+
assertTrue("JWK Set NOT empty", jwkSet.isEmpty());
157156
}
158157

159158
@Test

0 commit comments

Comments
 (0)