From 24d11f259d60aa1e26ca9eacac51785675ae55ad Mon Sep 17 00:00:00 2001 From: pabloFuente Date: Tue, 17 Jul 2018 13:48:58 +0200 Subject: [PATCH] openvidu-server: dashboard asks for token --- .../dashboard/dashboard.component.ts | 38 +++++-------- .../frontend/src/app/services/rest.service.ts | 54 ++++++++++++++++++- 2 files changed, 65 insertions(+), 27 deletions(-) diff --git a/openvidu-server/src/angular/frontend/src/app/components/dashboard/dashboard.component.ts b/openvidu-server/src/angular/frontend/src/app/components/dashboard/dashboard.component.ts index 788e5781..0e1788fb 100644 --- a/openvidu-server/src/angular/frontend/src/app/components/dashboard/dashboard.component.ts +++ b/openvidu-server/src/angular/frontend/src/app/components/dashboard/dashboard.component.ts @@ -107,18 +107,18 @@ export class DashboardComponent implements OnInit, OnDestroy { dialogRef.afterClosed().subscribe(secret => { if (secret) { - if (!this.openviduPublicUrl) { - this.restService.getOpenViduPublicUrl() - .then((url => { - this.openviduPublicUrl = url.replace('https://', 'wss://').replace('http://', 'ws://'); - this.connectToSession(this.openviduPublicUrl + '?sessionId=testSession&secret=' + secret); - })) - .catch(error => { + this.restService.getOpenViduToken(secret) + .then((token => { + this.connectToSession(token); + })) + .catch(error => { + if (error === 401) { // User unauthorized error. OpenVidu security is active + this.testVideo(); + } else { console.error(error); - }); - } else { - this.connectToSession(this.openviduPublicUrl + '?sessionId=testSession&secret=' + secret); - } + this.msgChain.push('Error connecting to session: ' + error); + } + }); } }); } @@ -181,21 +181,7 @@ export class DashboardComponent implements OnInit, OnDestroy { }) .catch(error => { - if (error.code === 401) { // User unauthorized error. OpenVidu security is active - this.endTestVideo(); - let dialogRef: MatDialogRef; - dialogRef = this.dialog.open(CredentialsDialogComponent); - dialogRef.componentInstance.myReference = dialogRef; - - dialogRef.afterClosed().subscribe(secret => { - if (secret) { - this.connectToSession(this.openviduPublicUrl + '?sessionId=testSession&secret=' + secret); - } - }); - } else { - console.error(error); - this.msgChain.push('Error connecting to session'); - } + this.msgChain.push('Error connecting to session: ' + error); }); } diff --git a/openvidu-server/src/angular/frontend/src/app/services/rest.service.ts b/openvidu-server/src/angular/frontend/src/app/services/rest.service.ts index c4e54e1b..d4f05b94 100644 --- a/openvidu-server/src/angular/frontend/src/app/services/rest.service.ts +++ b/openvidu-server/src/angular/frontend/src/app/services/rest.service.ts @@ -1,5 +1,4 @@ import { Injectable } from '@angular/core'; -import { Subject } from 'rxjs'; @Injectable() export class RestService { @@ -31,4 +30,57 @@ export class RestService { }); } + getOpenViduToken(secret: string): Promise { + if (!this.openviduPublicUrl) { + this.getOpenViduPublicUrl().then(() => { + return this.getOpenViduToken(secret); + }); + } else { + return new Promise((resolve, reject) => { + const url1 = 'https://OPENVIDUAPP:' + secret + '@' + this.openviduPublicUrl.split('://')[1] + 'api/sessions'; + const http1 = new XMLHttpRequest(); + const data1 = {}; + data1['mediaMode'] = 'ROUTED'; + data1['recordingMode'] = 'MANUAL'; + data1['RECORDING_LAYOUT'] = 'BEST_FIT'; + const json1 = JSON.stringify(data1); + + http1.onreadystatechange = () => { + if (http1.status === 401) { + reject(401); + } else if (http1.readyState === 4) { + if (http1.status === 200) { + const sessionId = JSON.parse(http1.responseText).id; + + const url2 = this.openviduPublicUrl + 'api/tokens'; + const http2 = new XMLHttpRequest(); + const data2 = {}; + data2['session'] = sessionId; + const json2 = JSON.stringify(data2); + + http2.onreadystatechange = () => { + if (http2.readyState === 4) { + if (http2.status === 200) { + resolve(JSON.parse(http2.responseText).id); + } else { + reject(http2.status); + } + }; + } + http2.open('POST', url2, true); + http2.setRequestHeader('Content-type', 'application/json'); + http2.setRequestHeader('Authorization', 'Basic ' + btoa('OPENVIDUAPP:' + secret)); + http2.send(json2); + } else { + reject(http1.status); + } + }; + } + http1.open('POST', url1, true); + http1.setRequestHeader('Content-type', 'application/json'); + http1.send(json1); + }); + } + } + }