auth.dev.lua 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. -- 简单的权限验证
  2. -- by Sunmoon
  3. local function forbidden()
  4. ngx.status = ngx.HTTP_UNAUTHORIZED
  5. ngx.header.content_type = "application/json; charset=utf-8"
  6. ngx.say("{\"status\": 401, \"message\": \"Unauthorized\", \"error\": 1}")
  7. return ngx.exit(ngx.HTTP_OK)
  8. end
  9. local function simple_debug(msg)
  10. ngx.say(msg)
  11. return ngx.exit(ngx.HTTP_OK)
  12. end
  13. local upload_user = ngx.req.get_headers()["UPLOAD-SERVER-USER"]
  14. local upload_token = ngx.req.get_headers()["UPLOAD-SERVER-TOKEN"]
  15. local upload_date = ngx.req.get_headers()["UPLOAD-SERVER-DATE"]
  16. local upload_notify_url = ngx.req.get_headers()["UPLOAD-SERVER-NOTIFY-URL"]
  17. local secretkey='k4Ao7KWVbvg3Z2L6KLwN9OoDjQL5SioJffIPoODATxCynuEVEAt0278kg7r9FHiS'
  18. local date = os.date("%Y%m%d%H")
  19. if upload_user == nil or upload_token == nil then
  20. return forbidden()
  21. end
  22. if upload_notify_url == nil then
  23. upload_notify_url = ''
  24. end
  25. if upload_date == nil then
  26. upload_date = date
  27. end
  28. local string = 'uid:' .. tostring(upload_user) .. '&secretkey:' .. tostring(secretkey) .. '&datetime:' .. tostring(upload_date) .. '&notifyurl:' .. tostring(upload_notify_url)
  29. local token = ngx.md5(string)
  30. if token ~= upload_token then
  31. return forbidden()
  32. end