Permalink
Show file tree
Hide file tree
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
16 changed files
with
531 additions
and
39 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
@@ -0,0 +1,33 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You 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 org.apache.geronimo.microprofile.impl.jwtauth.config; | ||
|
||
import javax.enterprise.inject.Vetoed; | ||
|
||
@Vetoed | ||
class PrefixedConfig implements GeronimoJwtAuthConfig { | ||
private final GeronimoJwtAuthConfig delegate; | ||
|
||
PrefixedConfig(final GeronimoJwtAuthConfig geronimoJwtAuthConfig) { | ||
this.delegate = geronimoJwtAuthConfig; | ||
} | ||
|
||
@Override | ||
public String read(final String value, final String def) { | ||
return delegate.read("geronimo.jwt-auth." + value, def); | ||
} | ||
} |
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
@@ -0,0 +1,38 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You 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 org.apache.geronimo.microprofile.impl.jwtauth.io; | ||
|
||
import java.io.IOException; | ||
import java.io.Reader; | ||
import java.io.StringReader; | ||
import java.util.Properties; | ||
|
||
public final class PropertiesLoader { | ||
private PropertiesLoader() { | ||
// no-op | ||
} | ||
|
||
public static Properties load(final String value) { | ||
final Properties properties = new Properties(); | ||
try (final Reader reader = new StringReader(value)) { | ||
properties.load(reader); | ||
} catch (final IOException e) { | ||
throw new IllegalArgumentException(e); | ||
} | ||
return properties; | ||
} | ||
} |
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
@@ -0,0 +1,81 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You 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 org.apache.geronimo.microprofile.impl.jwtauth.jwt; | ||
|
||
import java.net.HttpURLConnection; | ||
|
||
import javax.annotation.PostConstruct; | ||
import javax.enterprise.context.ApplicationScoped; | ||
import javax.inject.Inject; | ||
import javax.json.JsonNumber; | ||
import javax.json.JsonObject; | ||
|
||
import org.apache.geronimo.microprofile.impl.jwtauth.JwtException; | ||
import org.apache.geronimo.microprofile.impl.jwtauth.config.GeronimoJwtAuthConfig; | ||
import org.eclipse.microprofile.jwt.Claims; | ||
|
||
@ApplicationScoped | ||
public class DateValidator { | ||
@Inject | ||
private GeronimoJwtAuthConfig config; | ||
private boolean expirationMandatory; | ||
private boolean issuedAtTimeMandatory; | ||
private long tolerance; | ||
|
||
@PostConstruct | ||
private void init() { | ||
expirationMandatory = Boolean.parseBoolean(config.read("exp.required", "true")); | ||
issuedAtTimeMandatory = Boolean.parseBoolean(config.read("iat.required", "true")); | ||
tolerance = Long.parseLong(config.read("date.tolerance", "60000")); // 1mn | ||
} | ||
|
||
void checkInterval(final JsonObject payload) { | ||
long now = -1; | ||
|
||
final JsonNumber exp = payload.getJsonNumber(Claims.exp.name()); | ||
if (exp == null) { | ||
if (expirationMandatory) { | ||
throw new JwtException("No exp in the JWT", HttpURLConnection.HTTP_UNAUTHORIZED); | ||
} | ||
} else { | ||
final long expValue = exp.longValue(); | ||
now = now(); | ||
if (expValue < now - tolerance) { | ||
throw new JwtException("Token expired", HttpURLConnection.HTTP_UNAUTHORIZED); | ||
} | ||
} | ||
|
||
final JsonNumber iat = payload.getJsonNumber(Claims.iat.name()); | ||
if (iat == null) { | ||
if (issuedAtTimeMandatory) { | ||
throw new JwtException("No iat in the JWT", HttpURLConnection.HTTP_UNAUTHORIZED); | ||
} | ||
} else { | ||
final long iatValue = iat.longValue(); | ||
if (now < 0) { | ||
now = now(); | ||
} | ||
if (iatValue > now + tolerance) { | ||
throw new JwtException("Token issued after current time", HttpURLConnection.HTTP_UNAUTHORIZED); | ||
} | ||
} | ||
} | ||
|
||
private long now() { | ||
return System.currentTimeMillis() / 1000; | ||
} | ||
} |
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
Oops, something went wrong.