---
--- Generated by EmmyLua(https://github.com/EmmyLua)
--- Created by hartnett.
--- DateTime: 2019/11/8 16:37
---


local core = require("apisix.core")

-- 从http basic auth 字符串中获取username
local function get_username_from_basic_auth(basic_auth_str)
    local username = ""
    local basic_auth = basic_auth_str or ""
    local auth = string.gsub(basic_auth, "Basic ", "")
    local user_pass = ngx.decode_base64(auth)
    local table_user = core.strings.split(user_pass, ":")
    if #table_user > 0 then
        username = table_user[1]
    end

    return username
end

-- 从域账户中获取用户名
local function get_username_from_ad_account(ad_account_string, ad_domain_name)
    local ad_account = ad_account_string or ""
    local username = ""
    local email = string.gsub(string.lower(ad_account), ad_domain_name .. "\\", "")
    local email_part = core.strings.split(email, "@")
    if #email_part > 0 then
        username = email_part[1]
    end
    return username
end

local _M = {
    get_username_from_basic_auth = get_username_from_basic_auth,
    get_username_from_ad_account = get_username_from_ad_account,
}

return _M
