Class: Clavis::Providers::Github

Inherits:
Base
  • Object
show all
Defined in:
lib/clavis/providers/github.rb

Instance Attribute Summary

Attributes inherited from Base

#authorize_endpoint_url, #client_id, #client_secret, #provider_name, #redirect_uri, #scope, #token_endpoint_url, #userinfo_endpoint_url

Instance Method Summary collapse

Methods inherited from Base

#authorize_url, #openid_provider?, #process_callback, #refresh_token, #token_exchange

Methods included from TokenExchangeHandler

#build_token_exchange_params, #handle_connection_error, #handle_error_response, #handle_faraday_error, #handle_parser_error, #handle_standard_error, #make_token_request, #parse_response, #skip_error_for_test?, #test_token_response, #validate_and_clean_code

Constructor Details

#initialize(config = {}) ⇒ Github

Returns a new instance of Github.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/clavis/providers/github.rb', line 6

def initialize(config = {})
  # Support GitHub Enterprise by allowing configuration of base URLs
  site_url = config[:site_url] || "https://api.github.com"
  auth_url = config[:authorize_url] || "https://github.com/login/oauth/authorize"
  token_url = config[:token_url] || "https://github.com/login/oauth/access_token"

  config[:authorization_endpoint] = auth_url
  config[:token_endpoint] = token_url
  config[:userinfo_endpoint] = "#{site_url}/user"
  config[:scope] = config[:scope] || "user:email"

  # Store for later use
  @site_url = site_url

  super
end

Instance Method Details

#authorization_endpointObject



23
24
25
# File 'lib/clavis/providers/github.rb', line 23

def authorization_endpoint
  @config[:authorization_endpoint]
end

#default_scopesObject



35
36
37
# File 'lib/clavis/providers/github.rb', line 35

def default_scopes
  "user:email"
end

#get_user_info(access_token) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/clavis/providers/github.rb', line 39

def (access_token)
  return {} unless userinfo_endpoint

  # Validate inputs
  raise Clavis::InvalidToken unless Clavis::Security::InputValidator.valid_token?(access_token)

  response = http_client.get(userinfo_endpoint) do |req|
    req.headers["Authorization"] = "Bearer #{access_token}"
    req.headers["Accept"] = "application/vnd.github.v3+json"
  end

  if response.status != 200
    Clavis::Logging.log_userinfo_request(provider_name, false)
    handle_userinfo_error_response(response)
  end

  Clavis::Logging.log_userinfo_request(provider_name, true)

  process_userinfo_response(response)
end

#token_endpointObject



27
28
29
# File 'lib/clavis/providers/github.rb', line 27

def token_endpoint
  @config[:token_endpoint]
end

#userinfo_endpointObject



31
32
33
# File 'lib/clavis/providers/github.rb', line 31

def userinfo_endpoint
  @config[:userinfo_endpoint]
end