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


local core = require("apisix.core")
local exchange_mobile = require("apisix.plugins.exchange-mobile.exchange-mobile")

local plugin_name = "exchange-mobile"

local schema = {
    type = "object",
    properties = {
        debug = { type = "boolean",
                  enum = { true, false },
        },
        ad_domain_name = {
            type = "string"
        }
    },
    required = { "ad_domain_name" }
}

local _M = {
    version = 0.1,
    priority = 2002,
    name = plugin_name,
    schema = schema,
}

function _M.check_schema(conf)
    local ok, err = core.schema.check(schema, conf)

    if not ok then
        return false, err
    end

    return true
end

-- rewrite阶段，移动设备的可信认证与注册、激活逻辑
function _M.rewrite(conf, ctx)
    core.log.warn("plugin rewrite phase, conf: ", core.json.encode(conf))
    exchange_mobile.mobile(ctx, conf.ad_domain_name)
end

-- body_filter阶段，在可信设备没有认证成功前，过滤掉敏感指令
function _M.body_filter(conf, ctx)
    exchange_mobile.filter_response()
end

return _M