2019-05-03 06:17:27 +08:00
# frozen_string_literal: true
2013-08-23 14:20:43 +08:00
class Auth :: OAuth2Authenticator < Auth :: Authenticator
def name
@name
end
# only option at the moment is :trusted
def initialize ( name , opts = { } )
2022-01-07 00:50:18 +08:00
Discourse . deprecate ( " OAuth2Authenticator is deprecated. Use `ManagedAuthenticator` and `UserAssociatedAccount` instead. For more information, see https://meta.discourse.org/t/106695 " , drop_from : '2.9.0' , output_in_test : true )
2013-08-23 14:20:43 +08:00
@name = name
@opts = opts
end
def after_authenticate ( auth_token )
result = Auth :: Result . new
oauth2_provider = auth_token [ :provider ]
oauth2_uid = auth_token [ :uid ]
data = auth_token [ :info ]
result . email = email = data [ :email ]
result . name = name = data [ :name ]
2014-05-06 21:41:59 +08:00
oauth2_user_info = Oauth2UserInfo . find_by ( uid : oauth2_uid , provider : oauth2_provider )
2013-08-23 14:20:43 +08:00
if ! oauth2_user_info && @opts [ :trusted ] && user = User . find_by_email ( email )
oauth2_user_info = Oauth2UserInfo . create ( uid : oauth2_uid ,
provider : oauth2_provider ,
name : name ,
email : email ,
user : user )
end
result . user = oauth2_user_info . try ( :user )
result . email_valid = @opts [ :trusted ]
result . extra_data = {
uid : oauth2_uid ,
provider : oauth2_provider
}
result
end
def after_create_account ( user , auth )
data = auth [ :extra_data ]
2021-12-11 03:53:14 +08:00
association = Oauth2UserInfo . find_or_initialize_by ( provider : data [ :provider ] , uid : data [ :uid ] )
association . user = user
association . email = auth [ :email ]
association . save!
2013-08-23 14:20:43 +08:00
end
2018-07-23 23:51:57 +08:00
def description_for_user ( user )
info = Oauth2UserInfo . find_by ( user_id : user . id , provider : @name )
info & . email || info & . name || info & . uid || " "
end
2013-08-23 14:20:43 +08:00
end