Skip to content

Commit b6a84c6

Browse files
authored
Support for the keys on the file system (#107)
1 parent 3cc9667 commit b6a84c6

2 files changed

Lines changed: 46 additions & 26 deletions

File tree

implementation/src/main/java/io/smallrye/jwt/auth/principal/KeyLocationResolver.java

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,14 @@
1717
package io.smallrye.jwt.auth.principal;
1818

1919
import java.io.BufferedReader;
20+
import java.io.File;
21+
import java.io.FileInputStream;
2022
import java.io.IOException;
2123
import java.io.InputStream;
2224
import java.io.InputStreamReader;
2325
import java.io.StringReader;
2426
import java.io.StringWriter;
27+
import java.net.URI;
2528
import java.net.URL;
2629
import java.security.Key;
2730
import java.security.PublicKey;
@@ -157,21 +160,36 @@ private PublicKey tryAsJWK(JsonWebSignature jws) throws UnresolvableKeyException
157160
}
158161

159162
private void loadContents() throws Exception {
160-
final String location = authContextInfo.getPublicKeyLocation();
161-
if (location.startsWith("https:")) {
162-
httpsJwks = new HttpsJwks(location);
163+
final URI location = URI.create(authContextInfo.getPublicKeyLocation());
164+
165+
if ("https".equals(location.getScheme())) {
166+
httpsJwks = new HttpsJwks(authContextInfo.getPublicKeyLocation());
163167
httpsJwks.setDefaultCacheDuration(authContextInfo.getJwksRefreshInterval().longValue() * 60L);
164168
return;
165169
}
166170

167-
StringWriter contents = new StringWriter();
168-
final InputStream is;
169-
if (location.startsWith("classpath:") || location.indexOf(':') < 0) {
170-
is = getAsResource(location);
171+
InputStream is = null;
172+
173+
if (location.getScheme() != null) {
174+
if ("classpath".equals(location.getScheme())) {
175+
is = getAsClasspathResource(location.getSchemeSpecificPart());
176+
} else if ("file".equals(location.getScheme())) {
177+
is = getAsFileSystemResource(location.getRawSchemeSpecificPart());
178+
} else {
179+
is = new URL(authContextInfo.getPublicKeyLocation()).openStream();
180+
}
171181
} else {
172-
URL locationURL = new URL(location);
173-
is = locationURL.openStream();
182+
is = getAsClasspathResource(authContextInfo.getPublicKeyLocation());
183+
if (is == null) {
184+
is = getAsFileSystemResource(authContextInfo.getPublicKeyLocation());
185+
}
186+
}
187+
188+
if (is == null) {
189+
throw new IOException("No resource with the named " + location + " location exists");
174190
}
191+
192+
StringWriter contents = new StringWriter();
175193
try (BufferedReader reader = new BufferedReader(new InputStreamReader(is))) {
176194
String line = reader.readLine();
177195
while (line != null) {
@@ -185,20 +203,12 @@ private void loadContents() throws Exception {
185203
content = contents.toString();
186204
}
187205

188-
private static InputStream getAsResource(String location) throws IOException {
189-
190-
final String path;
191-
if (location.startsWith("classpath:")) {
192-
path = location.substring(10);
193-
} else {
194-
path = location;
195-
}
196-
ClassLoader loader = Thread.currentThread().getContextClassLoader();
197-
final InputStream is = loader.getResourceAsStream(path);
198-
if (is == null) {
199-
throw new IOException("No resource with named " + location + " exists");
200-
}
206+
private static InputStream getAsFileSystemResource(String publicKeyLocation) throws IOException {
207+
File f = new File(publicKeyLocation);
208+
return f.exists() ? new FileInputStream(f) : null;
209+
}
201210

202-
return is;
211+
private static InputStream getAsClasspathResource(String location) throws IOException {
212+
return Thread.currentThread().getContextClassLoader().getResourceAsStream(location);
203213
}
204214
}

testsuite/basic/src/test/java/io/smallrye/jwt/KeyLocationResolverTest.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,23 @@ public void testVerifyWithJwkKeyWithNonMatchingKidFromSet() throws Exception {
7676
}
7777

7878
@Test
79-
public void testVerifyWithPemKey() throws Exception {
79+
public void testVerifyWithClassPathPemKey() throws Exception {
8080
verifyToken("key3", null, "publicKey.pem");
8181
}
8282

8383
@Test
84-
public void testVerifyWithPemKeyWithMatchingKid() throws Exception {
85-
verifyToken("key3", null, "publicKey.pem");
84+
public void testVerifyWithClassPathPemKey2() throws Exception {
85+
verifyToken("key3", null, "classpath:publicKey.pem");
86+
}
87+
88+
@Test
89+
public void testVerifyWithFileSystemPemKey() throws Exception {
90+
verifyToken("key3", null, "target/test-classes/publicKey.pem");
91+
}
92+
93+
@Test
94+
public void testVerifyWithFileSystemPemKey2() throws Exception {
95+
verifyToken("key3", null, "file:target/test-classes/publicKey.pem");
8696
}
8797

8898
private static void verifyToken(String kid, String requiredKeyId, String publicKeyLocation) throws Exception {

0 commit comments

Comments
 (0)