openvidu-call: Catched 401 error when session initialized

This commit is contained in:
csantosm 2022-08-25 11:13:28 +02:00
parent 5833b30efb
commit 6c3d101eff

View File

@ -32,92 +32,96 @@ import io.openvidu.java.client.Session;
@RestController
public class SessionController {
@Value("${RECORDING}")
private String RECORDING;
@Autowired
private OpenViduService openviduService;
@PostMapping("/sessions")
public ResponseEntity<Map<String, Object>> createConnection(@RequestBody(required = false) Map<String, Object> params,
public ResponseEntity<Map<String, Object>> createConnection(
@RequestBody(required = false) Map<String, Object> params,
@CookieValue(name = OpenViduService.RECORDING_TOKEN_NAME, defaultValue = "") String recordingTokenCookie,
HttpServletResponse res) {
Map<String, Object> response = new HashMap<String, Object>();
try {
long date = -1;
String nickname = "";
String sessionId = params.get("sessionId").toString();
if(params.containsKey("nickname")) {
nickname = params.get("nickname").toString();
if (params.containsKey("nickname")) {
nickname = params.get("nickname").toString();
}
Session sessionCreated = this.openviduService.createSession(sessionId);
Session sessionCreated = this.openviduService.createSession(sessionId);
boolean IS_RECORDING_ENABLED = RECORDING.toUpperCase().equals("ENABLED");
boolean hasValidToken = this.openviduService.isValidToken(sessionId, recordingTokenCookie);
boolean isSessionCreator = hasValidToken || sessionCreated.getActiveConnections().size() == 0;
OpenViduRole role = isSessionCreator && IS_RECORDING_ENABLED ? OpenViduRole.MODERATOR : OpenViduRole.PUBLISHER;
OpenViduRole role = isSessionCreator && IS_RECORDING_ENABLED ? OpenViduRole.MODERATOR
: OpenViduRole.PUBLISHER;
response.put("recordingEnabled", IS_RECORDING_ENABLED);
response.put("recordings", new ArrayList<Recording>());
Connection cameraConnection = this.openviduService.createConnection(sessionCreated, nickname, role);
Connection screenConnection = this.openviduService.createConnection(sessionCreated, nickname, role);
response.put("cameraToken", cameraConnection.getToken());
response.put("screenToken", screenConnection.getToken());
if (IS_RECORDING_ENABLED && isSessionCreator && !hasValidToken) {
/**
* ! *********** WARN *********** !
*
* To identify who is able to manage session recording, the code sends a cookie with a token to the session creator.
* The relation between cookies and sessions are stored in backend memory.
* To identify who is able to manage session recording, the code sends a cookie
* with a token to the session creator. The relation between cookies and
* sessions are stored in backend memory.
*
* This authentication & authorization system is pretty basic and it is not for production.
* We highly recommend IMPLEMENT YOUR OWN USER MANAGEMENT with persistence for a properly and secure recording feature.
* This authentication & authorization system is pretty basic and it is not for
* production. We highly recommend IMPLEMENT YOUR OWN USER MANAGEMENT with
* persistence for a properly and secure recording feature.
*
* ! *********** WARN *********** !
**/
String uuid = UUID.randomUUID().toString();
date = System.currentTimeMillis();
String recordingToken = cameraConnection.getToken() + "&" + OpenViduService.RECORDING_TOKEN_NAME + "=" + uuid + "&createdAt=" +date;
String recordingToken = cameraConnection.getToken() + "&" + OpenViduService.RECORDING_TOKEN_NAME + "="
+ uuid + "&createdAt=" + date;
Cookie cookie = new Cookie(OpenViduService.RECORDING_TOKEN_NAME, recordingToken);
res.addCookie(cookie);
RecordingData recData = new RecordingData(recordingToken, "");
this.openviduService.recordingMap.put(sessionId, recData);
}
if(IS_RECORDING_ENABLED){
if(date == -1) {
if (IS_RECORDING_ENABLED) {
if (date == -1) {
date = openviduService.getDateFromCookie(recordingTokenCookie);
}
List<Recording> recordings = openviduService.listRecordingsBySessionIdAndDate(sessionId, date);
response.put("recordings", recordings);
}
return new ResponseEntity<>(response, HttpStatus.OK);
} catch (OpenViduJavaClientException | OpenViduHttpException e) {
if(Integer.parseInt(e.getMessage()) == 501) {
if (Integer.parseInt(e.getMessage()) == 501) {
System.err.println("OpenVidu Server recording module is disabled");
response.put("recordingEnabled", false);
return new ResponseEntity<>(response, HttpStatus.OK);
} else if (Integer.parseInt(e.getMessage()) == 401) {
System.err.println("OpenVidu credentials are wrong.");
return new ResponseEntity<>(null, HttpStatus.UNAUTHORIZED);
} else {
e.printStackTrace();
System.out.println(e.getMessage());
System.err.println(e.getMessage());
return new ResponseEntity<>(response, HttpStatus.INTERNAL_SERVER_ERROR);
}
}