add lb option

This commit is contained in:
Josh
2018-05-11 15:57:31 -04:00
parent b452932767
commit 0f8a4734b2
13 changed files with 183 additions and 41 deletions

View File

@ -20,10 +20,21 @@ class ApplicationController < ActionController::Base
# Determines if the BigBlueButton endpoint is configured (or set to default).
def bigbluebutton_endpoint_default?
return false if loadbalanced_configuration?
Rails.configuration.bigbluebutton_endpoint_default == Rails.configuration.bigbluebutton_endpoint
end
helper_method :bigbluebutton_endpoint_default?
def loadbalanced_configuration?
Rails.configuration.loadbalanced_configuration
end
helper_method :loadbalanced_configuration?
def allow_greenlight_users?
Rails.configuration.greenlight_accounts
end
helper_method :allow_greenlight_users?
private
# Ensure the user is logged into the room they are accessing.

View File

@ -6,8 +6,8 @@ class SessionsController < ApplicationController
# GET /logout
def destroy
logout if current_user
head :no_content
logout
redirect_to root_path
end
# POST /login
@ -22,6 +22,18 @@ class SessionsController < ApplicationController
end
end
# POST /launch
def launch
# This will recieve a encoded POST from a launcher that
# contains the provider, and all user information. The
# launcher is what does the authentication, so we know
# that the user is who they say they are. We just need
# to use our secret to decode it and then log them in
# to GreenLight (or sign them up).
# User.from_launch()
end
# GET/POST /auth/:provider/callback
def omniauth
user = User.from_omniauth(request.env['omniauth.auth'])

View File

@ -8,7 +8,6 @@ module SessionsHelper
# Logs current user out of GreenLight.
def logout
session.delete(:user_id) if current_user
redirect_to root_path
end
# Retrieves the current user.

View File

@ -6,6 +6,8 @@ class Meeting < ApplicationRecord
belongs_to :room
RETURNCODE_SUCCESS = "SUCCESS"
# Creates a meeting on the BigBlueButton server.
def create(options = {})
create_options = {
@ -86,9 +88,59 @@ class Meeting < ApplicationRecord
Rails.configuration.bigbluebutton_secret
end
# Use one common instance of the BigBlueButton API for all meetings.
# Sets a BigBlueButtonApi object for interacting with the API.
def bbb
@@bbb ||= BigBlueButton::BigBlueButtonApi.new(bbb_endpoint + "api", bbb_secret, "0.8")
@bbb ||= if Rails.configuration.loadbalanced_configuration
lb_user = retrieve_loadbalanced_credentials(self.room.user.provider)
BigBlueButton::BigBlueButtonApi.new(remove_slash(lb_user["apiURL"]), lb_user["secret"], "0.8")
else
BigBlueButton::BigBlueButtonApi.new(remove_slash(bbb_endpoint), bbb_secret, "0.8")
end
end
# Rereives the loadbalanced BigBlueButton credentials for a user.
def retrieve_loadbalanced_credentials(provider)
# Include Omniauth accounts under the Greenlight provider.
provider = "greenlight" if Rails.configuration.providers.include?(provider.to_sym)
# Build the URI.
uri = encode_bbb_url(
Rails.configuration.loadbalancer_endpoint,
Rails.configuration.loadbalancer_secret,
{name: provider}
)
# Make the request.
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = (uri.scheme == 'https')
response = http.get(uri.request_uri)
unless response.kind_of?(Net::HTTPSuccess)
raise "Error retrieving provider credentials: #{response.code} #{response.message}"
end
# Parse XML.
doc = XmlSimple.xml_in(response.body, 'ForceArray' => false)
# Return the user credentials if the request succeeded on the loadbalancer.
return doc['user'] if doc['returncode'] == RETURNCODE_SUCCESS
raise "User with provider #{provider} does not exist." if doc['messageKey'] == "noSuchUser"
raise "API call #{url} failed with #{doc['messageKey']}."
end
# Builds a request to retrieve credentials from the load balancer.
def encode_bbb_url(base_url, secret, params)
encoded_params = OAuth::Helper.normalize(params)
string = "getUser" + encoded_params + secret
checksum = OpenSSL::Digest.digest('sha1', string).unpack("H*").first
URI.parse("#{base_url}?#{encoded_params}&checksum=#{checksum}")
end
# Removes trailing forward slash from BigBlueButton URL.
def remove_slash(s)
s.nil? ? nil : s.chomp("/")
end
# Generates a BigBlueButton meeting id from a meeting token.

View File

@ -1,14 +1,14 @@
class User < ApplicationRecord
after_create :initialize_room
before_save { email.downcase! }
before_save { email.downcase! unless email.nil? }
has_one :room
validates :name, length: { maximum: 24 }, presence: true
validates :username, presence: true
validates :provider, presence: true
validates :email, length: { maximum: 60 }, presence: true,
validates :email, length: { maximum: 60 }, allow_nil: true,
uniqueness: { case_sensitive: false },
format: {with: /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i }
@ -31,6 +31,11 @@ class User < ApplicationRecord
user
end
# Generates a user from a trusted launcher.
def from_launch(auth)
end
private
# Provider attributes.
@ -57,7 +62,6 @@ class User < ApplicationRecord
def google_email(auth)
auth['info']['email']
end
end
private

View File

@ -24,20 +24,22 @@
<% end %>
<% end %>
<center><p>or...<br><br></p></center>
<% if allow_greenlight_users? %>
<center><p>or...<br><br></p></center>
<%= form_for(:session, url: login_path) do |f| %>
<div class="input-field col s12">
<%= f.label :email, "Email Address" %>
<%= f.text_field :email %>
</div>
<div class="input-field col s12">
<%= f.label :password %>
<%= f.password_field :password %>
</div>
<br>
<%= f.submit "Login", class: "btn white-text light-green" %>
<%= link_to "Don't have an account? Sign up!", signup_path %>
<%= form_for(:session, url: login_path) do |f| %>
<div class="input-field col s12">
<%= f.label :email, "Email Address" %>
<%= f.text_field :email %>
</div>
<div class="input-field col s12">
<%= f.label :password %>
<%= f.password_field :password %>
</div>
<br>
<%= f.submit "Login", class: "btn white-text light-green" %>
<%= link_to "Don't have an account? Sign up!", signup_path %>
<% end %>
<% end %>
</div>
<% end %>