Passed
Push — master ( 71f35c...b1a999 )
by Ahmad
06:52 queued 10s
created

SessionsController.ldap()   B

Complexity

Conditions 8

Size

Total Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
c 0
b 0
f 0
dl 0
loc 33
rs 7.2213
1
# frozen_string_literal: true
2
3
# BigBlueButton open source conferencing system - http://www.bigbluebutton.org/.
4
#
5
# Copyright (c) 2018 BigBlueButton Inc. and by respective authors (see below).
6
#
7
# This program is free software; you can redistribute it and/or modify it under the
8
# terms of the GNU Lesser General Public License as published by the Free Software
9
# Foundation; either version 3.0 of the License, or (at your option) any later
10
# version.
11
#
12
# BigBlueButton is distributed in the hope that it will be useful, but WITHOUT ANY
13
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
14
# PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
15
#
16
# You should have received a copy of the GNU Lesser General Public License along
17
# with BigBlueButton; if not, see <http://www.gnu.org/licenses/>.
18
19
class SessionsController < ApplicationController
20
  include Authenticator
21
  include Registrar
22
  include Emailer
23
  include LdapAuthenticator
24
25
  skip_before_action :verify_authenticity_token, only: [:omniauth, :fail]
26
  before_action :check_user_signup_allowed, only: [:new]
27
  before_action :ensure_unauthenticated_except_twitter, only: [:new, :signin, :ldap_signin]
28
29
  # GET /signin
30
  def signin
31
    check_if_twitter_account
32
33
    @providers = configured_providers
34
35
    if one_provider
36
      provider_path = if Rails.configuration.omniauth_ldap
37
        ldap_signin_path
38
      else
39
        "#{Rails.configuration.relative_url_root}/auth/#{@providers.first}"
40
      end
41
42
      return redirect_to provider_path
43
    end
44
  end
45
46
  # GET /ldap_signin
47
  def ldap_signin
48
  end
49
50
  # GET /signup
51
  def new
52
    # Check if the user needs to be invited
53
    if invite_registration
54
      redirect_to root_path, flash: { alert: I18n.t("registration.invite.no_invite") } unless params[:invite_token]
55
56
      session[:invite_token] = params[:invite_token]
57
    end
58
59
    check_if_twitter_account(true)
60
61
    @user = User.new
62
  end
63
64
  # POST /users/login
65
  def create
66
    logger.info "Support: #{session_params[:email]} is attempting to login."
67
68
    user = User.include_deleted.find_by(email: session_params[:email])
69
70
    is_super_admin = user&.has_role? :super_admin
71
72
    # Scope user to domain if the user is not a super admin
73
    user = User.include_deleted.find_by(email: session_params[:email], provider: @user_domain) unless is_super_admin
74
75
    # Check user with that email exists
76
    return redirect_to(signin_path, alert: I18n.t("invalid_credentials")) unless user
77
78
    # Check if authenticators have switched
79
    return switch_account_to_local(user) if !is_super_admin && auth_changed_to_local?(user)
80
81
    # Check correct password was entered
82
    return redirect_to(signin_path, alert: I18n.t("invalid_credentials")) unless user.try(:authenticate,
83
      session_params[:password])
84
    # Check that the user is not deleted
85
    return redirect_to root_path, flash: { alert: I18n.t("registration.banned.fail") } if user.deleted?
86
87
    unless is_super_admin
88
      # Check that the user is a Greenlight account
89
      return redirect_to(root_path, alert: I18n.t("invalid_login_method")) unless user.greenlight_account?
90
      # Check that the user has verified their account
91
      unless user.activated?
92
        user.create_activation_token
93
        return redirect_to(account_activation_path(token: user.activation_token))
94
      end
95
    end
96
97
    login(user)
98
  end
99
100
  # GET /users/logout
101
  def destroy
102
    logout
103
    redirect_to root_path
104
  end
105
106
  # GET/POST /auth/:provider/callback
107
  def omniauth
108
    @auth = request.env['omniauth.auth']
109
110
    begin
111
      process_signin
112
    rescue => e
113
      logger.error "Error authenticating via omniauth: #{e}"
114
      omniauth_fail
115
    end
116
  end
117
118
  # POST /auth/failure
119
  def omniauth_fail
120
    if params[:message].nil?
121
      redirect_to root_path, alert: I18n.t("omniauth_error")
122
    else
123
      redirect_to root_path, alert: I18n.t("omniauth_specific_error", error: params["message"])
124
    end
125
  end
126
127
  # GET /auth/ldap
128
  def ldap
129
    ldap_config = {}
130
    ldap_config[:host] = ENV['LDAP_SERVER']
131
    ldap_config[:port] = ENV['LDAP_PORT'].to_i != 0 ? ENV['LDAP_PORT'].to_i : 389
132
    ldap_config[:bind_dn] = ENV['LDAP_BIND_DN']
133
    ldap_config[:password] = ENV['LDAP_PASSWORD']
134
    ldap_config[:auth_method] = ENV['LDAP_AUTH']
135
    ldap_config[:encryption] = if ENV['LDAP_METHOD'] == 'ssl'
136
                                    'simple_tls'
137
                                elsif ENV['LDAP_METHOD'] == 'tls'
138
                                    'start_tls'
139
                                end
140
    ldap_config[:base] = ENV['LDAP_BASE']
141
    ldap_config[:filter] = ENV['LDAP_FILTER']
142
    ldap_config[:uid] = ENV['LDAP_UID']
143
144
    if params[:session][:username].blank? || session_params[:password].blank?
145
      return redirect_to(ldap_signin_path, alert: I18n.t("invalid_credentials"))
146
    end
147
148
    result = send_ldap_request(params[:session], ldap_config)
149
150
    return redirect_to(ldap_signin_path, alert: I18n.t("invalid_credentials")) unless result
151
152
    @auth = parse_auth(result.first, ENV['LDAP_ROLE_FIELD'])
153
154
    begin
155
      process_signin
156
    rescue => e
157
      logger.error "Support: Error authenticating via omniauth: #{e}"
158
      omniauth_fail
159
    end
160
  end
161
162
  private
163
164
  # Verify that GreenLight is configured to allow user signup.
165
  def check_user_signup_allowed
166
    redirect_to root_path unless Rails.configuration.allow_user_signup
167
  end
168
169
  def session_params
170
    params.require(:session).permit(:email, :password)
171
  end
172
173
  def one_provider
174
    (!allow_user_signup? || !allow_greenlight_accounts?) && @providers.count == 1 &&
175
      !Rails.configuration.loadbalanced_configuration
176
  end
177
178
  def check_user_exists
179
    User.exists?(social_uid: @auth['uid'], provider: current_provider)
180
  end
181
182
  def check_user_deleted(email)
183
    User.deleted.exists?(email: email, provider: @user_domain)
184
  end
185
186
  def check_auth_deleted
187
    User.deleted.exists?(social_uid: @auth['uid'], provider: current_provider)
188
  end
189
190
  def current_provider
191
    @auth['provider'] == "bn_launcher" ? @auth['info']['customer'] : @auth['provider']
192
  end
193
194
  # Check if the user already exists, if not then check for invitation
195
  def passes_invite_reqs
196
    return true if @user_exists
197
198
    invitation = check_user_invited("", session[:invite_token], @user_domain)
199
    invitation[:present]
200
  end
201
202
  def process_signin
203
    @user_exists = check_user_exists
204
205
    if !@user_exists && @auth['provider'] == "twitter"
206
      return redirect_to root_path, flash: { alert: I18n.t("registration.deprecated.twitter_signup") }
207
    end
208
209
    # Check if user is deleted
210
    return redirect_to root_path, flash: { alert: I18n.t("registration.banned.fail") } if check_auth_deleted
211
212
    # If using invitation registration method, make sure user is invited
213 View Code Duplication
    return redirect_to root_path, flash: { alert: I18n.t("registration.invite.no_invite") } unless passes_invite_reqs
214
215
    # Switch the user to a social account if they exist under the same email with no social uid
216
    switch_account_to_social if !@user_exists && auth_changed_to_social?(@auth['info']['email'])
217
218
    user = User.from_omniauth(@auth)
219
220
    logger.info "Support: Auth user #{user.email} is attempting to login."
221
222
    # Add pending role if approval method and is a new user
223
    if approval_registration && !@user_exists
224
      user.add_role :pending
225
226
      # Inform admins that a user signed up if emails are turned on
227
      send_approval_user_signup_email(user)
228
229
      return redirect_to root_path, flash: { success: I18n.t("registration.approval.signup") }
230
    end
231
232
    send_invite_user_signup_email(user) if invite_registration && !@user_exists
233
234
    login(user)
235
236
    if @auth['provider'] == "twitter"
237
      flash[:alert] = if allow_user_signup? && allow_greenlight_accounts?
238
        I18n.t("registration.deprecated.twitter_signin", link: signup_path(old_twitter_user_id: user.id))
239
      else
240
        I18n.t("registration.deprecated.twitter_signin", link: signin_path(old_twitter_user_id: user.id))
241
      end
242
    end
243
  end
244
245
  # Send the user a password reset email to allow them to set their password
246
  def switch_account_to_local(user)
247
    logger.info "Switching social account to local account for #{user.uid}"
248
249
    # Send the user a reset password email
250
    user.create_reset_digest
251
    send_password_reset_email(user)
252
253
    # Overwrite the flash with a more descriptive message if successful
254
    flash[:success] = I18n.t("reset_password.auth_change") if flash[:success].present?
255
256
    redirect_to signin_path
257
  end
258
259
  # Set the user's social id to the new id being passed
260
  def switch_account_to_social
261
    user = User.find_by(email: @auth['info']['email'], provider: @user_domain, social_uid: nil)
262
263
    logger.info "Switching account to social account for #{user.uid}"
264
265
    # Set the user's social id to the one being returned from auth
266
    user.update_attribute(:social_uid, @auth['uid'])
267
  end
268
end
269