forked from midou/invidious
Add option for user to delete their account
This commit is contained in:
@@ -389,3 +389,51 @@ def extract_items(nodeset, ucid = nil)
|
||||
|
||||
return items
|
||||
end
|
||||
|
||||
def create_response(user_id, operation, key)
|
||||
nonce = Random::Secure.hex(4)
|
||||
expire = Time.now + 6.hours
|
||||
|
||||
challenge = "#{expire.to_unix}-#{nonce}-#{user_id}-#{operation}"
|
||||
token = OpenSSL::HMAC.digest(:sha256, key, challenge)
|
||||
|
||||
challenge = Base64.urlsafe_encode(challenge)
|
||||
token = Base64.urlsafe_encode(token)
|
||||
|
||||
return challenge, token
|
||||
end
|
||||
|
||||
def validate_response(challenge, token, action, key)
|
||||
if !challenge
|
||||
raise "Hidden field \"challenge\" is a required field"
|
||||
end
|
||||
|
||||
if !token
|
||||
raise "Hidden field \"token\" is a required field"
|
||||
end
|
||||
|
||||
challenge = Base64.decode_string(challenge)
|
||||
if challenge.split("-").size == 4
|
||||
expire, nonce, user_id, operation = challenge.split("-")
|
||||
|
||||
expire = expire.to_i?
|
||||
expire ||= 0
|
||||
else
|
||||
raise "Invalid challenge"
|
||||
end
|
||||
|
||||
challenge = OpenSSL::HMAC.digest(:sha256, HMAC_KEY, challenge)
|
||||
challenge = Base64.urlsafe_encode(challenge)
|
||||
|
||||
if challenge != token
|
||||
raise "Invalid token"
|
||||
end
|
||||
|
||||
if operation != action
|
||||
raise "Invalid token"
|
||||
end
|
||||
|
||||
if expire < Time.now.to_unix
|
||||
raise "Token is expired, please try again"
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user