Update Login Module.

- Added an API to mange user OAuth authentication.
   - Updated the Filter to use the new API.
   - Updated the Module to initialize if it needed the storages needed by the login module.
   - Updated gmail callback to use the new API.
   - Added a Persistence Layer

CMS_USER_API
   - clean api and related persistence code.
This commit is contained in:
jvelilla
2015-06-08 18:32:34 -03:00
parent 96ba3c35a2
commit e188625c43
11 changed files with 413 additions and 169 deletions

View File

@@ -0,0 +1,64 @@
note
description: "[
API to manage CMS User OAuth authentication.
]"
date: "$Date$"
revision: "$Revision$"
class
CMS_USER_OAUTH_API
inherit
CMS_MODULE_API
REFACTORING_HELPER
create {LOGIN_MODULE}
make_with_storage
feature {NONE} -- Initialization
make_with_storage (a_api: CMS_API; a_user_oauth_storage: CMS_USER_OAUTH_STORAGE_I)
do
user_oauth_storage := a_user_oauth_storage
make (a_api)
end
feature {CMS_MODULE} -- Access user oauth storage.
user_oauth_storage: CMS_USER_OAUTH_STORAGE_I
feature -- Access: OAuth2 Gmail
user_oauth2_gmail_by_id (a_uid: like {CMS_USER}.id): detachable CMS_USER
do
Result := user_oauth_storage.user_oauth2_gmail_by_id (a_uid)
end
user_by_oauth2_gmail_token (a_token: READABLE_STRING_32): detachable CMS_USER
do
Result := user_oauth_storage.user_by_oauth2_gmail_token (a_token)
end
feature -- Change: OAuth2 Gmail
new_user_oauth2_gmail (a_token: READABLE_STRING_32; a_user_profile: READABLE_STRING_32; a_user: CMS_USER)
-- Add a new user with oauth2 gmail authentication.
require
has_id: a_user.has_id
do
user_oauth_storage.new_user_oauth2_gmail (a_token, a_user_profile, a_user)
end
update_user_oauth2_gmail (a_token: READABLE_STRING_32; a_user_profile: READABLE_STRING_32; a_user: CMS_USER)
-- Updaate user `a_user' with oauth2 gmail authentication.
require
has_id: a_user.has_id
do
user_oauth_storage.update_user_oauth2_gmail (a_token, a_user_profile, a_user)
end
end

View File

@@ -10,11 +10,25 @@ class
inherit
WSF_URI_TEMPLATE_HANDLER
CMS_HANDLER
rename
make as make_handler
end
WSF_FILTER
create
make
feature {NONE} -- Initialization
make (a_api: CMS_API; a_user_oauth_api: CMS_USER_OAUTH_API)
do
make_handler (a_api)
user_oauth_api := a_user_oauth_api
end
user_oauth_api: CMS_USER_OAUTH_API
feature -- Basic operations
execute (req: WSF_REQUEST; res: WSF_RESPONSE)
@@ -23,14 +37,14 @@ feature -- Basic operations
utf: UTF_CONVERTER
do
api.logger.put_debug (generator + ".execute ", Void)
-- if attached req.raw_header_data as l_raw_data then
-- api.logger.put_debug (generator + ".execute " + utf.escaped_utf_32_string_to_utf_8_string_8 (l_raw_data), Void)
-- end
if attached req.raw_header_data as l_raw_data then
api.logger.put_debug (generator + ".execute " + utf.escaped_utf_32_string_to_utf_8_string_8 (l_raw_data), Void)
end
-- A valid user
if
attached {WSF_STRING} req.cookie ("EWF_ROC_OAUTH_GMAIL_SESSION_") as l_roc_auth_session_token
then
if attached {CMS_USER} api.user_api.user_by_oauth2_gmail_token (l_roc_auth_session_token.value) as l_user then
if attached {CMS_USER} user_oauth_api.user_by_oauth2_gmail_token (l_roc_auth_session_token.value) as l_user then
set_current_user (req, l_user)
execute_next (req, res)
else

View File

@@ -18,8 +18,9 @@
<library name="http" location="$ISE_LIBRARY\contrib\library\network\protocol\http\http-safe.ecf"/>
<library name="time" location="$ISE_LIBRARY\library\time\time-safe.ecf"/>
<library name="wsf" location="$ISE_LIBRARY\contrib\library\web\framework\ewf\wsf\wsf-safe.ecf"/>
<library name="error" location="$ISE_LIBRARY\contrib\library\utility\general\error\error-safe.ecf"/>
<library name="apis" location="..\..\..\cypress_jv\consumer\apis\apis.ecf" readonly="false"/>
<library name="apis" location="..\..\..\cypress_jv\consumer\apis\apis.ecf" readonly="false"/>
<library name="cypress_consumer" location="..\..\..\cypress_jv\consumer\cypress_consumer-safe.ecf" readonly="false"/>
<library name="json" location="$ISE_LIBRARY\contrib\library\text\parser\json\library\json-safe.ecf" readonly="false"/>

View File

@@ -8,11 +8,18 @@ class
inherit
CMS_MODULE
rename
module_api as user_oauth_api
redefine
filters,
register_hooks
register_hooks,
initialize,
is_installed,
install,
user_oauth_api
end
CMS_HOOK_BLOCK
CMS_HOOK_AUTO_REGISTER
@@ -49,6 +56,65 @@ feature {NONE} -- Initialization
cache_duration := 0
end
feature {CMS_API} -- Module Initialization
initialize (a_api: CMS_API)
-- <Precursor>
local
l_user_auth_api: like user_oauth_api
l_user_auth_storage: CMS_USER_OAUTH_STORAGE_I
do
Precursor (a_api)
-- Storage initialization
if attached {CMS_STORAGE_SQL_I} a_api.storage as l_storage_sql then
create {CMS_USER_OAUTH_STORAGE_SQL} l_user_auth_storage.make (l_storage_sql)
else
-- FIXME: in case of NULL storage, should Current be disabled?
create {CMS_USER_OAUTH_STORAGE_NULL} l_user_auth_storage
end
-- Node API initialization
create l_user_auth_api.make_with_storage (a_api, l_user_auth_storage)
user_oauth_api := l_user_auth_api
ensure then
user_oauth_api_set: user_oauth_api /= Void
end
feature {CMS_API} -- Module management
is_installed (api: CMS_API): BOOLEAN
-- Is Current module installed?
do
Result := attached api.storage.custom_value ("is_initialized", "module-" + name) as v and then v.is_case_insensitive_equal_general ("yes")
end
install (api: CMS_API)
local
sql: STRING
l_setup: CMS_SETUP
do
l_setup := api.setup
-- Schema
if attached {CMS_STORAGE_SQL_I} api.storage as l_sql_storage then
if not l_sql_storage.sql_table_exists ("oauth2_gmail") then
--| Schema
l_sql_storage.sql_execute_file_script (l_setup.environment.path.extended ("scripts").extended ("core.sql"))
if l_sql_storage.has_error then
api.logger.put_error ("Could not initialize database for blog module", generating_type)
end
end
api.storage.set_custom_value ("is_initialized", "module-" + name, "yes")
end
end
feature {CMS_API} -- Access: API
user_oauth_api: detachable CMS_USER_OAUTH_API
-- <Precursor>
feature -- Filters
@@ -56,7 +122,9 @@ feature -- Filters
-- Possibly list of Filter's module.
do
create {ARRAYED_LIST [WSF_FILTER]} Result.make (1)
Result.extend (create {OAUTH_GMAIL_FILTER}.make (a_api))
if attached user_oauth_api as l_user_oauth_api then
Result.extend (create {OAUTH_GMAIL_FILTER}.make (a_api, l_user_oauth_api))
end
end
feature -- Access: docs
@@ -76,8 +144,17 @@ feature -- Access: docs
feature -- Router
setup_router (a_router: WSF_ROUTER; a_api: CMS_API)
-- Router configuration.
-- <Precursor>
do
if attached user_oauth_api as l_user_oauth_api then
configure_web (a_api, l_user_oauth_api, a_router)
end
end
configure_web (a_api: CMS_API; a_user_oauth_api: CMS_USER_OAUTH_API; a_router: WSF_ROUTER)
do
a_router.handle_with_request_methods ("/roc-login", create {WSF_URI_AGENT_HANDLER}.make (agent handle_login (a_api, ?, ?)), a_router.methods_head_get)
a_router.handle_with_request_methods ("/roc-register", create {WSF_URI_AGENT_HANDLER}.make (agent handle_register (a_api, ?, ?)), a_router.methods_get_post)
@@ -87,9 +164,11 @@ feature -- Router
a_router.handle_with_request_methods ("/reset-password", create {WSF_URI_AGENT_HANDLER}.make (agent handle_reset_password (a_api, ?, ?)), a_router.methods_get_post)
a_router.handle_with_request_methods ("/roc-logout", create {WSF_URI_AGENT_HANDLER}.make (agent handle_logout (a_api, ?, ?)), a_router.methods_get_post)
a_router.handle_with_request_methods ("/login-with-google", create {WSF_URI_AGENT_HANDLER}.make (agent handle_login_with_google (a_api, ?, ?)), a_router.methods_get_post)
a_router.handle_with_request_methods ("/oauthgmail", create {WSF_URI_AGENT_HANDLER}.make (agent handle_callback_gmail (a_api, ?, ?)), a_router.methods_get_post)
a_router.handle_with_request_methods ("/oauthgmail", create {WSF_URI_AGENT_HANDLER}.make (agent handle_callback_gmail (a_api, a_user_oauth_api, ?, ?)), a_router.methods_get_post)
end
feature -- Hooks configuration
register_hooks (a_response: CMS_RESPONSE)
@@ -656,7 +735,7 @@ feature -- OAuth2 Login with google.
end
end
handle_callback_gmail (api: CMS_API; req: WSF_REQUEST; res: WSF_RESPONSE)
handle_callback_gmail (api: CMS_API; a_user_oauth_api: CMS_USER_OAUTH_API; req: WSF_REQUEST; res: WSF_RESPONSE)
local
r: CMS_RESPONSE
l_auth_gmail: OAUTH_LOGIN_GMAIL
@@ -682,12 +761,12 @@ feature -- OAuth2 Login with google.
then
if attached {CMS_USER} l_user_api.user_by_email (l_email) as p_user then
-- User with email exist
if attached {CMS_USER} l_user_api.user_oauth2_gmail_by_id (p_user.id) then
if attached {CMS_USER} a_user_oauth_api.user_oauth2_gmail_by_id (p_user.id) then
-- Update oauth entry
l_user_api.update_user_oauth2_gmail (l_access_token.token, l_user_profile, p_user )
a_user_oauth_api.update_user_oauth2_gmail (l_access_token.token, l_user_profile, p_user )
else
-- create a oauth entry
l_user_api.new_user_oauth2_gmail (l_access_token.token, l_user_profile, p_user )
a_user_oauth_api.new_user_oauth2_gmail (l_access_token.token, l_user_profile, p_user )
end
create l_cookie.make ("EWF_ROC_OAUTH_GMAIL_SESSION_", l_access_token.token)
l_cookie.set_max_age (l_access_token.expires_in)
@@ -706,7 +785,7 @@ feature -- OAuth2 Login with google.
l_user_api.new_user (l_user)
-- Add oauth entry
l_user_api.new_user_oauth2_gmail (l_access_token.token, l_user_profile, l_user )
a_user_oauth_api.new_user_oauth2_gmail (l_access_token.token, l_user_profile, l_user )
create l_cookie.make ("EWF_ROC_OAUTH_GMAIL_SESSION_", l_access_token.token)
l_cookie.set_max_age (l_access_token.expires_in)
res.add_cookie (l_cookie)

View File

@@ -0,0 +1,44 @@
note
description: "Summary description for {CMS_USER_OAUTH_STORAGE_I}."
date: "$Date$"
revision: "$Revision$"
deferred class
CMS_USER_OAUTH_STORAGE_I
inherit
SHARED_LOGGER
feature -- Error Handling
error_handler: ERROR_HANDLER
-- Error handler.
deferred
end
feature -- Access
user_oauth2_gmail_by_id (a_uid: like {CMS_USER}.id): detachable CMS_USER
-- CMS User with Oauth gmail credential by id if any.
deferred
end
user_by_oauth2_gmail_token (a_token: READABLE_STRING_32): detachable CMS_USER
-- -- CMS User with Oauth gmail credential by access token `a_token' if any.
deferred
end
feature -- Change: User Oauth2
new_user_oauth2_gmail (a_token: READABLE_STRING_32; a_user_profile: READABLE_STRING_32; a_user: CMS_USER)
-- Add a new user with oauth2 gmail authentication.
deferred
end
update_user_oauth2_gmail (a_token: READABLE_STRING_32; a_user_profile: READABLE_STRING_32; a_user: CMS_USER)
-- Update user `a_user' with oauth2 gmail authentication.
deferred
end
end

View File

@@ -0,0 +1,48 @@
note
description: "Summary description for {CMS_USER_OAUTH_STORAGE_NULL}."
author: ""
date: "$Date$"
revision: "$Revision$"
class
CMS_USER_OAUTH_STORAGE_NULL
inherit
CMS_USER_OAUTH_STORAGE_I
feature -- Error handler
error_handler: ERROR_HANDLER
-- Error handler.
do
create Result.make
end
feature -- Access
user_oauth2_gmail_by_id (a_uid: like {CMS_USER}.id): detachable CMS_USER
-- CMS User with Oauth gmail credential by id if any.
do
end
user_by_oauth2_gmail_token (a_token: READABLE_STRING_32): detachable CMS_USER
-- -- CMS User with Oauth gmail credential by access token `a_token' if any.
do
end
feature -- Change: User Oauth2
new_user_oauth2_gmail (a_token: READABLE_STRING_32; a_user_profile: READABLE_STRING_32; a_user: CMS_USER)
-- Add a new user with oauth2 gmail authentication.
do
end
update_user_oauth2_gmail (a_token: READABLE_STRING_32; a_user_profile: READABLE_STRING_32; a_user: CMS_USER)
-- Update user `a_user' with oauth2 gmail authentication.
do
end
end

View File

@@ -0,0 +1,149 @@
note
description: "Summary description for {CMS_USER_OAUTH_STORAGE_SQL}."
date: "$Date$"
revision: "$Revision$"
class
CMS_USER_OAUTH_STORAGE_SQL
inherit
CMS_USER_OAUTH_STORAGE_I
CMS_PROXY_STORAGE_SQL
CMS_USER_OAUTH_STORAGE_I
CMS_STORAGE_SQL_I
REFACTORING_HELPER
create
make
feature -- Access User Outh Gmail
user_oauth2_gmail_by_id (a_uid: like {CMS_USER}.id): detachable CMS_USER
-- <Precursor>
local
l_parameters: STRING_TABLE [detachable ANY]
do
error_handler.reset
write_information_log (generator + ".user_oauth2_gmail_by_id")
create l_parameters.make (1)
l_parameters.put (a_uid, "uid")
sql_query (select_user_oauth2_gmail_by_id, l_parameters)
if sql_rows_count = 1 then
Result := fetch_user
else
check no_more_than_one: sql_rows_count = 0 end
end
end
user_by_oauth2_gmail_token (a_token: READABLE_STRING_32): detachable CMS_USER
-- <Precursor>
local
l_parameters: STRING_TABLE [detachable ANY]
do
error_handler.reset
write_information_log (generator + ".user_by_oauth2_gmail_token")
create l_parameters.make (1)
l_parameters.put (a_token, "token")
sql_query (select_user_by_oauth2_gmail_token, l_parameters)
if sql_rows_count = 1 then
Result := fetch_user
else
check no_more_than_one: sql_rows_count = 0 end
end
end
feature -- Change: User Oauth2 Gmail
new_user_oauth2_gmail (a_token: READABLE_STRING_32; a_user_profile: READABLE_STRING_32; a_user: CMS_USER)
-- <Precursor>.
local
l_parameters: STRING_TABLE [detachable ANY]
do
error_handler.reset
sql_begin_transaction
write_information_log (generator + ".new_user_oauth2_gmail")
create l_parameters.make (4)
l_parameters.put (a_user.id, "uid")
l_parameters.put (a_token, "token")
l_parameters.put (a_user_profile, "profile")
l_parameters.put (create {DATE_TIME}.make_now_utc, "utc_date")
sql_change (sql_insert_oauth2_gmail, l_parameters)
sql_commit_transaction
end
update_user_oauth2_gmail (a_token: READABLE_STRING_32; a_user_profile: READABLE_STRING_32; a_user: CMS_USER)
-- <Precursor>
local
l_parameters: STRING_TABLE [detachable ANY]
do
error_handler.reset
sql_begin_transaction
write_information_log (generator + ".new_user_oauth2_gmail")
create l_parameters.make (4)
l_parameters.put (a_user.id, "uid")
l_parameters.put (a_token, "token")
l_parameters.put (a_user_profile, "profile")
sql_change (sql_update_oauth2_gmail, l_parameters)
sql_commit_transaction
end
feature {NONE} -- Implementation: User
fetch_user: detachable CMS_USER
local
l_id: INTEGER_64
l_name: detachable READABLE_STRING_32
do
if attached sql_read_integer_32 (1) as i then
l_id := i
end
if attached sql_read_string_32 (2) as s and then not s.is_whitespace then
l_name := s
end
if l_name /= Void then
create Result.make (l_name)
if l_id > 0 then
Result.set_id (l_id)
end
elseif l_id > 0 then
create Result.make_with_id (l_id)
end
if Result /= Void then
if attached sql_read_string (3) as l_password then
-- FIXME: should we return the password here ???
Result.set_hashed_password (l_password)
end
if attached sql_read_string (5) as l_email then
Result.set_email (l_email)
end
if attached sql_read_integer_32 (6) as l_status then
Result.set_status (l_status)
end
else
check expected_valid_user: False end
end
end
feature {NONE}-- User Oauth2 Gmail.
Sql_insert_oauth2_gmail: STRING = "INSERT INTO oauth2_gmail (uid, access_token, details, created) VALUES (:uid, :token, :profile, :utc_date);"
Sql_update_oauth2_gmail: STRING = "UPDATE oauth2_gmail SET access_token = :token, details = :profile WHERE uid =:uid;"
Select_user_by_oauth2_gmail_token: STRING = "SELECT u.* FROM users as u JOIN oauth2_gmail as og ON og.uid = u.uid and og.access_token = :token;"
Select_user_oauth2_gmail_by_id: STRING = "SELECT u.* FROM users as u JOIN oauth2_gmail as og ON og.uid = u.uid and og.uid = :uid;"
end

View File

@@ -179,24 +179,4 @@ feature -- Change: User password recovery
deferred
end
feature -- Change: User Oauth2
new_user_oauth2_gmail (a_token: READABLE_STRING_32; a_user_profile: READABLE_STRING_32; a_user: CMS_USER)
-- Add a new user with oauth2 gmail authentication.
deferred
end
update_user_oauth2_gmail (a_token: READABLE_STRING_32; a_user_profile: READABLE_STRING_32; a_user: CMS_USER)
-- Update user `a_user' with oauth2 gmail authentication.
deferred
end
user_oauth2_gmail_by_id (a_uid: like {CMS_USER}.id): detachable CMS_USER
deferred
end
user_by_oauth2_gmail_token (a_token: READABLE_STRING_32): detachable CMS_USER
deferred
end
end

View File

@@ -108,23 +108,4 @@ feature -- Change: User password recovery
do
end
feature -- Change User Oauth
new_user_oauth2_gmail (a_token: READABLE_STRING_32; a_user_profile: READABLE_STRING_32; a_user: CMS_USER)
-- Add a new user with oauth2 gmail authentication.
do
end
update_user_oauth2_gmail (a_token: READABLE_STRING_32; a_user_profile: READABLE_STRING_32; a_user: CMS_USER)
-- Update user `a_user' with oauth2 gmail authentication.
do
end
user_oauth2_gmail_by_id (a_uid: like {CMS_USER}.id): detachable CMS_USER
do
end
user_by_oauth2_gmail_token (a_token: READABLE_STRING_32): detachable CMS_USER
do
end
end

View File

@@ -578,83 +578,6 @@ feature -- Change: User password recovery
sql_commit_transaction
end
feature -- User Oauth2
new_user_oauth2_gmail (a_token: READABLE_STRING_32; a_user_profile: READABLE_STRING_32; a_user: CMS_USER)
-- Add a new user with oauth2 gmail authentication.
local
l_parameters: STRING_TABLE [detachable ANY]
do
error_handler.reset
sql_begin_transaction
write_information_log (generator + ".new_user_oauth2_gmail")
create l_parameters.make (4)
l_parameters.put (a_user.id, "uid")
l_parameters.put (a_token, "token")
l_parameters.put (a_user_profile, "profile")
l_parameters.put (create {DATE_TIME}.make_now_utc, "utc_date")
sql_change (sql_insert_oauth2_gmail, l_parameters)
sql_commit_transaction
end
update_user_oauth2_gmail (a_token: READABLE_STRING_32; a_user_profile: READABLE_STRING_32; a_user: CMS_USER)
-- Add a new user with oauth2 gmail authentication.
local
l_parameters: STRING_TABLE [detachable ANY]
do
error_handler.reset
sql_begin_transaction
write_information_log (generator + ".new_user_oauth2_gmail")
create l_parameters.make (4)
l_parameters.put (a_user.id, "uid")
l_parameters.put (a_token, "token")
l_parameters.put (a_user_profile, "profile")
sql_change (sql_update_oauth2_gmail, l_parameters)
sql_commit_transaction
end
user_by_oauth2_gmail_token (a_token: READABLE_STRING_32): detachable CMS_USER
-- User for the given password token `a_token', if any.
local
l_parameters: STRING_TABLE [detachable ANY]
do
error_handler.reset
write_information_log (generator + ".user_by_oauth2_gmail_token")
create l_parameters.make (1)
l_parameters.put (a_token, "token")
sql_query (select_user_by_oauth2_gmail_token, l_parameters)
if sql_rows_count = 1 then
Result := fetch_user
else
check no_more_than_one: sql_rows_count = 0 end
end
end
user_oauth2_gmail_by_id (a_uid: like {CMS_USER}.id): detachable CMS_USER
-- User for the given password token `a_token', if any.
local
l_parameters: STRING_TABLE [detachable ANY]
do
error_handler.reset
write_information_log (generator + ".user_oauth2_gmail_by_id")
create l_parameters.make (1)
l_parameters.put (a_uid, "uid")
sql_query (select_user_oauth2_gmail_by_id, l_parameters)
if sql_rows_count = 1 then
Result := fetch_user
else
check no_more_than_one: sql_rows_count = 0 end
end
end
feature {NONE} -- Implementation: User
user_salt (a_username: READABLE_STRING_32): detachable READABLE_STRING_8
@@ -830,14 +753,4 @@ feature {NONE} -- User Password Recovery
Select_user_by_password_token: STRING = "SELECT u.* FROM users as u JOIN users_password_recovery as ua ON ua.uid = u.uid and ua.token = :token;"
-- Retrieve user by password token if exist.
feature {NONE}-- User Oauth2 Gmail.
Sql_insert_oauth2_gmail: STRING = "INSERT INTO oauth2_gmail (uid, access_token, details, created) VALUES (:uid, :token, :profile, :utc_date);"
Sql_update_oauth2_gmail: STRING = "UPDATE oauth2_gmail SET access_token = :token, details = :profile WHERE uid =:uid;"
Select_user_by_oauth2_gmail_token: STRING = "SELECT u.* FROM users as u JOIN oauth2_gmail as og ON og.uid = u.uid and og.access_token = :token;"
Select_user_oauth2_gmail_by_id: STRING = "SELECT u.* FROM users as u JOIN oauth2_gmail as og ON og.uid = u.uid and og.uid = :uid;"
end

View File

@@ -151,35 +151,6 @@ feature -- Change User
storage.update_user (a_user)
end
new_user_oauth2_gmail (a_token: READABLE_STRING_32; a_user_profile: READABLE_STRING_32; a_user: CMS_USER)
-- Add a new user with oauth2 gmail authentication.
require
has_id: a_user.has_id
do
storage.new_user_oauth2_gmail (a_token, a_user_profile, a_user)
end
update_user_oauth2_gmail (a_token: READABLE_STRING_32; a_user_profile: READABLE_STRING_32; a_user: CMS_USER)
-- Updaate user `a_user' with oauth2 gmail authentication.
require
has_id: a_user.has_id
do
storage.update_user_oauth2_gmail (a_token, a_user_profile, a_user)
end
user_oauth2_gmail_by_id (a_uid: like {CMS_USER}.id): detachable CMS_USER
do
Result := storage.user_oauth2_gmail_by_id (a_uid)
end
user_by_oauth2_gmail_token (a_token: READABLE_STRING_32): detachable CMS_USER
do
Result := storage.user_by_oauth2_gmail_token (a_token)
end
feature -- User Activation
new_activation (a_token: READABLE_STRING_32; a_id: INTEGER_64)