1
|
|
|
require 'net/http' |
2
|
|
|
|
3
|
|
|
class ExternalApplicationsController < ApplicationController |
4
|
|
|
|
5
|
|
|
skip_before_action :verify_authenticity_token, only: [:start, :http_options] |
6
|
|
|
before_action :token_to_omniauth_user, only: [:start] |
7
|
|
|
before_action :set_user, only: [:start] |
8
|
|
|
before_action :set_server_url, only: [:start] |
9
|
|
|
|
10
|
|
|
# POST /external_applications/start |
11
|
|
|
def start |
12
|
|
|
add_allow_credentials_headers |
13
|
|
|
respond_to do |format| |
14
|
|
|
format.json { |
15
|
|
|
render json: {server_url: @server_url, code: @oauth_response_code} |
16
|
|
|
} |
17
|
|
|
end |
18
|
|
|
end |
19
|
|
|
|
20
|
|
|
# OPTIONS /external_applications/start |
21
|
|
|
def http_options |
22
|
|
|
add_allow_credentials_headers |
23
|
|
|
head :ok |
24
|
|
|
end |
25
|
|
|
|
26
|
|
|
# GET /external_applications/auto_close |
27
|
|
|
def auto_close |
28
|
|
|
end |
29
|
|
|
|
30
|
|
|
private |
31
|
|
|
def add_allow_credentials_headers |
32
|
|
|
# TODO: allow only headers stored internally |
33
|
|
|
response.headers['Access-Control-Allow-Origin'] = request.headers['Origin'] || '*' |
34
|
|
|
response.headers['Access-Control-Allow-Credentials'] = 'true' |
35
|
|
|
response.headers['Access-Control-Allow-Headers'] = 'accept, content-type' |
36
|
|
|
end |
37
|
|
|
|
38
|
|
|
def token_to_omniauth_user |
39
|
|
|
token = params[:oauth_token] |
40
|
|
|
if token.present? |
41
|
|
|
uri = URI.parse("https://www.googleapis.com/oauth2/v1/userinfo?alt=json&access_token=" + token) |
42
|
|
|
http = Net::HTTP.new(uri.host, uri.port) |
43
|
|
|
http.use_ssl = true |
44
|
|
|
|
45
|
|
|
request = Net::HTTP::Get.new(uri.request_uri) |
46
|
|
|
request.add_field 'Authorization', 'Bearer ' + token |
47
|
|
|
request['Accept'] = 'application/json' |
48
|
|
|
request.body = nil |
49
|
|
|
|
50
|
|
|
response = http.request(request) |
51
|
|
|
|
52
|
|
|
@oauth_response_code = response.code.to_i |
53
|
|
|
@omniauth_user = JSON.parse(response.body) |
54
|
|
|
else |
55
|
|
|
@oauth_response_code = 500 |
56
|
|
|
end |
57
|
|
|
end |
58
|
|
|
|
59
|
|
|
def set_user |
60
|
|
|
if @oauth_response_code === 200 |
61
|
|
|
@auth = { |
62
|
|
|
'provider' => 'google', |
63
|
|
|
'uid' => @omniauth_user['id'], |
64
|
|
|
'info' => @omniauth_user, |
65
|
|
|
} |
66
|
|
|
@auth['info']['image'] = @omniauth_user['picture'] |
67
|
|
|
@user = User.from_omniauth(@auth) |
68
|
|
|
end |
69
|
|
|
end |
70
|
|
|
|
71
|
|
|
def set_server_url |
72
|
|
|
if @oauth_response_code === 200 |
73
|
|
|
@room = @user.main_room |
74
|
|
|
@server_url = @room.join_path(@user.name, room_opts, @user.uid) |
75
|
|
|
elsif @oauth_response_code === 500 |
76
|
|
|
@server_url = internal_error_url |
77
|
|
|
else |
78
|
|
|
@server_url = unauthorized_url |
79
|
|
|
end |
80
|
|
|
end |
81
|
|
|
|
82
|
|
|
def room_opts |
83
|
|
|
# Join the user in and start the meeting. |
84
|
|
|
opts = default_meeting_options |
85
|
|
|
opts[:user_is_moderator] = true |
86
|
|
|
opts[:meeting_logout_url] = auto_close_url; |
87
|
|
|
|
88
|
|
|
# Include the user's choices for the room settings |
89
|
|
|
room_settings = JSON.parse(@room[:room_settings]) |
90
|
|
|
opts[:mute_on_start] = room_settings["muteOnStart"] if room_settings["muteOnStart"] |
91
|
|
|
opts[:require_moderator_approval] = room_settings["requireModeratorApproval"] |
92
|
|
|
opts |
93
|
|
|
end |
94
|
|
|
end |