-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Renamed JwtDeserializer to JsonObjectDeserializer that defaults to …
…throwing MalformedJwtException. Added two subclasses, JwkDeserializer and JwkSetDeserializer that throws JWK and JWK Set-specific exceptions. - Changed ParserBuilder#deserializer method name to ParserBuilder#jsonReader
- Loading branch information
1 parent
edb397c
commit e45c27f
Showing
20 changed files
with
219 additions
and
238 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
impl/src/main/java/io/jsonwebtoken/impl/io/JsonObjectDeserializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* Copyright (C) 2021 jsonwebtoken.io | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.jsonwebtoken.impl.io; | ||
|
||
import io.jsonwebtoken.MalformedJwtException; | ||
import io.jsonwebtoken.impl.lang.Function; | ||
import io.jsonwebtoken.io.DeserializationException; | ||
import io.jsonwebtoken.io.Reader; | ||
import io.jsonwebtoken.lang.Assert; | ||
|
||
import java.util.Map; | ||
|
||
/** | ||
* Function that wraps a {@link Reader} to add JWT-related error handling. | ||
* | ||
* @since 0.11.3 (renamed from JwtDeserializer) | ||
*/ | ||
public class JsonObjectDeserializer implements Function<java.io.Reader, Map<String, ?>> { | ||
|
||
private static final String MALFORMED_ERROR = "Malformed %s JSON: %s"; | ||
private static final String MALFORMED_COMPLEX_ERROR = "Malformed or excessively complex %s JSON. " + | ||
"If experienced in a production environment, this could reflect a potential malicious %s, please " + | ||
"investigate the source further. Cause: %s"; | ||
|
||
private final Reader<?> reader; | ||
private final String name; | ||
|
||
public JsonObjectDeserializer(Reader<?> reader, String name) { | ||
this.reader = Assert.notNull(reader, "reader cannot be null."); | ||
this.name = Assert.hasText(name, "name cannot be null or empty."); | ||
} | ||
|
||
@Override | ||
public Map<String, ?> apply(java.io.Reader reader) { | ||
Assert.notNull(reader, "Reader argument cannot be null."); | ||
Object value; | ||
try { | ||
value = this.reader.read(reader); | ||
if (value == null) { | ||
String msg = "Deserialized data resulted in a null value; cannot create Map<String,?>"; | ||
throw new DeserializationException(msg); | ||
} | ||
if (!(value instanceof Map)) { | ||
String msg = "Deserialized data is not a JSON Object; cannot create Map<String,?>"; | ||
throw new DeserializationException(msg); | ||
} | ||
// JSON Specification requires all JSON Objects to have string-only keys. So instead of | ||
// checking that the val.keySet() has all Strings, we blindly cast to a Map<String,?> | ||
// since input would rarely, if ever, have non-string keys. | ||
//noinspection unchecked | ||
return (Map<String, ?>) value; | ||
} catch (StackOverflowError e) { | ||
String msg = String.format(MALFORMED_COMPLEX_ERROR, this.name, this.name, e.getMessage()); | ||
throw new DeserializationException(msg, e); | ||
} catch (Throwable t) { | ||
throw malformed(t); | ||
} | ||
} | ||
|
||
protected RuntimeException malformed(Throwable t) { | ||
String msg = String.format(MALFORMED_ERROR, this.name, t.getMessage()); | ||
throw new MalformedJwtException(msg, t); | ||
} | ||
} |
57 changes: 0 additions & 57 deletions
57
impl/src/main/java/io/jsonwebtoken/impl/io/JwtDeserializer.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.