Added Initial Basic Auth Module

This commit is contained in:
jvelilla
2014-10-02 15:32:30 -03:00
parent 5525134148
commit a0cb7c0ecc
15 changed files with 372 additions and 11 deletions

View File

@@ -0,0 +1,79 @@
note
description: "Summary description for {BASIC_AUTH_MODULE}."
date: "$Date$"
revision: "$Revision$"
class
BASIC_AUTH_MODULE
inherit
CMS_MODULE
create
make
feature {NONE} -- Initialization
make (a_config: CMS_SETUP)
do
name := "basic auth"
version := "1.0"
description := "Service to manage basic authentication"
package := "core"
config := a_config
setup_router
setup_filter
enable
end
feature -- Access
router: WSF_ROUTER
-- Node router.
config: CMS_SETUP
-- Node configuration.
feature -- Implementation
setup_router
-- Setup `router'.
do
create router.make (2)
configure_api_login
configure_api_logoff
end
setup_filter
-- Setup `filter'.
do
add_filter (create {CORS_FILTER})
add_filter (create {BASIC_AUTH_FILTER}.make (config))
end
feature -- Configure Node Resources Routes
configure_api_login
local
l_bal_handler: BASIC_AUTH_LOGIN_HANDLER
l_methods: WSF_REQUEST_METHODS
do
create l_bal_handler.make (config)
create l_methods
l_methods.enable_get
router.handle_with_request_methods ("/basic_auth/login", l_bal_handler, l_methods)
end
configure_api_logoff
local
l_bal_handler: BASIC_AUTH_LOGOFF_HANDLER
l_methods: WSF_REQUEST_METHODS
do
create l_bal_handler.make (config)
create l_methods
l_methods.enable_get
router.handle_with_request_methods ("/basic_auth/logoff", l_bal_handler, l_methods)
end
end

View File

@@ -0,0 +1,51 @@
note
description: "Summary description for {BASIC_AUTH_FILTER}."
author: ""
date: "$Date$"
revision: "$Revision$"
class
BASIC_AUTH_FILTER
inherit
WSF_URI_TEMPLATE_HANDLER
CMS_HANDLER
WSF_FILTER
create
make
feature -- Basic operations
execute (req: WSF_REQUEST; res: WSF_RESPONSE)
-- Execute the filter
local
l_auth: HTTP_AUTHORIZATION
do
log.write_debug (generator + ".execute " )
create l_auth.make (req.http_authorization)
if attached req.raw_header_data as l_raw_data then
log.write_debug (generator + ".execute " + l_raw_data )
end
-- A valid user
if (attached l_auth.type as l_auth_type and then l_auth_type.is_case_insensitive_equal_general ("basic")) and then
attached l_auth.login as l_auth_login and then attached l_auth.password as l_auth_password then
if api_service.login_valid (l_auth_login, l_auth_password) then
if attached api_service.user_by_name (l_auth_login) as l_user then
req.set_execution_variable ("user", l_user)
execute_next (req, res)
else
-- Internal server error
end
else
log.write_error (generator + ".execute login_valid failed for: " + l_auth_login )
execute_next (req, res)
end
else
log.write_error (generator + ".execute Not valid")
execute_next (req, res)
end
end
end

View File

@@ -0,0 +1,28 @@
note
description: "CORS filter"
date: "$Date: 2014-08-08 16:02:11 -0300 (vi., 08 ago. 2014) $"
revision: "$Revision: 95593 $"
class
CORS_FILTER
inherit
WSF_FILTER
feature -- Basic operations
execute (req: WSF_REQUEST; res: WSF_RESPONSE)
-- Execute the filter.
local
l_header: HTTP_HEADER
do
create l_header.make
-- l_header.add_header_key_value ("Access-Control-Allow-Origin", "localhost")
l_header.add_header_key_value ("Access-Control-Allow-Headers", "*")
l_header.add_header_key_value ("Access-Control-Allow-Methods", "*")
l_header.add_header_key_value ("Access-Control-Allow-Credentials", "true")
res.put_header_lines (l_header)
execute_next (req, res)
end
end

View File

@@ -0,0 +1,62 @@
note
description: "Summary description for {BASIC_AUTH_LOGIN_HANDLER}."
date: "$Date$"
revision: "$Revision$"
class
BASIC_AUTH_LOGIN_HANDLER
inherit
CMS_HANDLER
WSF_URI_HANDLER
rename
execute as uri_execute,
new_mapping as new_uri_mapping
end
WSF_FILTER
WSF_RESOURCE_HANDLER_HELPER
redefine
do_get
end
REFACTORING_HELPER
create
make
feature -- execute
execute (req: WSF_REQUEST; res: WSF_RESPONSE)
-- Execute request handler.
do
execute_methods (req, res)
execute_next (req, res)
end
uri_execute (req: WSF_REQUEST; res: WSF_RESPONSE)
-- Execute request handler.
do
execute_methods (req, res)
end
feature -- HTTP Methods
do_get (req: WSF_REQUEST; res: WSF_RESPONSE)
-- <Precursor>
local
l_page: CMS_RESPONSE
do
log.write_information(generator + ".do_get Processing basic auth login")
if attached {STRING_32} current_user_name (req) as l_user then
(create {CMS_GENERIC_RESPONSE}).new_response_redirect (req, res, req.absolute_script_url(""))
else
(create {CMS_GENERIC_RESPONSE}).new_response_authenticate (req, res)
end
end
end

View File

@@ -0,0 +1,59 @@
note
description: "Summary description for {BASIC_AUTH_LOGOFF_HANDLER}."
date: "$Date$"
revision: "$Revision$"
class
BASIC_AUTH_LOGOFF_HANDLER
inherit
CMS_HANDLER
WSF_URI_HANDLER
rename
execute as uri_execute,
new_mapping as new_uri_mapping
end
WSF_RESOURCE_HANDLER_HELPER
redefine
do_get
end
REFACTORING_HELPER
create
make
feature -- execute
execute (req: WSF_REQUEST; res: WSF_RESPONSE)
-- Execute request handler.
do
execute_methods (req, res)
end
uri_execute (req: WSF_REQUEST; res: WSF_RESPONSE)
-- Execute request handler.
do
execute_methods (req, res)
end
feature -- HTTP Methods
do_get (req: WSF_REQUEST; res: WSF_RESPONSE)
-- <Precursor>
local
l_page: CMS_RESPONSE
do
log.write_information(generator + ".do_get Processing basic auth logoff")
if attached req.query_parameter ("prompt") as l_prompt then
(create {CMS_GENERIC_RESPONSE}).new_response_unauthorized (req, res)
else
create {GENERIC_VIEW_CMS_RESPONSE} l_page.make (req, res, setup, "master2/basic_auth/logoff")
l_page.set_status_code ({HTTP_STATUS_CODE}.unauthorized)
l_page.execute
end
end
end

View File

@@ -25,6 +25,26 @@ feature -- Router
deferred
end
feature -- Filter
filters: detachable LIST[WSF_FILTER]
-- Possibly list of Filter's module.
feature -- Element Change: Filter
add_filter (a_filter: WSF_FILTER)
-- Add a filter `a_filter' to the list of module filters `filters'.
local
l_filters: like filters
do
l_filters := filters
if l_filters = Void then
create {ARRAYED_LIST[WSF_FILTER]}l_filters.make (1)
filters := l_filters
end
l_filters.force (a_filter)
end
feature -- Settings
enable