Added code to create an HTTP_AUTHORIZATION from the client side as well.

So now we can either interpret an HTTP_AUTHORIZATION  or build one HTTP_AUTHORIZATION

So far , only Basic auth is supported.
This commit is contained in:
Jocelyn Fiat
2011-09-22 15:13:59 +02:00
parent dae8e1d67d
commit ab1c696837

View File

@@ -7,42 +7,87 @@ note
class class
HTTP_AUTHORIZATION HTTP_AUTHORIZATION
inherit
REFACTORING_HELPER
create create
make make,
make_basic_auth,
make_custom_auth
feature {NONE} -- Initialization feature {NONE} -- Initialization
make (a_http_authorization: detachable READABLE_STRING_GENERAL) make (a_http_authorization: detachable READABLE_STRING_GENERAL)
-- Initialize `Current'. -- Initialize `Current'.
local local
p: INTEGER i: INTEGER
s: STRING_8 t, s: STRING_8
u,p: READABLE_STRING_8
do do
if attached a_http_authorization as l_auth then if attached a_http_authorization as l_auth then
s := l_auth.as_string_8 s := l_auth.as_string_8
if not s.is_empty then if not s.is_empty then
p := 1 i := 1
if s[p] = ' ' then if s[i] = ' ' then
p := p + 1 i := i + 1
end end
p := s.index_of (' ', p) i := s.index_of (' ', i)
if p > 0 then if i > 0 then
s := (create {BASE64}).decoded_string (s.substring (p + 1, s.count)) t := s.substring (1, i - 1).as_lower
p := s.index_of (':', 1) --| Let's assume ':' is forbidden in login ... t.right_adjust; t.left_adjust
if p > 0 then type := t
login := s.substring (1, p - 1).as_string_32 if t.same_string ("basic") then
password := s.substring (p + 1, s.count).as_string_32 s := (create {BASE64}).decoded_string (s.substring (i + 1, s.count))
i := s.index_of (':', 1) --| Let's assume ':' is forbidden in login ...
if i > 0 then
u := s.substring (1, i - 1).as_string_32
p := s.substring (i + 1, s.count).as_string_32
login := u
password := p
check
(create {HTTP_AUTHORIZATION}.make_custom_auth (u, p, t)).http_authorization ~ http_authorization
end
end
elseif t.same_string ("digest") then
to_implement ("HTTP Authorization %"digest%", not yet implemented")
else
to_implement ("HTTP Authorization %""+ t +"%", not yet implemented")
end end
end end
end end
end end
end end
make_basic_auth (u: READABLE_STRING_32; p: READABLE_STRING_32)
do
make_custom_auth (u, p, "basic")
end
make_custom_auth (u: READABLE_STRING_32; p: READABLE_STRING_32; a_type: READABLE_STRING_8)
local
t: STRING_8
do
login := u
password := p
create t.make_from_string (a_type.as_lower)
t.left_adjust; t.right_adjust
type := t
if t.same_string ("basic") then
http_authorization := "Basic " + (create {BASE64}).encoded_string (u + ":" + p)
else
to_implement ("HTTP Authorization %""+ t +"%", not yet implemented")
end
end
feature -- Access feature -- Access
type: detachable READABLE_STRING_8
login: detachable READABLE_STRING_32 login: detachable READABLE_STRING_32
password: detachable READABLE_STRING_32 password: detachable READABLE_STRING_32
http_authorization: detachable READABLE_STRING_32
end end