Class: Arcp::Auth::Bearer

Inherits:
Object
  • Object
show all
Includes:
AuthScheme
Defined in:
lib/arcp/auth/bearer.rb

Overview

Static-token bearer verifier. Maps token strings to Principals. For production, plug a custom verifier implementing ‘#verify(token) -> Principal | nil`.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tokens: {}) ⇒ Bearer

Returns a new instance of Bearer.



11
12
13
# File 'lib/arcp/auth/bearer.rb', line 11

def initialize(tokens: {})
  @tokens = tokens.dup.freeze
end

Class Method Details

.from_token(token, principal_id: 'anonymous', scopes: []) ⇒ Object



33
34
35
# File 'lib/arcp/auth/bearer.rb', line 33

def self.from_token(token, principal_id: 'anonymous', scopes: [])
  new(tokens: { token => Principal.new(id: principal_id, name: principal_id, scopes: scopes.freeze) })
end

Instance Method Details

#verify(token) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/arcp/auth/bearer.rb', line 15

def verify(token)
  return nil if token.nil?

  principal = @tokens[token]
  return nil unless principal

  case principal
  when Principal then principal
  when String then Principal.new(id: principal, name: principal, scopes: [].freeze)
  when Hash
    Principal.new(
      id: principal[:id] || principal['id'],
      name: principal[:name] || principal['name'],
      scopes: Array(principal[:scopes] || principal['scopes']).freeze
    )
  end
end