forked from omniauth/omniauth-github
/
github.rb
84 lines (70 loc) · 2.15 KB
/
github.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
require 'omniauth-oauth2'
module OmniAuth
module Strategies
class GitHub < OmniAuth::Strategies::OAuth2
option :client_options, {
:site => 'https://api.github.com',
:authorize_url => 'https://github.com/login/oauth/authorize',
:token_url => 'https://github.com/login/oauth/access_token'
}
def request_phase
super
end
def authorize_params
super.tap do |params|
%w[scope client_options].each do |v|
if request.params[v]
params[v.to_sym] = request.params[v]
end
end
end
end
uid { raw_info['id'].to_s }
info do
{
'nickname' => raw_info['login'],
'email' => email,
'name' => raw_info['name'],
'image' => raw_info['avatar_url'],
'urls' => {
'GitHub' => raw_info['html_url'],
'Blog' => raw_info['blog'],
},
}
end
extra do
{:raw_info => raw_info, :all_emails => emails, :scope => scope }
end
def raw_info
access_token.options[:mode] = :query
@raw_info ||= access_token.get('user').parsed
end
def email
(email_access_allowed?) ? primary_email : raw_info['email']
end
def scope
access_token['scope']
end
def primary_email
primary = emails.find{ |i| i['primary'] && i['verified'] }
primary && primary['email'] || nil
end
# The new /user/emails API - http://developer.github.com/v3/users/emails/#future-response
def emails
return [] unless email_access_allowed?
access_token.options[:mode] = :query
@emails ||= access_token.get('user/emails', :headers => { 'Accept' => 'application/vnd.github.v3' }).parsed
end
def email_access_allowed?
return false unless options['scope']
email_scopes = ['user', 'user:email']
scopes = options['scope'].split(',')
(scopes & email_scopes).any?
end
def callback_url
full_host + script_name + callback_path
end
end
end
end
OmniAuth.config.add_camelization 'github', 'GitHub'