mirror of
				https://gitea.invidious.io/iv-org/shard-kemal.git
				synced 2024-08-15 00:53:36 +00:00 
			
		
		
		
	Remove basic auth middleware
This commit is contained in:
		
							parent
							
								
									13293a675d
								
							
						
					
					
						commit
						b4c10a3f59
					
				
					 4 changed files with 0 additions and 79 deletions
				
			
		|  | @ -3,12 +3,6 @@ def add_handler(handler) | |||
|   Kemal.config.add_handler handler | ||||
| end | ||||
| 
 | ||||
| # Uses Kemal::Middleware::HTTPBasicAuth to easily add HTTP Basic Auth support. | ||||
| def basic_auth(username, password) | ||||
|   auth_handler = Kemal::Middleware::HTTPBasicAuth.new(username, password) | ||||
|   add_handler auth_handler | ||||
| end | ||||
| 
 | ||||
| # Sets public folder from which the static assets will be served. | ||||
| # By default this is `/public` not `src/public`. | ||||
| def public_folder(path) | ||||
|  |  | |||
|  | @ -1,38 +0,0 @@ | |||
| require "base64" | ||||
| 
 | ||||
| module Kemal::Middleware | ||||
|   # This middleware adds HTTP Basic Auth support to your application. | ||||
|   # Returns 401 "Unauthorized" with wrong credentials. | ||||
|   # | ||||
|   # auth_handler = Kemal::Middleware::HTTPBasicAuth.new("username", "password") | ||||
|   # Kemal.config.add_handler auth_handler | ||||
|   # | ||||
|   class HTTPBasicAuth < HTTP::Handler | ||||
|     BASIC                 = "Basic" | ||||
|     AUTH                  = "Authorization" | ||||
|     AUTH_MESSAGE          = "Could not verify your access level for that URL.\nYou have to login with proper credentials" | ||||
|     HEADER_LOGIN_REQUIRED = "Basic realm=\"Login Required\"" | ||||
| 
 | ||||
|     def initialize(@username : String?, @password : String?) | ||||
|     end | ||||
| 
 | ||||
|     def call(context) | ||||
|       if context.request.headers[AUTH]? | ||||
|         if value = context.request.headers[AUTH] | ||||
|           if value.size > 0 && value.starts_with?(BASIC) | ||||
|             return call_next(context) if authorized?(value) | ||||
|           end | ||||
|         end | ||||
|       end | ||||
|       headers = HTTP::Headers.new | ||||
|       context.response.status_code = 401 | ||||
|       context.response.headers["WWW-Authenticate"] = HEADER_LOGIN_REQUIRED | ||||
|       context.response.print AUTH_MESSAGE | ||||
|     end | ||||
| 
 | ||||
|     def authorized?(value) | ||||
|       username, password = Base64.decode_string(value[BASIC.size + 1..-1]).split(":") | ||||
|       @username == username && @password == password | ||||
|     end | ||||
|   end | ||||
| end | ||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue