-
Notifications
You must be signed in to change notification settings - Fork 547
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
123 additions
and
180 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
package io.supertokens.oauth; | ||
|
||
import com.auth0.jwt.exceptions.JWTCreationException; | ||
import com.google.gson.JsonElement; | ||
import com.google.gson.JsonObject; | ||
import io.supertokens.Main; | ||
import io.supertokens.exceptions.TryRefreshTokenException; | ||
import io.supertokens.jwt.JWTSigningFunctions; | ||
import io.supertokens.jwt.exceptions.UnsupportedJWTSigningAlgorithmException; | ||
import io.supertokens.pluginInterface.exceptions.StorageQueryException; | ||
import io.supertokens.pluginInterface.exceptions.StorageTransactionLogicException; | ||
import io.supertokens.pluginInterface.jwt.JWTAsymmetricSigningKeyInfo; | ||
import io.supertokens.pluginInterface.jwt.JWTSigningKeyInfo; | ||
import io.supertokens.pluginInterface.multitenancy.AppIdentifier; | ||
import io.supertokens.pluginInterface.multitenancy.exceptions.TenantOrAppNotFoundException; | ||
import io.supertokens.session.jwt.JWT; | ||
import io.supertokens.session.jwt.JWT.JWTException; | ||
import io.supertokens.signingkeys.JWTSigningKey; | ||
import io.supertokens.signingkeys.SigningKeys; | ||
import io.supertokens.utils.Utils; | ||
|
||
import javax.annotation.Nonnull; | ||
import java.io.IOException; | ||
import java.security.InvalidKeyException; | ||
import java.security.KeyException; | ||
import java.security.NoSuchAlgorithmException; | ||
import java.security.spec.InvalidKeySpecException; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class OAuthToken { | ||
public enum TokenType { | ||
ACCESS_TOKEN(1), | ||
ID_TOKEN(2); | ||
|
||
private final int value; | ||
|
||
TokenType(int value) { | ||
this.value = value; | ||
} | ||
|
||
public int getValue() { | ||
return value; | ||
} | ||
} | ||
|
||
public static JsonObject getPayloadFromJWTToken(AppIdentifier appIdentifier, | ||
@Nonnull Main main, @Nonnull String token) | ||
throws TenantOrAppNotFoundException, TryRefreshTokenException, StorageQueryException, | ||
UnsupportedJWTSigningAlgorithmException, StorageTransactionLogicException { | ||
List<JWTSigningKeyInfo> keyInfoList = SigningKeys.getInstance(appIdentifier, main).getAllKeys(); | ||
Exception error = null; | ||
JWT.JWTInfo jwtInfo = null; | ||
JWT.JWTPreParseInfo preParseJWTInfo = null; | ||
try { | ||
preParseJWTInfo = JWT.preParseJWTInfo(token); | ||
} catch (JWTException e) { | ||
// This basically should never happen, but it means, that the token structure is | ||
// wrong, can't verify | ||
throw new TryRefreshTokenException(e); | ||
} | ||
|
||
for (JWTSigningKeyInfo keyInfo : keyInfoList) { | ||
try { | ||
jwtInfo = JWT.verifyJWTAndGetPayload(preParseJWTInfo, | ||
((JWTAsymmetricSigningKeyInfo) keyInfo).publicKey); | ||
error = null; | ||
break; | ||
} catch (NoSuchAlgorithmException e) { | ||
// This basically should never happen, but it means, that can't verify any | ||
// tokens, no need to retry | ||
throw new TryRefreshTokenException(e); | ||
} catch (KeyException | JWTException e) { | ||
error = e; | ||
} | ||
} | ||
|
||
if (jwtInfo == null) { | ||
throw new TryRefreshTokenException(error); | ||
} | ||
|
||
if (jwtInfo.payload.get("exp").getAsLong() * 1000 < System.currentTimeMillis()) { | ||
throw new TryRefreshTokenException("Access token expired"); | ||
} | ||
|
||
return jwtInfo.payload; | ||
} | ||
|
||
public static String reSignToken(AppIdentifier appIdentifier, Main main, String token, String iss, TokenType tokenType, boolean useDynamicSigningKey, int retryCount) throws IOException, JWTException, InvalidKeyException, NoSuchAlgorithmException, StorageQueryException, StorageTransactionLogicException, UnsupportedJWTSigningAlgorithmException, TenantOrAppNotFoundException, InvalidKeySpecException, | ||
JWTCreationException { | ||
// Load the JWKS from the specified URL | ||
JsonObject payload = JWT.getPayloadWithoutVerifying(token).payload; | ||
|
||
// move keys in ext to root | ||
if (tokenType == TokenType.ACCESS_TOKEN && payload.has("ext")) { | ||
JsonObject ext = payload.getAsJsonObject("ext"); | ||
for (Map.Entry<String, JsonElement> entry : ext.entrySet()) { | ||
payload.add(entry.getKey(), entry.getValue()); | ||
} | ||
payload.remove("ext"); | ||
} | ||
payload.addProperty("iss", iss); | ||
payload.addProperty("stt", tokenType.getValue()); | ||
|
||
JWTSigningKeyInfo keyToUse; | ||
if (useDynamicSigningKey) { | ||
keyToUse = Utils.getJWTSigningKeyInfoFromKeyInfo( | ||
SigningKeys.getInstance(appIdentifier, main).getLatestIssuedDynamicKey()); | ||
} else { | ||
keyToUse = SigningKeys.getInstance(appIdentifier, main) | ||
.getStaticKeyForAlgorithm(JWTSigningKey.SupportedAlgorithms.RS256); | ||
} | ||
|
||
token = JWTSigningFunctions.createJWTToken(JWTSigningKey.SupportedAlgorithms.RS256, new HashMap<>(), | ||
payload, null, payload.get("exp").getAsLong(), payload.get("iat").getAsLong(), keyToUse); | ||
return token; | ||
} | ||
} |
29 changes: 0 additions & 29 deletions
29
src/main/java/io/supertokens/oauth/exceptions/OAuthAPIInvalidInputException.java
This file was deleted.
Oops, something went wrong.
28 changes: 0 additions & 28 deletions
28
src/main/java/io/supertokens/oauth/exceptions/OAuthClientUpdateException.java
This file was deleted.
Oops, something went wrong.
35 changes: 0 additions & 35 deletions
35
src/main/java/io/supertokens/oauth/exceptions/OAuthException.java
This file was deleted.
Oops, something went wrong.
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