mirror of
https://github.com/hkalexling/Mango.git
synced 2025-08-05 12:25:32 -04:00
Include port number in token
This commit is contained in:
parent
9b5aea223d
commit
e214e00dfb
@ -9,7 +9,9 @@ class AuthHandler < Kemal::Handler
|
|||||||
def call(env)
|
def call(env)
|
||||||
return call_next(env) if request_path_startswith env, ["/login", "/logout"]
|
return call_next(env) if request_path_startswith env, ["/login", "/logout"]
|
||||||
|
|
||||||
cookie = env.request.cookies.find { |c| c.name == "token" }
|
cookie = env.request.cookies.find do |c|
|
||||||
|
c.name == "token-#{Config.current.port}"
|
||||||
|
end
|
||||||
if cookie.nil? || !@storage.verify_token cookie.value
|
if cookie.nil? || !@storage.verify_token cookie.value
|
||||||
return redirect env, "/login"
|
return redirect env, "/login"
|
||||||
end
|
end
|
||||||
|
@ -9,7 +9,9 @@ class MainRouter < Router
|
|||||||
|
|
||||||
get "/logout" do |env|
|
get "/logout" do |env|
|
||||||
begin
|
begin
|
||||||
cookie = env.request.cookies.find { |c| c.name == "token" }.not_nil!
|
cookie = env.request.cookies.find do |c|
|
||||||
|
c.name == "token-#{Config.current.port}"
|
||||||
|
end.not_nil!
|
||||||
@context.storage.logout cookie.value
|
@context.storage.logout cookie.value
|
||||||
rescue e
|
rescue e
|
||||||
@context.error "Error when attempting to log out: #{e}"
|
@context.error "Error when attempting to log out: #{e}"
|
||||||
@ -24,7 +26,7 @@ class MainRouter < Router
|
|||||||
password = env.params.body["password"]
|
password = env.params.body["password"]
|
||||||
token = @context.storage.verify_user(username, password).not_nil!
|
token = @context.storage.verify_user(username, password).not_nil!
|
||||||
|
|
||||||
cookie = HTTP::Cookie.new "token", token
|
cookie = HTTP::Cookie.new "token-#{Config.current.port}", token
|
||||||
cookie.path = Config.current.base_url
|
cookie.path = Config.current.base_url
|
||||||
cookie.expires = Time.local.shift years: 1
|
cookie.expires = Time.local.shift years: 1
|
||||||
env.response.cookies << cookie
|
env.response.cookies << cookie
|
||||||
|
@ -6,7 +6,9 @@ UPLOAD_URL_PREFIX = "/uploads"
|
|||||||
macro layout(name)
|
macro layout(name)
|
||||||
base_url = Config.current.base_url
|
base_url = Config.current.base_url
|
||||||
begin
|
begin
|
||||||
cookie = env.request.cookies.find { |c| c.name == "token" }
|
cookie = env.request.cookies.find do |c|
|
||||||
|
c.name == "token-#{Config.current.port}"
|
||||||
|
end
|
||||||
is_admin = false
|
is_admin = false
|
||||||
unless cookie.nil?
|
unless cookie.nil?
|
||||||
is_admin = @context.storage.verify_admin cookie.value
|
is_admin = @context.storage.verify_admin cookie.value
|
||||||
@ -26,7 +28,9 @@ end
|
|||||||
macro get_username(env)
|
macro get_username(env)
|
||||||
# if the request gets here, it has gone through the auth handler, and
|
# if the request gets here, it has gone through the auth handler, and
|
||||||
# we can be sure that a valid token exists, so we can use not_nil! here
|
# we can be sure that a valid token exists, so we can use not_nil! here
|
||||||
cookie = {{env}}.request.cookies.find { |c| c.name == "token" }.not_nil!
|
cookie = {{env}}.request.cookies.find do |c|
|
||||||
|
c.name == "token-#{Config.current.port}"
|
||||||
|
end.not_nil!
|
||||||
(@context.storage.verify_token cookie.value).not_nil!
|
(@context.storage.verify_token cookie.value).not_nil!
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user