File: auth0.rb

package info (click to toggle)
ruby-omniauth-auth0 2.0.0-0%2Bdeb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 144 kB
  • sloc: ruby: 397; makefile: 6
file content (105 lines) | stat: -rw-r--r-- 2,408 bytes parent folder | download | duplicates (2)
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
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
require 'base64'
require 'uri'
require 'omniauth-oauth2'

module OmniAuth
  module Strategies
    # Auth0 OmniAuth strategy
    class Auth0 < OmniAuth::Strategies::OAuth2
      option :name, 'auth0'

      args [
        :client_id,
        :client_secret,
        :domain
      ]

      def client
        options.client_options.site = domain_url
        options.client_options.authorize_url = '/authorize'
        options.client_options.token_url = '/oauth/token'
        options.client_options.userinfo_url = '/userinfo'
        super
      end

      uid { raw_info['sub'] }

      credentials do
        hash = { 'token' => access_token.token }
        hash['expires'] = true
        if access_token.params
          hash['id_token'] = access_token.params['id_token']
          hash['token_type'] = access_token.params['token_type']
          hash['refresh_token'] = access_token.refresh_token
        end
        hash
      end

      extra do
        {
          raw_info: raw_info
        }
      end

      info do
        {
          name: raw_info['name'] || raw_info['sub'],
          nickname: raw_info['nickname'],
          email: raw_info['email'],
          image: raw_info['picture']
        }
      end

      def authorize_params
        params = super
        params['auth0Client'] = client_info
        params
      end

      def request_phase
        if no_client_id?
          fail!(:missing_client_id)
        elsif no_client_secret?
          fail!(:missing_client_secret)
        elsif no_domain?
          fail!(:missing_domain)
        else
          super
        end
      end

      private

      def raw_info
        userinfo_url = options.client_options.userinfo_url
        @raw_info ||= access_token.get(userinfo_url).parsed
      end

      def no_client_id?
        ['', nil].include?(options.client_id)
      end

      def no_client_secret?
        ['', nil].include?(options.client_secret)
      end

      def no_domain?
        ['', nil].include?(options.domain)
      end

      def domain_url
        domain_url = URI(options.domain)
        domain_url = URI("https://#{domain_url}") if domain_url.scheme.nil?
        domain_url.to_s
      end

      def client_info
        client_info = JSON.dump(
          name: 'omniauth-auth0',
          version: OmniAuth::Auth0::VERSION
        )
        Base64.urlsafe_encode64(client_info)
      end
    end
  end
end