Skip to content

Commit a2cbcbb

Browse files
committed
Updated demo_basic example to be easier to read, and demonstrate various scenario.
1 parent a16a735 commit a2cbcbb

File tree

1 file changed

+121
-33
lines changed
  • library/server/authentication/http_authorization/example

1 file changed

+121
-33
lines changed

library/server/authentication/http_authorization/example/demo_basic.e

Lines changed: 121 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -73,75 +73,163 @@ feature -- Basic operations
7373
-- <Precursor>
7474
local
7575
auth: HTTP_AUTHORIZATION
76+
l_authenticated_username: detachable READABLE_STRING_32
77+
l_invalid_credential: BOOLEAN
7678
do
7779
if attached req.http_authorization as l_http_auth then
7880
create auth.make (l_http_auth)
7981
if attached auth.login as l_login and then is_valid_credential (l_login, auth.password) then
80-
handle_authorized (l_login, req, res)
82+
l_authenticated_username := auth.login
8183
else
82-
handle_unauthorized ("ERROR: Invalid credential", req, res)
84+
l_invalid_credential := True
8385
end
86+
end
87+
if l_invalid_credential then
88+
handle_unauthorized ("ERROR: Invalid credential", req, res)
8489
else
85-
handle_unauthorized ("ERROR: Authentication information is missing ...", req, res)
90+
if l_authenticated_username /= Void then
91+
handle_authenticated (l_authenticated_username, req, res)
92+
elseif req.path_info.same_string_general ("/login") then
93+
handle_unauthorized ("Please provide credential ...", req, res)
94+
elseif req.path_info.starts_with_general ("/protected/") then
95+
-- any "/protected/*" url
96+
handle_unauthorized ("Protected area, please sign in before", req, res)
97+
else
98+
handle_anonymous (req, res)
99+
end
86100
end
87101
end
88102

89-
handle_authorized (a_username: READABLE_STRING_32; req: WSF_REQUEST; res: WSF_RESPONSE)
103+
handle_authenticated (a_username: READABLE_STRING_32; req: WSF_REQUEST; res: WSF_RESPONSE)
90104
-- User `a_username' is authenticated, execute request `req' with response `res'.
91105
require
92106
valid_username: not a_username.is_empty
93107
known_username: is_known_login (a_username)
94108
local
95109
s: STRING
96-
l_logout_url: STRING
110+
page: WSF_HTML_PAGE_RESPONSE
97111
do
98112
create s.make_empty
99-
s.append ("Welcome %"")
113+
114+
append_html_header (req, s)
115+
116+
s.append ("<p>The authenticated user is <strong>")
100117
s.append (html_encoder.general_encoded_string (a_username))
101-
s.append ("%" ...<br/>")
118+
s.append ("</strong> ...</p>")
102119

103-
l_logout_url := req.absolute_script_url ("/")
104-
l_logout_url.replace_substring_all ("://", "://_@") -- Hack to clear http authorization, i.e connect with bad username.
105-
s.append ("<a href=%""+ l_logout_url +"%">logout</a>")
120+
append_html_menu (a_username, req, s)
121+
append_html_logout (a_username, req, s)
122+
append_html_footer (req, s)
106123

107-
-- Append the raw header data for information
108-
if attached req.raw_header_data as l_header then
109-
s.append ("<hr/><pre>")
110-
s.append (l_header)
111-
s.append ("</pre>")
112-
end
124+
create page.make
125+
page.set_body (s)
126+
res.send (page)
127+
end
128+
129+
handle_anonymous (req: WSF_REQUEST; res: WSF_RESPONSE)
130+
-- No user is authenticated, execute request `req' with response `res'.
131+
local
132+
s: STRING
133+
page: WSF_HTML_PAGE_RESPONSE
134+
do
135+
create s.make_empty
136+
append_html_header (req, s)
137+
138+
s.append ("Anonymous visitor ...<br/>")
139+
140+
append_html_login (req, s)
141+
append_html_menu (Void, req, s)
142+
append_html_footer (req, s)
113143

114-
res.put_header ({HTTP_STATUS_CODE}.ok, <<["Content-Type", "text/html"], ["Content-Length", s.count.out]>>)
115-
res.put_string (s)
144+
create page.make
145+
page.set_body (s)
146+
res.send (page)
116147
end
117148

118149
handle_unauthorized (a_description: STRING; req: WSF_REQUEST; res: WSF_RESPONSE)
119-
-- Handle forbidden.
150+
-- Restricted page, authenticated user is required.
151+
-- Send `a_description' as part of the response.
120152
local
121153
h: HTTP_HEADER
122154
s: STRING
155+
page: WSF_HTML_PAGE_RESPONSE
123156
do
124157
create s.make_from_string (a_description)
125158

126-
-- Append the raw header data for information
127-
if attached req.raw_header_data as l_header then
128-
s.append ("<hr/><pre>")
129-
s.append (l_header)
130-
s.append ("</pre>")
131-
end
159+
append_html_login (req, s)
160+
append_html_menu (Void, req, s)
161+
append_html_footer (req, s)
132162

133-
create h.make
134-
h.put_content_type_text_html
135-
h.put_content_length (s.count)
136-
h.put_current_date
137-
h.put_header_key_value ({HTTP_HEADER_NAMES}.header_www_authenticate,
163+
create page.make
164+
page.set_status_code ({HTTP_STATUS_CODE}.unauthorized)
165+
page.header.put_header_key_value ({HTTP_HEADER_NAMES}.header_www_authenticate,
138166
"Basic realm=%"Please enter a valid username and password (demo [" + html_encoder.encoded_string (demo_credential) + "])%""
139167
--| warning: for this example: a valid credential is provided in the message, of course that for real application.
140168
)
141-
res.set_status_code ({HTTP_STATUS_CODE}.unauthorized)
142-
res.put_header_text (h.string)
143-
res.put_string (s)
169+
page.set_body (s)
170+
res.send (page)
171+
end
172+
173+
feature -- Helper
174+
175+
append_html_header (req: WSF_REQUEST; s: STRING)
176+
-- Append header paragraph to `s'.
177+
do
178+
s.append ("<p>The current page is " + html_encoder.encoded_string (req.path_info) + "</p>")
179+
end
180+
181+
append_html_menu (a_username: detachable READABLE_STRING_32; req: WSF_REQUEST; s: STRING)
182+
-- Append menu to `s'.
183+
-- when an user is authenticated, `a_username' is attached.
184+
do
185+
if a_username /= Void then
186+
s.append ("<li><a href=%""+ req.absolute_script_url ("") +"%">Your account</a> (displayed only is user is authenticated!)</li>")
187+
end
188+
s.append ("<li><a href=%""+ req.absolute_script_url ("") +"%">home</a></li>")
189+
s.append ("<li><a href=%""+ req.script_url ("/public/area") +"%">public area</a></li>")
190+
s.append ("<li><a href=%""+ req.script_url ("/protected/area") +"%">protected area</a></li>")
191+
end
192+
193+
append_html_login (req: WSF_REQUEST; s: STRING)
194+
-- Append login link to `s'.
195+
do
196+
s.append ("<li><a href=%""+ req.script_url ("/login") +"%">sign in</a></li>")
197+
end
198+
199+
append_html_logout (a_username: detachable READABLE_STRING_32; req: WSF_REQUEST; s: STRING)
200+
-- Append logout link to `s'.
201+
local
202+
l_logout_url: STRING
203+
do
204+
l_logout_url := req.absolute_script_url ("/login")
205+
l_logout_url.replace_substring_all ("://", "://_@") -- Hack to clear http authorization, i.e connect with bad username "_".
206+
s.append ("<li><a href=%""+ l_logout_url +"%">logout</a></li>")
144207
end
145208

209+
append_html_footer (req: WSF_REQUEST; s: STRING)
210+
-- Append html footer to `s'.
211+
local
212+
hauth: HTTP_AUTHORIZATION
213+
do
214+
s.append ("<hr/>")
215+
if attached req.http_authorization as l_http_authorization then
216+
s.append ("Has <em>Authorization:</em> header: ")
217+
create hauth.make (req.http_authorization)
218+
if attached hauth.login as l_login then
219+
s.append (" login=<strong>" + html_encoder.encoded_string (l_login)+ "</strong>")
220+
end
221+
if attached hauth.password as l_password then
222+
s.append (" password=<strong>" + html_encoder.encoded_string (l_password)+ "</strong>")
223+
end
224+
s.append ("<br/>")
225+
end
226+
if attached req.raw_header_data as l_header then
227+
-- Append the raw header data for information
228+
s.append ("Raw header data:")
229+
s.append ("<pre>")
230+
s.append (l_header)
231+
s.append ("</pre>")
232+
end
233+
end
146234

147235
end

0 commit comments

Comments
 (0)