class Lita::Adapters::Discord::CachedToken

Public Class Methods

new(data = nil) click to toggle source
# File lib/lita/adapters/discord/token_cache.rb, line 12
def initialize(data = nil)
        if data
                @verify_salt = Base64.decode64(data['verify_salt'])
                @password_hash = Base64.decode64(data['password_hash'])
                @encrypt_salt = Base64.decode64(data['encrypt_salt'])
                @iv = Base64.decode64(data['iv'])
                @encrypted_token = Base64.decode64(data['encrypted_token'])
        else
                generate_salts
        end
end

Public Instance Methods

data() click to toggle source

@return [Hash<Symbol => String>] the data representing the token and encryption data, all encrypted and base64-encoded

# File lib/lita/adapters/discord/token_cache.rb, line 25
def data
            {
                    verify_salt: Base64.encode64(@verify_salt),
                    password_hash: Base64.encode64(@password_hash),
                    encrypt_salt: Base64.encode64(@encrypt_salt),
                    iv: Base64.encode64(@iv),
                    encrypted_token: Base64.encode64(@encrypted_token)
            }
    end
decrypt_token(password) click to toggle source
# File lib/lita/adapters/discord/token_cache.rb, line 52
def decrypt_token(password)
        key = obtain_key(password)
        decipher = OpenSSL::Cipher::AES256.new(:CBC)
        decipher.decrypt
        decipher.key = key
        decipher.iv = @iv
        decipher.update(@encrypted_token) + decipher.final
end
encrypt_token(password, token) click to toggle source
# File lib/lita/adapters/discord/token_cache.rb, line 61
def encrypt_token(password, token)
        key = obtain_key(password)
        cipher = OpenSSL::Cipher::AES256.new(:CBC)
        cipher.encrypt
        cipher.key = key
        @iv = cipher.random_iv
        @encrypted_token = cipher.update(token) + cipher.final
end
generate_salts() click to toggle source
# File lib/lita/adapters/discord/token_cache.rb, line 47
def generate_salts
        @verify_salt = OpenSSL::Random.random_bytes(KEYLEN)
        @encrypt_salt = OpenSSL::Random.random_bytes(KEYLEN)
end
generate_verify_hash(password) click to toggle source
# File lib/lita/adapters/discord/token_cache.rb, line 39
def generate_verify_hash(password)
        @password_hash = hash_password(password)
end
hash_password(password) click to toggle source
# File lib/lita/adapters/discord/token_cache.rb, line 74
def hash_password(password)
        OpenSSL::PKCS5.pbkdf2_hmac_sha1(password, @verify_salt, 300_000, KEYLEN)
end
obtain_key(password) click to toggle source
# File lib/lita/adapters/discord/token_cache.rb, line 43
def obtain_key(password)
        @key = OpenSSL::PKCS5.pbkdf2_hmac_sha1(password, @encrypt_salt, 300_000, KEYLEN)
end
test_token(token) click to toggle source
# File lib/lita/adapters/discord/token_cache.rb, line 70
def test_token(token)
        API.validate_token(token)
end
verify_password(password) click to toggle source
# File lib/lita/adapters/discord/token_cache.rb, line 35
def verify_password(password)
        hash_password(password) == @password_hash
end