From 6769075de2e69e49910a1f03b52cc919443bc50a Mon Sep 17 00:00:00 2001 From: pabloFuente Date: Mon, 3 Apr 2017 18:11:15 +0200 Subject: [PATCH] Fixes for previouscommit suggestions --- .../openvidu/client/OpenViduException.java | 4 +- .../server/core/NotificationRoomManager.java | 4 +- .../org/openvidu/server/core/RoomManager.java | 8 +-- .../openvidu/server/rest/RoomController.java | 9 ++-- .../server/rpc/JsonRpcUserControl.java | 4 +- ...icipantRoles.java => ParticipantRole.java} | 2 +- .../server/security/ParticipantSecurity.java | 51 ------------------- .../server/security/SecurityConfig.java | 2 +- 8 files changed, 18 insertions(+), 66 deletions(-) rename openvidu-server/src/main/java/org/openvidu/server/security/{ParticipantRoles.java => ParticipantRole.java} (71%) delete mode 100644 openvidu-server/src/main/java/org/openvidu/server/security/ParticipantSecurity.java diff --git a/openvidu-client/src/main/java/org/openvidu/client/OpenViduException.java b/openvidu-client/src/main/java/org/openvidu/client/OpenViduException.java index a8c320bb..d11e1a0c 100644 --- a/openvidu-client/src/main/java/org/openvidu/client/OpenViduException.java +++ b/openvidu-client/src/main/java/org/openvidu/client/OpenViduException.java @@ -36,7 +36,9 @@ public class OpenViduException extends RuntimeException { USER_NOT_STREAMING_ERROR_CODE(105), EXISTING_USER_IN_ROOM_ERROR_CODE( 104), USER_CLOSED_ERROR_CODE( - 103), USER_NOT_FOUND_ERROR_CODE(102), USER_GENERIC_ERROR_CODE(101); + 103), USER_NOT_FOUND_ERROR_CODE(102), USER_GENERIC_ERROR_CODE(101), + + USER_UNAUTHORIZED(401); private int value; diff --git a/openvidu-server/src/main/java/org/openvidu/server/core/NotificationRoomManager.java b/openvidu-server/src/main/java/org/openvidu/server/core/NotificationRoomManager.java index de52a5c1..3cb2da22 100644 --- a/openvidu-server/src/main/java/org/openvidu/server/core/NotificationRoomManager.java +++ b/openvidu-server/src/main/java/org/openvidu/server/core/NotificationRoomManager.java @@ -36,7 +36,7 @@ import org.openvidu.server.core.api.pojo.ParticipantRequest; import org.openvidu.server.core.api.pojo.UserParticipant; import org.openvidu.server.core.internal.DefaultKurentoClientSessionInfo; import org.openvidu.server.core.internal.DefaultNotificationRoomHandler; -import org.openvidu.server.security.ParticipantRoles; +import org.openvidu.server.security.ParticipantRole; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -454,7 +454,7 @@ public class NotificationRoomManager { return this.internalManager.newSessionId(); } - public String newToken(String sessionId, ParticipantRoles role){ + public String newToken(String sessionId, ParticipantRole role){ return this.internalManager.newToken(sessionId, role); } } diff --git a/openvidu-server/src/main/java/org/openvidu/server/core/RoomManager.java b/openvidu-server/src/main/java/org/openvidu/server/core/RoomManager.java index 4f6d92d3..649c7578 100644 --- a/openvidu-server/src/main/java/org/openvidu/server/core/RoomManager.java +++ b/openvidu-server/src/main/java/org/openvidu/server/core/RoomManager.java @@ -45,7 +45,7 @@ import org.openvidu.server.core.api.pojo.UserParticipant; import org.openvidu.server.core.endpoint.SdpType; import org.openvidu.server.core.internal.Participant; import org.openvidu.server.core.internal.Room; -import org.openvidu.server.security.ParticipantRoles; +import org.openvidu.server.security.ParticipantRole; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -67,7 +67,7 @@ public class RoomManager { private final ConcurrentMap rooms = new ConcurrentHashMap(); - private final ConcurrentMap> sessionIdTokenRole = new ConcurrentHashMap<>(); + private final ConcurrentMap> sessionIdTokenRole = new ConcurrentHashMap<>(); private volatile boolean closed = false; @@ -938,7 +938,7 @@ public class RoomManager { } public boolean isPublisherInRoom(String participantName, String roomName) { - return this.sessionIdTokenRole.get(roomName).get(participantName).equals(ParticipantRoles.PUBLISHER); + return this.sessionIdTokenRole.get(roomName).get(participantName).equals(ParticipantRole.PUBLISHER); } public String newSessionId(){ @@ -951,7 +951,7 @@ public class RoomManager { return sessionId; } - public String newToken(String sessionId, ParticipantRoles role){ + public String newToken(String sessionId, ParticipantRole role){ if (this.sessionIdTokenRole.get(sessionId) != null) { String token = new BigInteger(130, new SecureRandom()).toString(32); diff --git a/openvidu-server/src/main/java/org/openvidu/server/rest/RoomController.java b/openvidu-server/src/main/java/org/openvidu/server/rest/RoomController.java index 064bfa17..d182a6e2 100644 --- a/openvidu-server/src/main/java/org/openvidu/server/rest/RoomController.java +++ b/openvidu-server/src/main/java/org/openvidu/server/rest/RoomController.java @@ -22,12 +22,13 @@ import java.util.Map; import java.util.Set; import org.openvidu.server.core.NotificationRoomManager; -import org.openvidu.server.security.ParticipantRoles; +import org.openvidu.server.security.ParticipantRole; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; /** @@ -58,16 +59,16 @@ public class RoomController { return Integer.valueOf(getProperty("thresholdSpeaker", THRESHOLD_SPEAKER_DEFAULT)); } - @RequestMapping("/getSessionId") + @RequestMapping(value = "/getSessionId", method = RequestMethod.GET) public ResponseEntity getSessionId() { String sessionId = roomManager.newSessionId(); return new ResponseEntity(sessionId, HttpStatus.OK); } - @RequestMapping("/getToken") + @RequestMapping(value = "/newToken", method = RequestMethod.POST) public ResponseEntity getToken(@RequestBody Map sessionIdAndRole) { System.out.println("SESSIONID: " + sessionIdAndRole.get("0") + " - ROLE: " + sessionIdAndRole.get("1")); - String token = roomManager.newToken((String) sessionIdAndRole.get("0"), ParticipantRoles.valueOf((String) sessionIdAndRole.get("1"))); + String token = roomManager.newToken((String) sessionIdAndRole.get("0"), ParticipantRole.valueOf((String) sessionIdAndRole.get("1"))); return new ResponseEntity(token, HttpStatus.OK); } } diff --git a/openvidu-server/src/main/java/org/openvidu/server/rpc/JsonRpcUserControl.java b/openvidu-server/src/main/java/org/openvidu/server/rpc/JsonRpcUserControl.java index c6ddd359..ac9a325e 100644 --- a/openvidu-server/src/main/java/org/openvidu/server/rpc/JsonRpcUserControl.java +++ b/openvidu-server/src/main/java/org/openvidu/server/rpc/JsonRpcUserControl.java @@ -74,7 +74,7 @@ public class JsonRpcUserControl { } else { System.out.println("Error: sessionId or token not valid"); - throw new OpenViduException(Code.GENERIC_ERROR_CODE, + throw new OpenViduException(Code.USER_UNAUTHORIZED, "Unable to join room. The user does not have a valid token"); } } @@ -95,7 +95,7 @@ public class JsonRpcUserControl { } else { System.out.println("Error: user is not a publisher"); - throw new OpenViduException(Code.GENERIC_ERROR_CODE, + throw new OpenViduException(Code.USER_UNAUTHORIZED, "Unable to join room. The user does not have a valid token"); } } diff --git a/openvidu-server/src/main/java/org/openvidu/server/security/ParticipantRoles.java b/openvidu-server/src/main/java/org/openvidu/server/security/ParticipantRole.java similarity index 71% rename from openvidu-server/src/main/java/org/openvidu/server/security/ParticipantRoles.java rename to openvidu-server/src/main/java/org/openvidu/server/security/ParticipantRole.java index 6be2f2bc..af462760 100644 --- a/openvidu-server/src/main/java/org/openvidu/server/security/ParticipantRoles.java +++ b/openvidu-server/src/main/java/org/openvidu/server/security/ParticipantRole.java @@ -1,6 +1,6 @@ package org.openvidu.server.security; -public enum ParticipantRoles { +public enum ParticipantRole { SUBSCRIBER, PUBLISHER, MODERATOR; diff --git a/openvidu-server/src/main/java/org/openvidu/server/security/ParticipantSecurity.java b/openvidu-server/src/main/java/org/openvidu/server/security/ParticipantSecurity.java deleted file mode 100644 index c4be89e3..00000000 --- a/openvidu-server/src/main/java/org/openvidu/server/security/ParticipantSecurity.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * (C) Copyright 2014 Kurento (http://kurento.org/) - * - * 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 org.openvidu.server.security; - -import org.kurento.client.MediaPipeline; -import org.openvidu.client.OpenViduException; -import org.openvidu.client.OpenViduException.Code; -import org.openvidu.server.core.internal.Participant; -import org.openvidu.server.core.internal.Room; - -public class ParticipantSecurity extends Participant{ - - ParticipantRoles role; - - public ParticipantSecurity(String id, String name, String role, Room room, MediaPipeline pipeline, boolean dataChannels, - boolean web) { - super(id, name, room, pipeline, dataChannels, web); - - this.role = ParticipantRoles.valueOf(role); - } - - @Override - public void createPublishingEndpoint() { - if (this.isPublisher()){ - super.createPublishingEndpoint(); - } else { - throw new OpenViduException(Code.GENERIC_ERROR_CODE, - "Unable to create publisher endpoint"); - } - } - - private boolean isPublisher(){ - return (this.role.equals(ParticipantRoles.PUBLISHER) || - this.role.equals(ParticipantRoles.MODERATOR)); - } - -} diff --git a/openvidu-server/src/main/java/org/openvidu/server/security/SecurityConfig.java b/openvidu-server/src/main/java/org/openvidu/server/security/SecurityConfig.java index 20b478af..5ed5a85f 100644 --- a/openvidu-server/src/main/java/org/openvidu/server/security/SecurityConfig.java +++ b/openvidu-server/src/main/java/org/openvidu/server/security/SecurityConfig.java @@ -35,7 +35,7 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter { protected void configureUrlAuthorization(HttpSecurity http) throws Exception { http.csrf().disable() .authorizeRequests() - .antMatchers(HttpMethod.POST, "/getSessionId").authenticated() + .antMatchers(HttpMethod.GET, "/getSessionId").authenticated() .antMatchers(HttpMethod.POST, "/getToken").authenticated() .and().sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS); }