Created
July 21, 2024 18:22
-
-
Save vol1ura/dd9e7424a94961e747c3b663e3ad08e7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# https://core.telegram.org/widgets/login | |
require 'openssl' | |
require 'uri' | |
BOT_API_KEY = '1234567:asdfasdf' | |
SECRET_KEY = OpenSSL::Digest::SHA256.digest BOT_API_KEY | |
HASH_EXPIRATION_TIME = 60 * 60 | |
def validate_auth(params_string) | |
params = URI.decode_www_form(params_string).to_h | |
return false if Time.at(params['auth_date'].to_i) < Time.now - HASH_EXPIRATION_TIME | |
hash = params.delete 'hash' | |
data_check_string = params.map { |k, v| "#{k}=#{v}" }.sort.join("\n") | |
eval_hash = OpenSSL::HMAC.hexdigest 'sha256', SECRET_KEY, data_check_string | |
eval_hash == hash | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment