1 |
332 |
ahitrov |
package tokbox::Keeper; |
2 |
|
|
|
3 |
|
|
use strict; |
4 |
|
|
use warnings 'all'; |
5 |
336 |
ahitrov |
use Time::HiRes qw( gettimeofday ); |
6 |
332 |
ahitrov |
use MIME::Base64; |
7 |
336 |
ahitrov |
use Digest::SHA qw(hmac_sha1 hmac_sha1_hex); |
8 |
332 |
ahitrov |
use LWP::UserAgent; |
9 |
|
|
use URI; |
10 |
|
|
use Data::Dumper; |
11 |
|
|
use XML::Fast; |
12 |
|
|
|
13 |
|
|
use base qw(Contenido::Keeper); |
14 |
|
|
|
15 |
|
|
use Contenido::Globals; |
16 |
|
|
|
17 |
336 |
ahitrov |
my $SUBSCRIBER = "subscriber"; |
18 |
|
|
my $PUBLISHER = "publisher"; |
19 |
|
|
my $MODERATOR = "moderator"; |
20 |
332 |
ahitrov |
|
21 |
|
|
sub OpenTokSession { |
22 |
|
|
my ($self, $sessionId, $properties) = @_; |
23 |
|
|
return tokbox::Session->new( sessionId => $sessionId, properties => $properties ); |
24 |
|
|
} |
25 |
|
|
|
26 |
|
|
|
27 |
|
|
### - Generate a token |
28 |
|
|
# |
29 |
|
|
# session_id - If session_id is not blank, this token can only join the call with the specified session_id. |
30 |
|
|
# role - One of the constants defined in RoleConstants. Default is publisher, look in the documentation to learn more about roles. |
31 |
|
|
# expire_time - Optional timestamp to change when the token expires. See documentation on token for details. |
32 |
|
|
# connection_data - Optional string data to pass into the stream. See documentation on token for details. |
33 |
|
|
# |
34 |
|
|
########################################################################################################## |
35 |
|
|
sub generateToken { |
36 |
|
|
my ($self, %opts) = @_; |
37 |
|
|
|
38 |
|
|
my $session_id = delete $opts{session_id} || ''; |
39 |
|
|
my $role = delete $opts{role} || ''; |
40 |
|
|
my $expire_time = delete $opts{expire_time}; |
41 |
|
|
my $connection_data = delete $opts{connection_data} || ''; |
42 |
|
|
|
43 |
|
|
my $create_time = time; |
44 |
336 |
ahitrov |
my $nonce = rand(); |
45 |
332 |
ahitrov |
|
46 |
|
|
if ( !$role ) { |
47 |
|
|
$role = $PUBLISHER; |
48 |
|
|
} elsif ( $role ne $SUBSCRIBER && $role ne $PUBLISHER && $role ne $MODERATOR ) { |
49 |
|
|
warn "unknown role $role\n"; |
50 |
|
|
return; |
51 |
|
|
} |
52 |
336 |
ahitrov |
my $data_string = "role=$role&session_id=$session_id&create_time=$create_time&nonce=$nonce"; |
53 |
332 |
ahitrov |
if ( defined $expire_time ) { |
54 |
|
|
if ( $expire_time =~ /\D/ ) { |
55 |
|
|
warn "Expire time must be a number\n"; |
56 |
|
|
return; |
57 |
|
|
} elsif ( $expire_time < $create_time ) { |
58 |
|
|
warn "Expire time must be in the future\n"; |
59 |
|
|
return; |
60 |
|
|
} elsif ( $expire_time > $create_time + 2592000 ) { |
61 |
|
|
warn "Expire time must be in the next 30 days\n"; |
62 |
|
|
return; |
63 |
|
|
} |
64 |
|
|
$data_string .= "&expire_time=$expire_time"; |
65 |
|
|
} |
66 |
|
|
if ( $connection_data ) { |
67 |
|
|
if ( length $connection_data > 1000 ) { |
68 |
|
|
warn "Connection data must be less than 1000 characters\n"; |
69 |
|
|
return; |
70 |
|
|
} |
71 |
336 |
ahitrov |
$data_string .= "&connection_data=" . Utils::HTML::url_escape( $connection_data ); |
72 |
|
|
} else { |
73 |
|
|
$data_string .= "&connection_data="; |
74 |
332 |
ahitrov |
} |
75 |
336 |
ahitrov |
|
76 |
332 |
ahitrov |
my $sig = $self->_sign_string($data_string, $self->state->{tokbox_secret}); |
77 |
|
|
my $api_key = $self->state->{tokbox_api_key}; |
78 |
|
|
|
79 |
336 |
ahitrov |
warn "AUTH: [partner_id=$api_key&sig=$sig:$data_string]\n" if $DEBUG; |
80 |
|
|
return "T1==" . MIME::Base64::encode_base64("partner_id=$api_key&sig=$sig:$data_string", ''); |
81 |
332 |
ahitrov |
} |
82 |
|
|
|
83 |
|
|
|
84 |
|
|
### |
85 |
|
|
# |
86 |
|
|
# Creates a new session. |
87 |
|
|
# location - IP address to geolocate the call around. |
88 |
|
|
# properties - Optional array, keys are defined in SessionPropertyConstants |
89 |
|
|
# |
90 |
|
|
################################################################################### |
91 |
|
|
sub createSession { |
92 |
|
|
my ($self, %opts) = @_; |
93 |
|
|
|
94 |
|
|
my $location = delete $opts{location} || ''; |
95 |
|
|
my $properties = delete $opts{properties} || {}; |
96 |
|
|
$properties->{"location"} = $location; |
97 |
|
|
$properties->{"api_key"} = $self->state->{tokbox_api_key}; |
98 |
|
|
|
99 |
|
|
my $createSessionResult = $self->_do_request("/session/create", $properties); |
100 |
|
|
return unless $createSessionResult; |
101 |
|
|
my $createSessionXML = xml2hash ($createSessionResult); |
102 |
|
|
unless ( ref $createSessionXML ) { |
103 |
|
|
warn "Failed to create session: Invalid response from server\n"; |
104 |
|
|
return; |
105 |
|
|
} |
106 |
|
|
|
107 |
|
|
unless( exists $createSessionXML->{sessions}{Session}{session_id} ) { |
108 |
|
|
warn "Failed to create session.\n"; |
109 |
|
|
warn Dumper $createSessionXML; |
110 |
|
|
return; |
111 |
|
|
} |
112 |
|
|
my $sessionId = $createSessionXML->{sessions}{Session}{session_id}; |
113 |
|
|
|
114 |
|
|
return $self->OpenTokSession( $sessionId ); |
115 |
|
|
} |
116 |
|
|
|
117 |
|
|
|
118 |
|
|
|
119 |
|
|
######################################################## |
120 |
|
|
# Inner functions |
121 |
|
|
######################################################## |
122 |
|
|
sub _sign_string { |
123 |
|
|
my ($self, $string, $secret) = @_; |
124 |
336 |
ahitrov |
return hmac_sha1_hex($string, $secret); |
125 |
332 |
ahitrov |
} |
126 |
|
|
|
127 |
|
|
sub _do_request { |
128 |
|
|
my ($self, $url, $data, $auth) = @_; |
129 |
|
|
|
130 |
|
|
$auth = {} unless ref $auth; |
131 |
|
|
$auth->{'type'} = 'partner' unless exists $auth->{type}; |
132 |
|
|
|
133 |
|
|
$url = $self->state->{tokbox_server} . $url; |
134 |
|
|
|
135 |
|
|
my %authHeader; |
136 |
|
|
if ( $auth->{type} eq 'token' ) { |
137 |
|
|
$authHeader{"X-TB-TOKEN-AUTH"} = $auth->{'token'}; |
138 |
|
|
} else { |
139 |
|
|
$authHeader{"X-TB-PARTNER-AUTH"} = $self->state->{tokbox_api_key}.":".$self->state->{tokbox_secret}; |
140 |
|
|
} |
141 |
|
|
|
142 |
|
|
my $req = URI->new( $url ); |
143 |
|
|
my $ua = LWP::UserAgent->new; |
144 |
|
|
$ua->timeout(3); |
145 |
|
|
$ua->default_header( 'Content-type' => 'application/x-www-form-urlencoded' ); |
146 |
|
|
$ua->default_header( %authHeader ); |
147 |
|
|
warn "Post: [$url] params:".Dumper($data) if $DEBUG; |
148 |
|
|
my $res = $ua->post( $req, $data ); |
149 |
|
|
unless ($res->code == 200) { |
150 |
|
|
warn "Request failed: ".$res->status_line; |
151 |
|
|
return undef; |
152 |
|
|
} else { |
153 |
|
|
warn "Responce: ".Dumper($res) if $DEBUG; |
154 |
|
|
} |
155 |
|
|
return $res->content; |
156 |
|
|
} |
157 |
|
|
|
158 |
|
|
|
159 |
|
|
1; |