2013-08-23 02:20:43 -04:00
|
|
|
class Auth::OpenIdAuthenticator < Auth::Authenticator
|
|
|
|
|
2013-08-25 21:04:16 -04:00
|
|
|
attr_reader :name, :identifier
|
|
|
|
|
|
|
|
def initialize(name, identifier, opts = {})
|
2013-08-23 02:20:43 -04:00
|
|
|
@name = name
|
2013-08-25 21:04:16 -04:00
|
|
|
@identifier = identifier
|
2013-08-23 02:20:43 -04:00
|
|
|
@opts = opts
|
|
|
|
end
|
|
|
|
|
|
|
|
def after_authenticate(auth_token)
|
|
|
|
result = Auth::Result.new
|
|
|
|
|
|
|
|
data = auth_token[:info]
|
2014-03-25 23:52:50 -04:00
|
|
|
identity_url = auth_token[:extra][:response].identity_url
|
2013-08-23 02:20:43 -04:00
|
|
|
result.email = email = data[:email]
|
|
|
|
|
2014-08-07 13:28:50 -04:00
|
|
|
raise Discourse::InvalidParameters.new(:email) if email.blank?
|
|
|
|
|
2013-08-23 02:20:43 -04:00
|
|
|
# If the auth supplies a name / username, use those. Otherwise start with email.
|
2014-03-25 23:52:50 -04:00
|
|
|
result.name = data[:name] || data[:email]
|
|
|
|
result.username = data[:nickname] || data[:email]
|
2013-08-23 02:20:43 -04:00
|
|
|
|
|
|
|
user_open_id = UserOpenId.find_by_url(identity_url)
|
|
|
|
|
|
|
|
if !user_open_id && @opts[:trusted] && user = User.find_by_email(email)
|
|
|
|
user_open_id = UserOpenId.create(url: identity_url , user_id: user.id, email: email, active: true)
|
|
|
|
end
|
|
|
|
|
|
|
|
result.user = user_open_id.try(:user)
|
|
|
|
result.extra_data = {
|
2013-08-23 03:00:01 -04:00
|
|
|
openid_url: identity_url,
|
|
|
|
# note email may change by the time after_create_account runs
|
|
|
|
email: email
|
2013-08-23 02:20:43 -04:00
|
|
|
}
|
2014-03-25 23:52:50 -04:00
|
|
|
|
2013-08-23 02:20:43 -04:00
|
|
|
result.email_valid = @opts[:trusted]
|
|
|
|
|
|
|
|
result
|
|
|
|
end
|
2013-08-23 03:00:01 -04:00
|
|
|
|
|
|
|
def after_create_account(user, auth)
|
|
|
|
data = auth[:extra_data]
|
|
|
|
UserOpenId.create(
|
|
|
|
user_id: user.id,
|
|
|
|
url: data[:openid_url],
|
|
|
|
email: data[:email],
|
|
|
|
active: true
|
|
|
|
)
|
|
|
|
end
|
2013-08-25 21:04:16 -04:00
|
|
|
|
|
|
|
|
|
|
|
def register_middleware(omniauth)
|
|
|
|
omniauth.provider :open_id,
|
2013-08-27 00:44:06 -04:00
|
|
|
:setup => lambda { |env|
|
|
|
|
strategy = env["omniauth.strategy"]
|
|
|
|
strategy.options[:store] = OpenID::Store::Redis.new($redis)
|
|
|
|
},
|
2013-08-25 21:04:16 -04:00
|
|
|
:name => name,
|
|
|
|
:identifier => identifier,
|
|
|
|
:require => "omniauth-openid"
|
|
|
|
end
|
2013-08-23 02:20:43 -04:00
|
|
|
end
|