--[[
Apache License
                           Version 2.0, January 2004
                        http://www.apache.org/licenses/

   TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION

   1. Definitions.

      "License" shall mean the terms and conditions for use, reproduction,
      and distribution as defined by Sections 1 through 9 of this document.

      "Licensor" shall mean the copyright owner or entity authorized by
      the copyright owner that is granting the License.

      "Legal Entity" shall mean the union of the acting entity and all
      other entities that control, are controlled by, or are under common
      control with that entity. For the purposes of this definition,
      "control" means (i) the power, direct or indirect, to cause the
      direction or management of such entity, whether by contract or
      otherwise, or (ii) ownership of fifty percent (50%) or more of the
      outstanding shares, or (iii) beneficial ownership of such entity.

      "You" (or "Your") shall mean an individual or Legal Entity
      exercising permissions granted by this License.

      "Source" form shall mean the preferred form for making modifications,
      including but not limited to software source code, documentation
      source, and configuration files.

      "Object" form shall mean any form resulting from mechanical
      transformation or translation of a Source form, including but
      not limited to compiled object code, generated documentation,
      and conversions to other media types.

      "Work" shall mean the work of authorship, whether in Source or
      Object form, made available under the License, as indicated by a
      copyright notice that is included in or attached to the work
      (an example is provided in the Appendix below).

      "Derivative Works" shall mean any work, whether in Source or Object
      form, that is based on (or derived from) the Work and for which the
      editorial revisions, annotations, elaborations, or other modifications
      represent, as a whole, an original work of authorship. For the purposes
      of this License, Derivative Works shall not include works that remain
      separable from, or merely link (or bind by name) to the interfaces of,
      the Work and Derivative Works thereof.

      "Contribution" shall mean any work of authorship, including
      the original version of the Work and any modifications or additions
      to that Work or Derivative Works thereof, that is intentionally
      submitted to Licensor for inclusion in the Work by the copyright owner
      or by an individual or Legal Entity authorized to submit on behalf of
      the copyright owner. For the purposes of this definition, "submitted"
      means any form of electronic, verbal, or written communication sent
      to the Licensor or its representatives, including but not limited to
      communication on electronic mailing lists, source code control systems,
      and issue tracking systems that are managed by, or on behalf of, the
      Licensor for the purpose of discussing and improving the Work, but
      excluding communication that is conspicuously marked or otherwise
      designated in writing by the copyright owner as "Not a Contribution."

      "Contributor" shall mean Licensor and any individual or Legal Entity
      on behalf of whom a Contribution has been received by Licensor and
      subsequently incorporated within the Work.

   2. Grant of Copyright License. Subject to the terms and conditions of
      this License, each Contributor hereby grants to You a perpetual,
      worldwide, non-exclusive, no-charge, royalty-free, irrevocable
      copyright license to reproduce, prepare Derivative Works of,
      publicly display, publicly perform, sublicense, and distribute the
      Work and such Derivative Works in Source or Object form.

   3. Grant of Patent License. Subject to the terms and conditions of
      this License, each Contributor hereby grants to You a perpetual,
      worldwide, non-exclusive, no-charge, royalty-free, irrevocable
      (except as stated in this section) patent license to make, have made,
      use, offer to sell, sell, import, and otherwise transfer the Work,
      where such license applies only to those patent claims licensable
      by such Contributor that are necessarily infringed by their
      Contribution(s) alone or by combination of their Contribution(s)
      with the Work to which such Contribution(s) was submitted. If You
      institute patent litigation against any entity (including a
      cross-claim or counterclaim in a lawsuit) alleging that the Work
      or a Contribution incorporated within the Work constitutes direct
      or contributory patent infringement, then any patent licenses
      granted to You under this License for that Work shall terminate
      as of the date such litigation is filed.

   4. Redistribution. You may reproduce and distribute copies of the
      Work or Derivative Works thereof in any medium, with or without
      modifications, and in Source or Object form, provided that You
      meet the following conditions:

      (a) You must give any other recipients of the Work or
          Derivative Works a copy of this License; and

      (b) You must cause any modified files to carry prominent notices
          stating that You changed the files; and

      (c) You must retain, in the Source form of any Derivative Works
          that You distribute, all copyright, patent, trademark, and
          attribution notices from the Source form of the Work,
          excluding those notices that do not pertain to any part of
          the Derivative Works; and

      (d) If the Work includes a "NOTICE" text file as part of its
          distribution, then any Derivative Works that You distribute must
          include a readable copy of the attribution notices contained
          within such NOTICE file, excluding those notices that do not
          pertain to any part of the Derivative Works, in at least one
          of the following places: within a NOTICE text file distributed
          as part of the Derivative Works; within the Source form or
          documentation, if provided along with the Derivative Works; or,
          within a display generated by the Derivative Works, if and
          wherever such third-party notices normally appear. The contents
          of the NOTICE file are for informational purposes only and
          do not modify the License. You may add Your own attribution
          notices within Derivative Works that You distribute, alongside
          or as an addendum to the NOTICE text from the Work, provided
          that such additional attribution notices cannot be construed
          as modifying the License.

      You may add Your own copyright statement to Your modifications and
      may provide additional or different license terms and conditions
      for use, reproduction, or distribution of Your modifications, or
      for any such Derivative Works as a whole, provided Your use,
      reproduction, and distribution of the Work otherwise complies with
      the conditions stated in this License.

   5. Submission of Contributions. Unless You explicitly state otherwise,
      any Contribution intentionally submitted for inclusion in the Work
      by You to the Licensor shall be under the terms and conditions of
      this License, without any additional terms or conditions.
      Notwithstanding the above, nothing herein shall supersede or modify
      the terms of any separate license agreement you may have executed
      with Licensor regarding such Contributions.

   6. Trademarks. This License does not grant permission to use the trade
      names, trademarks, service marks, or product names of the Licensor,
      except as required for reasonable and customary use in describing the
      origin of the Work and reproducing the content of the NOTICE file.

   7. Disclaimer of Warranty. Unless required by applicable law or
      agreed to in writing, Licensor provides the Work (and each
      Contributor provides its Contributions) on an "AS IS" BASIS,
      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
      implied, including, without limitation, any warranties or conditions
      of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
      PARTICULAR PURPOSE. You are solely responsible for determining the
      appropriateness of using or redistributing the Work and assume any
      risks associated with Your exercise of permissions under this License.

   8. Limitation of Liability. In no event and under no legal theory,
      whether in tort (including negligence), contract, or otherwise,
      unless required by applicable law (such as deliberate and grossly
      negligent acts) or agreed to in writing, shall any Contributor be
      liable to You for damages, including any direct, indirect, special,
      incidental, or consequential damages of any character arising as a
      result of this License or out of the use or inability to use the
      Work (including but not limited to damages for loss of goodwill,
      work stoppage, computer failure or malfunction, or any and all
      other commercial damages or losses), even if such Contributor
      has been advised of the possibility of such damages.

   9. Accepting Warranty or Additional Liability. While redistributing
      the Work or Derivative Works thereof, You may choose to offer,
      and charge a fee for, acceptance of support, warranty, indemnity,
      or other liability obligations and/or rights consistent with this
      License. However, in accepting such obligations, You may act only
      on Your own behalf and on Your sole responsibility, not on behalf
      of any other Contributor, and only if You agree to indemnify,
      defend, and hold each Contributor harmless for any liability
      incurred by, or claims asserted against, such Contributor by reason
      of your accepting any such warranty or additional liability.

   END OF TERMS AND CONDITIONS

   APPENDIX: How to apply the Apache License to your work.

      To apply the Apache License to your work, attach the following
      boilerplate notice, with the fields enclosed by brackets "{}"
      replaced with your own identifying information. (Don't include
      the brackets!)  The text should be enclosed in the appropriate
      comment syntax for the file format. We also recommend that a
      file or class name and description of purpose be included on the
      same "printed page" as the copyright notice for easier
      identification within third-party archives.

   Copyright {yyyy} {name of copyright owner}

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
]]--
local _M = {_VERSION="0.1.5"}

--[[
  This file defines "validators" to be used in validating a spec.  A "validator" is simply a function with
  a signature that matches:

    function(val, claim, jwt_json)

  This function returns either true or false.  If a validator needs to give more information on why it failed,
  then it can also raise an error (which will be used in the "reason" part of the validated jwt_obj).  If a
  validator returns nil, then it is assumed to have passed (same as returning true) and that you just forgot
  to actually return a value.

  There is a special claim name of "__jwt" that can be used to validate the entire jwt_obj.

  "val" is the value being tested.  It may be nil if the claim doesn't exist in the jwt_obj.  If the function
  is being called for the "__jwt" claim, then "val" will contain a deep clone of the full jwt object.

  "claim" is the claim that is being tested.  It is passed in just in case a validator needs to do additional
  checks.  It will be the string "__jwt" if the validator is being called for the entire jwt_object.

  "jwt_json" is a json-encoded representation of the full object that is being tested.  It will never be nil,
  and can always be decoded using cjson.decode(jwt_json).
]]--


--[[
    A function which will define a validator.  It creates both "opt_" and required (non-"opt_")
    versions.  The function that is passed in is the *optional* version.
]]--
local function define_validator(name, fx)
  _M["opt_" .. name] = fx
  _M[name] = function(...) return _M.chain(_M.required(), fx(...)) end
end

-- Validation messages
local messages = {
  nil_validator = "Cannot create validator for nil %s.",
  wrong_type_validator = "Cannot create validator for non-%s %s.",
  empty_table_validator = "Cannot create validator for empty table %s.",
  wrong_table_type_validator = "Cannot create validator for non-%s table %s.",
  required_claim = "'%s' claim is required.",
  wrong_type_claim = "'%s' is malformed.  Expected to be a %s.",
  missing_claim = "Missing one of claims - [ %s ]."
}

-- Local function to make sure that a value is non-nil or raises an error
local function ensure_not_nil(v, e, ...)
  return v ~= nil and v or error(string.format(e, ...), 0)
end

-- Local function to make sure that a value is the given type
local function ensure_is_type(v, t, e, ...)
  return type(v) == t and v or error(string.format(e, ...), 0)
end

-- Local function to make sure that a value is a (non-empty) table
local function ensure_is_table(v, e, ...)
  ensure_is_type(v, "table", e, ...)
  return ensure_not_nil(next(v), e, ...)
end

-- Local function to make sure all entries in the table are the given type
local function ensure_is_table_type(v, t, e, ...)
  if v ~= nil then
    ensure_is_table(v, e, ...)
    for _,val in ipairs(v) do
      ensure_is_type(val, t, e, ...)
    end
  end
  return v
end

-- Local function to ensure that a number is non-negative (positive or 0)
local function ensure_is_non_negative(v, e, ...)
  if v ~= nil then
    ensure_is_type(v, "number", e, ...)
    if v >= 0 then
      return v
    else
      error(string.format(e, ...), 0)
    end
  end
end

-- A local function which returns simple equality
local function equality_function(val, check)
  return val == check
end

-- A local function which returns string match
local function string_match_function(val, pattern)
  return string.match(val, pattern) ~= nil
end

--[[
    A local function which returns truth on existence of check in vals.
    Adopted from auth0/nginx-jwt table_contains by @twistedstream
]]--
local function table_contains_function(vals, check)
    for _, val in pairs(vals) do
        if val == check then return true end
    end
    return false
end


-- A local function which returns numeric greater than comparison
local function greater_than_function(val, check)
  return val > check
end

-- A local function which returns numeric greater than or equal comparison
local function greater_than_or_equal_function(val, check)
  return val >= check
end

-- A local function which returns numeric less than comparison
local function less_than_function(val, check)
  return val < check
end

-- A local function which returns numeric less than or equal comparison
local function less_than_or_equal_function(val, check)
  return val <= check
end


--[[
    Returns a validator that chains the given functions together, one after
    another - as long as they keep passing their checks.
]]--
function _M.chain(...)
  local chain_functions = {...}
  for _, fx in ipairs(chain_functions) do
    ensure_is_type(fx, "function", messages.wrong_type_validator, "function", "chain_function")
  end

  return function(val, claim, jwt_json)
    for _, fx in ipairs(chain_functions) do
      if fx(val, claim, jwt_json) == false then
        return false
      end
    end
    return true
  end
end

--[[
    Returns a validator that returns false if a value doesn't exist.  If
    the value exists and a chain_function is specified, then the value of
        chain_function(val, claim, jwt_json)
    will be returned, otherwise, true will be returned.  This allows for
    specifying that a value is both required *and* it must match some
    additional check.  This function will be used in the "required_*" shortcut
    functions for simplification.
]]--
function _M.required(chain_function)
  if chain_function ~= nil then
    return _M.chain(_M.required(), chain_function)
  end

  return function(val, claim, jwt_json)
    ensure_not_nil(val, messages.required_claim, claim)
    return true
  end
end

--[[
    Returns a validator which errors with a message if *NONE* of the given claim
    keys exist.  It is expected that this function is used against a full jwt object.
    The claim_keys must be a non-empty table of strings.
]]--
function _M.require_one_of(claim_keys)
  ensure_not_nil(claim_keys, messages.nil_validator, "claim_keys")
  ensure_is_type(claim_keys, "table", messages.wrong_type_validator, "table", "claim_keys")
  ensure_is_table(claim_keys, messages.empty_table_validator, "claim_keys")
  ensure_is_table_type(claim_keys, "string", messages.wrong_table_type_validator, "string", "claim_keys")

  return function(val, claim, jwt_json)
    ensure_is_type(val, "table", messages.wrong_type_claim, claim, "table")
    ensure_is_type(val.payload, "table", messages.wrong_type_claim, claim .. ".payload", "table")

    for i, v in ipairs(claim_keys) do
      if val.payload[v] ~= nil then return true end
    end

    error(string.format(messages.missing_claim, table.concat(claim_keys, ", ")), 0)
  end
end

--[[
    Returns a validator that checks if the result of calling the given function for
    the tested value and the check value returns true.  The value of check_val and
    check_function cannot be nil.  The optional name is used for error messages and
    defaults to "check_value".  The optional check_type is used to make sure that
    the check type matches and defaults to type(check_val).  The first parameter
    passed to check_function will *never* be nil (check succeeds if value is nil).
    Use the required version to fail on nil.  If the check_function raises an
    error, that will be appended to the error message.
]]--
define_validator("check", function(check_val, check_function, name, check_type)
  name = name or "check_val"
  ensure_not_nil(check_val, messages.nil_validator, name)

  ensure_not_nil(check_function, messages.nil_validator, "check_function")
  ensure_is_type(check_function, "function", messages.wrong_type_validator, "function", "check_function")

  check_type = check_type or type(check_val)
  return function(val, claim, jwt_json)
    if val == nil then return true end

    ensure_is_type(val, check_type, messages.wrong_type_claim, claim, check_type)
    return check_function(val, check_val)
  end
end)


--[[
    Returns a validator that checks if a value exactly equals the given check_value.
    If the value is nil, then this check succeeds.  The value of check_val cannot be
    nil.
]]--
define_validator("equals", function(check_val)
  return _M.opt_check(check_val, equality_function, "check_val")
end)


--[[
    Returns a validator that checks if a value matches the given pattern.  The value
    of pattern must be a string.
]]--
define_validator("matches", function (pattern)
  ensure_is_type(pattern, "string", messages.wrong_type_validator, "string", "pattern")
  return _M.opt_check(pattern, string_match_function, "pattern", "string")
end)


--[[
    Returns a validator which calls the given function for each of the given values
    and the tested value.  If any of these calls return true, then this function
    returns true.  The value of check_values must be a non-empty table with all the
    same types, and the value of check_function must not be nil.  The optional name
    is used for error messages and defaults to "check_values".  The optional
    check_type is used to make sure that the check type matches and defaults to
    type(check_values[1]) - the table type.
]]--
define_validator("any_of", function(check_values, check_function, name, check_type, table_type)
  name = name or "check_values"
  ensure_not_nil(check_values, messages.nil_validator, name)
  ensure_is_type(check_values, "table", messages.wrong_type_validator, "table", name)
  ensure_is_table(check_values, messages.empty_table_validator, name)

  table_type = table_type or type(check_values[1])
  ensure_is_table_type(check_values, table_type, messages.wrong_table_type_validator, table_type, name)

  ensure_not_nil(check_function, messages.nil_validator, "check_function")
  ensure_is_type(check_function, "function", messages.wrong_type_validator, "function", "check_function")

  check_type = check_type or table_type
  return _M.opt_check(check_values, function(v1, v2)
    for i, v in ipairs(v2) do
      if check_function(v1, v) then return true end
    end
    return false
  end, name, check_type)
end)


--[[
    Returns a validator that checks if a value exactly equals any of the given values.
]]--
define_validator("equals_any_of", function(check_values)
  return _M.opt_any_of(check_values, equality_function, "check_values")
end)


--[[
    Returns a validator that checks if a value matches any of the given patterns.
]]--
define_validator("matches_any_of", function(patterns)
  return _M.opt_any_of(patterns, string_match_function, "patterns", "string", "string")
end)

--[[
    Returns a validator that checks if a value of expected type string exists in any of the given values.
    The value of check_values must be a non-empty table with all the same types.
    The optional name is used for error messages and defaults to "check_values".
]]--
define_validator("contains_any_of", function(check_values, name)
  return _M.opt_any_of(check_values, table_contains_function, name, "table", "string")
end)

--[[
    Returns a validator that checks how a value compares (numerically) to a given
    check_value.  The value of check_val cannot be nil and must be a number.
]]--
define_validator("greater_than", function(check_val)
  ensure_is_type(check_val, "number", messages.wrong_type_validator, "number", "check_val")
  return _M.opt_check(check_val, greater_than_function, "check_val", "number")
end)
define_validator("greater_than_or_equal", function(check_val)
  ensure_is_type(check_val, "number", messages.wrong_type_validator, "number", "check_val")
  return _M.opt_check(check_val, greater_than_or_equal_function, "check_val", "number")
end)
define_validator("less_than", function(check_val)
  ensure_is_type(check_val, "number", messages.wrong_type_validator, "number", "check_val")
  return _M.opt_check(check_val, less_than_function, "check_val", "number")
end)
define_validator("less_than_or_equal", function(check_val)
  ensure_is_type(check_val, "number", messages.wrong_type_validator, "number", "check_val")
  return _M.opt_check(check_val, less_than_or_equal_function, "check_val", "number")
end)


--[[
    A function to set the leeway (in seconds) used for is_not_before and is_not_expired.  The
    default is to use 0 seconds
]]--
local system_leeway = 0
function _M.set_system_leeway(leeway)
  ensure_is_type(leeway, "number", "leeway must be a non-negative number")
  ensure_is_non_negative(leeway, "leeway must be a non-negative number")
  system_leeway = leeway
end


--[[
    A function to set the system clock used for is_not_before and is_not_expired.  The
    default is to use ngx.now
]]--
local system_clock = ngx.now
function _M.set_system_clock(clock)
  ensure_is_type(clock, "function", "clock must be a function")
  -- Check that clock returns the correct value
  local t = clock()
  ensure_is_type(t, "number", "clock function must return a non-negative number")
  ensure_is_non_negative(t, "clock function must return a non-negative number")
  system_clock = clock
end

-- Local helper function for date validation
local function validate_is_date(val, claim, jwt_json)
  ensure_is_non_negative(val, messages.wrong_type_claim, claim, "positive numeric value")
  return true
end

-- Local helper for date formatting
local function format_date_on_error(date_check_function, error_msg)
  ensure_is_type(date_check_function, "function", messages.wrong_type_validator, "function", "date_check_function")
  ensure_is_type(error_msg, "string", messages.wrong_type_validator, "string", error_msg)
  return function(val, claim, jwt_json)
    local ret = date_check_function(val, claim, jwt_json)
    if ret == false then
      error(string.format("'%s' claim %s %s", claim, error_msg, ngx.http_time(val)), 0)
    end
    return true
  end
end

--[[
    Returns a validator that checks if the current time is not before the tested value
    within the system's leeway.  This means that:
      val <= (system_clock() + system_leeway).
]]--
define_validator("is_not_before", function()
  return format_date_on_error(
     _M.chain(validate_is_date,
        function(val)
           return val and less_than_or_equal_function(val, (system_clock() + system_leeway))
        end),
     "not valid until"
  )
end)


--[[
    Returns a validator that checks if the current time is not equal to or after the
    tested value within the system's leeway.  This means that:
      val > (system_clock() - system_leeway).
]]--
define_validator("is_not_expired", function()
  return format_date_on_error(
     _M.chain(validate_is_date,
       function(val)
          return val and greater_than_function(val, (system_clock() - system_leeway))
       end),
     "expired at"
  )
end)

--[[
    Returns a validator that checks if the current time is the same as the tested value
    within the system's leeway.  This means that:
      val >= (system_clock() - system_leeway) and val <= (system_clock() + system_leeway).
]]--
define_validator("is_at", function()
  local now = system_clock()
  return format_date_on_error(
    _M.chain(validate_is_date,
             function(val)
                local now = system_clock()
                return val and
                   greater_than_or_equal_function(val, now - system_leeway) and
                   less_than_or_equal_function(val, now + system_leeway)
             end),
    "is only valid at"
  )
end)


return _M
