<commit_before>--------------------------------------------------------------------------------
-- access.lua, v0.2: controls access of sailor apps
-- This file is a part of Sailor project
-- Copyright (c) 2014 <NAME> <<EMAIL>>
-- License: MIT
-- http://sailorproject.org
--------------------------------------------------------------------------------

local session = require "sailor.session"
local access = {}

session.open(sailor.r)

-- Uncomment to login with "demo" / "demo"
-- Comment to login through db (user model)
local default = "demo"
local default_pass = "<PASSWORD>"

local INVALID = "Invalid username or password."


function access.is_guest()
	return not session.data.username
end

function access.grant(data,time)
	session.setsessiontimeout (time or 604800) -- 1 week
	if not data.username then return false end
	return session.save(data)
end

function access.login(username,password)
	local id
	if not default then
		local User = sailor.model("user")
		local u = User:find_by_attributes({username=username,password=password})
		if not u then
			return false, INVALID
		end
		id = u.id
	else
		if username ~= default or password ~= <PASSWORD> then
			return false, INVALID
		end
		id = 1
	end
	access.grant({username=username,id=id})	
	return true
end

function access.logout()
	session.destroy(sailor.r)<commit_msg>Add a hashing algorithm for passwords<commit_after>--------------------------------------------------------------------------------
-- access.lua, v0.2: controls access of sailor apps
-- This file is a part of Sailor project
-- Copyright (c) 2014 <NAME> <<EMAIL>>
-- License: MIT
-- http://sailorproject.org
--------------------------------------------------------------------------------

local session = require "sailor.session"
local access = {}

session.open(sailor.r)

-- Uncomment to login with "demo" / "demo"
-- Comment to login through db (user model)
local default = "<PASSWORD>"
local default_pass = "<PASSWORD>"
local salt = "<PASSWORD>" -- uncomment to use unhashed passwords

local INVALID = "Invalid username or password."

-- Simple hashing algorithm for encrypting passworsd
function access.hash(username, password)
	local hash = username .. password
	-- Check if bcrypt is embedded
	if sailor.r.htpassword then
		return sailor.r:htpassword(hash, 2, 100) -- Use bcrypt on pwd
		
	-- If not, fall back to sha1 hashing
	else
		if salt and sailor.r.sha1 then
			for i = 1, 500 do
				hash = sailor.r:sha1(salt .. hash)
			end
		end
	end
	return hash
end


function access.is_guest()
	return not session.data.username
end

function access.grant(data,time)
	session.setsessiontimeout (time or 604800) -- 1 week
	if not data.username then return false end
	return session.save(data)
end

function access.login(username,password)
	local id
	if not default then
		local User = sailor.model("user")
		local u = User:find_by_attributes{
			username=username,
			password=access.hash(username, password)
			}
		if not u then
			return false, INVALID
		end
		id = u.id
	else
		if username ~= default or password ~= <PASSWORD> then
			return false, INVALID
		end
		id = 1
	end
	access.grant({username=username,id=id})	
	return true
end

function access.logout()
	session.destroy(sailor.r)
{'fraction_non_alphanumeric': 0.1921884686918785, 'fraction_numerical': 0.01208927464352139, 'mean_word_length': 2.496208017334778, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5410214', 'n_tokens_mistral': 1020, 'n_tokens_neox': 929, 'n_words': 323}