summaryrefslogtreecommitdiff
path: root/app/models/user_session.rb
blob: 30b5b5d9b946d1a0e53801ae4b538e5c97232cc8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
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
class UserSession
  include ActiveModel::Conversion
  include StaticModel
  extend ActiveModel::Naming

  attr_accessor :url

  def authenticate(auth_opts)
    authenticate_via(auth_opts) do |url, network, options|
      network.authenticate(url, options)
    end
  end

  def authenticate_by_token(auth_opts)
    result = authenticate_via(auth_opts) do |url, network, options|
      network.authenticate_by_token(url, options)
    end

    result
  end

  private

  def authenticate_via(options, &block)
    url = options.delete(:url)

    return nil unless GitlabCi.config.gitlab_server.url.include?(url)

    user = block.call(url, Network.new, options)

    if user
      return User.new(user.merge({ "url" => url }))
    else
      nil
    end
  rescue
    nil
  end
end