Skip to content

Commit e7bd046

Browse files
committed
Add exceptions to Encryptor#decrypt(String)
1 parent 3a0903e commit e7bd046

File tree

5 files changed

+52
-9
lines changed

5 files changed

+52
-9
lines changed

src/main/java/xyz/srnyx/javautilities/objects/Encryptor.java renamed to src/main/java/xyz/srnyx/javautilities/objects/encryptor/Encryptor.java

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
package xyz.srnyx.javautilities.objects;
1+
package xyz.srnyx.javautilities.objects.encryptor;
22

33
import org.jetbrains.annotations.NotNull;
44
import org.jetbrains.annotations.Nullable;
55

66
import xyz.srnyx.javautilities.manipulation.Mapper;
7+
import xyz.srnyx.javautilities.objects.encryptor.exceptions.TokenExpiredException;
8+
import xyz.srnyx.javautilities.objects.encryptor.exceptions.TokenInvalidException;
9+
import xyz.srnyx.javautilities.objects.encryptor.exceptions.TokenTamperedException;
710

811
import javax.crypto.Mac;
912
import javax.crypto.spec.SecretKeySpec;
@@ -94,32 +97,36 @@ public String encrypt(@NotNull Object value) {
9497
/**
9598
* Decrypts a token by verifying its signature and timestamp
9699
*
97-
* @param token the token to decrypt
100+
* @param token the token to decrypt
98101
*
99-
* @return the original value if the token is valid and not expired, otherwise {@code null}
102+
* @return the original value if the token is valid and not expired, otherwise {@code null}
103+
*
104+
* @throws TokenInvalidException if the token is invalid
105+
* @throws TokenExpiredException if the token has expired
106+
* @throws TokenTamperedException if the token has been tampered with
100107
*/
101108
@Nullable
102-
public String decrypt(@NotNull String token) {
109+
public String decrypt(@NotNull String token) throws TokenInvalidException, TokenExpiredException, TokenTamperedException {
103110
// Decode token
104111
final String decoded = new String(Base64.getUrlDecoder().decode(token), StandardCharsets.UTF_8);
105112
final String[] parts = decoded.split(":");
106-
if (parts.length != 3) return null;
113+
if (parts.length != 3) throw new TokenInvalidException("Token does not have 3 parts");
107114

108115
// Get casted value and timestamp
109116
final String value = parts[0];
110-
if (value == null) return null;
117+
if (value == null) throw new TokenInvalidException("Value is null");
111118
final Optional<Long> timestamp = Mapper.toLong(parts[1]);
112-
if (!timestamp.isPresent()) return null;
119+
if (!timestamp.isPresent()) throw new TokenInvalidException("Timestamp is not a valid long");
113120

114121
// Check age
115-
if (maxAge != null && System.currentTimeMillis() - timestamp.get() > maxAge.toMillis()) return null; // Expired
122+
if (maxAge != null && System.currentTimeMillis() - timestamp.get() > maxAge.toMillis()) throw new TokenExpiredException();
116123

117124
// Recompute signature
118125
final String payload = parts[0] + ":" + parts[1];
119126
final byte[] expectedSig = getSignature(payload);
120127
if (expectedSig == null) return null; // Should never happen
121128
final byte[] actualSig = Base64.getUrlDecoder().decode(parts[2]);
122-
if (!Arrays.equals(expectedSig, actualSig)) return null; // Tampered
129+
if (!Arrays.equals(expectedSig, actualSig)) throw new TokenTamperedException();
123130

124131
return value;
125132
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package xyz.srnyx.javautilities.objects.encryptor.exceptions;
2+
3+
import org.jetbrains.annotations.NotNull;
4+
5+
6+
public class DecryptionException extends Exception {
7+
public DecryptionException(@NotNull String message) {
8+
super(message);
9+
}
10+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package xyz.srnyx.javautilities.objects.encryptor.exceptions;
2+
3+
4+
public class TokenExpiredException extends DecryptionException {
5+
public TokenExpiredException() {
6+
super("Token has expired");
7+
}
8+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package xyz.srnyx.javautilities.objects.encryptor.exceptions;
2+
3+
import org.jetbrains.annotations.NotNull;
4+
5+
6+
public class TokenInvalidException extends DecryptionException {
7+
public TokenInvalidException(@NotNull String message) {
8+
super(message);
9+
}
10+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package xyz.srnyx.javautilities.objects.encryptor.exceptions;
2+
3+
4+
public class TokenTamperedException extends DecryptionException {
5+
public TokenTamperedException() {
6+
super("Token has been tampered with");
7+
}
8+
}

0 commit comments

Comments
 (0)