module JunglePath::API::Helpers::AuthOld

Public Instance Methods

authenticate(no_cache=false) click to toggle source
# File lib/jungle_path/api/helpers/auth_old.rb, line 5
def authenticate no_cache=false
        user_name = request.env['REMOTE_USER']
        password = request.env['REMOTE_PASSWORD']
        valid, authentication_messages = basic_authentication(user_name, password, no_cache)
        unless valid
                valid, authentication_messages = basic_authentication(user_name, password, true)
                halt 401, authentication_messages.join("\n") unless valid
        end
        request.body.rewind
end
authenticate_key(key_string, no_cache=false) click to toggle source
# File lib/jungle_path/api/helpers/auth_old.rb, line 59
def authenticate_key key_string, no_cache=false
        key = get_key(key_string, no_cache)
        set_current_key(key)
        valid = (key and key.valid?)
end
authenticate_user(user_name, password, no_cache=false) click to toggle source
# File lib/jungle_path/api/helpers/auth_old.rb, line 65
def authenticate_user user_name, password, no_cache=false
        user = get_user(user_name, password, no_cache)
        set_current_user(user)
        valid = (user and user.is_valid)
end
basic_authentication(user_name, password, no_cache=false) click to toggle source
# File lib/jungle_path/api/helpers/auth_old.rb, line 16
def basic_authentication user_name, password, no_cache=false
        authentication_messages = []
        if user_name and user_name.start_with?("sk_")
                valid = authenticate_key(user_name, no_cache)
                if valid
                        authentication_messages << "key is valid: #{current_key.to_h}."
                        user = get_user_from_key(current_key, no_cache)
                        unless user
                                authentication_messages << "User not found for current_key."
                                valid = false
                        end
                        set_current_user user
                else
                        authentication_messages << "key #{user_name} is not valid."
                        set_current_user nil
                end
        else
                valid = authenticate_user(user_name, password, no_cache)
                if valid
                        authentication_messages << "User is valid: #{current_user.to_h}."
                        key = get_default_key(current_user.id, no_cache)
                        unless key
                                authentication_messages << "Default key not found for current_user."
                                valid = false
                        end
                        set_current_key(key)
                else
                        authentication_messages << "User #{user_name} is not valid."
                        set_current_key( nil)
                end
        end
        messages = authentication_messages.join("\n    ")
        if valid
                roles = get_roles(no_cache)
                set_current_roles roles
                set_current_auth
                set_current_query_filters(no_cache)
        else
                set_current_roles nil
        end
        [valid, authentication_messages]
end
current_auth() click to toggle source
# File lib/jungle_path/api/helpers/auth_old.rb, line 222
def current_auth
        @current_auth
end
current_key() click to toggle source
# File lib/jungle_path/api/helpers/auth_old.rb, line 210
def current_key
        @current_key
end
current_query_filters() click to toggle source
# File lib/jungle_path/api/helpers/auth_old.rb, line 226
def current_query_filters
        @current_query_filters
end
current_role() click to toggle source
# File lib/jungle_path/api/helpers/auth_old.rb, line 218
def current_role
        @current_role
end
current_roles() click to toggle source
# File lib/jungle_path/api/helpers/auth_old.rb, line 214
def current_roles
        @current_roles
end
current_user() click to toggle source
# File lib/jungle_path/api/helpers/auth_old.rb, line 206
def current_user
        @current_user
end
get_any_user(user_name, password, no_cache=false) click to toggle source
# File lib/jungle_path/api/helpers/auth_old.rb, line 98
def get_any_user user_name, password, no_cache=false
        cache_key = "#{user_name}.#{password}"
        user = cache.get(cache_key)
        puts "user: #{user}."
        if user == nil or no_cache
                hash = SQL::AnyUser.by_user_name(db, user_name)
                puts "hash: #{hash}."
                user = Schema::User.new(hash, false) if hash
                user.is_valid = valid_user?(user, password) if user
                cache.set cache_key, user if user
        end
        user
end
get_default_key(user_id, no_cache=false) click to toggle source
# File lib/jungle_path/api/helpers/auth_old.rb, line 128
def get_default_key user_id, no_cache=false
        cache_key = "#{user_id}.key"
        key = cache.get(cache_key)
        if key == nil or no_cache
                array = SQL::Key.default_by_user_id(db, user_id)
                hash = array.first if array
                key = Schema::Key.new(hash) if hash
                cache.set cache_key, key if key
        end
        if key
                puts "default key.key: #{key.key}."
        else
                puts "A default key was not found for user ID: #{user_id}."
        end
        key
end
get_key(key_string, no_cache=false) click to toggle source
# File lib/jungle_path/api/helpers/auth_old.rb, line 116
def get_key key_string, no_cache=false
        key = cache.get(key_string)
        if key == nil or no_cache
                array = SQL::Key.by_key(db, key_string)
                hash = array.first if array
                key = Schema::Key.new(hash) if hash
                cache.set key_string, key if key
        end
        puts "key.key: #{key.key}."
        key
end
get_query_filters(no_cache=false) click to toggle source
# File lib/jungle_path/api/helpers/auth_old.rb, line 156
def get_query_filters no_cache=false
        # get filters from query_filter table.
        return nil unless current_key
        cache_key = "#{current_key.id}_query_filters"
        query_filters = cache.get(cache_key)
        if query_filters == nil or no_cache
                query_filters = []
                filters = SQL::QueryFilter.by_key(db, current_key)
                filters.each do |filter|
                        query_filters << Query::Filter.new(filter[:base_table_name], filter[:sub_select])
                end
                if app_defined_query_filters
                        app_defined_query_filters.each do |filter|
                                query_filters << filter
                        end
                end
                cache.set cache_key, query_filters
        end
        query_filters
end
get_roles(no_cache=false) click to toggle source
# File lib/jungle_path/api/helpers/auth_old.rb, line 145
def get_roles no_cache=false
        return nil unless current_key
        cache_key = "#{current_key.id}_roles"
        roles = cache.get(cache_key)
        if roles == nil or no_cache
                roles = SQL::Role.by_key(db, current_key)
                cache.set cache_key, roles if roles
        end
        roles
end
get_user(user_name, password, no_cache=false) click to toggle source
# File lib/jungle_path/api/helpers/auth_old.rb, line 71
def get_user user_name, password, no_cache=false
        cache_key = "#{user_name}.#{password}"
        user = cache.get(cache_key)
        #puts "user: #{user}."
        if user == nil or no_cache
                hash = SQL::User.by_user_name(db, user_name)
                puts "hash: #{hash}."
                user = Schema::User.new(hash, false) if hash
                user.is_valid = valid_user?(user, password) if user
                cache.set cache_key, user if user
        end
        user
end
get_user_from_key(key, no_cache=false) click to toggle source
# File lib/jungle_path/api/helpers/auth_old.rb, line 85
def get_user_from_key key, no_cache=false
        user = nil
        if key
                cache_key = "user_by_key_#{key.key}"
                user = cache.get(cache_key)
                if user == nil or no_cache
                        user = Controller::User.new(current_user, current_key, {id: key.user_id}, db).select
                        cache.set cache_key, user if user
                end
        end
        user
end
set_current_auth() click to toggle source
# File lib/jungle_path/api/helpers/auth_old.rb, line 196
def set_current_auth
        @current_auth = ::Authorization::Filter.new current_roles, configatron.application.role_permissions, configatron.application.role_restrictions
        @current_user.auth = @current_auth
end
set_current_key(value) click to toggle source
# File lib/jungle_path/api/helpers/auth_old.rb, line 181
def set_current_key(value)
        @current_key = value
end
set_current_query_filters(no_cache=false) click to toggle source
# File lib/jungle_path/api/helpers/auth_old.rb, line 201
def set_current_query_filters no_cache=false
        @current_query_filters = get_query_filters(no_cache)
        @current_user.query_filters = @current_query_filters
end
set_current_roles(roles) click to toggle source
# File lib/jungle_path/api/helpers/auth_old.rb, line 185
def set_current_roles roles
        @current_role = nil
        @current_roles = roles
        if roles
                roles.each do |role|
                        @current_role = role[:name]
                        break
                end
        end
end
set_current_user(user) click to toggle source
# File lib/jungle_path/api/helpers/auth_old.rb, line 177
def set_current_user(user)
        @current_user = user
end
valid_user?(user, password) click to toggle source
# File lib/jungle_path/api/helpers/auth_old.rb, line 112
def valid_user? user, password
        valid = (user and PasswordHash.validatePassword(password, user.hash))
end