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

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

Instance Attribute Summary

Attributes inherited from BaseOauth

#config_loader

Instance Method Summary collapse

Methods inherited from BaseOauth

#initialize, #load_identity

Constructor Details

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

Instance Method Details

#authorization_urlObject



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

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) ⇒ Object



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

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(:github, identifier, username, email, nil, nil)
end

#private_email(access_token, token_type) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# File 'lib/code0/identities/provider/github.rb', line 26

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) ⇒ Object



11
12
13
14
15
16
# File 'lib/code0/identities/provider/github.rb', line 11

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

#token_urlObject



7
8
9
# File 'lib/code0/identities/provider/github.rb', line 7

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

#user_details_urlObject



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

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