GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#869)
by Ahmad
04:03
created

SessionsController   C

Complexity

Total Complexity 56

Size/Duplication

Total Lines 241
Duplicated Lines 0.41 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
dl 1
loc 241
rs 5.5199
c 4
b 0
f 0
wmc 56

17 Methods

Rating   Name   Duplication   Size   Complexity  
A ldap_signin() 0 2 1
A signin() 0 13 3
A new() 0 12 3
A current_provider() 0 3 2
A session_params() 0 3 1
C create() 0 31 10
A check_user_signup_allowed() 0 3 2
A check_user_exists() 0 3 1
A destroy() 0 4 1
A one_provider() 0 6 1
A passes_invite_reqs() 0 6 2
B ldap() 0 27 6
A check_auth_deleted() 0 3 1
A check_user_deleted() 0 3 1
A switch_account_to_local() 0 12 2
F process_signin() 1 42 14
A switch_account_to_social() 0 8 1

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like SessionsController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

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]
28
29
  # GET /signin
30
  def signin
31
    check_if_twitter_account
32
33
    if one_provider
34
      provider_path = if Rails.configuration.omniauth_ldap
35
        ldap_signin_path
36
      else
37
        "#{Rails.configuration.relative_url_root}/auth/#{providers.first}"
38
      end
39
40
      return redirect_to provider_path
41
    end
42
  end
43
44
  # GET /ldap_signin
45
  def ldap_signin
46
  end
47
48
  # GET /signup
49
  def new
50
    # Check if the user needs to be invited
51
    if invite_registration
52
      redirect_to root_path, flash: { alert: I18n.t("registration.invite.no_invite") } unless params[:invite_token]
53
54
      session[:invite_token] = params[:invite_token]
55
    end
56
57
    check_if_twitter_account(true)
58
59
    @user = User.new
60
  end
61
62
  # POST /users/login
63
  def create
64
    logger.info "Support: #{session_params[:email]} is attempting to login."
65
66
    user = User.include_deleted.find_by(email: session_params[:email])
67
68
    is_super_admin = user&.has_role? :super_admin
69
70
    # Scope user to domain if the user is not a super admin
71
    user = User.include_deleted.find_by(email: session_params[:email], provider: @user_domain) unless is_super_admin
72
73
    # Check user with that email exists
74
    return redirect_to(signin_path, alert: I18n.t("invalid_credentials")) unless user
75
76
    # Check if authenticators have switched
77
    return switch_account_to_local(user) if !is_super_admin && auth_changed_to_local?(user)
78
79
    # Check correct password was entered
80
    return redirect_to(signin_path, alert: I18n.t("invalid_credentials")) unless user.try(:authenticate,
81
      session_params[:password])
82
    # Check that the user is not deleted
83
    return redirect_to root_path, flash: { alert: I18n.t("registration.banned.fail") } if user.deleted?
84
85
    unless is_super_admin
86
      # Check that the user is a Greenlight account
87
      return redirect_to(root_path, alert: I18n.t("invalid_login_method")) unless user.greenlight_account?
88
      # Check that the user has verified their account
89
      return redirect_to(account_activation_path(email: user.email)) unless user.activated?
90
    end
91
92
    login(user)
93
  end
94
95
  # GET /users/logout
96
  def destroy
97
    logout
98
    redirect_to root_path
99
  end
100
101
  # GET/POST /auth/:provider/callback
102
  def omniauth
103
    @auth = request.env['omniauth.auth']
104
105
    begin
106
      process_signin
107
    rescue => e
108
      logger.error "Error authenticating via omniauth: #{e}"
109
      omniauth_fail
110
    end
111
  end
112
113
  # POST /auth/failure
114
  def omniauth_fail
115
    if params[:message].nil?
116
      redirect_to root_path, alert: I18n.t("omniauth_error")
117
    else
118
      redirect_to root_path, alert: I18n.t("omniauth_specific_error", error: params["message"])
119
    end
120
  end
121
122
  # GET /auth/ldap
123
  def ldap
124
    ldap_config = {}
125
    ldap_config[:host] = ENV['LDAP_SERVER']
126
    ldap_config[:port] = ENV['LDAP_PORT'].to_i != 0 ? ENV['LDAP_PORT'].to_i : 389
127
    ldap_config[:bind_dn] = ENV['LDAP_BIND_DN']
128
    ldap_config[:password] = ENV['LDAP_PASSWORD']
129
    ldap_config[:encryption] = if ENV['LDAP_METHOD'] == 'ssl'
130
                                    'simple_tls'
131
                                elsif ENV['LDAP_METHOD'] == 'tls'
132
                                    'start_tls'
133
                                end
134
    ldap_config[:base] = ENV['LDAP_BASE']
135
    ldap_config[:uid] = ENV['LDAP_UID']
136
137
    result = send_ldap_request(params[:session], ldap_config)
138
139
    return redirect_to(ldap_signin_path, alert: I18n.t("invalid_credentials")) unless result
140
141
    @auth = parse_auth(result.first, ENV['LDAP_ROLE_FIELD'])
142
143
    begin
144
      process_signin
145
    rescue => e
146
      logger.error "Support: Error authenticating via omniauth: #{e}"
147
      omniauth_fail
148
    end
149
  end
150
151
  private
152
153
  # Verify that GreenLight is configured to allow user signup.
154
  def check_user_signup_allowed
155
    redirect_to root_path unless Rails.configuration.allow_user_signup
156
  end
157
158
  def session_params
159
    params.require(:session).permit(:email, :password)
160
  end
161
162
  def one_provider
163
    providers = configured_providers
164
165
    (!allow_user_signup? || !allow_greenlight_accounts?) && providers.count == 1 &&
166
      !Rails.configuration.loadbalanced_configuration
167
  end
168
169
  def check_user_exists
170
    User.exists?(social_uid: @auth['uid'], provider: current_provider)
171
  end
172
173
  def check_user_deleted(email)
174
    User.deleted.exists?(email: email, provider: @user_domain)
175
  end
176
177
  def check_auth_deleted
178
    User.deleted.exists?(social_uid: @auth['uid'], provider: current_provider)
179
  end
180
181
  def current_provider
182
    @auth['provider'] == "bn_launcher" ? @auth['info']['customer'] : @auth['provider']
183
  end
184
185
  # Check if the user already exists, if not then check for invitation
186
  def passes_invite_reqs
187
    return true if @user_exists
188
189
    invitation = check_user_invited("", session[:invite_token], @user_domain)
190
    invitation[:present]
191
  end
192
193
  def process_signin
194
    @user_exists = check_user_exists
195
196
    if !@user_exists && @auth['provider'] == "twitter"
197
      return redirect_to root_path, flash: { alert: I18n.t("registration.deprecated.twitter_signup") }
198
    end
199
200
    # Check if user is deleted
201
    return redirect_to root_path, flash: { alert: I18n.t("registration.banned.fail") } if check_auth_deleted
202
203
    # If using invitation registration method, make sure user is invited
204 View Code Duplication
    return redirect_to root_path, flash: { alert: I18n.t("registration.invite.no_invite") } unless passes_invite_reqs
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
205
206
    # Switch the user to a social account if they exist under the same email with no social uid
207
    switch_account_to_social if !@user_exists && auth_changed_to_social?(@auth['info']['email'])
208
209
    user = User.from_omniauth(@auth)
210
211
    logger.info "Support: Auth user #{user.email} is attempting to login."
212
213
    # Add pending role if approval method and is a new user
214
    if approval_registration && !@user_exists
215
      user.add_role :pending
216
217
      # Inform admins that a user signed up if emails are turned on
218
      send_approval_user_signup_email(user)
219
220
      return redirect_to root_path, flash: { success: I18n.t("registration.approval.signup") }
221
    end
222
223
    send_invite_user_signup_email(user) if invite_registration && !@user_exists
224
225
    login(user)
226
227
    if @auth['provider'] == "twitter"
228
      flash[:alert] = if allow_user_signup? && allow_greenlight_accounts?
229
        I18n.t("registration.deprecated.twitter_signin", link: signup_path(old_twitter_user_id: user.id))
230
      else
231
        I18n.t("registration.deprecated.twitter_signin", link: signin_path(old_twitter_user_id: user.id))
232
      end
233
    end
234
  end
235
236
  # Send the user a password reset email to allow them to set their password
237
  def switch_account_to_local(user)
238
    logger.info "Switching social account to local account for #{user.uid}"
239
240
    # Send the user a reset password email
241
    user.create_reset_digest
242
    send_password_reset_email(user)
243
244
    # Overwrite the flash with a more descriptive message if successful
245
    flash[:success] = I18n.t("reset_password.auth_change") if flash[:success].present?
246
247
    redirect_to signin_path
248
  end
249
250
  # Set the user's social id to the new id being passed
251
  def switch_account_to_social
252
    user = User.find_by(email: @auth['info']['email'], provider: @user_domain, social_uid: nil)
253
254
    logger.info "Switching account to social account for #{user.uid}"
255
256
    # Set the user's social id to the one being returned from auth
257
    user.update_atrribute(:social_uid, @auth['uid'])
258
  end
259
end
260