openvidu-server: Improve tokenRegister check

pull/621/head
cruizba 2021-04-07 09:40:00 +02:00
parent 51a7c56fb5
commit c5ceade2a5
2 changed files with 42 additions and 13 deletions

View File

@ -330,7 +330,6 @@ public abstract class SessionManager {
Token tokenObj = tokenGenerator.generateToken(session.getSessionId(), serverMetadata, record, role,
kurentoOptions);
session.storeToken(tokenObj);
tokenRegister.registerToken(session.getSessionId(), tokenObj);
return tokenObj;
}
@ -339,7 +338,6 @@ public abstract class SessionManager {
Token tokenObj = new Token(token, session.getSessionId(), connectionProperties,
this.openviduConfig.isTurnadminAvailable() ? this.coturnCredentialsService.createUser() : null);
session.storeToken(tokenObj);
tokenRegister.registerToken(session.getSessionId(), tokenObj);
return tokenObj;
}
@ -390,6 +388,8 @@ public abstract class SessionManager {
session.getUniqueSessionId(), token, clientMetadata, location, platform,
EndpointType.WEBRTC_ENDPOINT, null);
this.tokenRegister.registerToken(sessionId, p, token);
this.sessionidParticipantpublicidParticipant.get(sessionId).put(p.getParticipantPublicId(), p);
this.sessionidFinalUsers.get(sessionId).computeIfAbsent(finalUserId, k -> {
@ -412,6 +412,7 @@ public abstract class SessionManager {
Participant p = new Participant(null, participantPrivateId, ProtocolElements.RECORDER_PARTICIPANT_PUBLICID,
sessionId, session.getUniqueSessionId(), token, clientMetadata, null, null,
EndpointType.WEBRTC_ENDPOINT, null);
this.tokenRegister.registerToken(sessionId, p, token);
this.sessionidParticipantpublicidParticipant.get(sessionId)
.put(ProtocolElements.RECORDER_PARTICIPANT_PUBLICID, p);
return p;
@ -426,6 +427,7 @@ public abstract class SessionManager {
if (this.sessionidParticipantpublicidParticipant.get(sessionId) != null) {
Participant p = new Participant(ipcamId, ipcamId, ipcamId, sessionId, session.getUniqueSessionId(), token,
null, location, platform, EndpointType.PLAYER_ENDPOINT, null);
this.tokenRegister.registerToken(sessionId, p, token);
this.sessionidParticipantpublicidParticipant.get(sessionId).put(ipcamId, p);
return p;
} else {

View File

@ -5,26 +5,28 @@ import java.util.concurrent.ConcurrentHashMap;
/**
* This service class represents all the tokens currently registered by an active sessions
* Each time a token is created, it is registered by {@link SessionManager} using {@link #registerToken(String, Token)}
* Each time a token is created, it is registered by {@link SessionManager} using {@link #registerToken(String, Participant, Token)} (String, Participant, Token)}
* All registered tokens will be present until {@link SessionManager} calls the method {@link #deregisterTokens(String)}
*
* The purpose of this service is to know when a token was registered into a session by using
* public method {@link #isTokenRegistered(String)}
* The purpose of this service is to know when a token was registered into a session with its correspondent connectionId
* All maps of this class are present to be able to verify this in the most optimal way
*/
public class TokenRegister {
private ConcurrentHashMap<String, Token> tokensRegistered = new ConcurrentHashMap<>();
private ConcurrentHashMap<String, ConcurrentHashMap<String, Token>> tokensRegisteredBySession = new ConcurrentHashMap<>();
private final ConcurrentHashMap<String, Token> tokensRegistered = new ConcurrentHashMap<>();
private final ConcurrentHashMap<String, ConcurrentHashMap<String, Token>> tokensRegisteredBySession = new ConcurrentHashMap<>();
private final ConcurrentHashMap<String, Participant> participantsByTokens = new ConcurrentHashMap<>();
/**
* Register a token of an specific active session
* @param sessionId Id of the sessions where the token is generated
* @param token Token to register
*/
protected synchronized void registerToken(String sessionId, Token token) {
protected synchronized void registerToken(String sessionId, Participant participant, Token token) {
this.tokensRegisteredBySession.putIfAbsent(sessionId, new ConcurrentHashMap<>());
ConcurrentHashMap<String, Token> registeredTokensInSession = this.tokensRegisteredBySession.get(sessionId);
this.tokensRegistered.put(token.getToken(), token);
this.participantsByTokens.put(token.getToken(), participant);
registeredTokensInSession.put(token.getToken(), token);
}
@ -33,21 +35,46 @@ public class TokenRegister {
* @param sessionId Id of the session which is no longer active
*/
protected synchronized void deregisterTokens(String sessionId) {
if (tokensRegisteredBySession.containsKey(sessionId)) {
if (this.tokensRegisteredBySession.containsKey(sessionId)) {
for(Map.Entry<String, Token> tokenRegisteredInSession: tokensRegistered.entrySet()) {
tokensRegistered.remove(tokenRegisteredInSession.getKey());
this.tokensRegistered.remove(tokenRegisteredInSession.getKey());
this.participantsByTokens.remove(tokenRegisteredInSession.getKey());
}
tokensRegisteredBySession.remove(sessionId);
this.tokensRegisteredBySession.remove(sessionId);
}
}
/**
* Check if the current token string was registered in an active session
* @param token Token string to check if it is registered
* @param connectionId Id of the connection to check
* @param sessionId Id of session to check
* @return <code>true</code> if token was registered. <code>false</code> otherwise
*/
public boolean isTokenRegistered(String token) {
return this.tokensRegistered.containsKey(token);
public boolean isTokenRegistered(String token, String connectionId, String sessionId) {
if (!this.tokensRegistered.containsKey(token)) {
// False because token is not registered
return false;
}
if (!this.tokensRegisteredBySession.containsKey(sessionId)) {
// False because session is not registered with the specified token
return false;
}
if (!this.tokensRegisteredBySession.get(sessionId).containsKey(token)) {
// Token is not registered in the existing session
return false;
}
if (!this.participantsByTokens.containsKey(connectionId)) {
// Participant is not registered for specific token
return false;
}
// In this final state, if connectionId is equal to participant public id, the token is registered
return participantsByTokens.get(connectionId).getParticipantPublicId().equals(connectionId);
}
/**