Skip to content

Commit 0bd2319

Browse files
author
lastpeony
committed
issue #4809 jwt blacklist implementation and stop play/ publish using
jwt. blacklist using already existed token db with flag
1 parent fc28eed commit 0bd2319

File tree

11 files changed

+271
-174
lines changed

11 files changed

+271
-174
lines changed

src/main/java/io/antmedia/datastore/db/DataStore.java

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -427,25 +427,27 @@ public List<Token> listAllTokens (Map<String, String> tokenMap, String streamId,
427427
public abstract boolean deleteToken (String tokenId);
428428

429429
/**
430-
* Delete specific token from blacklist.
430+
* Whitelist specific token.
431431
* @param tokenId id of the token
432432
*/
433-
public abstract boolean deleteTokenFromBlacklist (String tokenId);
433+
public abstract boolean whiteListToken(String tokenId);
434434

435435
/**
436-
* Get all tokens from jwt blacklist.
436+
* Get all blacklisted tokens.
437437
*/
438-
public abstract List<String> getJwtBlacklist();
438+
public abstract List<String> getBlackListedTokens();
439439

440440
/**
441-
* Delete all expired tokens from jwt blacklist.
441+
* Delete all blacklisted expired tokens.
442442
*/
443-
public abstract Result deleteAllExpiredJwtFromBlacklist(ITokenService tokenService);
443+
public abstract Result deleteAllBlacklistedExpiredTokens(ITokenService tokenService);
444444

445445
/**
446-
* Delete all tokens from jwt blacklist.
446+
* Whitelist all blacklisted tokens.
447+
*
448+
* @return
447449
*/
448-
public abstract void clearJwtBlacklist();
450+
public abstract boolean whiteListAllTokens();
449451

450452
/**
451453
* retrieve specific token
@@ -1377,16 +1379,16 @@ public List<WebRTCViewerInfo> getWebRTCViewerList(Map<String, String> webRTCView
13771379
public abstract boolean updateStreamMetaData(String streamId, String metaData);
13781380

13791381
/**
1380-
* Add jwt token to black list.
1381-
* @param token which will be added to black list.
1382+
* Blacklist token.
1383+
* @param token which will be blacklisted.
13821384
*/
1383-
public abstract boolean addTokenToBlacklist(Token token);
1385+
public abstract boolean blackListToken(Token token);
13841386

13851387
/**
1386-
* Get token from black list.
1388+
* Get token from blacklist.
13871389
* @param tokenId id of the token.
13881390
*/
1389-
public abstract Token getTokenFromBlacklist(String tokenId);
1391+
public abstract Token getBlackListedToken(String tokenId);
13901392

13911393
//**************************************
13921394
//ATTENTION: Write function descriptions while adding new functions

src/main/java/io/antmedia/datastore/db/InMemoryDataStore.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -898,23 +898,23 @@ public boolean deleteToken(String tokenId) {
898898
}
899899

900900
@Override
901-
public boolean deleteTokenFromBlacklist(String tokenId) {
901+
public boolean whiteListToken(String tokenId) {
902902
return false;
903903
}
904904

905905
@Override
906-
public List<String> getJwtBlacklist() {
906+
public List<String> getBlackListedTokens() {
907907
return Collections.emptyList();
908908
}
909909

910910
@Override
911-
public Result deleteAllExpiredJwtFromBlacklist(ITokenService tokenService) {
911+
public Result deleteAllBlacklistedExpiredTokens(ITokenService tokenService) {
912912
return null;
913913
}
914914

915915
@Override
916-
public void clearJwtBlacklist() {
917-
throw new UnsupportedOperationException("JWT blacklist must be stored as map based db on disk, not in memory.");
916+
public boolean whiteListAllTokens() {
917+
throw new UnsupportedOperationException("");
918918
}
919919

920920
@Override
@@ -1041,12 +1041,12 @@ public boolean updateStreamMetaData(String streamId, String metaData) {
10411041
}
10421042

10431043
@Override
1044-
public boolean addTokenToBlacklist(Token token) {
1044+
public boolean blackListToken(Token token) {
10451045
return false;
10461046
}
10471047

10481048
@Override
1049-
public Token getTokenFromBlacklist(String tokenId) {
1049+
public Token getBlackListedToken(String tokenId) {
10501050
return null;
10511051
}
10521052
}

src/main/java/io/antmedia/datastore/db/MapBasedDataStore.java

Lines changed: 47 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,6 @@ public abstract class MapBasedDataStore extends DataStore {
4444
protected Map<String, String> vodMap;
4545
protected Map<String, String> detectionMap;
4646
protected Map<String, String> tokenMap;
47-
protected Map<String, String> tokenBlacklistMap;
48-
4947
protected Map<String, String> subscriberMap;
5048
protected Map<String, String> conferenceRoomMap;
5149
protected Map<String, String> webRTCViewerMap;
@@ -949,36 +947,43 @@ public boolean deleteToken(String tokenId) {
949947
}
950948

951949
@Override
952-
public boolean deleteTokenFromBlacklist(String tokenId) {
953-
boolean result;
954-
955-
synchronized (this) {
956-
result = tokenBlacklistMap.remove(tokenId) != null;
950+
public boolean whiteListToken(String tokenId) {
951+
synchronized (this){
952+
Token token = getToken(tokenId);
953+
if(token != null && token.isBlackListed()){
954+
token.setBlackListed(false);
955+
return saveToken(token);
956+
}
957957
}
958-
return result;
958+
959+
return false;
959960
}
960961

961962
@Override
962-
public List<String> getJwtBlacklist(){
963-
963+
public List<String> getBlackListedTokens(){
964+
ArrayList<String> tokenBlacklist = new ArrayList<>();
964965
synchronized (this){
965-
return new ArrayList<>(tokenBlacklistMap.keySet());
966-
966+
tokenMap.forEach((tokenId, tokenAsJson) -> {
967+
Token token = gson.fromJson(tokenAsJson,Token.class);
968+
if(token.isBlackListed()){
969+
tokenBlacklist.add(tokenId);
970+
}
971+
});
972+
return tokenBlacklist;
967973
}
968-
969974
}
970975

971976
@Override
972-
public Result deleteAllExpiredJwtFromBlacklist(ITokenService tokenService){
973-
logger.info("Deleting all expired JWTs from black list.");
977+
public Result deleteAllBlacklistedExpiredTokens(ITokenService tokenService){
978+
logger.info("Deleting all expired JWTs from blacklist.");
974979
AtomicInteger deletedTokenCount = new AtomicInteger();
975980

976-
synchronized (this){
977-
tokenBlacklistMap.forEach((key, value) -> {
978-
Token token = gson.fromJson(value,Token.class);
979-
String tokenId = token.getTokenId();
980-
if(!tokenService.verifyJwt(tokenId,token.getStreamId(),token.getType())){
981-
if(deleteTokenFromBlacklist(tokenId)){
981+
synchronized (this) {
982+
983+
tokenMap.forEach((tokenId, tokenAsJson) -> {
984+
Token token = gson.fromJson(tokenAsJson,Token.class);
985+
if(token.isBlackListed() && !tokenService.verifyJwt(tokenId,token.getStreamId(),token.getType())){
986+
if(deleteToken(tokenId)){
982987
deletedTokenCount.getAndIncrement();
983988
}else{
984989
logger.warn("Couldn't delete JWT:{}", tokenId);
@@ -988,23 +993,30 @@ public Result deleteAllExpiredJwtFromBlacklist(ITokenService tokenService){
988993
}
989994

990995
if(deletedTokenCount.get() > 0){
991-
final String successMsg = deletedTokenCount+" JWT deleted successfully from black list.";
996+
final String successMsg = deletedTokenCount+" JWT deleted successfully from blacklist.";
992997
logger.info(successMsg);
993998
return new Result(true, successMsg);
994999
}else{
995-
final String failMsg = "No JWT deleted from black list.";
1000+
final String failMsg = "No JWT deleted from blacklist.";
9961001
logger.warn(failMsg);
9971002
return new Result(false, failMsg);
998-
9991003
}
10001004

10011005
}
10021006

10031007
@Override
1004-
public void clearJwtBlacklist(){
1008+
public boolean whiteListAllTokens(){
1009+
10051010
synchronized (this) {
1006-
tokenBlacklistMap.clear();
1011+
tokenMap.forEach((tokenId, tokenAsJson) -> {
1012+
Token token = gson.fromJson(tokenAsJson,Token.class);
1013+
if(token.isBlackListed()){
1014+
whiteListToken(tokenId);
1015+
}
1016+
});
10071017
}
1018+
return true;
1019+
10081020
}
10091021

10101022
@Override
@@ -1120,29 +1132,27 @@ public Broadcast getBroadcastFromMap(String streamId)
11201132
}
11211133

11221134
@Override
1123-
public boolean addTokenToBlacklist(Token token) {
1135+
public boolean blackListToken(Token token) {
11241136
boolean result = false;
11251137

11261138
synchronized (this) {
11271139

11281140
if (token.getStreamId() != null && token.getTokenId() != null) {
1129-
1130-
try {
1131-
tokenBlacklistMap.put(token.getTokenId(), gson.toJson(token));
1132-
result = true;
1133-
} catch (Exception e) {
1134-
logger.error(ExceptionUtils.getStackTrace(e));
1135-
}
1141+
token.setBlackListed(true);
1142+
return saveToken(token);
11361143
}
11371144
}
11381145
return result;
11391146

11401147
}
11411148

11421149
@Override
1143-
public Token getTokenFromBlacklist(String tokenId) {
1144-
return super.getToken(tokenBlacklistMap, tokenId, gson);
1145-
1150+
public Token getBlackListedToken(String tokenId) {
1151+
Token token = getToken(tokenId);
1152+
if(token != null && token.isBlackListed()){
1153+
return token;
1154+
}
1155+
return null;
11461156
}
11471157

11481158
}

src/main/java/io/antmedia/datastore/db/MapDBStore.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@ public class MapDBStore extends MapBasedDataStore {
2828
private static final String VOD_MAP_NAME = "VOD";
2929
private static final String DETECTION_MAP_NAME = "DETECTION";
3030
private static final String TOKEN = "TOKEN";
31-
private static final String TOKEN_BLACKLIST = "TOKEN_BLACKLIST";
32-
3331
private static final String SUBSCRIBER = "SUBSCRIBER";
3432
private static final String CONFERENCE_ROOM_MAP_NAME = "CONFERENCE_ROOM";
3533
private static final String WEBRTC_VIEWER = "WEBRTC_VIEWER";
@@ -70,9 +68,6 @@ public MapDBStore(String dbName, Vertx vertx) {
7068
webRTCViewerMap = db.treeMap(WEBRTC_VIEWER).keySerializer(Serializer.STRING).valueSerializer(Serializer.STRING)
7169
.counterEnable().createOrOpen();
7270

73-
tokenBlacklistMap = db.treeMap(TOKEN_BLACKLIST).keySerializer(Serializer.STRING).valueSerializer(Serializer.STRING)
74-
.counterEnable().createOrOpen();
75-
7671

7772
timerId = vertx.setPeriodic(5000, id ->
7873

0 commit comments

Comments
 (0)