summaryrefslogtreecommitdiff
path: root/vendor/gems/omniauth-gitlab/lib/omniauth/strategies/gitlab.rb
blob: 3c23d8e2b195ce885c0f50adc54a88d3390bdb5d (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
40
41
42
43
44
45
46
# frozen_string_literal: true

require 'omniauth-oauth2'

module OmniAuth
  module Strategies
    class GitLab < OmniAuth::Strategies::OAuth2
      API_SUFFIX_REGEX = %r{/api/v(\d+)/?$}.freeze

      option :client_options, site: 'https://gitlab.com'

      option :redirect_url

      uid { raw_info['id'].to_s }

      info do
        {
          name: raw_info['name'],
          username: raw_info['username'],
          email: raw_info['email'],
          image: raw_info['avatar_url']
        }
      end

      extra do
        { raw_info: raw_info }
      end

      def raw_info
        @raw_info ||= access_token.get(user_endpoint_url).parsed
      end

      def callback_url
        options.redirect_url || (full_host + callback_path)
      end

      private

      def user_endpoint_url
        options.client_options.site.match(API_SUFFIX_REGEX) ? 'user' : 'api/v4/user'
      end
    end
  end
end

OmniAuth.config.add_camelization 'gitlab', 'GitLab'