Use password from config in basic auth

This commit is contained in:
Roberto Alsina 2024-07-07 13:29:12 -03:00
parent 31ab54478a
commit 7dd5248a6e
4 changed files with 31 additions and 4 deletions

View File

@ -19,6 +19,9 @@
* ✅ Streaming responses in slow operations like scaling down * ✅ Streaming responses in slow operations like scaling down
or building or building
* Make more things configurable / remove hardcoded stuff * Make more things configurable / remove hardcoded stuff
* ✅ Make server take options from file
* ✅ Make server take options from environment
* ✅ Make server password configurable
* admin/admin auth * admin/admin auth
* CD for binaries and images for at least arm64/x86 * CD for binaries and images for at least arm64/x86
* Multi-container docker logs [faaso logs -f FUNKO] * Multi-container docker logs [faaso logs -f FUNKO]

View File

@ -26,7 +26,7 @@ shards:
docr: docr:
git: https://github.com/ralsina/docr.git git: https://github.com/ralsina/docr.git
version: 0.1.1+git.commit.18f15cc7111b1d0c63347c7cca07aee9ec87a7a8 version: 0.1.1+git.commit.98a20178d5ae1391f1cd56e372530de6aa2b1ebc
exception_page: exception_page:
git: https://github.com/crystal-loot/exception_page.git git: https://github.com/crystal-loot/exception_page.git

View File

@ -1,4 +1,5 @@
require "cr-config" require "cr-config"
require "kemal-basic-auth"
class Config class Config
include CrConfig include CrConfig
@ -17,3 +18,23 @@ class Config
Config.set_instance config Config.set_instance config
end end
end end
class ConfigAuthHandler < Kemal::BasicAuth::Handler
def initialize
# Ignored, just make the compiler happy
@credentials = Kemal::BasicAuth::Credentials.new({"foo" => "bar"})
end
def authorize?(value) : String?
username, password = Base64.decode_string(value[BASIC.size + 1..-1]).split(":")
if username == "admin" && password == Config.instance.password
username
else
nil
end
end
end
# Tie auth to config
add_handler ConfigAuthHandler.new

View File

@ -6,14 +6,11 @@ require "./terminal.cr"
require "compress/gzip" require "compress/gzip"
require "crystar" require "crystar"
require "docr" require "docr"
require "kemal-basic-auth"
require "kemal" require "kemal"
require "uuid" require "uuid"
Config.load Config.load
basic_auth "admin", Config.instance.password
macro version macro version
"{{ `grep version shard.yml | cut -d: -f2` }}".strip() "{{ `grep version shard.yml | cut -d: -f2` }}".strip()
end end
@ -26,4 +23,10 @@ get "/version" do
"#{version}" "#{version}"
end end
get "/reload" do
Log.info { "Reloading configuration" }
Config.load
"Config reloaded"
end
Kemal.run Kemal.run