forked from iris-connect/iris-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: revoke jwt tokens of modified, deleted or logged out users
Introduce JWT token revocation functionality by storing issued tokens in a "whitelist" table and removing all tokens corresponding to a user for following actions: - User is deleted - User is updated (e.g. gets different role, changes password etc.) - User logs out Furthermore, a scheduled task was added, to remove expired tokens from the whitelist table. Refs: iris-connect/iris-backlog#90
- Loading branch information
Fabio Aversente
authored
May 29, 2021
1 parent
596c7b8
commit cd1ffb9
Showing
15 changed files
with
3,311 additions
and
2,473 deletions.
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
iris-client-bff/src/main/java/iris/client_bff/auth/db/CustomLogoutHandler.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 @@ | ||
package iris.client_bff.auth.db; | ||
|
||
import static iris.client_bff.auth.db.SecurityConstants.BEARER_TOKEN_PREFIX; | ||
|
||
import iris.client_bff.auth.db.jwt.JWTService; | ||
import lombok.AllArgsConstructor; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
|
||
import org.apache.commons.lang3.StringUtils; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.web.authentication.logout.LogoutHandler; | ||
import org.springframework.stereotype.Service; | ||
|
||
import com.auth0.jwt.interfaces.DecodedJWT; | ||
|
||
@Service | ||
@AllArgsConstructor | ||
public class CustomLogoutHandler implements LogoutHandler { | ||
|
||
private final JWTService jwtService; | ||
|
||
@Override | ||
public void logout(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, | ||
Authentication authentication) { | ||
String header = httpServletRequest.getHeader(HttpHeaders.AUTHORIZATION); | ||
|
||
if (StringUtils.isEmpty(header) || !header.startsWith(BEARER_TOKEN_PREFIX)) { | ||
return; | ||
} | ||
var token = header.replace(BEARER_TOKEN_PREFIX, ""); | ||
DecodedJWT jwt = jwtService.verify(token); | ||
jwtService.invalidateTokensOfUser(jwt.getSubject()); | ||
} | ||
} |
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
28 changes: 28 additions & 0 deletions
28
iris-client-bff/src/main/java/iris/client_bff/auth/db/jwt/AllowedToken.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,28 @@ | ||
package iris.client_bff.auth.db.jwt; | ||
|
||
import lombok.*; | ||
|
||
import java.io.Serializable; | ||
import java.time.Instant; | ||
|
||
import javax.persistence.*; | ||
|
||
@Entity | ||
@Table(name = "allowed_tokens") | ||
@Data | ||
public class AllowedToken implements Serializable { | ||
|
||
@Id | ||
private String jwtTokenDigest; | ||
|
||
@Column(nullable = false) | ||
private String userName; | ||
|
||
@Column(nullable = false) | ||
private Instant expirationTime; | ||
|
||
@Column(nullable = false) | ||
private Instant created; | ||
} | ||
|
||
|
17 changes: 17 additions & 0 deletions
17
iris-client-bff/src/main/java/iris/client_bff/auth/db/jwt/AllowedTokenRepository.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,17 @@ | ||
package iris.client_bff.auth.db.jwt; | ||
|
||
import java.time.Instant; | ||
import java.util.Optional; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
public interface AllowedTokenRepository extends JpaRepository<AllowedToken, String> { | ||
Optional<AllowedToken> findByJwtTokenDigest(String token); | ||
|
||
@Transactional | ||
void deleteByUserName(String userName); | ||
|
||
@Transactional | ||
void deleteByExpirationTimeBefore(Instant expirationTime); | ||
} |
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
24 changes: 24 additions & 0 deletions
24
iris-client-bff/src/main/java/iris/client_bff/auth/db/jwt/JWTWhitelistCleanup.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,24 @@ | ||
package iris.client_bff.auth.db.jwt; | ||
|
||
import lombok.AllArgsConstructor; | ||
|
||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.scheduling.annotation.EnableScheduling; | ||
import org.springframework.scheduling.annotation.Scheduled; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
@Configuration | ||
@EnableScheduling | ||
@AllArgsConstructor | ||
public class JWTWhitelistCleanup { | ||
|
||
private final JWTService jwtService; | ||
private final long DELETION_RATE = 30*60*1000; // 30 minutes | ||
|
||
@Scheduled(fixedDelay = DELETION_RATE) | ||
public void clean() { | ||
jwtService.removeExpiredTokens(); | ||
} | ||
|
||
} |
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
6 changes: 6 additions & 0 deletions
6
iris-client-bff/src/main/resources/db/migration/V1003__introduce_allowed_tokens.sql
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,6 @@ | ||
CREATE TABLE allowed_tokens ( | ||
jwt_token_digest varchar(255) primary key, | ||
user_name varchar(50) NOT NULL, | ||
expiration_time timestamp NOT NULL, | ||
created timestamp default now() | ||
); |
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.