Class: Steppe::Auth::Basic
- Inherits:
-
Object
- Object
- Steppe::Auth::Basic
- Includes:
- Responses
- Defined in:
- lib/steppe/auth/basic.rb
Overview
HTTP Basic authentication security scheme. Validates username and password credentials from the Authorization header against a credentials store.
Defined Under Namespace
Classes: SimpleUserPasswordStore
Constant Summary collapse
- SCHEME =
'basic'- EXP =
/^Basic\s+([A-Za-z0-9+\/=]+)\s*$/- CredentialsStoreInterface =
Interface for custom credentials store implementations. Required methods:
-
lookup(username): Returns the password for the given username, or nil if not found
-
Types::Interface[:lookup]
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
-
#handle(conn, _required_scopes = nil) ⇒ Steppe::Result::Continue, Steppe::Result::Halt
Handle authentication for a connection.
-
#initialize(name, store:) ⇒ Basic
constructor
A new instance of Basic.
-
#to_openapi ⇒ Hash
Convert this security scheme to OpenAPI 3.0 format.
Constructor Details
#initialize(name, store:) ⇒ Basic
Returns a new instance of Basic.
75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/steppe/auth/basic.rb', line 75 def initialize(name, store:) @name = name @scheme = SCHEME @store = case store when SimpleUserPasswordStore::HashInterface SimpleUserPasswordStore.new(store) when CredentialsStoreInterface store else raise ArgumentError, "expected a CredentialsStoreInterface interface #{CredentialsStoreInterface}, but got #{store.inspect}" end end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
71 72 73 |
# File 'lib/steppe/auth/basic.rb', line 71 def name @name end |
Instance Method Details
#handle(conn, _required_scopes = nil) ⇒ Steppe::Result::Continue, Steppe::Result::Halt
Handle authentication for a connection. Validates the Basic credentials from the Authorization header and checks username/password match.
94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/steppe/auth/basic.rb', line 94 def handle(conn, _required_scopes = nil) auth_str = conn.request.env[HTTP_AUTHORIZATION] return (conn) if auth_str.nil? match = auth_str.match(EXP) return (conn) if match.nil? username, password = decode(match[1]) return forbidden(conn) if @store.lookup(username) != password conn end |
#to_openapi ⇒ Hash
Convert this security scheme to OpenAPI 3.0 format.
110 111 112 113 114 115 |
# File 'lib/steppe/auth/basic.rb', line 110 def to_openapi { 'type' => 'http', 'scheme' => scheme } end |