openvidu-server: TokenGenerator abstraction

pull/255/head
pabloFuente 2019-03-01 18:30:46 +01:00
parent a2a7828c9d
commit 91ffb266c4
8 changed files with 136 additions and 26 deletions

View File

@ -53,6 +53,8 @@ import io.openvidu.server.config.HttpHandshakeInterceptor;
import io.openvidu.server.config.OpenviduConfig; import io.openvidu.server.config.OpenviduConfig;
import io.openvidu.server.core.SessionEventsHandler; import io.openvidu.server.core.SessionEventsHandler;
import io.openvidu.server.core.SessionManager; import io.openvidu.server.core.SessionManager;
import io.openvidu.server.core.TokenGenerator;
import io.openvidu.server.core.TokenGeneratorDefault;
import io.openvidu.server.coturn.CoturnCredentialsService; import io.openvidu.server.coturn.CoturnCredentialsService;
import io.openvidu.server.coturn.CoturnCredentialsServiceFactory; import io.openvidu.server.coturn.CoturnCredentialsServiceFactory;
import io.openvidu.server.kurento.AutodiscoveryKurentoClientProvider; import io.openvidu.server.kurento.AutodiscoveryKurentoClientProvider;
@ -142,6 +144,12 @@ public class OpenViduServer implements JsonRpcConfigurer {
return new CallDetailRecord(Arrays.asList(new CDRLoggerFile())); return new CallDetailRecord(Arrays.asList(new CDRLoggerFile()));
} }
@Bean
@ConditionalOnMissingBean
public TokenGenerator tokenGenerator() {
return new TokenGeneratorDefault();
}
@Bean @Bean
@ConditionalOnMissingBean @ConditionalOnMissingBean
public OpenviduConfig openviduConfig() { public OpenviduConfig openviduConfig() {

View File

@ -28,7 +28,6 @@ import java.util.stream.Collectors;
import javax.annotation.PreDestroy; import javax.annotation.PreDestroy;
import org.apache.commons.lang3.RandomStringUtils;
import org.kurento.jsonrpc.message.Request; import org.kurento.jsonrpc.message.Request;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -42,15 +41,14 @@ import io.openvidu.client.OpenViduException.Code;
import io.openvidu.client.internal.ProtocolElements; import io.openvidu.client.internal.ProtocolElements;
import io.openvidu.java.client.OpenViduRole; import io.openvidu.java.client.OpenViduRole;
import io.openvidu.java.client.SessionProperties; import io.openvidu.java.client.SessionProperties;
import io.openvidu.server.OpenViduServer;
import io.openvidu.server.cdr.CDREventRecording; import io.openvidu.server.cdr.CDREventRecording;
import io.openvidu.server.cdr.CallDetailRecord; import io.openvidu.server.cdr.CallDetailRecord;
import io.openvidu.server.config.OpenviduConfig; import io.openvidu.server.config.OpenviduConfig;
import io.openvidu.server.coturn.CoturnCredentialsService; import io.openvidu.server.coturn.CoturnCredentialsService;
import io.openvidu.server.coturn.TurnCredentials;
import io.openvidu.server.kurento.core.KurentoTokenOptions; import io.openvidu.server.kurento.core.KurentoTokenOptions;
import io.openvidu.server.recording.service.RecordingManager; import io.openvidu.server.recording.service.RecordingManager;
import io.openvidu.server.utils.FormatChecker; import io.openvidu.server.utils.FormatChecker;
import io.openvidu.server.utils.RandomStringGenerator;
public abstract class SessionManager { public abstract class SessionManager {
@ -71,6 +69,9 @@ public abstract class SessionManager {
@Autowired @Autowired
protected CoturnCredentialsService coturnCredentialsService; protected CoturnCredentialsService coturnCredentialsService;
@Autowired
protected TokenGenerator tokenGenerator;
public FormatChecker formatChecker = new FormatChecker(); public FormatChecker formatChecker = new FormatChecker();
protected ConcurrentMap<String, Session> sessions = new ConcurrentHashMap<>(); protected ConcurrentMap<String, Session> sessions = new ConcurrentHashMap<>();
@ -263,23 +264,11 @@ public abstract class SessionManager {
throw new OpenViduException(Code.GENERIC_ERROR_CODE, "Data invalid format"); throw new OpenViduException(Code.GENERIC_ERROR_CODE, "Data invalid format");
} }
String token = OpenViduServer.wsUrl; Token token = tokenGenerator.generateToken(sessionId, role, serverMetadata, kurentoTokenOptions);
token += "?sessionId=" + sessionId;
token += "&token=" + this.generateRandomChain();
token += "&role=" + role.name();
TurnCredentials turnCredentials = null;
if (this.coturnCredentialsService.isCoturnAvailable()) {
turnCredentials = coturnCredentialsService.createUser();
if (turnCredentials != null) {
token += "&turnUsername=" + turnCredentials.getUsername();
token += "&turnCredential=" + turnCredentials.getCredential();
}
}
Token t = new Token(token, role, serverMetadata, turnCredentials, kurentoTokenOptions);
map.putIfAbsent(token, t); map.putIfAbsent(token.getToken(), token);
showTokens(); showTokens();
return token; return token.getToken();
} else { } else {
this.sessionidTokenTokenobj.remove(sessionId); this.sessionidTokenTokenobj.remove(sessionId);
@ -352,12 +341,12 @@ public abstract class SessionManager {
public Participant newParticipant(String sessionId, String participantPrivatetId, Token token, public Participant newParticipant(String sessionId, String participantPrivatetId, Token token,
String clientMetadata, String location, String platform, String finalUserId) { String clientMetadata, String location, String platform, String finalUserId) {
if (this.sessionidParticipantpublicidParticipant.get(sessionId) != null) { if (this.sessionidParticipantpublicidParticipant.get(sessionId) != null) {
String participantPublicId = this.generateRandomChain(); String participantPublicId = RandomStringGenerator.generateRandomChain();
Participant p = new Participant(finalUserId, participantPrivatetId, participantPublicId, token, Participant p = new Participant(finalUserId, participantPrivatetId, participantPublicId, token,
clientMetadata, location, platform, null); clientMetadata, location, platform, null);
while (this.sessionidParticipantpublicidParticipant.get(sessionId).putIfAbsent(participantPublicId, while (this.sessionidParticipantpublicidParticipant.get(sessionId).putIfAbsent(participantPublicId,
p) != null) { p) != null) {
participantPublicId = this.generateRandomChain(); participantPublicId = RandomStringGenerator.generateRandomChain();
p.setParticipantPublicId(participantPublicId); p.setParticipantPublicId(participantPublicId);
} }
@ -410,10 +399,6 @@ public abstract class SessionManager {
log.info("<SESSIONID, TOKENS>: {}", this.sessionidTokenTokenobj.toString()); log.info("<SESSIONID, TOKENS>: {}", this.sessionidTokenTokenobj.toString());
} }
public String generateRandomChain() {
return RandomStringUtils.randomAlphanumeric(16).toLowerCase();
}
/** /**
* Closes all resources. This method has been annotated with the @PreDestroy * Closes all resources. This method has been annotated with the @PreDestroy
* directive (javax.annotation package) so that it will be automatically called * directive (javax.annotation package) so that it will be automatically called

View File

@ -47,6 +47,10 @@ public class Token {
return token; return token;
} }
public void setToken(String token) {
this.token = token;
}
public OpenViduRole getRole() { public OpenViduRole getRole() {
return role; return role;
} }

View File

@ -0,0 +1,28 @@
/*
* (C) Copyright 2017-2019 OpenVidu (https://openvidu.io/)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package io.openvidu.server.core;
import io.openvidu.java.client.OpenViduRole;
import io.openvidu.server.kurento.core.KurentoTokenOptions;
public interface TokenGenerator {
public Token generateToken(String sessionId, OpenViduRole role, String serverMetadata,
KurentoTokenOptions kurentoTokenOptions);
}

View File

@ -0,0 +1,51 @@
/*
* (C) Copyright 2017-2019 OpenVidu (https://openvidu.io/)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package io.openvidu.server.core;
import org.springframework.beans.factory.annotation.Autowired;
import io.openvidu.java.client.OpenViduRole;
import io.openvidu.server.OpenViduServer;
import io.openvidu.server.coturn.CoturnCredentialsService;
import io.openvidu.server.coturn.TurnCredentials;
import io.openvidu.server.kurento.core.KurentoTokenOptions;
import io.openvidu.server.utils.RandomStringGenerator;
public class TokenGeneratorDefault implements TokenGenerator {
@Autowired
private CoturnCredentialsService coturnCredentialsService;
@Override
public Token generateToken(String sessionId, OpenViduRole role, String serverMetadata,
KurentoTokenOptions kurentoTokenOptions) {
String token = OpenViduServer.wsUrl;
token += "?sessionId=" + sessionId;
token += "&token=" + RandomStringGenerator.generateRandomChain();
token += "&role=" + role.name();
TurnCredentials turnCredentials = null;
if (this.coturnCredentialsService.isCoturnAvailable()) {
turnCredentials = coturnCredentialsService.createUser();
if (turnCredentials != null) {
token += "&turnUsername=" + turnCredentials.getUsername();
token += "&turnCredential=" + turnCredentials.getCredential();
}
}
return new Token(token, role, serverMetadata, turnCredentials, kurentoTokenOptions);
}
}

View File

@ -56,6 +56,7 @@ import io.openvidu.server.core.SessionManager;
import io.openvidu.server.kurento.core.KurentoTokenOptions; import io.openvidu.server.kurento.core.KurentoTokenOptions;
import io.openvidu.server.recording.Recording; import io.openvidu.server.recording.Recording;
import io.openvidu.server.recording.service.RecordingManager; import io.openvidu.server.recording.service.RecordingManager;
import io.openvidu.server.utils.RandomStringGenerator;
/** /**
* *
@ -153,7 +154,7 @@ public class SessionRestController {
} }
sessionId = customSessionId; sessionId = customSessionId;
} else { } else {
sessionId = sessionManager.generateRandomChain(); sessionId = RandomStringGenerator.generateRandomChain();
sessionManager.sessionidTokenTokenobj.putIfAbsent(sessionId, new ConcurrentHashMap<>()); sessionManager.sessionidTokenTokenobj.putIfAbsent(sessionId, new ConcurrentHashMap<>());
} }

View File

@ -50,6 +50,7 @@ import io.openvidu.server.core.Participant;
import io.openvidu.server.core.SessionManager; import io.openvidu.server.core.SessionManager;
import io.openvidu.server.core.Token; import io.openvidu.server.core.Token;
import io.openvidu.server.utils.GeoLocationByIp; import io.openvidu.server.utils.GeoLocationByIp;
import io.openvidu.server.utils.RandomStringGenerator;
public class RpcHandler extends DefaultJsonRpcHandler<JsonObject> { public class RpcHandler extends DefaultJsonRpcHandler<JsonObject> {
@ -228,7 +229,7 @@ public class RpcHandler extends DefaultJsonRpcHandler<JsonObject> {
if (openviduConfig.isOpenViduSecret(secret)) { if (openviduConfig.isOpenViduSecret(secret)) {
sessionManager.newInsecureParticipant(participantPrivatetId); sessionManager.newInsecureParticipant(participantPrivatetId);
token = sessionManager.generateRandomChain(); token = RandomStringGenerator.generateRandomChain();
if (recorder) { if (recorder) {
generateRecorderParticipant = true; generateRecorderParticipant = true;
} }

View File

@ -0,0 +1,32 @@
/*
* (C) Copyright 2017-2019 OpenVidu (https://openvidu.io/)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package io.openvidu.server.utils;
import org.apache.commons.lang3.RandomStringUtils;
public class RandomStringGenerator {
public static String generateRandomChain() {
return RandomStringUtils.randomAlphanumeric(16).toLowerCase();
}
public static String generateRandomChain(int length) {
return RandomStringUtils.randomAlphanumeric(length).toLowerCase();
}
}