Skip to content

Commit

Permalink
v1
Browse files Browse the repository at this point in the history
  • Loading branch information
essenciary committed Jun 17, 2021
1 parent b91ab6f commit 0ffd43d
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 33 deletions.
4 changes: 2 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "GenieAuthentication"
uuid = "e115e502-7e3a-11e9-29b2-aba8be6c6778"
authors = ["Adrian Salceanu <e@essenciary.com>"]
version = "1.0-DEV"
version = "1.0"

[deps]
Genie = "c43c736e-a2d1-11e8-161f-af95117fbd1e"
Expand All @@ -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"
41 changes: 23 additions & 18 deletions files/app/resources/authentication/AuthenticationController.jl
Original file line number Diff line number Diff line change
@@ -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

Expand Down
11 changes: 11 additions & 0 deletions files/app/resources/authentication/views/success.jl.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<h1 class="display-3">Success!</h1>

<span>$(output_flash())</span>

<div class="bs-callout bs-callout-primary">
<p>
You are now logged in!

<a href="/" class="btn btn-link">Go home</a>
</p>
</div>
7 changes: 6 additions & 1 deletion files/plugins/genie_authentication.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)

#===#
Expand Down
39 changes: 27 additions & 12 deletions src/GenieAuthentication.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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])
Expand Down Expand Up @@ -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.
"""
Expand All @@ -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

Expand Down Expand Up @@ -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"
Expand All @@ -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

0 comments on commit 0ffd43d

Please sign in to comment.