Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#18 Implement PersistentTokenService, token invalidation #24

Merged
merged 1 commit into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package io.github.ilyalisov.jwt.service;

import io.github.ilyalisov.jwt.config.TokenParameters;

/**
* Interface if PersistentTokenService.
*/
public interface PersistentTokenService extends TokenService {

/**
* Removes JWT token from storage. Method removes all entries
* of the same token.
*
* @param token JWT token to be removed
* @return true - if JWT token was removed, false - otherwise
*/
boolean invalidate(
String token
);

/**
* Removes JWT token from storage.
*
* @param params params of JWT token
* @return true - if JWT token was removed, false - otherwise
*/
boolean invalidate(
TokenParameters params
);

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
/**
* Implementation of TokenService with JWT token storage.
*/
public class PersistentTokenServiceImpl implements TokenService {
public class PersistentTokenServiceImpl implements PersistentTokenService {

/**
* Secret key for verifying JWT token.
Expand Down Expand Up @@ -171,4 +171,18 @@ public Map<String, Object> claims(
return new HashMap<>(claims.getPayload());
}

@Override
public boolean invalidate(
final String token
) {
return tokenStorage.remove(token);
}

@Override
public boolean invalidate(
final TokenParameters params
) {
return tokenStorage.remove(params);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,39 @@ public String get(
}
}

@Override
public boolean remove(
final String token
) {
try (Jedis jedis = jedisPool.getResource()) {
String script = """
local keys = redis.call('keys', ARGV[1])
for _, key in ipairs(keys) do
redis.call('del', key)
end
return #keys > 0
""";
Long result = (Long) jedis.eval(
script,
0,
"*",
token
);
return result != null && result > 0;
}
}

@Override
public boolean remove(
final TokenParameters params
) {
try (Jedis jedis = jedisPool.getResource()) {
String tokenKey = redisSchema.subjectTokenKey(
params.getSubject(),
params.getType()
);
return jedis.del(tokenKey) > 0;
}
}

}
20 changes: 20 additions & 0 deletions src/main/java/io/github/ilyalisov/jwt/storage/TokenStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,24 @@ String get(
TokenParameters params
);

/**
* Removes JWT token from storage.
*
* @param token JWT token to be removed
* @return true - if JWT token was removed, false - otherwise
*/
boolean remove(
String token
);

/**
* Removes JWT token from storage.
*
* @param params params of JWT token
* @return true - if JWT token was removed, false - otherwise
*/
boolean remove(
TokenParameters params
);

}
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,29 @@ public String get(
return tokens.get(tokenKey);
}

@Override
public boolean remove(
final String token
) {
boolean deleted = false;
for (Map.Entry<String, String> entry : tokens.entrySet()) {
if (entry.getValue().equals(token)) {
tokens.remove(entry.getKey());
deleted = true;
}
}
return deleted;
}

@Override
public boolean remove(
final TokenParameters params
) {
String tokenKey = subjectTokenKey(
params.getSubject(),
params.getType()
);
return tokens.remove(tokenKey) != null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,29 @@ public boolean exists(
return token.equals(tokens.get(tokenKey));
}

@Override
public boolean remove(
final String token
) {
boolean deleted = false;
for (Map.Entry<String, String> entry : tokens.entrySet()) {
if (entry.getValue().equals(token)) {
tokens.remove(entry.getKey());
deleted = true;
}
}
return deleted;
}

@Override
public boolean remove(
final TokenParameters params
) {
String tokenKey = subjectTokenKey(
params.getSubject(),
params.getType()
);
return tokens.remove(tokenKey) != null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

Expand Down Expand Up @@ -197,9 +198,74 @@ void shouldReturnCorrectClaims() {
Map<String, Object> claims = tokenService.claims(token);

assertNotNull(claims);
System.out.println(claims.get("key1"));
assertEquals("value1", claims.get("key1"));
assertEquals(123, claims.get("key2"));
}

@Test
void shouldInvalidateByToken() {
String subject = "testSubject";
String type = "any";
Duration duration = Duration.ofMinutes(30);

TokenParameters params = TokenParameters.builder(
subject,
type,
duration
)
.build();
String token = tokenService.create(params);

tokenService.invalidate(token);

try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}

TokenParameters newParams = TokenParameters.builder(
subject,
type,
duration
)
.build();
String newToken = tokenService.create(newParams);
System.out.println(token);
System.out.println(newToken);
assertNotEquals(token, newToken);
}

@Test
void shouldInvalidateBySubjectAndType() {
String subject = "testSubject";
String type = "any";
Duration duration = Duration.ofMinutes(30);

TokenParameters params = TokenParameters.builder(
subject,
type,
duration
)
.build();
String token = tokenService.create(params);

tokenService.invalidate(params);

try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}

TokenParameters newParams = TokenParameters.builder(
subject,
type,
duration
)
.build();
String newToken = tokenService.create(newParams);
assertNotEquals(token, newToken);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,52 @@ void getWithNonExistingTokenShouldReturnNull() {
assertNull(tokenStorage.get(params));
}

@Test
void shouldInvalidateByToken() {
String subject = "testSubject";
String type = "any";
Duration duration = Duration.ofMinutes(30);

TokenParameters params = TokenParameters.builder(
subject,
type,
duration
)
.build();
String token = "testToken";
tokenStorage.save(
token,
params
);

tokenStorage.remove(token);

String existingToken = tokenStorage.get(params);
assertNull(existingToken);
}

@Test
void shouldInvalidateBySubjectAndType() {
String subject = "testSubject";
String type = "any";
Duration duration = Duration.ofMinutes(30);

TokenParameters params = TokenParameters.builder(
subject,
type,
duration
)
.build();
String token = "testToken";
tokenStorage.save(
token,
params
);

tokenStorage.remove(params);

String existingToken = tokenStorage.get(params);
assertNull(existingToken);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -109,5 +109,54 @@ void getWithNonExistingTokenShouldReturnNull() {

assertNull(tokenStorage.get(params));
}

@Test
void shouldInvalidateByToken() {
String subject = "testSubject";
String type = "any";
Duration duration = Duration.ofMinutes(30);

TokenParameters params = TokenParameters.builder(
subject,
type,
duration
)
.build();
String token = "testToken";
tokenStorage.save(
token,
params
);

tokenStorage.remove(token);

String existingToken = tokenStorage.get(params);
assertNull(existingToken);
}

@Test
void shouldInvalidateBySubjectAndType() {
String subject = "testSubject";
String type = "any";
Duration duration = Duration.ofMinutes(30);

TokenParameters params = TokenParameters.builder(
subject,
type,
duration
)
.build();
String token = "testToken";
tokenStorage.save(
token,
params
);

tokenStorage.remove(params);

String existingToken = tokenStorage.get(params);
assertNull(existingToken);
}

}

Loading