Module: Account::Oauth::OmniauthCallbacks::ControllerBase

Extended by:
ActiveSupport::Concern
Defined in:
app/controllers/concerns/account/oauth/omniauth_callbacks/controller_base.rb

Instance Method Summary collapse

Instance Method Details

#callback(class_name, team_id) ⇒ Object



13
14
15
16
17
18
19
20
21
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'app/controllers/concerns/account/oauth/omniauth_callbacks/controller_base.rb', line 13

def callback(class_name, team_id)
   = "Oauth::#{class_name}Account".constantize
  # oauth_accounts_collection = "oauth_#{class_name.underscore}_accounts".to_sym
  oauth_accounts_attribute = "oauth_#{class_name.underscore}_account".to_sym
  integrations_installations_class = "::Integrations::#{class_name}Installation".constantize
  integrations_installations_collection = "integrations_#{class_name.underscore}_installations".to_sym

  auth = request.env["omniauth.auth"]

  # if the user didn't click "authorize" on the providers confirmation page, just show them an error.
  if params[:denied]
    message = t("omniauth.team.denied", provider: t(auth.provider))

    # redirect them to either the integrations page or the registration page.
    path = if team_id
      [:account, team, integrations_installations_collection]
    elsif current_user
      [:account, current_user]
    else
      new_user_registration_path
    end

    redirect_to path, notice: message
  end

  # first, keep a record of the oauth account.
  # this belongs to the system, and sometimes becomes associated with a user.
  begin
     = .find_or_create_by(uid: auth.uid)
    .update_from_oauth(auth)
  rescue PG::UniqueViolation
    retry
  end

  # if they're trying to use this account to add an integration to a team.
  if team_id

    # they must be signed in.
    authenticate_user!

    unless (team = current_user.teams.find_by(id: team_id))
      raise "user is adding an integration to a team they don't belong to?"
    end

    # now we check whether they're able to install new integrations for this team.
    if can? :create, integrations_installations_class.new(:team => team, oauth_accounts_attribute => )

      # if the oauth account is already present for the team ..
      if team.send(integrations_installations_collection).find_by(oauth_accounts_attribute => )
        message = t("omniauth.team.already_present", provider: t(auth.provider))

      else
        # otherwise, create a new integration for this team.
        team.send(integrations_installations_collection).create(:name => .name, oauth_accounts_attribute => )
        message = t("omniauth.team.connected", provider: t(auth.provider))

      end

    else

      # if they don't have access to add integrations to the team, show them an error.
      message = t("omniauth.team.not_allowed", provider: t(auth.provider))

    end

    # in all of these situations, we take them back to the team's oauth accounts index page for this provider.
    redirect_to [:account, team, integrations_installations_collection], notice: message

  # if they're already signed in, they're trying to add it to their account.
  elsif current_user

    # if the account is already connected to a user, we can't connect it again.
    # this would potentially lock someone else out of their account.
    if .user
      message_key = (.user == current_user) ? "omniauth.user.reconnected" : "omniauth.user.already_registered"
      redirect_to (current_user), notice: t(message_key, provider: t(auth.provider))
    else
      .update(user: current_user)
      redirect_to (current_user), notice: t("omniauth.user.connected", provider: t(auth.provider))
    end

  # if they're not trying to add it to a team, they're trying to sign in or sign up.
  # if they're already associated with a user record, just sign them in.
  elsif .user

     .user
    handle_outstanding_invitation
    redirect_to 

  # if they're not associated with a user and they're allowed to sign up.
  elsif 

    # we're going to try to create a new user account.
    # if the oauth provider gave us an email address, use that, otherwise use a
    # temporary email placeholder. (we'll never show the temporary to the user.)
    # this has to be a "real" email address so we don't trip up any email validators.
    # however, this is secure because even if someone controlled `bullettrain.co`,
    # they would never be able to guess the hex code to reset the password.
    # even that is a rare edge case, because we force people to update this in onboarding.
    email = auth.info.email.present? ? auth.info.email : "noreply@#{SecureRandom.hex}.example.com"

    # if the user already exists with the email address on the account ..
    if User.find_by(email: email)

      # we can't sign them in, because they haven't added the oauth account to
      # their account yet for sign in. there is a potential security loophole
      # here if you allow them to do this at worst, and at best you're
      # depending on the security of the oauth provider to have verified the
      # email address on the account.
      redirect_to new_user_session_path(user: {email: email}, email_exists: true), notice: "Sorry, there is already a user registered with the email address #{email}, but this #{t(auth.provider)} account isn't configured for login with that account. Please sign in using your password and then add this account."

    else

      # if the user doesn't exist in the database yet, great!
      # let's create it and sign them in.
      password = Devise.friendly_token[0, 20]
      user = User.create(email: email, password: password, password_confirmation: password)

      .update(user: user)

       user
      handle_outstanding_invitation

      # if the user doesn't have a team at this point, create one.
      unless user.teams.any?
        user.create_default_team

        # if a user is signing up for the first time *and* creating their own team,
        # this is a ux shortcut that makes sense: automatically install their oauth account as an integration.
        user.teams.first.send(integrations_installations_collection).create(:name => .name, oauth_accounts_attribute => )
      end

      redirect_to 
    end

  else
    redirect_to new_user_session_path, notice: t("omniauth.user.account_not_found", provider: t(auth.provider))
  end
end

#failureObject



8
9
10
11
# File 'app/controllers/concerns/account/oauth/omniauth_callbacks/controller_base.rb', line 8

def failure
  flash[:danger] = "Failed to sign in"
  redirect_to root_path
end

#team_id_from_envObject



4
5
6
# File 'app/controllers/concerns/account/oauth/omniauth_callbacks/controller_base.rb', line 4

def team_id_from_env
  request.env.dig("omniauth.params", "team_id")&.to_i
end