Skip to content

Commit

Permalink
v2
Browse files Browse the repository at this point in the history
  • Loading branch information
essenciary committed Jul 28, 2022
2 parents 08522cc + f84b877 commit 239e4ce
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 31 deletions.
10 changes: 8 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
name = "GenieAuthentication"
uuid = "e115e502-7e3a-11e9-29b2-aba8be6c6778"
authors = ["Adrian Salceanu <e@essenciary.com>"]
version = "1.2.1"
version = "2.0.0"

[deps]
Genie = "c43c736e-a2d1-11e8-161f-af95117fbd1e"
GeniePlugins = "07906039-e6ad-44b0-b988-2f2de6a70eec"
GenieSession = "03cc5b98-4f21-4eb6-99f2-22eced81f962"
GenieSessionFileSession = "5c4fdc26-39e3-47cf-9034-e533e09961c2"
SHA = "ea8e919c-243c-51af-8825-aaa63cd721ce"
SearchLight = "340e8cb6-72eb-11e8-37ce-c97ebeb32050"

[compat]
Genie = "3, 4"
Genie = "5"
GeniePlugins = "1"
GenieSession = "1"
GenieSessionFileSession = "1"
SearchLight = "1, 2"
julia = "1.6"
12 changes: 8 additions & 4 deletions files/app/resources/authentication/AuthenticationController.jl
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
module AuthenticationController

using Genie, Genie.Renderer, Genie.Renderer.Html, Genie.Flash
using Genie, Genie.Renderer, Genie.Renderer.Html
using SearchLight
using GenieAuthentication
using ViewHelper
using Users
using Logging

using GenieSession
using GenieSession.Flash
using GenieSessionFileSession


function show_login()
html(:authentication, :login, context = @__MODULE__)
Expand All @@ -15,7 +19,7 @@ end
function login()
try
user = findone(User, username = params(:username), password = Users.hash_password(params(:password)))
authenticate(user.id, Genie.Sessions.session(params()))
authenticate(user.id, GenieSession.session(params()))

redirect(:success)
catch ex
Expand All @@ -30,7 +34,7 @@ function success()
end

function logout()
deauthenticate(Genie.Sessions.session(params()))
deauthenticate(GenieSession.session(params()))

flash("Good bye! ")

Expand All @@ -48,7 +52,7 @@ function register()
name = params(:name),
email = params(:email)) |> save!

authenticate(user.id, Genie.Sessions.session(params()))
authenticate(user.id, GenieSession.session(params()))

"Registration successful"
catch ex
Expand Down
2 changes: 0 additions & 2 deletions files/plugins/genie_authentication.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using Genie

Genie.Sessions.init()

import AuthenticationController
import SearchLight: findone
import Users
Expand Down
48 changes: 25 additions & 23 deletions src/GenieAuthentication.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ Functionality for authenticating Genie users.
module GenieAuthentication

import Genie, SearchLight
import GenieSession, GenieSessionFileSession
import GeniePlugins

export authenticate, deauthenticate, is_authenticated, get_authentication, authenticated
export login, logout, with_authentication, without_authentication, @authenticated!
Expand All @@ -14,27 +16,27 @@ const USER_ID_KEY = :__auth_user_id
"""
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)
function authenticate(user_id::Any, session::GenieSession.Session) :: GenieSession.Session
GenieSession.set!(session, USER_ID_KEY, user_id)
end
function authenticate(user_id::SearchLight.DbId, session::Genie.Sessions.Session)
function authenticate(user_id::SearchLight.DbId, session::GenieSession.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
function authenticate(user_id::Union{String,Symbol,Int,SearchLight.DbId}, params::Dict{Symbol,Any} = Genie.Requests.payload()) :: GenieSession.Session
authenticate(user_id, params[:SESSION])
end


"""
deauthenticate(session) :: Sessions.Session
deauthenticate(params::Dict{Symbol,Any}) :: Sessions.Session
deauthenticate(session)
deauthenticate(params::Dict{Symbol,Any})
Removes the user id from the session.
"""
function deauthenticate(session::Genie.Sessions.Session) :: Genie.Sessions.Session
Genie.Sessions.unset!(session, USER_ID_KEY)
function deauthenticate(session::GenieSession.Session) :: GenieSession.Session
GenieSession.unset!(session, USER_ID_KEY)
end
function deauthenticate(params::Dict = Genie.Requests.payload()) :: Genie.Sessions.Session
function deauthenticate(params::Dict = Genie.Requests.payload()) :: GenieSession.Session
deauthenticate(params[:SESSION])
end

Expand All @@ -45,8 +47,8 @@ end
Returns `true` if a user id is stored on the session.
"""
function is_authenticated(session::Union{Genie.Sessions.Session,Nothing}) :: Bool
Genie.Sessions.isset(session, USER_ID_KEY)
function is_authenticated(session::Union{GenieSession.Session,Nothing}) :: Bool
GenieSession.isset(session, USER_ID_KEY)
end
function is_authenticated(params::Dict = Genie.Requests.payload()) :: Bool
is_authenticated(params[:SESSION])
Expand All @@ -71,8 +73,8 @@ end
Returns the user id stored on the session, if available.
"""
function get_authentication(session::Genie.Sessions.Session) :: Union{Nothing,Any}
Genie.Sessions.get(session, USER_ID_KEY)
function get_authentication(session::GenieSession.Session) :: Union{Nothing,Any}
GenieSession.get(session, USER_ID_KEY)
end
function get_authentication(params::Dict = Genie.Requests.payload()) :: Union{Nothing,Any}
get_authentication(params[:SESSION])
Expand All @@ -82,15 +84,15 @@ const authentication = get_authentication


"""
login(user, session) :: Union{Nothing,Genie.Sessions.Session}
login(user, params::Dict{Symbol,Any}) :: Union{Nothing,Genie.Sessions.Session}
login(user, session)
login(user, params::Dict{Symbol,Any})
Persists on session the id of the user object and returns the session.
"""
function login(user::M, session::Genie.Sessions.Session)::Union{Nothing,Genie.Sessions.Session} where {M<:SearchLight.AbstractModel}
function login(user::M, session::GenieSession.Session)::Union{Nothing,GenieSession.Session} where {M<:SearchLight.AbstractModel}
authenticate(getfield(user, Symbol(pk(user))), session)
end
function login(user::M, params::Dict = Genie.Requests.payload())::Union{Nothing,Genie.Sessions.Session} where {M<:SearchLight.AbstractModel}
function login(user::M, params::Dict = Genie.Requests.payload())::Union{Nothing,GenieSession.Session} where {M<:SearchLight.AbstractModel}
login(user, params[:SESSION])
end

Expand All @@ -101,10 +103,10 @@ end
Deletes the id of the user object from the session, effectively logging the user off.
"""
function logout(session::Genie.Sessions.Session) :: Genie.Sessions.Session
function logout(session::GenieSession.Session) :: GenieSession.Session
deauthenticate(session)
end
function logout(params::Dict = Genie.Requests.payload()) :: Genie.Sessions.Session
function logout(params::Dict = Genie.Requests.payload()) :: GenieSession.Session
logout(params[:SESSION])
end

Expand All @@ -115,7 +117,7 @@ end
Invokes `f` only if a user is currently authenticated on the session, `fallback` is invoked otherwise.
"""
function with_authentication(f::Function, fallback::Function, session::Union{Genie.Sessions.Session,Nothing})
function with_authentication(f::Function, fallback::Function, session::Union{GenieSession.Session,Nothing})
if ! is_authenticated(session)
fallback()
else
Expand All @@ -133,7 +135,7 @@ end
Invokes `f` if there is no user authenticated on the current session.
"""
function without_authentication(f::Function, session::Genie.Sessions.Session)
function without_authentication(f::Function, session::GenieSession.Session)
! is_authenticated(session) && f()
end
function without_authentication(f::Function, params::Dict = Genie.Requests.payload())
Expand All @@ -147,7 +149,7 @@ end
Copies the plugin's files into the host Genie application.
"""
function install(dest::String; force = false, debug = false) :: Nothing
src = abspath(normpath(joinpath(pathof(@__MODULE__) |> dirname, "..", Genie.Plugins.FILES_FOLDER)))
src = abspath(normpath(joinpath(pathof(@__MODULE__) |> dirname, "..", GeniePlugins.FILES_FOLDER)))

debug && @info "Preparing to install from $src into $dest"
debug && @info "Found these to install $(readdir(src))"
Expand All @@ -160,7 +162,7 @@ function install(dest::String; force = false, debug = false) :: Nothing

debug && "Installing from $(joinpath(src, f))"

Genie.Plugins.install(joinpath(src, f), dest, force = force)
GeniePlugins.install(joinpath(src, f), dest, force = force)
end

nothing
Expand Down

2 comments on commit 239e4ce

@essenciary
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/65203

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v2.0.0 -m "<description of version>" 239e4cea13be0272acb4b923823f772ead727087
git push origin v2.0.0

Please sign in to comment.