2014-02-25 11:30:49 +08:00
class SingleSignOn
2015-05-20 00:16:02 +08:00
ACCESSORS = [ :nonce , :name , :username , :email , :avatar_url , :avatar_force_update , :require_activation ,
2017-02-01 08:42:27 +08:00
:bio , :external_id , :return_sso_url , :admin , :moderator , :suppress_welcome_message , :title ,
2016-11-11 13:57:31 +08:00
:add_groups , :remove_groups ]
2014-02-25 11:30:49 +08:00
FIXNUMS = [ ]
2015-05-21 21:41:36 +08:00
BOOLS = [ :avatar_force_update , :admin , :moderator , :require_activation , :suppress_welcome_message ]
2014-02-25 11:30:49 +08:00
NONCE_EXPIRY_TIME = 10 . minutes
attr_accessor ( * ACCESSORS )
attr_accessor :sso_secret , :sso_url
def self . sso_secret
raise RuntimeError , " sso_secret not implemented on class, be sure to set it on instance "
end
def self . sso_url
raise RuntimeError , " sso_url not implemented on class, be sure to set it on instance "
end
def self . parse ( payload , sso_secret = nil )
sso = new
sso . sso_secret = sso_secret if sso_secret
parsed = Rack :: Utils . parse_query ( payload )
if sso . sign ( parsed [ " sso " ] ) != parsed [ " sig " ]
2014-12-30 06:23:21 +08:00
diags = " \n \n sso: #{ parsed [ " sso " ] } \n \n sig: #{ parsed [ " sig " ] } \n \n expected sig: #{ sso . sign ( parsed [ " sso " ] ) } "
2014-12-30 06:28:44 +08:00
if parsed [ " sso " ] =~ / [^a-zA-Z0-9= \ r \ n \/ +] /m
2014-12-30 07:45:33 +08:00
raise RuntimeError , " The SSO field should be Base64 encoded, using only A-Z, a-z, 0-9, +, /, and = characters. Your input contains characters we don't understand as Base64, see http://en.wikipedia.org/wiki/Base64 #{ diags } "
2014-12-30 06:23:21 +08:00
else
raise RuntimeError , " Bad signature for payload #{ diags } "
end
2014-02-25 11:30:49 +08:00
end
decoded = Base64 . decode64 ( parsed [ " sso " ] )
decoded_hash = Rack :: Utils . parse_query ( decoded )
ACCESSORS . each do | k |
val = decoded_hash [ k . to_s ]
val = val . to_i if FIXNUMS . include? k
2014-11-27 09:39:00 +08:00
if BOOLS . include? k
val = [ " true " , " false " ] . include? ( val ) ? val == " true " : nil
end
2014-02-25 11:30:49 +08:00
sso . send ( " #{ k } = " , val )
end
2014-04-22 11:52:13 +08:00
2017-07-28 09:20:09 +08:00
decoded_hash . each do | k , v |
2017-03-27 22:21:38 +08:00
if field = k [ / ^custom \ .(.+)$ / , 1 ]
2014-04-22 11:52:13 +08:00
sso . custom_fields [ field ] = v
end
end
2014-02-25 11:30:49 +08:00
sso
end
2016-04-08 09:20:01 +08:00
def diagnostics
2017-03-27 22:21:38 +08:00
SingleSignOn :: ACCESSORS . map { | a | " #{ a } : #{ send ( a ) } " } . join ( " \n " )
2016-04-08 09:20:01 +08:00
end
2014-04-22 11:52:13 +08:00
def sso_secret
@sso_secret || self . class . sso_secret
end
def sso_url
@sso_url || self . class . sso_url
end
def custom_fields
@custom_fields || = { }
end
2014-02-25 11:30:49 +08:00
def sign ( payload )
2014-02-26 06:44:41 +08:00
OpenSSL :: HMAC . hexdigest ( " sha256 " , sso_secret , payload )
2014-02-25 11:30:49 +08:00
end
2017-07-28 09:20:09 +08:00
def to_url ( base_url = nil )
2014-03-20 05:14:09 +08:00
base = " #{ base_url || sso_url } "
" #{ base } #{ base . include? ( '?' ) ? '&' : '?' } #{ payload } "
2014-02-25 11:30:49 +08:00
end
def payload
payload = Base64 . encode64 ( unsigned_payload )
" sso= #{ CGI :: escape ( payload ) } &sig= #{ sign ( payload ) } "
end
def unsigned_payload
payload = { }
2017-03-27 22:21:38 +08:00
2014-02-25 11:30:49 +08:00
ACCESSORS . each do | k |
2017-07-28 09:20:09 +08:00
next if ( val = send k ) == nil
2014-02-25 11:30:49 +08:00
payload [ k ] = val
end
2017-03-27 22:21:38 +08:00
@custom_fields & . each do | k , v |
payload [ " custom. #{ k } " ] = v . to_s
2014-04-22 11:52:13 +08:00
end
2014-02-25 11:30:49 +08:00
Rack :: Utils . build_query ( payload )
end
end