Class: Code0::Identities::Provider::Github

Inherits:
BaseOauth
  • Object
show all
Defined in:
lib/code0/identities/provider/github.rb,
sig/code0/identities/provider/github.rbs

Instance Attribute Summary

Attributes inherited from BaseOauth

#config_loader

Instance Method Summary collapse

Methods inherited from BaseOauth

#access_token, #check_response, #config, #initialize, #load_identity

Constructor Details

This class inherits a constructor from Code0::Identities::Provider::BaseOauth

Instance Method Details

#authorization_url::String

Returns:

  • (::String)


32
33
34
# File 'lib/code0/identities/provider/github.rb', line 32

def authorization_url
  "https://github.com/login/oauth/authorize?client_id=#{config[:client_id]}&redirect_uri=#{URI.encode_uri_component(config[:redirect_uri])}&scope=read:user+user:email"
end

#create_identity(response, access_token, token_type) ⇒ Identity

Parameters:

  • response: (Net::HTTPResponse)
  • access_token: (String)
  • token_type: (String)

Returns:



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/code0/identities/provider/github.rb', line 47

def create_identity(response, access_token, token_type)
  body = response.parsed_response

  identifier = body["id"]
  username = body["login"]
  email = body["email"]

  email = private_email(access_token, token_type) if email.nil?

  Identity.new(config[:provider_name], identifier, username, email, nil, nil)
end

#private_email(access_token, token_type) ⇒ String

Parameters:

  • access_token: (String)
  • token_type: (String)

Returns:

  • (String)


36
37
38
39
40
41
42
43
44
45
# File 'lib/code0/identities/provider/github.rb', line 36

def private_email(access_token, token_type)
  response = HTTParty.get("#{user_details_url}/emails",
                          headers: {
                            Authorization: "#{token_type} #{access_token}",
                            "Accept" => "application/json"
                          })
  body = response.parsed_response

  body.find { |email_object| email_object["primary"] }["email"]
end

#token_payload(code) ⇒ { code: String, redirect_uri: String, client_id: String, client_secret: String }

Parameters:

  • code: (String)

Returns:

  • ({ code: String, redirect_uri: String, client_id: String, client_secret: String })


21
22
23
24
25
26
# File 'lib/code0/identities/provider/github.rb', line 21

def token_payload(code)
  { code: code,
    redirect_uri: config[:redirect_uri],
    client_id: config[:client_id],
    client_secret: config[:client_secret] }
end

#token_url"https://github.com/login/oauth/access_token"

Returns:



17
18
19
# File 'lib/code0/identities/provider/github.rb', line 17

def token_url
  "https://github.com/login/oauth/access_token"
end

#user_details_url"https://api.github.com/user"

Returns:

  • ("https://api.github.com/user")


28
29
30
# File 'lib/code0/identities/provider/github.rb', line 28

def user_details_url
  "https://api.github.com/user"
end

#validate_config!Object



7
8
9
10
11
12
13
14
15
# File 'lib/code0/identities/provider/github.rb', line 7

def validate_config!
  required_keys = %i[redirect_uri client_id client_secret]

  missing_keys = required_keys - config.keys
  invalid_keys = config.keys - required_keys - [:provider_name]

  raise MissingConfigurationError, "Missing: #{missing_keys.inspect}" if missing_keys.any?
  raise InvalidConfigurationError, "Invalid: #{invalid_keys.inspect}" if invalid_keys.any?
end