Class: Sorcery::Providers::Github

Inherits:
Base
  • Object
show all
Includes:
Sorcery::Protocols::Oauth2
Defined in:
lib/sorcery/providers/github.rb

Overview

This class adds support for OAuth with github.com.

config.github.key = <key>
config.github.secret = <secret>
...

Instance Attribute Summary collapse

Attributes inherited from Base

#access_token, #callback_url, #key, #original_callback_url, #secret, #site, #state, #user_info_mapping

Instance Method Summary collapse

Methods included from Sorcery::Protocols::Oauth2

#authorize_url, #build_client, #get_access_token, #oauth_version

Methods inherited from Base

#auth_hash, descendants, #has_callback?, name

Constructor Details

#initializeGithub

Returns a new instance of Github.



14
15
16
17
18
19
20
21
22
# File 'lib/sorcery/providers/github.rb', line 14

def initialize
  super

  @scope          = nil
  @site           = 'https://github.com/'
  @user_info_path = 'https://api.github.com/user'
  @auth_path      = '/login/oauth/authorize'
  @token_url      = '/login/oauth/access_token'
end

Instance Attribute Details

#auth_pathObject

Returns the value of attribute auth_path.



12
13
14
# File 'lib/sorcery/providers/github.rb', line 12

def auth_path
  @auth_path
end

#scopeObject

Returns the value of attribute scope.



12
13
14
# File 'lib/sorcery/providers/github.rb', line 12

def scope
  @scope
end

#token_urlObject

Returns the value of attribute token_url.



12
13
14
# File 'lib/sorcery/providers/github.rb', line 12

def token_url
  @token_url
end

#user_info_pathObject

Returns the value of attribute user_info_path.



12
13
14
# File 'lib/sorcery/providers/github.rb', line 12

def 
  @user_info_path
end

Instance Method Details

#get_user_hash(access_token) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/sorcery/providers/github.rb', line 24

def get_user_hash(access_token)
  response = access_token.get()

  auth_hash(access_token).tap do |h|
    h[:user_info] = JSON.parse(response.body).tap do |uih|
      uih['email'] = primary_email(access_token) if scope =~ /user/
    end
    h[:uid] = h[:user_info]['id']
  end
end

#login_url(_params, _session) ⇒ Object

calculates and returns the url to which the user should be redirected, to get authenticated at the external provider’s site.



37
38
39
# File 'lib/sorcery/providers/github.rb', line 37

def (_params, _session)
  authorize_url(authorize_url: auth_path)
end

#primary_email(access_token) ⇒ Object



50
51
52
53
54
55
# File 'lib/sorcery/providers/github.rb', line 50

def primary_email(access_token)
  response = access_token.get( + '/emails')
  emails = JSON.parse(response.body)
  primary = emails.find { |i| i['primary'] }
  primary && primary['email'] || emails.first && emails.first['email']
end

#process_callback(params, _session) ⇒ Object

tries to login the user from access token



42
43
44
45
46
47
48
# File 'lib/sorcery/providers/github.rb', line 42

def process_callback(params, _session)
  args = {}.tap do |a|
    a[:code] = params[:code] if params[:code]
  end

  get_access_token(args, token_url: token_url, token_method: :post)
end