class Sorcery::Providers::Github

This class adds support for OAuth with github.com.

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

Attributes

auth_path[RW]
scope[RW]
token_url[RW]
user_info_path[RW]

Public Class Methods

new() click to toggle source
Calls superclass method Sorcery::Providers::Base::new
# 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

Public Instance Methods

get_user_hash(access_token) click to toggle source
# File lib/sorcery/providers/github.rb, line 24
def get_user_hash(access_token)
  response = access_token.get(user_info_path)

  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) click to toggle source

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

# File lib/sorcery/providers/github.rb, line 37
def login_url(_params, _session)
  authorize_url(authorize_url: auth_path)
end
primary_email(access_token) click to toggle source
# File lib/sorcery/providers/github.rb, line 50
def primary_email(access_token)
  response = access_token.get(user_info_path + '/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) click to toggle source

tries to login the user from access token

# 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