From 0ffd43dd5ce5cf974d63670c58bbc87287b2a3a1 Mon Sep 17 00:00:00 2001 From: Adrian Salceanu Date: Thu, 17 Jun 2021 19:42:55 +0200 Subject: [PATCH] v1 --- Project.toml | 4 +- .../AuthenticationController.jl | 41 +++++++++++-------- .../authentication/views/success.jl.html | 11 +++++ files/plugins/genie_authentication.jl | 7 +++- src/GenieAuthentication.jl | 39 ++++++++++++------ 5 files changed, 69 insertions(+), 33 deletions(-) create mode 100644 files/app/resources/authentication/views/success.jl.html diff --git a/Project.toml b/Project.toml index 6d5e42e..3fd4cf0 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "GenieAuthentication" uuid = "e115e502-7e3a-11e9-29b2-aba8be6c6778" authors = ["Adrian Salceanu "] -version = "1.0-DEV" +version = "1.0" [deps] Genie = "c43c736e-a2d1-11e8-161f-af95117fbd1e" @@ -10,5 +10,5 @@ SearchLight = "340e8cb6-72eb-11e8-37ce-c97ebeb32050" [compat] Genie = "1" -SearchLight = "0.21, 0.22" +SearchLight = "0.21, 0.22, 1" julia = "1" \ No newline at end of file diff --git a/files/app/resources/authentication/AuthenticationController.jl b/files/app/resources/authentication/AuthenticationController.jl index b187b16..2f08c54 100644 --- a/files/app/resources/authentication/AuthenticationController.jl +++ b/files/app/resources/authentication/AuthenticationController.jl @@ -1,61 +1,66 @@ module AuthenticationController -using Genie, Genie.Renderer, Genie.Renderer.Html, Genie.Router +using Genie, Genie.Renderer, Genie.Renderer.Html, Genie.Flash using SearchLight using GenieAuthentication using ViewHelper using Users using Logging + function show_login() html(:authentication, :login, context = @__MODULE__) end function login() try - user = SearchLight.findone(User, username = Genie.Router.@params(:username), password = Users.hash_password(Genie.Router.@params(:password))) - GenieAuthentication.authenticate(user.id, Genie.Sessions.session(Genie.Router.@params)) + user = findone(User, username = @params(:username), password = Users.hash_password(@params(:password))) + authenticate(user.id, Genie.Sessions.session(@params)) - Genie.Renderer.redirect(:get_home) + redirect(:success) catch ex - Genie.Flash.flash("Authentication failed") + flash("Authentication failed! ") - Genie.Renderer.redirect(:show_login) + redirect(:show_login) end end +function success() + html(:authentication, :success, context = @__MODULE__) +end + function logout() - GenieAuthentication.deauthenticate(Genie.Sessions.session(Genie.Router.@params)) + deauthenticate(Genie.Sessions.session(@params)) - Genie.Flash.flash("Good bye! ") + flash("Good bye! ") - Genie.Renderer.redirect(:show_login) + redirect(:show_login) end function show_register() - Genie.Renderer.Html.html(:authentication, :register, context = @__MODULE__) + html(:authentication, :register, context = @__MODULE__) end function register() try - user = User(username = Genie.Router.@params(:username), - password = Genie.Router.@params(:password) |> Users.hash_password, - name = Genie.Router.@params(:name), - email = Genie.Router.@params(:email)) |> SearchLight.save! + user = User(username = @params(:username), + password = @params(:password) |> Users.hash_password, + name = @params(:name), + email = @params(:email)) |> save! - GenieAuthentication.authenticate(user.id, Genie.Sessions.session(Genie.Router.@params)) + authenticate(user.id, Genie.Sessions.session(@params)) "Registration successful" catch ex @error ex if hasfield(typeof(ex), :msg) - Genie.Flash.flash(ex.msg) + flash(ex.msg) else - Genie.Flash.flash(string(ex)) + flash(string(ex)) end - Genie.Renderer.redirect(:show_register) + redirect(:show_register) end end diff --git a/files/app/resources/authentication/views/success.jl.html b/files/app/resources/authentication/views/success.jl.html new file mode 100644 index 0000000..e37a506 --- /dev/null +++ b/files/app/resources/authentication/views/success.jl.html @@ -0,0 +1,11 @@ +

Success!

+ +$(output_flash()) + +
+

+ You are now logged in! + + Go home +

+
\ No newline at end of file diff --git a/files/plugins/genie_authentication.jl b/files/plugins/genie_authentication.jl index 47ca633..0b3898c 100644 --- a/files/plugins/genie_authentication.jl +++ b/files/plugins/genie_authentication.jl @@ -2,11 +2,16 @@ using Genie Genie.Sessions.init() -using Genie.Router import AuthenticationController +import SearchLight: findone +import Users + +export current_user +current_user() = findone(Users.User, id = get_authentication()) route("/login", AuthenticationController.show_login, named = :show_login) route("/login", AuthenticationController.login, method = POST, named = :login) +route("/success", AuthenticationController.success, method = GET, named = :success) route("/logout", AuthenticationController.logout, named = :logout) #===# diff --git a/src/GenieAuthentication.jl b/src/GenieAuthentication.jl index 4af48c2..377b8c9 100644 --- a/src/GenieAuthentication.jl +++ b/src/GenieAuthentication.jl @@ -6,7 +6,7 @@ module GenieAuthentication import Genie, SearchLight export authenticate, deauthenticate, is_authenticated, get_authentication, authenticated -export login, logout, with_authentication, without_authentication +export login, logout, with_authentication, without_authentication, @authenticated! const USER_ID_KEY = :__auth_user_id @@ -17,8 +17,8 @@ Stores the user id on the session. function authenticate(user_id::Any, session::Genie.Sessions.Session) :: Genie.Sessions.Session Genie.Sessions.set!(session, USER_ID_KEY, user_id) end -function authenticate(user::SearchLight.DbId, session::Genie.Sessions.Session) - authenticate(Int(user.value), session) +function authenticate(user_id::SearchLight.DbId, session::Genie.Sessions.Session) + authenticate(Int(user_id.value), session) end function authenticate(user_id::Union{String,Symbol,Int,SearchLight.DbId}, params::Dict{Symbol,Any} = Genie.Requests.payload()) :: Genie.Sessions.Session authenticate(user_id, params[:SESSION]) @@ -56,8 +56,18 @@ const authenticated = is_authenticated """ - get_authentication(session) :: Nullable - get_authentication(params::Dict{Symbol,Any}) :: Nullable + @authenticate!(exception::E = ExceptionalResponse(Genie.Renderer.redirect(:show_login))) + +If the current request is not authenticated it throws an ExceptionalResponse exception. +""" +macro authenticated!(exception = Genie.Exceptions.ExceptionalResponse(Genie.Renderer.redirect(:show_login))) + :(GenieAuthentication.authenticated() || throw($exception)) +end + + +""" + get_authentication(session) :: Union{Nothing,Any} + get_authentication(params::Dict{Symbol,Any}) :: Union{Nothing,Any} Returns the user id stored on the session, if available. """ @@ -72,15 +82,15 @@ const authentication = get_authentication """ - login(user, session) :: Nullable{Sessions.Session} - login(user, params::Dict{Symbol,Any}) :: Nullable{Sessions.Session} + login(user, session) :: Union{Nothing,Genie.Sessions.Session} + login(user, params::Dict{Symbol,Any}) :: Union{Nothing,Genie.Sessions.Session} Persists on session the id of the user object and returns the session. """ -function login(user, session::Genie.Sessions.Session) :: Union{Nothing,Genie.Sessions.Session} - authenticate(getfield(user, Symbol(user._id)), session) +function login(user::M, session::Genie.Sessions.Session)::Union{Nothing,Genie.Sessions.Session} where {M<:SearchLight.AbstractModel} + authenticate(getfield(user, Symbol(pk(user))), session) end -function login(user, params::Dict{Symbol,Any} = Genie.Requests.payload()) :: Union{Nothing,Genie.Sessions.Session} +function login(user::M, params::Dict{Symbol,Any} = Genie.Requests.payload())::Union{Nothing,Genie.Sessions.Session} where {M<:SearchLight.AbstractModel} login(user, params[:SESSION]) end @@ -132,8 +142,11 @@ end """ + install(dest::String; force = false, debug = false) :: Nothing + +Copies the plugin's files into the host Genie application. """ -function install(dest::String; force = false, debug = false) +function install(dest::String; force = false, debug = false) :: Nothing src = abspath(normpath(joinpath(pathof(@__MODULE__) |> dirname, "..", Genie.Plugins.FILES_FOLDER))) debug && @info "Preparing to install from $src into $dest" @@ -149,6 +162,8 @@ function install(dest::String; force = false, debug = false) Genie.Plugins.install(joinpath(src, f), dest, force = force) end -end + nothing end + +end \ No newline at end of file