class Lita::Adapters::Discord::TokenCache

Public Class Methods

new() click to toggle source
# File lib/lita/adapters/discord/token_cache.rb, line 81
def initialize
        if File.file? CACHE_PATH
                @data = JSON.parse(File.read(CACHE_PATH))
        else
                Lita.logger.debug("Cache file #{CACHE_PATH} not found. Using empty cache")
                @data = {}
        end
rescue => e
        Lita.logger.debug('Exception occurred while parsing token cache file:')
        # I guess start here if stuff is super broken
        Lita.logger.debug('Continuing with empty cache')
        @data = {}
end

Public Instance Methods

store_token(email, password, token) click to toggle source
# File lib/lita/adapters/discord/token_cache.rb, line 124
def store_token(email, password, token)
        cached = CachedToken.new
        cached.generate_verify_hash(password)
        cached.encrypt_token(password, token)
        @data[email] = cached.data
        write_cache
end
token(email, password) click to toggle source
# File lib/lita/adapters/discord/token_cache.rb, line 95
def token(email, password)
        if @data[email]
                begin
                        cached = CachedToken.new(@data[email])
                        if cached.verify_password(password)
                                token = cached.decrypt_token(password)
                                if token
                                        begin
                                                cached.test_token
                                                token
                                        rescue => e
                                                fail_token('Token cached, verified and decrypted, but rejected by Discord', email)
                                                sleep 1
                                                nil
                                        end
                                else
                                        fail_token('Token cached and verified, but decryption failed', email)
                                end
                        else
                                fail_token('Token verification failed', email)
                        end
                rescue => e
                        fail_token('Token cached but invalid', email)
                end
        else
                fail_token('Token not cached at all')
        end
end
write_cache() click to toggle source
# File lib/lita/adapters/discord/token_cache.rb, line 132
def write_cache
        File.write(CACHE_PATH, @data.to_json)
end

Private Instance Methods

fail_token(msg, email = nil) click to toggle source
# File lib/lita/adapters/discord/token_cache.rb, line 138
def fail_token(msg, email = nil)
        Lita.logger.warn("Token not retrieved from cache - #{msg}")
        @data.delete(email) if email
        nil
end