Class: Toolchest::Auth::Token

Inherits:
Base
  • Object
show all
Defined in:
lib/toolchest/auth/token.rb

Defined Under Namespace

Classes: EnvTokenRecord

Instance Method Summary collapse

Instance Method Details

#authenticate(request) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/toolchest/auth/token.rb', line 6

def authenticate(request)
  token_string = extract_bearer_token(request)
  return nil unless token_string

  token_record = find_token(token_string)
  return nil unless token_record

  scopes = if token_record.respond_to?(:scopes_array)
    token_record.scopes_array
  elsif token_record.respond_to?(:scopes)
    Array(token_record.scopes).flat_map { |s| s.split(" ") }.reject(&:empty?)
  else
    []
  end

  config = Toolchest.configuration
  owner = if config.respond_to?(:authenticate_with) && config.send(:instance_variable_get, :@authenticate_block)
    config.authenticate_with(token_record)
  end

  AuthContext.new(
    resource_owner: owner,
    scopes: scopes,
    token: token_record
  )
end