lib/omniauth/strategies/telegram.rb in omniauth-telegram-0.1.0 vs lib/omniauth/strategies/telegram.rb in omniauth-telegram-0.2.0

- old
+ new

@@ -43,23 +43,15 @@ Rack::Response.new(html, 200, 'content-type' => 'text/html').finish end def callback_phase - unless FIELDS.all? { |f| request.params.include?(f) } - fail!(:field_missing) + if error = check_errors + fail!(error) + else + super end - - unless check_signature - fail!(:signature_mismatch) - end - - if Time.now.to_i - request.params["auth_date"].to_i > 86400 - fail!(:session_expired) - end - - super end uid do request.params["id"] end @@ -79,16 +71,30 @@ auth_date: Time.at(request.params["auth_date"].to_i) } end private + + def check_errors + return :field_missing unless check_fields + return :signature_mismatch unless check_signature + return :session_expired unless check_session + end + + def check_fields + FIELDS.all? { |f| request.params.include?(f) } + end def check_signature secret = OpenSSL::Digest::SHA256.digest(options[:bot_secret]) signature = HASH_FIELDS.map { |f| "%s=%s" % [f, request.params[f]] }.join("\n") hashed_signature = OpenSSL::HMAC.hexdigest(OpenSSL::Digest::SHA256.new, secret, signature) request.params["hash"] == hashed_signature + end + + def check_session + Time.now.to_i - request.params["auth_date"].to_i <= 86400 end end end end