-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix #532: OIDC: Implement activation using OAuth 2.0, openid scope
- Loading branch information
Showing
18 changed files
with
1,189 additions
and
4 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
65 changes: 65 additions & 0 deletions
65
...urity/powerauth/rest/api/spring/exception/PowerAuthApplicationConfigurationException.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,65 @@ | ||
/* | ||
* PowerAuth integration libraries for RESTful API applications, examples and | ||
* related software components | ||
* | ||
* Copyright (C) 2024 Wultra s.r.o. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published | ||
* by the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package io.getlime.security.powerauth.rest.api.spring.exception; | ||
|
||
import java.io.Serial; | ||
|
||
/** | ||
* Exception related to application configuration. | ||
* | ||
* @author Lubos Racansky, [email protected] | ||
*/ | ||
public class PowerAuthApplicationConfigurationException extends Exception { | ||
|
||
@Serial | ||
private static final long serialVersionUID = 8677977961740746599L; | ||
|
||
/** | ||
* No-arg constructor. | ||
*/ | ||
public PowerAuthApplicationConfigurationException() { | ||
super(); | ||
} | ||
|
||
/** | ||
* Constructor with a custom error message. | ||
* @param message Error message. | ||
*/ | ||
public PowerAuthApplicationConfigurationException(String message) { | ||
super(message); | ||
} | ||
|
||
/** | ||
* Constructor with a cause. | ||
* @param cause Error cause. | ||
*/ | ||
public PowerAuthApplicationConfigurationException(Throwable cause) { | ||
super(cause); | ||
} | ||
|
||
/** | ||
* Constructor with a message and cause. | ||
* @param message Error message. | ||
* @param cause Error cause. | ||
*/ | ||
public PowerAuthApplicationConfigurationException(String message, Throwable cause) { | ||
super(message, cause); | ||
} | ||
} |
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
37 changes: 37 additions & 0 deletions
37
...o/getlime/security/powerauth/rest/api/spring/service/oidc/ClientAuthenticationMethod.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,37 @@ | ||
/* | ||
* PowerAuth integration libraries for RESTful API applications, examples and | ||
* related software components | ||
* | ||
* Copyright (C) 2024 Wultra s.r.o. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published | ||
* by the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package io.getlime.security.powerauth.rest.api.spring.service.oidc; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
|
||
/** | ||
* OIDC client authentication methods. | ||
* | ||
* @author Lubos Racansky, [email protected] | ||
*/ | ||
enum ClientAuthenticationMethod { | ||
|
||
@JsonProperty("client_secret_basic") | ||
CLIENT_SECRET_BASIC, | ||
|
||
@JsonProperty("client_secret_post") | ||
CLIENT_SECRET_POST | ||
|
||
} |
82 changes: 82 additions & 0 deletions
82
...ain/java/io/getlime/security/powerauth/rest/api/spring/service/oidc/IdTokenValidator.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,82 @@ | ||
/* | ||
* PowerAuth integration libraries for RESTful API applications, examples and | ||
* related software components | ||
* | ||
* Copyright (C) 2024 Wultra s.r.o. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published | ||
* by the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package io.getlime.security.powerauth.rest.api.spring.service.oidc; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.security.oauth2.jose.jws.JwsAlgorithms; | ||
import org.springframework.security.oauth2.jwt.Jwt; | ||
|
||
import java.security.MessageDigest; | ||
import java.security.NoSuchAlgorithmException; | ||
import java.util.Base64; | ||
|
||
/** | ||
* Additional ID token validations. | ||
* | ||
* @author Lubos Racansky, [email protected] | ||
*/ | ||
@Slf4j | ||
final class IdTokenValidator { | ||
|
||
private IdTokenValidator() { | ||
throw new IllegalStateException("Should not be instantiated"); | ||
} | ||
|
||
static boolean isAtHashValid(final Jwt idToken, final String accessToken) { | ||
final String atHash = idToken.getClaimAsString("at_hash"); | ||
return atHash == null || isAtHashValid(accessToken, atHash, idToken.getHeaders().get("alg").toString()); | ||
} | ||
|
||
static boolean isNonceValid(final Jwt idToken, final String nonce) { | ||
return nonce.equals(idToken.getClaimAsString("nonce")); | ||
} | ||
|
||
/** | ||
* <ol> | ||
* <li>Hash the octets of the ASCII representation of the access_token with the hash algorithm for the alg Header Parameter of the ID Token's JOSE Header. For instance, if the alg is RS256, the hash algorithm used is SHA-256.</li> | ||
* <li>Take the left-most half of the hash and base64url-encode it.</li> | ||
* <li>The value of at_hash in the ID Token MUST match the value produced in the previous step.</li> | ||
* </ol> | ||
* | ||
* @see <a href="https://openid.net/specs/openid-connect-core-1_0.html#ImplicitTokenValidation">3.2.2.9. Access Token Validation</a> | ||
*/ | ||
private static boolean isAtHashValid(final String accessToken, final String atHash, final String signatureAlgorithm) { | ||
try { | ||
final MessageDigest digest = MessageDigest.getInstance(mapHashAlgorithm(signatureAlgorithm)); | ||
final byte[] hash = digest.digest(accessToken.getBytes()); | ||
final byte[] leftHalf = new byte[hash.length / 2]; | ||
System.arraycopy(hash, 0, leftHalf, 0, leftHalf.length); | ||
final String computedAtHash = Base64.getUrlEncoder().withoutPadding().encodeToString(leftHalf); | ||
return atHash.equals(computedAtHash); | ||
} catch (NoSuchAlgorithmException e) { | ||
logger.error("Unable to validate at_hash", e); | ||
return false; | ||
} | ||
} | ||
|
||
private static String mapHashAlgorithm(final String signatureAlgorithm) throws NoSuchAlgorithmException { | ||
return switch (signatureAlgorithm) { | ||
case JwsAlgorithms.RS256, JwsAlgorithms.ES256 -> "SHA-256"; | ||
case JwsAlgorithms.RS384, JwsAlgorithms.ES384 -> "SHA-384"; | ||
case JwsAlgorithms.RS512, JwsAlgorithms.ES512 -> "SHA-512"; | ||
default -> throw new NoSuchAlgorithmException("Unsupported signature algorithm: " + signatureAlgorithm); | ||
}; | ||
} | ||
} |
Oops, something went wrong.