1717package io .smallrye .jwt .auth .principal ;
1818
1919import java .io .BufferedReader ;
20+ import java .io .File ;
21+ import java .io .FileInputStream ;
2022import java .io .IOException ;
2123import java .io .InputStream ;
2224import java .io .InputStreamReader ;
2325import java .io .StringReader ;
2426import java .io .StringWriter ;
27+ import java .net .URI ;
2528import java .net .URL ;
2629import java .security .Key ;
2730import 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}
0 commit comments