Module: Zaikio::OAuthClient::Authenticatable

Extended by:
ActiveSupport::Concern
Included in:
ConnectionsController, SessionsController
Defined in:
lib/zaikio/oauth_client/authenticatable.rb

Overview

rubocop:disable Metrics/ModuleLength

Instance Method Summary collapse

Instance Method Details

#approveObject

rubocop:disable Metrics/MethodLength,Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/zaikio/oauth_client/authenticatable.rb', line 22

def approve  # rubocop:disable Metrics/MethodLength,Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity
  if params[:error].present?
    redirect_to send(
      respond_to?(:error_path_for) ? :error_path_for : :default_error_path_for,
      params[:error],
      description: params[:error_description]
    ) and return
  end

  if session[:state].present? && params[:state] != session[:state]
    return redirect_to send(
      respond_to?(:error_path_for) ? :error_path_for : :default_error_path_for,
      "invalid_state"
    )
  end

  access_token = create_access_token

  origin = session[:origin]
  session.delete(:origin)
  session.delete(:oauth_attempts)

  session[:zaikio_access_token_id] = access_token.id unless access_token.organization?

  redirect_to send(
    respond_to?(:after_approve_path_for) ? :after_approve_path_for : :default_after_approve_path_for,
    access_token, origin
  )
rescue OAuth2::Error => e
  raise e unless e.code == "invalid_grant" || e.code == "invalid_request"
  raise e if session[:oauth_attempts].to_i >= 3

  session[:oauth_attempts] = session[:oauth_attempts].to_i + 1

  redirect_to new_path(client_name: params[:client_name])
end

#destroyObject



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/zaikio/oauth_client/authenticatable.rb', line 59

def destroy
  if (access_token = Zaikio::AccessToken.valid.or(Zaikio::AccessToken.valid_refresh)
                                       .find_by(id: session[:zaikio_access_token_id]))
    access_token.revoke!
  end
  session.delete(:zaikio_access_token_id)
  session.delete(:origin)

  redirect_to send(
    respond_to?(:after_destroy_path_for) ? :after_destroy_path_for : :default_after_destroy_path_for,
    access_token&.id
  )
end

#newObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/zaikio/oauth_client/authenticatable.rb', line 6

def new
  opts = params.permit(:client_name, :show_signup, :prompt, :prompt_email_confirmation,
                       :force_login, :state, :lang,
                       person: %i[first_name name email],
                       organization: [:name, :country_code, { kinds: [] }])
  opts[:lang] ||= I18n.locale if defined?(I18n)
  client_name = opts.delete(:client_name)
  opts[:state] ||= session[:state] = SecureRandom.urlsafe_base64(32)

  redirect_to oauth_client.auth_code.authorize_url(
    redirect_uri: approve_url(client_name),
    scope: oauth_scope,
    **opts
  ), allow_other_host: true
end