Class: Whoosh::Auth::ApiKey

Inherits:
Object
  • Object
show all
Defined in:
lib/whoosh/auth/api_key.rb

Instance Method Summary collapse

Constructor Details

#initialize(keys: {}, header: "X-Api-Key") ⇒ ApiKey

Returns a new instance of ApiKey.



6
7
8
9
10
# File 'lib/whoosh/auth/api_key.rb', line 6

def initialize(keys: {}, header: "X-Api-Key")
  @keys = keys.dup
  @header = header
  @mutex = Mutex.new
end

Instance Method Details

#authenticate(request) ⇒ Object



12
13
14
15
16
17
18
19
# File 'lib/whoosh/auth/api_key.rb', line 12

def authenticate(request)
  raw_value = request.headers[@header]
  raise Errors::UnauthorizedError, "Missing API key" unless raw_value
  key = raw_value.sub(/\ABearer\s+/i, "")
   = @keys[key]
  raise Errors::UnauthorizedError, "Invalid API key" unless 
  { key: key, ** }
end

#register_key(key, **metadata) ⇒ Object



21
22
23
# File 'lib/whoosh/auth/api_key.rb', line 21

def register_key(key, **)
  @mutex.synchronize { @keys[key] =  }
end

#revoke_key(key) ⇒ Object



25
26
27
# File 'lib/whoosh/auth/api_key.rb', line 25

def revoke_key(key)
  @mutex.synchronize { @keys.delete(key) }
end