PageRenderTime 91ms CodeModel.GetById 87ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

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