/examples/rest_server/src/app/app_account_verify_credential.e

http://github.com/jocelyn/EiffelWebReloaded · Specman e · 103 lines · 90 code · 13 blank · 0 comment · 4 complexity · 8f159eafdae1019f24b350428e1bc39a MD5 · raw file

  1. note
  2. description: "Summary description for {APP_ACCOUNT_VERIFY_CREDENTIAL}."
  3. date: "$Date$"
  4. revision: "$Revision$"
  5. class
  6. APP_ACCOUNT_VERIFY_CREDENTIAL
  7. inherit
  8. APP_REQUEST_HANDLER
  9. redefine
  10. initialize,
  11. execute_unauthorized
  12. end
  13. create
  14. make
  15. feature {NONE} -- Initialization
  16. make (a_path: STRING)
  17. do
  18. path := a_path
  19. description := "Verify credentials"
  20. initialize
  21. end
  22. initialize
  23. do
  24. Precursor
  25. enable_request_method_get
  26. enable_format_json
  27. enable_format_xml
  28. enable_format_text
  29. end
  30. feature -- Access
  31. authentication_required: BOOLEAN = True
  32. feature -- Execution
  33. execute_unauthorized (ctx: REST_REQUEST_CONTEXT; a_format: detachable STRING; a_args: detachable STRING)
  34. local
  35. h: HTTPD_HEADER
  36. do
  37. create h.make
  38. h.put_status ({HTTP_STATUS_CODE}.unauthorized)
  39. h.put_header ("WWW-Authenticate: Basic realm=%"My Silly demo auth, password must be the same as login such as foo:foo%"")
  40. ctx.output.put_string (h.string)
  41. h.recycle
  42. end
  43. execute_application (ctx: REST_REQUEST_CONTEXT; a_format: detachable STRING; a_args: detachable STRING)
  44. local
  45. l_full: BOOLEAN
  46. rep: detachable REST_RESPONSE
  47. l_login: STRING_8
  48. s: STRING
  49. do
  50. if ctx.authenticated then
  51. l_full := attached ctx.variables_get.variable ("details") as v and then v.is_case_insensitive_equal ("true")
  52. if attached ctx.authenticated_identifier as log then
  53. l_login := log.as_string_8
  54. create rep.make (path)
  55. create s.make_empty
  56. inspect format_id (a_format)
  57. when {REST_FORMAT_CONSTANTS}.json then
  58. rep.headers.put_content_type_text_plain
  59. s.append_string ("{ %"login%": %"" + l_login + "%" }%N")
  60. when {REST_FORMAT_CONSTANTS}.xml then
  61. rep.headers.put_content_type_text_xml
  62. s.append_string ("<login>" + l_login + "</login>%N")
  63. when {REST_FORMAT_CONSTANTS}.text then -- Default
  64. rep.headers.put_content_type_text_plain
  65. s.append_string ("login: " + l_login + "%N")
  66. else
  67. end
  68. if not s.is_empty then
  69. rep.set_message (s)
  70. ctx.output.put_string (rep.string)
  71. end
  72. rep.recycle
  73. else
  74. process_error (ctx, "User/password unknown", a_format)
  75. end
  76. else
  77. process_error (ctx, "Authentication rejected", a_format)
  78. end
  79. end
  80. note
  81. copyright: "Copyright (c) 1984-2011, Eiffel Software and others"
  82. license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)"
  83. source: "[
  84. Eiffel Software
  85. 5949 Hollister Ave., Goleta, CA 93117 USA
  86. Telephone 805-685-1006, Fax 805-685-6869
  87. Website http://www.eiffel.com
  88. Customer support http://support.eiffel.com
  89. ]"
  90. end