Class: Foobara::AuthHttp::BearerAuthenticator

Inherits:
CommandConnector::Authenticator show all
Defined in:
foobara-auth-http-0.0.7/src/foobara/auth_http/bearer_authenticator.rb

Instance Attribute Summary

Attributes inherited from Value::Processor

#created_in_namespace, #declaration_data, #parent_declaration_data

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from CommandConnector::Authenticator

#authenticate, #explanation, #symbol, #to_proc, #transform

Methods inherited from Value::Transformer

create, error_classes, foobara_manifest, #process_value, subclass, #transform

Methods inherited from Value::Processor

#always_applicable?, #attribute_name, #build_error, default_declaration_data, #dup_processor, error_class, error_classes, #error_context, #error_message, #error_path, foobara_manifest, #foobara_manifest, #inspect, instance, #method_missing, #name, new_with_agnostic_args, #possible_errors, #priority, #process_outcome, #process_outcome!, #process_value, #process_value!, processor_name, requires_declaration_data?, requires_parent_declaration_data?, #respond_to_missing?, #runner, symbol

Methods included from IsManifestable

#foobara_domain, #foobara_manifest, #foobara_organization, #scoped_clear_caches

Methods included from Concern

foobara_class_methods_module_for, foobara_concern?, included

Constructor Details

#initialize(load_user: nil, relevant_entity_classes: Auth::Types::User) ⇒ BearerAuthenticator

Returns a new instance of BearerAuthenticator.

[View source]

10
11
12
13
14
15
16
17
# File 'foobara-auth-http-0.0.7/src/foobara/auth_http/bearer_authenticator.rb', line 10

def initialize(load_user: nil, relevant_entity_classes: Auth::Types::User, **)
  @load_user = load_user || ->(user_id) { Auth::FindUser.run!(id: user_id) }
  @relevant_entity_classes = relevant_entity_classes

  super(symbol: :bearer,
        explanation: "Expects an access token in authorization header in format of: Bearer <token>",
        **)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Foobara::Value::Processor

Class Method Details

.load_user(&block) ⇒ Object

[View source]

5
6
7
# File 'foobara-auth-http-0.0.7/src/foobara/auth_http/bearer_authenticator.rb', line 5

def load_user(**, &block)
  new(**, load_user: block)
end

Instance Method Details

#applicable?(request) ⇒ Boolean

Returns:

  • (Boolean)
[View source]

19
20
21
# File 'foobara-auth-http-0.0.7/src/foobara/auth_http/bearer_authenticator.rb', line 19

def applicable?(request)
  request.headers.key?("authorization")
end

#blockObject

[View source]

23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'foobara-auth-http-0.0.7/src/foobara/auth_http/bearer_authenticator.rb', line 23

def block
  return @block if @block

  authenticator = self

  @block = proc do
    token = authenticator.extract_token_from_headers(headers)

    if token
      user_id = authenticator.verify_access_token(token)

      if user_id
        authenticator.load_user_record(user_id)
      end
    end
  end
end

#extract_token_from_headers(headers) ⇒ Object

[View source]

41
42
43
44
# File 'foobara-auth-http-0.0.7/src/foobara/auth_http/bearer_authenticator.rb', line 41

def extract_token_from_headers(headers)
  token = headers["authorization"]
  token&.gsub(/^Bearer\s+/, "")&.strip
end

#load_user_record(user_id) ⇒ Object

[View source]

54
55
56
57
58
# File 'foobara-auth-http-0.0.7/src/foobara/auth_http/bearer_authenticator.rb', line 54

def load_user_record(user_id)
  if user_id
    @load_user.call(user_id)
  end
end

#relevant_entity_classes(request) ⇒ Object

[View source]

60
61
62
63
64
# File 'foobara-auth-http-0.0.7/src/foobara/auth_http/bearer_authenticator.rb', line 60

def relevant_entity_classes(request)
  if applicable?(request)
    @relevant_entity_classes
  end
end

#verify_access_token(token) ⇒ Object

[View source]

46
47
48
49
50
51
52
# File 'foobara-auth-http-0.0.7/src/foobara/auth_http/bearer_authenticator.rb', line 46

def verify_access_token(token)
  verification_and_payload = Foobara::Auth::VerifyAccessToken.run!(access_token: token)

  if verification_and_payload[:verified]
    verification_and_payload[:payload]["sub"]
  end
end