Skip to content

Commit 980137a

Browse files
committed
Support for base auth + cleanup
1 parent fad0717 commit 980137a

4 files changed

Lines changed: 40 additions & 5 deletions

File tree

Project.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
name = "GenieAuthentication"
22
uuid = "e115e502-7e3a-11e9-29b2-aba8be6c6778"
33
authors = ["Adrian Salceanu <e@essenciary.com>"]
4-
version = "2.2.0"
4+
version = "2.3.0"
55

66
[deps]
7+
Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"
78
Genie = "c43c736e-a2d1-11e8-161f-af95117fbd1e"
89
GeniePlugins = "07906039-e6ad-44b0-b988-2f2de6a70eec"
910
GenieSession = "03cc5b98-4f21-4eb6-99f2-22eced81f962"

files/app/resources/users/Users.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Validation.validator(u::Type{User}) = ModelValidator([
2424
ValidationRule(:name, UsersValidator.not_empty)
2525
])
2626

27-
function hash_password(password::String)
27+
function hash_password(password::AbstractString)
2828
sha256(password) |> bytes2hex
2929
end
3030

files/app/resources/users/UsersValidator.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ module UsersValidator
22

33
using SearchLight, SearchLight.Validation, SearchLight.QueryBuilder
44

5-
function not_empty(field::Symbol, m::T, args::Vararg{Any})::ValidationResult where {T<:AbstractModel}
5+
function not_empty(field::Symbol, m::T)::ValidationResult where {T<:AbstractModel}
66
isempty(getfield(m, field)) && return ValidationResult(invalid, :not_empty, "should not be empty")
77

88
ValidationResult(valid)
99
end
1010

11-
function unique(field::Symbol, m::T, args::Vararg{Any})::ValidationResult where {T<:AbstractModel}
11+
function unique(field::Symbol, m::T)::ValidationResult where {T<:AbstractModel}
1212
ispersisted(m) && return ValidationResult(valid) # don't validate updates
1313

1414
if SearchLight.count(typeof(m), where("$field = ?", getfield(m, field))) > 0

src/GenieAuthentication.jl

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,14 @@ import GenieSession, GenieSessionFileSession
88
import GeniePlugins
99
import SHA
1010

11+
using Base64
12+
1113
export authenticate, deauthenticate, is_authenticated, isauthenticated, get_authentication, authenticated
1214
export login, logout, with_authentication, without_authentication, @authenticated!, @with_authentication!, authenticated!
1315

1416
const USER_ID_KEY = :__auth_user_id
15-
17+
const PARAMS_USERNAME_KEY = :username
18+
const PARAMS_PASSWORD_KEY = :password
1619

1720
"""
1821
Stores the user id on the session.
@@ -184,4 +187,35 @@ function install(dest::String; force = false, debug = false) :: Nothing
184187
nothing
185188
end
186189

190+
191+
function basicauthparams(req, res, params)
192+
headers = Dict(req.headers)
193+
if haskey(headers, "Authorization")
194+
auth = headers["Authorization"]
195+
if startswith(auth, "Basic ")
196+
try
197+
auth = String(base64decode(auth[7:end]))
198+
auth = split(auth, ":")
199+
params[PARAMS_USERNAME_KEY] = auth[1]
200+
params[PARAMS_PASSWORD_KEY] = auth[2]
201+
catch _
202+
end
203+
end
204+
end
205+
206+
req, res, params
207+
end
208+
209+
210+
function isbasicauthrequest(params::Dict = Genie.Requests.payload()) :: Bool
211+
haskey(params, PARAMS_USERNAME_KEY) && haskey(params, PARAMS_PASSWORD_KEY)
212+
end
213+
214+
215+
function __init__() :: Nothing
216+
GenieAuthentication.basicauthparams in Genie.Router.pre_match_hooks || pushfirst!(Genie.Router.pre_match_hooks, GenieAuthentication.basicauthparams)
217+
218+
nothing
219+
end
220+
187221
end

0 commit comments

Comments
 (0)