<commit_before>local vouchera = require('voucher.vouchera')
local utils = require('voucher.utils')

handlers = {}

local TESTING_URL_INFO = '/info'
local TESTING_URL_FAIL = '/fail'
local TESTING_URL_AUTHENTICATED = '/authenticated'


function handlers.preactivate_voucher()
    local uci_cursor = require('uci').cursor()

    --! using defaults for testing as uci environment is not available
    local url_authenticated = uci_cursor:get("pirania", "base_config", "url_authenticated") or TESTING_URL_AUTHENTICATED
    local url_fail = uci_cursor:get("pirania", "base_config", "url_fail") or TESTING_URL_FAIL
    local url_info = uci_cursor:get("pirania", "base_config", "url_info") or TESTING_URL_INFO

    vouchera.init()

    local url

    local client_data = utils.getIpv4AndMac(os.getenv('REMOTE_ADDR'))
    local client_is_authorized = vouchera.is_mac_authorized(client_data.mac)

    if client_is_authorized then
        url = url_authenticated
    else
        local output
        local params = utils.urldecode_params(os.getenv("QUERY_STRING"))
        local code = params['voucher']
        local prevUrl = params['prev']
        --! if client does not have javascript then activate right away without going to the INFO portal
        if params['nojs'] == 'true' then
            if vouchera.activate(code, client_data.mac) then
                url = url_authenticated
            else
                url = url_fail
            end
        else
            local setParams = prevUrl and '?voucher=' .. code .. '&prev=' .. prevUrl or '?voucher=' .. code
            --! redirect to the INFO portal for some seconds with the url params already set to activate
            --! the voucher after this time
            if vouchera.is_activable(code) then
                url = url_info .. setParams
            else
                url = url_fail
            end
        end
    end
    return url
end

function handlers.activate_voucher()
    local uci_cursor = require('uci').cursor()

    local url_authenticated = uci_cursor:get("pirania", "base_config", "url_authenticated") or TESTING_URL_AUTHENTICATED
    local url_fail = uci_cursor:get("pirania", "base_config", "url_fail") or TESTING_URL_FAIL

    vouchera.init()
    local client_data = utils.getIpv4AndMac(os.getenv('REMOTE_ADDR'))
    local client_is_authorized = vouchera.is_mac_authorized(client_data.mac)

    local params = utils.urldecode_params(os.getenv("QUERY_STRING"))
    local code = params['voucher']
    local prevUrl = params['prev']

    if client_is_authorized then
        return url_authenticated
    end

    local url = url_fail

    if code and client_data.mac then
        if vouchera.activate(code, client_data.mac) then
            if prevUrl ~= nil then
                url = prevUrl
            else
                url = url_authenticated
            end
        end
    end
    return url
end

return handlers<commit_msg>pirania: maintain previous url on fail<commit_after>local vouchera = require('voucher.vouchera')
local utils = require('voucher.utils')

handlers = {}

local TESTING_URL_INFO = '/info'
local TESTING_URL_FAIL = '/fail'
local TESTING_URL_AUTHENTICATED = '/authenticated'


function handlers.preactivate_voucher()
    local uci_cursor = require('uci').cursor()

    --! using defaults for testing as uci environment is not available
    local url_authenticated = uci_cursor:get("pirania", "base_config", "url_authenticated") or TESTING_URL_AUTHENTICATED
    local url_fail = uci_cursor:get("pirania", "base_config", "url_fail") or TESTING_URL_FAIL
    local url_info = uci_cursor:get("pirania", "base_config", "url_info") or TESTING_URL_INFO

    vouchera.init()

    local url

    local client_data = utils.getIpv4AndMac(os.getenv('REMOTE_ADDR'))
    local client_is_authorized = vouchera.is_mac_authorized(client_data.mac)

    if client_is_authorized then
        url = url_authenticated
    else
        local output
        local params = utils.urldecode_params(os.getenv("QUERY_STRING"))
        local code = params['voucher']
        local prevUrl = params['prev']
        --! if client does not have javascript then activate right away without going to the INFO portal
        if params['nojs'] == 'true' then
            if vouchera.activate(code, client_data.mac) then
                url = url_authenticated
            else
                url = url_fail
            end
        else
            local setParams = prevUrl and '?voucher=' .. code .. '&prev=' .. prevUrl or '?voucher=' .. code
            --! redirect to the INFO portal for some seconds with the url params already set to activate
            --! the voucher after this time
            if vouchera.is_activable(code) then
                url = url_info .. setParams
            else
                url = url_fail .. (prevUrl and '&prev=' .. prevUrl or '')
            end
        end
    end
    return url
end

function handlers.activate_voucher()
    local uci_cursor = require('uci').cursor()

    local url_authenticated = uci_cursor:get("pirania", "base_config", "url_authenticated") or TESTING_URL_AUTHENTICATED
    local url_fail = uci_cursor:get("pirania", "base_config", "url_fail") or TESTING_URL_FAIL

    vouchera.init()
    local client_data = utils.getIpv4AndMac(os.getenv('REMOTE_ADDR'))
    local client_is_authorized = vouchera.is_mac_authorized(client_data.mac)

    local params = utils.urldecode_params(os.getenv("QUERY_STRING"))
    local code = params['voucher']
    local prevUrl = params['prev']

    if client_is_authorized then
        return url_authenticated
    end

    local url = url_fail .. (prevUrl and '&prev=' .. prevUrl or '')

    if code and client_data.mac then
        if vouchera.activate(code, client_data.mac) then
            if prevUrl ~= nil then
                url = prevUrl
            else
                url = url_authenticated
            end
        end
    end
    return url
end

return handlers
{'fraction_non_alphanumeric': 0.08858631702807195, 'fraction_numerical': 0.0006723819129265423, 'mean_word_length': 2.737437185929648, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '3748115', 'n_tokens_mistral': 1831, 'n_tokens_neox': 1763, 'n_words': 466}