2013-08-23 02:20:43 -04:00
|
|
|
class Auth::GithubAuthenticator < Auth::Authenticator
|
|
|
|
|
|
|
|
def name
|
|
|
|
"github"
|
|
|
|
end
|
|
|
|
|
|
|
|
def after_authenticate(auth_token)
|
|
|
|
result = Auth::Result.new
|
|
|
|
|
|
|
|
data = auth_token[:info]
|
|
|
|
|
|
|
|
result.username = screen_name = data["nickname"]
|
|
|
|
result.email = email = data["email"]
|
|
|
|
|
|
|
|
github_user_id = auth_token["uid"]
|
|
|
|
|
|
|
|
result.extra_data = {
|
|
|
|
github_user_id: github_user_id,
|
|
|
|
github_screen_name: screen_name,
|
|
|
|
}
|
|
|
|
|
2014-05-06 09:41:59 -04:00
|
|
|
user_info = GithubUserInfo.find_by(github_user_id: github_user_id)
|
2013-08-23 02:20:43 -04:00
|
|
|
|
|
|
|
if user_info
|
|
|
|
user = user_info.user
|
|
|
|
elsif user = User.find_by_email(email)
|
|
|
|
user_info = GithubUserInfo.create(
|
|
|
|
user_id: user.id,
|
|
|
|
screen_name: screen_name,
|
|
|
|
github_user_id: github_user_id
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
result.user = user
|
2014-03-25 19:55:36 -04:00
|
|
|
result.email_valid = false
|
2013-08-23 02:20:43 -04:00
|
|
|
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
|
|
|
def after_create_account(user, auth)
|
|
|
|
data = auth[:extra_data]
|
|
|
|
GithubUserInfo.create(
|
|
|
|
user_id: user.id,
|
|
|
|
screen_name: data[:github_screen_name],
|
|
|
|
github_user_id: data[:github_user_id]
|
|
|
|
)
|
|
|
|
end
|
2013-08-25 21:04:16 -04:00
|
|
|
|
|
|
|
|
|
|
|
def register_middleware(omniauth)
|
|
|
|
omniauth.provider :github,
|
|
|
|
:setup => lambda { |env|
|
|
|
|
strategy = env["omniauth.strategy"]
|
|
|
|
strategy.options[:client_id] = SiteSetting.github_client_id
|
|
|
|
strategy.options[:client_secret] = SiteSetting.github_client_secret
|
|
|
|
},
|
|
|
|
:scope => "user:email"
|
|
|
|
end
|
2013-08-23 02:20:43 -04:00
|
|
|
end
|