diff --git a/src/main/java/io/antmedia/datastore/db/DataStore.java b/src/main/java/io/antmedia/datastore/db/DataStore.java index 8a1b59cf8..a6bfcdbd4 100644 --- a/src/main/java/io/antmedia/datastore/db/DataStore.java +++ b/src/main/java/io/antmedia/datastore/db/DataStore.java @@ -427,25 +427,27 @@ public List listAllTokens (Map tokenMap, String streamId, public abstract boolean deleteToken (String tokenId); /** - * Delete specific token from blacklist. + * Whitelist specific token. * @param tokenId id of the token */ - public abstract boolean deleteTokenFromBlacklist (String tokenId); + public abstract boolean whiteListToken(String tokenId); /** - * Get all tokens from jwt blacklist. + * Get all blacklisted tokens. */ - public abstract List getJwtBlacklist(); + public abstract List getBlackListedTokens(); /** - * Delete all expired tokens from jwt blacklist. + * Whitelist all expired tokens. */ - public abstract Result deleteAllExpiredJwtFromBlacklist(ITokenService tokenService); + public abstract Result whiteListAllExpiredTokens(ITokenService tokenService); /** - * Delete all tokens from jwt blacklist. + * Whitelist all blacklisted tokens. + * + * @return */ - public abstract void clearJwtBlacklist(); + public abstract boolean whiteListAllTokens(); /** * retrieve specific token @@ -1377,16 +1379,16 @@ public List getWebRTCViewerList(Map webRTCView public abstract boolean updateStreamMetaData(String streamId, String metaData); /** - * Add jwt token to black list. - * @param token which will be added to black list. + * Blacklist token. + * @param token which will be blacklisted. */ - public abstract boolean addTokenToBlacklist(Token token); + public abstract boolean blackListToken(Token token); /** - * Get token from black list. + * Get token from blacklist. * @param tokenId id of the token. */ - public abstract Token getTokenFromBlacklist(String tokenId); + public abstract Token getBlackListedToken(String tokenId); //************************************** //ATTENTION: Write function descriptions while adding new functions diff --git a/src/main/java/io/antmedia/datastore/db/InMemoryDataStore.java b/src/main/java/io/antmedia/datastore/db/InMemoryDataStore.java index 5ea91a003..1d4817bf9 100644 --- a/src/main/java/io/antmedia/datastore/db/InMemoryDataStore.java +++ b/src/main/java/io/antmedia/datastore/db/InMemoryDataStore.java @@ -898,23 +898,23 @@ public boolean deleteToken(String tokenId) { } @Override - public boolean deleteTokenFromBlacklist(String tokenId) { + public boolean whiteListToken(String tokenId) { return false; } @Override - public List getJwtBlacklist() { + public List getBlackListedTokens() { return Collections.emptyList(); } @Override - public Result deleteAllExpiredJwtFromBlacklist(ITokenService tokenService) { + public Result whiteListAllExpiredTokens(ITokenService tokenService) { return null; } @Override - public void clearJwtBlacklist() { - throw new UnsupportedOperationException("JWT blacklist must be stored as map based db on disk, not in memory."); + public boolean whiteListAllTokens() { + throw new UnsupportedOperationException(""); } @Override @@ -1041,12 +1041,12 @@ public boolean updateStreamMetaData(String streamId, String metaData) { } @Override - public boolean addTokenToBlacklist(Token token) { + public boolean blackListToken(Token token) { return false; } @Override - public Token getTokenFromBlacklist(String tokenId) { + public Token getBlackListedToken(String tokenId) { return null; } } \ No newline at end of file diff --git a/src/main/java/io/antmedia/datastore/db/MapBasedDataStore.java b/src/main/java/io/antmedia/datastore/db/MapBasedDataStore.java index 1d25414df..0081abac9 100644 --- a/src/main/java/io/antmedia/datastore/db/MapBasedDataStore.java +++ b/src/main/java/io/antmedia/datastore/db/MapBasedDataStore.java @@ -44,8 +44,6 @@ public abstract class MapBasedDataStore extends DataStore { protected Map vodMap; protected Map detectionMap; protected Map tokenMap; - protected Map tokenBlacklistMap; - protected Map subscriberMap; protected Map conferenceRoomMap; protected Map webRTCViewerMap; @@ -949,36 +947,43 @@ public boolean deleteToken(String tokenId) { } @Override - public boolean deleteTokenFromBlacklist(String tokenId) { - boolean result; - - synchronized (this) { - result = tokenBlacklistMap.remove(tokenId) != null; + public boolean whiteListToken(String tokenId) { + synchronized (this){ + Token token = getToken(tokenId); + if(token != null && token.isBlackListed()){ + token.setBlackListed(false); + return saveToken(token); + } } - return result; + + return false; } @Override - public List getJwtBlacklist(){ - + public List getBlackListedTokens(){ + ArrayList tokenBlacklist = new ArrayList<>(); synchronized (this){ - return new ArrayList<>(tokenBlacklistMap.keySet()); - + tokenMap.forEach((tokenId, tokenAsJson) -> { + Token token = gson.fromJson(tokenAsJson,Token.class); + if(token.isBlackListed()){ + tokenBlacklist.add(tokenId); + } + }); + return tokenBlacklist; } - } @Override - public Result deleteAllExpiredJwtFromBlacklist(ITokenService tokenService){ - logger.info("Deleting all expired JWTs from black list."); + public Result whiteListAllExpiredTokens(ITokenService tokenService){ + logger.info("Deleting all expired JWTs from blacklist."); AtomicInteger deletedTokenCount = new AtomicInteger(); - synchronized (this){ - tokenBlacklistMap.forEach((key, value) -> { - Token token = gson.fromJson(value,Token.class); - String tokenId = token.getTokenId(); - if(!tokenService.verifyJwt(tokenId,token.getStreamId(),token.getType())){ - if(deleteTokenFromBlacklist(tokenId)){ + synchronized (this) { + + tokenMap.forEach((tokenId, tokenAsJson) -> { + Token token = gson.fromJson(tokenAsJson,Token.class); + if(token.isBlackListed() && !tokenService.verifyJwt(tokenId,token.getStreamId(),token.getType())){ + if(whiteListToken(tokenId)){ deletedTokenCount.getAndIncrement(); }else{ logger.warn("Couldn't delete JWT:{}", tokenId); @@ -988,23 +993,30 @@ public Result deleteAllExpiredJwtFromBlacklist(ITokenService tokenService){ } if(deletedTokenCount.get() > 0){ - final String successMsg = deletedTokenCount+" JWT deleted successfully from black list."; + final String successMsg = deletedTokenCount+" JWT deleted successfully from blacklist."; logger.info(successMsg); return new Result(true, successMsg); }else{ - final String failMsg = "No JWT deleted from black list."; + final String failMsg = "No JWT deleted from blacklist."; logger.warn(failMsg); return new Result(false, failMsg); - } } @Override - public void clearJwtBlacklist(){ + public boolean whiteListAllTokens(){ + synchronized (this) { - tokenBlacklistMap.clear(); + tokenMap.forEach((tokenId, tokenAsJson) -> { + Token token = gson.fromJson(tokenAsJson,Token.class); + if(token.isBlackListed()){ + whiteListToken(tokenId); + } + }); } + return true; + } @Override @@ -1120,19 +1132,14 @@ public Broadcast getBroadcastFromMap(String streamId) } @Override - public boolean addTokenToBlacklist(Token token) { + public boolean blackListToken(Token token) { boolean result = false; synchronized (this) { if (token.getStreamId() != null && token.getTokenId() != null) { - - try { - tokenBlacklistMap.put(token.getTokenId(), gson.toJson(token)); - result = true; - } catch (Exception e) { - logger.error(ExceptionUtils.getStackTrace(e)); - } + token.setBlackListed(true); + return saveToken(token); } } return result; @@ -1140,9 +1147,12 @@ public boolean addTokenToBlacklist(Token token) { } @Override - public Token getTokenFromBlacklist(String tokenId) { - return super.getToken(tokenBlacklistMap, tokenId, gson); - + public Token getBlackListedToken(String tokenId) { + Token token = getToken(tokenId); + if(token != null && token.isBlackListed()){ + return token; + } + return null; } } diff --git a/src/main/java/io/antmedia/datastore/db/MapDBStore.java b/src/main/java/io/antmedia/datastore/db/MapDBStore.java index 87eced277..291e0d246 100644 --- a/src/main/java/io/antmedia/datastore/db/MapDBStore.java +++ b/src/main/java/io/antmedia/datastore/db/MapDBStore.java @@ -28,8 +28,6 @@ public class MapDBStore extends MapBasedDataStore { private static final String VOD_MAP_NAME = "VOD"; private static final String DETECTION_MAP_NAME = "DETECTION"; private static final String TOKEN = "TOKEN"; - private static final String TOKEN_BLACKLIST = "TOKEN_BLACKLIST"; - private static final String SUBSCRIBER = "SUBSCRIBER"; private static final String CONFERENCE_ROOM_MAP_NAME = "CONFERENCE_ROOM"; private static final String WEBRTC_VIEWER = "WEBRTC_VIEWER"; @@ -70,9 +68,6 @@ public MapDBStore(String dbName, Vertx vertx) { webRTCViewerMap = db.treeMap(WEBRTC_VIEWER).keySerializer(Serializer.STRING).valueSerializer(Serializer.STRING) .counterEnable().createOrOpen(); - tokenBlacklistMap = db.treeMap(TOKEN_BLACKLIST).keySerializer(Serializer.STRING).valueSerializer(Serializer.STRING) - .counterEnable().createOrOpen(); - timerId = vertx.setPeriodic(5000, id -> diff --git a/src/main/java/io/antmedia/datastore/db/MongoStore.java b/src/main/java/io/antmedia/datastore/db/MongoStore.java index f555b8e85..1914adc84 100644 --- a/src/main/java/io/antmedia/datastore/db/MongoStore.java +++ b/src/main/java/io/antmedia/datastore/db/MongoStore.java @@ -9,8 +9,8 @@ import java.time.Instant; import java.util.ArrayList; import java.util.Arrays; -import java.util.Collections; import java.util.List; +import java.util.concurrent.atomic.AtomicInteger; import java.util.regex.Pattern; import io.antmedia.rest.model.Result; @@ -59,6 +59,7 @@ public class MongoStore extends DataStore { public static final String VOD_ID = "vodId"; private static final String VIEWER_ID = "viewerId"; private static final String TOKEN_ID = "tokenId"; + private static final String BLACKLISTED = "blackListed"; public static final String STREAM_ID = "streamId"; private Datastore datastore; private Datastore vodDatastore; @@ -1198,26 +1199,6 @@ public boolean deleteToken(String tokenId) { return result; } - @Override - public boolean deleteTokenFromBlacklist(String tokenId) { - return false; - } - - @Override - public List getJwtBlacklist() { - return Collections.emptyList(); - } - - @Override - public Result deleteAllExpiredJwtFromBlacklist(ITokenService tokenService) { - return null; - } - - @Override - public void clearJwtBlacklist() { - throw new UnsupportedOperationException("JWT blacklist must be stored as map based db on disk, not in mongodb."); - } - @Override public Token getToken(String tokenId) { Token token = null; @@ -1453,13 +1434,101 @@ public boolean updateStreamMetaData(String streamId, String metaData) { } @Override - public boolean addTokenToBlacklist(Token token) { + public boolean blackListToken(Token token) { + boolean result = false; + //update if exists, else insert + synchronized (this) { + if (token.getStreamId() != null && token.getTokenId() != null) { + Query query = tokenDatastore.find(Token.class).filter(Filters.eq(TOKEN_ID, token.getTokenId())); + + if(query.first() != null){ + final UpdateResult results = query.update(new UpdateOptions().multi(false), set(BLACKLISTED, true)); + if(results.getModifiedCount() == 1){ + result = true; + } + }else{ + token.setBlackListed(true); + result = saveToken(token); + } + } + } + + return result; - return false; } @Override - public Token getTokenFromBlacklist(String tokenId) { + public Token getBlackListedToken(String tokenId) { + synchronized (this){ + Query query = tokenDatastore.find(Token.class).filter(Filters.eq(TOKEN_ID, tokenId)); + Token fetchedToken = query.first(); + if(fetchedToken != null && fetchedToken.isBlackListed()){ + return fetchedToken; + } + } return null; } + + @Override + public boolean whiteListToken(String tokenId) { + synchronized (this){ + Query query = tokenDatastore.find(Token.class).filter(Filters.eq(TOKEN_ID, tokenId)); + final UpdateResult results = query.update(new UpdateOptions().multi(false), set(BLACKLISTED, false)); + return results.wasAcknowledged(); + } + } + + @Override + public List getBlackListedTokens() { + List tokenBlacklist = new ArrayList<>(); + synchronized (this){ + Query query = tokenDatastore.find(Token.class).filter(Filters.eq(BLACKLISTED, true)); + for (Token token : query) { + tokenBlacklist.add(token.getTokenId()); + } + return tokenBlacklist; + } + } + + @Override + public Result whiteListAllExpiredTokens(ITokenService tokenService) { + AtomicInteger deletedTokenCount = new AtomicInteger(); + + synchronized (this){ + List tokenBlacklist = getBlackListedTokens(); + tokenBlacklist.forEach(tokenId ->{ + + Token token = getToken(tokenId); + if(!tokenService.verifyJwt(tokenId,token.getStreamId(),token.getType())){ + if(whiteListToken(tokenId)){ + deletedTokenCount.getAndIncrement(); + }else{ + logger.warn("Couldn't delete JWT:{}", tokenId); + } + } + + }); + + } + if(deletedTokenCount.get() > 0){ + final String successMsg = deletedTokenCount+" JWT deleted successfully from blacklist."; + logger.info(successMsg); + return new Result(true, successMsg); + }else{ + final String failMsg = "No JWT deleted from blacklist."; + logger.warn(failMsg); + return new Result(false, failMsg); + } + + } + + @Override + public boolean whiteListAllTokens() { + synchronized (this) { + Query query = tokenDatastore.find(Token.class).filter(Filters.eq(BLACKLISTED, true)); + final UpdateResult results = query.update(new UpdateOptions().multi(true), set(BLACKLISTED, false)); + return results.wasAcknowledged(); + } + } + } diff --git a/src/main/java/io/antmedia/datastore/db/RedisStore.java b/src/main/java/io/antmedia/datastore/db/RedisStore.java index 0213d06f4..dd8e5fd4f 100644 --- a/src/main/java/io/antmedia/datastore/db/RedisStore.java +++ b/src/main/java/io/antmedia/datastore/db/RedisStore.java @@ -121,13 +121,13 @@ public int resetBroadcasts(String hostAddress) { } @Override - public boolean addTokenToBlacklist(Token token) { + public boolean blackListToken(Token token) { return false; } @Override - public Token getTokenFromBlacklist(String tokenId) { + public Token getBlackListedToken(String tokenId) { return null; } diff --git a/src/main/java/io/antmedia/datastore/db/types/Token.java b/src/main/java/io/antmedia/datastore/db/types/Token.java index a4cf8bdef..d38b79b48 100644 --- a/src/main/java/io/antmedia/datastore/db/types/Token.java +++ b/src/main/java/io/antmedia/datastore/db/types/Token.java @@ -51,6 +51,16 @@ public class Token { */ @ApiModelProperty(value = "the type of the token") private String type; + + public ObjectId getDbId() { + return dbId; + } + + public void setDbId(ObjectId dbId) { + this.dbId = dbId; + } + + private boolean blackListed; /** * the id of the conference room which requested streams belongs to. @@ -98,6 +108,13 @@ public long getExpireDate() { public void setExpireDate(long expireDate) { this.expireDate = expireDate; } - + + public boolean isBlackListed() { + return blackListed; + } + + public void setBlackListed(boolean blackListed) { + this.blackListed = blackListed; + } } diff --git a/src/main/java/io/antmedia/rest/BroadcastRestService.java b/src/main/java/io/antmedia/rest/BroadcastRestService.java index 74645cb0b..cf5739495 100644 --- a/src/main/java/io/antmedia/rest/BroadcastRestService.java +++ b/src/main/java/io/antmedia/rest/BroadcastRestService.java @@ -87,7 +87,7 @@ public class BroadcastRestService extends RestServiceBase{ private static final String ABSOLUTE_MOVE = "absolute"; private static final String CONTINUOUS_MOVE = "continuous"; - private final String blacklistNotEnabledMsg = "JWT black list is not enabled for this application."; + private final String blacklistNotEnabledMsg = "JWT blacklist is not enabled for this application."; @ApiModel(value="SimpleStat", description="Simple generic statistics class to return single values") @@ -606,13 +606,13 @@ public Result validateTokenV2(@ApiParam(value = "Token to be validated", require return new Result(result); } - @ApiOperation(value = "Add jwt token to black list. If added, success field is true, " + @ApiOperation(value = "Add jwt token to blacklist. If added, success field is true, " + "if not added success field false", response = Result.class) @POST @Consumes(MediaType.APPLICATION_JSON) @Path("/jwt-black-list") @Produces(MediaType.APPLICATION_JSON) - public Result addJwtToBlacklist(@ApiParam(value = "jwt to be added to black list.", required = true) @QueryParam("jwt") String jwt) + public Result addJwtToBlacklist(@ApiParam(value = "jwt to be added to blacklist.", required = true) @QueryParam("jwt") String jwt) { if(getAppSettings().isJwtBlacklistEnabled()){ try{ @@ -633,9 +633,9 @@ public Result addJwtToBlacklist(@ApiParam(value = "jwt to be added to black list if(!super.verifyJwt(jwt, streamId, tokenType)){ return new Result(false,"JWT is not valid."); - }else if(getDataStore().getTokenFromBlacklist(token.getTokenId()) != null){ + }else if(getDataStore().getBlackListedToken(jwt) != null){ return new Result(false, "JWT is already in blacklist."); - }else if(getDataStore().addTokenToBlacklist(token)){ + }else if(getDataStore().blackListToken(token)){ return new Result(true, "JWT successfully added to blacklist."); } @@ -654,23 +654,23 @@ public Result addJwtToBlacklist(@ApiParam(value = "jwt to be added to black list return new Result(false); } - @ApiOperation(value = "Delete jwt from black list. If deleted, success field is true, " + @ApiOperation(value = "Delete jwt from blacklist. If deleted, success field is true, " + "if not deleted success field false.", response = Result.class) @DELETE @Consumes(MediaType.APPLICATION_JSON) @Path("/jwt-black-list") @Produces(MediaType.APPLICATION_JSON) - public Result deleteJwtFromBlacklist(@ApiParam(value = "jwt to be deleted from black list.", required = true) @QueryParam("jwt") String jwt) + public Result deleteJwtFromBlacklist(@ApiParam(value = "jwt to be deleted from blacklist.", required = true) @QueryParam("jwt") String jwt) { if(getAppSettings().isJwtBlacklistEnabled()){ - if(getDataStore().getTokenFromBlacklist(jwt) == null){ - return new Result(false, "JWT does not exist in black list."); + if(getDataStore().getBlackListedToken(jwt) == null){ + return new Result(false, "JWT does not exist in blacklist."); - }else if(getDataStore().deleteTokenFromBlacklist(jwt)){ - return new Result(true, "JWT successfully deleted from black list."); + }else if(getDataStore().whiteListToken(jwt)){ + return new Result(true, "JWT successfully deleted from blacklist."); }else{ - return new Result(false, "JWT cannot be deleted from black list."); + return new Result(false, "JWT cannot be deleted from blacklist."); } }else{ logger.warn(blacklistNotEnabledMsg); @@ -685,39 +685,39 @@ public Result deleteJwtFromBlacklist(@ApiParam(value = "jwt to be deleted from b public List getJwtBlacklist() { if(getAppSettings().isJwtBlacklistEnabled()) { - return getDataStore().getJwtBlacklist(); + return getDataStore().getBlackListedTokens(); }else{ logger.warn(blacklistNotEnabledMsg); return null; } } - @ApiOperation(value = "Delete all expired JWTs from black list.", response = Result.class) + @ApiOperation(value = "Delete all expired JWTs from blacklist.", response = Result.class) @DELETE @Path("/jwt-black-list-delete-expired") @Produces(MediaType.APPLICATION_JSON) public Result deleteAllExpiredJwtFromBlacklist() { if(getAppSettings().isJwtBlacklistEnabled()) { - return getDataStore().deleteAllExpiredJwtFromBlacklist(getTokenService()); + return getDataStore().whiteListAllExpiredTokens(getTokenService()); }else{ logger.warn(blacklistNotEnabledMsg); return new Result(false, blacklistNotEnabledMsg); } } - @ApiOperation(value = "Delete all JWTs from black list.", response = Result.class) + @ApiOperation(value = "Delete all JWTs from blacklist.", response = Result.class) @DELETE @Path("/jwt-black-list-clear") @Produces(MediaType.APPLICATION_JSON) public Result clearJwtBlacklist() { if(getAppSettings().isJwtBlacklistEnabled()) { - getDataStore().clearJwtBlacklist(); - if(getDataStore().getJwtBlacklist().isEmpty()){ - return new Result(true, "JWT black list cleared successfully"); + getDataStore().whiteListAllTokens(); + if(getDataStore().getBlackListedTokens().isEmpty()){ + return new Result(true, "JWT blacklist cleared successfully"); }else{ - return new Result(false, "JWT black list clear failed."); + return new Result(false, "JWT blacklist clear failed."); } }else{ logger.warn(blacklistNotEnabledMsg); diff --git a/src/test/java/io/antmedia/test/db/DBStoresUnitTest.java b/src/test/java/io/antmedia/test/db/DBStoresUnitTest.java index b506358d3..7bef85018 100644 --- a/src/test/java/io/antmedia/test/db/DBStoresUnitTest.java +++ b/src/test/java/io/antmedia/test/db/DBStoresUnitTest.java @@ -29,7 +29,6 @@ import org.junit.After; import org.junit.Before; import org.junit.Test; -import org.mockito.Mockito; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.context.ApplicationContext; @@ -138,8 +137,8 @@ public void testMapDBStore() { testWebRTCViewerOperations(dataStore); testUpdateMetaData(dataStore); testStreamSourceList(dataStore); - testDeleteAllExpiredJwtFromBlacklist(dataStore); - testClearJwtBlacklist(dataStore); + testWhitelistAllExpiredTokens(dataStore); + testWhitelistAllTokens(dataStore); } @@ -271,6 +270,8 @@ public void testMongoStore() { testUpdateEndpointStatus(dataStore); testWebRTCViewerOperations(dataStore); testUpdateMetaData(dataStore); + testWhitelistAllExpiredTokens(dataStore); + testWhitelistAllTokens(dataStore); } @Test @@ -2254,6 +2255,18 @@ public void testMongoDBSaveStreamInfo() { deleteStreamInfos(dataStore); } + @Test + public void testMongoDBJwtBlacklist(){ + MongoStore dataStore = new MongoStore("localhost", "", "", "testdb"); + dataStore.whiteListToken(""); + dataStore.getBlackListedTokens(); + dataStore.whiteListAllExpiredTokens(null); + dataStore.whiteListAllTokens(); + dataStore.blackListToken(new Token()); + dataStore.getBlackListedToken(""); + + } + public void deleteStreamInfos(MongoStore datastore) { datastore.getDataStore().find(StreamInfo.class).delete(new DeleteOptions() .multi(true)); @@ -2841,7 +2854,7 @@ public void testUpdateMetaData(DataStore dataStore) { } - public void testDeleteAllExpiredJwtFromBlacklist(DataStore dataStore){ + public void testWhitelistAllExpiredTokens(DataStore dataStore){ Token token1 = new Token(); Token token2 = new Token(); @@ -2867,14 +2880,14 @@ public void testDeleteAllExpiredJwtFromBlacklist(DataStore dataStore){ token3.setStreamId(streamId); ITokenService tokenService = mock(ITokenService.class); - Result res = dataStore.deleteAllExpiredJwtFromBlacklist(tokenService); + Result res = dataStore.whiteListAllExpiredTokens(tokenService); assertFalse(res.isSuccess()); - dataStore.addTokenToBlacklist(token1); - dataStore.addTokenToBlacklist(token2); - dataStore.addTokenToBlacklist(token3); + dataStore.blackListToken(token1); + dataStore.blackListToken(token2); + dataStore.blackListToken(token3); - Token jwt = dataStore.getTokenFromBlacklist(token1.getTokenId()); + Token jwt = dataStore.getBlackListedToken(token1.getTokenId()); assertNotNull(jwt); when(tokenService.verifyJwt(jwt1,streamId,tokenType)).thenReturn(false); @@ -2882,20 +2895,22 @@ public void testDeleteAllExpiredJwtFromBlacklist(DataStore dataStore){ when(tokenService.verifyJwt(jwt3,streamId,tokenType)).thenReturn(false); - res = dataStore.deleteAllExpiredJwtFromBlacklist(tokenService); + res = dataStore.whiteListAllExpiredTokens(tokenService); assertTrue(res.isSuccess()); } - public void testClearJwtBlacklist(DataStore dataStore){ + public void testWhitelistAllTokens(DataStore dataStore){ + addJwtsToBlacklist(dataStore); - List jwtBlacklist = dataStore.getJwtBlacklist(); + List jwtBlacklist = dataStore.getBlackListedTokens(); assertEquals(3, jwtBlacklist.size()); - dataStore.clearJwtBlacklist(); - jwtBlacklist = dataStore.getJwtBlacklist(); + Token token = dataStore.getBlackListedToken("jwt1"); + dataStore.whiteListAllTokens(); + jwtBlacklist = dataStore.getBlackListedTokens(); assertEquals(0, jwtBlacklist.size()); @@ -2918,6 +2933,7 @@ private void addJwtsToBlacklist(DataStore dataStore){ token2.setTokenId(jwt2); token3.setTokenId(jwt3); + token1.setType(tokenType); token2.setType(tokenType); token3.setType(tokenType); @@ -2926,9 +2942,9 @@ private void addJwtsToBlacklist(DataStore dataStore){ token2.setStreamId(streamId); token3.setStreamId(streamId); - dataStore.addTokenToBlacklist(token1); - dataStore.addTokenToBlacklist(token2); - dataStore.addTokenToBlacklist(token3); + dataStore.blackListToken(token1); + dataStore.blackListToken(token2); + dataStore.blackListToken(token3); } } diff --git a/src/test/java/io/antmedia/test/rest/BroadcastRestServiceV2UnitTest.java b/src/test/java/io/antmedia/test/rest/BroadcastRestServiceV2UnitTest.java index db8dec72e..64123e48a 100644 --- a/src/test/java/io/antmedia/test/rest/BroadcastRestServiceV2UnitTest.java +++ b/src/test/java/io/antmedia/test/rest/BroadcastRestServiceV2UnitTest.java @@ -3074,7 +3074,7 @@ public void testAddJwtToBlacklist() { when(token.getStreamId()).thenReturn("test-stream"); - when(store.addTokenToBlacklist(token)).thenReturn(true); + when(store.blackListToken(token)).thenReturn(true); result = restServiceSpy.addJwtToBlacklist(jwt); assertFalse(result.isSuccess()); String invalidJwt = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdHJlYW1JZCI6InRlc3Qtc3RyZWFtIiwidHlwZSI6InF3ZSIsImV4cCI6OTk5OTk5OTk5OTk5OX0.DqfFkRJgKPVXgAkIzucuQtfwP2Oj-Qf9dhUuO_-04bU"; @@ -3086,12 +3086,12 @@ public void testAddJwtToBlacklist() { when(tokenService.verifyJwt(validJwt,"test-stream","publish")).thenReturn(true); - when(store.addTokenToBlacklist(any())).thenReturn(true); + when(store.blackListToken(any())).thenReturn(true); result = restServiceSpy.addJwtToBlacklist(validJwt); assertTrue(result.isSuccess()); - when(store.getTokenFromBlacklist(validJwt)).thenReturn(token); + when(store.getBlackListedToken(validJwt)).thenReturn(token); result = restServiceSpy.addJwtToBlacklist(validJwt); assertFalse(result.isSuccess()); @@ -3125,16 +3125,16 @@ public void testDeleteJwtFromBlacklist() { when(appSettings.isJwtBlacklistEnabled()).thenReturn(true); - when(store.getTokenFromBlacklist(jwt)).thenReturn(null); + when(store.getBlackListedToken(jwt)).thenReturn(null); Result result2 = restServiceSpy.deleteJwtFromBlacklist(jwt); assertFalse(result2.isSuccess()); Token token = mock(Token.class); - when(store.getTokenFromBlacklist(jwt)).thenReturn(token); + when(store.getBlackListedToken(jwt)).thenReturn(token); Result result3 = restServiceSpy.deleteJwtFromBlacklist(jwt); assertFalse(result3.isSuccess()); - when(store.deleteTokenFromBlacklist(jwt)).thenReturn(true); + when(store.whiteListToken(jwt)).thenReturn(true); Result result4 = restServiceSpy.deleteJwtFromBlacklist(jwt); assertTrue(result4.isSuccess()); @@ -3155,7 +3155,7 @@ public void testGetJwtBlacklist(){ BroadcastRestService restServiceSpy = Mockito.spy(restServiceReal); restServiceSpy.setAppCtx(appContext); - store.addTokenToBlacklist(token); + store.blackListToken(token); restServiceSpy.setDataStore(store); when(restServiceSpy.getDataStore()).thenReturn(store); @@ -3163,7 +3163,7 @@ public void testGetJwtBlacklist(){ tokenList.add(jwt); when(restServiceSpy.getAppSettings()).thenReturn(appSettings); when(appSettings.isJwtBlacklistEnabled()).thenReturn(true); - when(store.getJwtBlacklist()).thenReturn(tokenList); + when(store.getBlackListedTokens()).thenReturn(tokenList); List jwtBlacklist = restServiceSpy.getJwtBlacklist(); @@ -3186,7 +3186,7 @@ public void testClearJwtBlacklist() { Token token = new Token(); token.setTokenId(jwt); token.setType("publish"); - store.addTokenToBlacklist(token); + store.blackListToken(token); restServiceSpy.setDataStore(store); when(appSettings.isJwtBlacklistEnabled()).thenReturn(true); assertTrue(restServiceSpy.clearJwtBlacklist().isSuccess()); @@ -3206,13 +3206,13 @@ public void testDeleteAllExpiredJwtFromBlacklist(){ Token token = new Token(); token.setTokenId("token"); token.setType("publish"); - store.addTokenToBlacklist(token); + store.blackListToken(token); restServiceSpy.setDataStore(store); when(appSettings.isJwtBlacklistEnabled()).thenReturn(true); ITokenService tokenService = mock(ITokenService.class); when(appContext.getBean(ITokenService.BeanName.TOKEN_SERVICE.toString())).thenReturn(tokenService); - when(store.deleteAllExpiredJwtFromBlacklist(tokenService)).thenReturn(new Result(true)); + when(store.whiteListAllExpiredTokens(tokenService)).thenReturn(new Result(true)); assertTrue(restServiceSpy.deleteAllExpiredJwtFromBlacklist().isSuccess()); }