Class: RackJwtAegis::RequestContext

Inherits:
Object
  • Object
show all
Defined in:
lib/rack_jwt_aegis/request_context.rb

Overview

Request context manager for storing JWT authentication data in Rack env

Stores authenticated user and tenant information in the Rack environment hash for easy access by downstream application code. Provides both instance methods for setting context and class methods for reading.

Examples:

Setting context (done by middleware)

context = RequestContext.new(config)
context.set_context(env, jwt_payload)

Reading context in application

user_id = RequestContext.user_id(request.env)
tenant_id = RequestContext.tenant_id(request.env)
authenticated = RequestContext.authenticated?(request.env)

Author:

  • Ken Camajalan Demanawa

Since:

  • 0.1.0

Constant Summary collapse

JWT_PAYLOAD_KEY =

Standard environment keys for JWT data

Since:

  • 0.1.0

'rack_jwt_aegis.payload'
USER_ID_KEY =

Since:

  • 0.1.0

'rack_jwt_aegis.user_id'
TENANT_ID_KEY =

Since:

  • 0.1.0

'rack_jwt_aegis.tenant_id'
SUBDOMAIN_KEY =

Since:

  • 0.1.0

'rack_jwt_aegis.subdomain'
PATHNAME_SLUGS_KEY =

Since:

  • 0.1.0

'rack_jwt_aegis.pathname_slugs'
AUTHENTICATED_KEY =

Since:

  • 0.1.0

'rack_jwt_aegis.authenticated'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ RequestContext

Initialize the request context manager

Parameters:

Since:

  • 0.1.0



33
34
35
# File 'lib/rack_jwt_aegis/request_context.rb', line 33

def initialize(config)
  @config = config
end

Class Method Details

.authenticated?(env) ⇒ Boolean

Check if the request is authenticated

Parameters:

  • env (Hash)

    the Rack environment hash

Returns:

  • (Boolean)

    true if request is authenticated

Since:

  • 0.1.0



59
60
61
# File 'lib/rack_jwt_aegis/request_context.rb', line 59

def self.authenticated?(env)
  !!env[AUTHENTICATED_KEY]
end

.current_tenant_id(request) ⇒ Object

Since:

  • 0.1.0



99
100
101
# File 'lib/rack_jwt_aegis/request_context.rb', line 99

def self.current_tenant_id(request)
  tenant_id(request.env)
end

.current_user_id(request) ⇒ Object

Since:

  • 0.1.0



95
96
97
# File 'lib/rack_jwt_aegis/request_context.rb', line 95

def self.current_user_id(request)
  user_id(request.env)
end

.has_pathname_slug_access?(env, pathname_slug) ⇒ Boolean

Returns:

  • (Boolean)

Since:

  • 0.1.0



103
104
105
# File 'lib/rack_jwt_aegis/request_context.rb', line 103

def self.has_pathname_slug_access?(env, pathname_slug)
  pathname_slugs(env).include?(pathname_slug)
end

.pathname_slugs(env) ⇒ Object

Since:

  • 0.1.0



91
92
93
# File 'lib/rack_jwt_aegis/request_context.rb', line 91

def self.pathname_slugs(env)
  env[PATHNAME_SLUGS_KEY] || []
end

.payload(env) ⇒ Hash?

Get the full JWT payload from the request

Parameters:

  • env (Hash)

    the Rack environment hash

Returns:

  • (Hash, nil)

    the JWT payload or nil if not authenticated

Since:

  • 0.1.0



67
68
69
# File 'lib/rack_jwt_aegis/request_context.rb', line 67

def self.payload(env)
  env[JWT_PAYLOAD_KEY]
end

.subdomain(env) ⇒ Object

Since:

  • 0.1.0



87
88
89
# File 'lib/rack_jwt_aegis/request_context.rb', line 87

def self.subdomain(env)
  env[SUBDOMAIN_KEY]
end

.tenant_id(env) ⇒ String, ...

Get the tenant ID

Parameters:

  • env (Hash)

    the Rack environment hash

Returns:

  • (String, Integer, nil)

    the tenant ID or nil if not available

Since:

  • 0.1.0



83
84
85
# File 'lib/rack_jwt_aegis/request_context.rb', line 83

def self.tenant_id(env)
  env[TENANT_ID_KEY]
end

.user_id(env) ⇒ String, ...

Get the authenticated user ID

Parameters:

  • env (Hash)

    the Rack environment hash

Returns:

  • (String, Integer, nil)

    the user ID or nil if not available

Since:

  • 0.1.0



75
76
77
# File 'lib/rack_jwt_aegis/request_context.rb', line 75

def self.user_id(env)
  env[USER_ID_KEY]
end

Instance Method Details

#set_context(env, payload) ⇒ Object

Set JWT authentication context in the Rack environment

Parameters:

  • env (Hash)

    the Rack environment hash

  • payload (Hash)

    the validated JWT payload

Since:

  • 0.1.0



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/rack_jwt_aegis/request_context.rb', line 41

def set_context(env, payload)
  # Set the full payload
  env[JWT_PAYLOAD_KEY] = payload

  # Set authentication flag
  env[AUTHENTICATED_KEY] = true

  # Extract and set commonly used values for easy access
  set_user_context(env, payload)
  set_tenant_context(env, payload)
end

#set_tenant_context(env, payload) ⇒ Object (private)

Since:

  • 0.1.0



113
114
115
116
117
118
119
120
# File 'lib/rack_jwt_aegis/request_context.rb', line 113

def set_tenant_context(env, payload)
  # Set multi-tenant information
  env[TENANT_ID_KEY] = payload[@config.payload_key(:tenant_id).to_s] if @config.validate_tenant_id?
  env[SUBDOMAIN_KEY] = payload[@config.payload_key(:subdomain).to_s] if @config.validate_subdomain?
  return unless @config.validate_pathname_slug? || @config.payload_mapping.key?(:pathname_slugs)

  env[PATHNAME_SLUGS_KEY] = Array(payload[@config.payload_key(:pathname_slugs).to_s]).flatten
end

#set_user_context(env, payload) ⇒ Object (private)

Since:

  • 0.1.0



109
110
111
# File 'lib/rack_jwt_aegis/request_context.rb', line 109

def set_user_context(env, payload)
  env[USER_ID_KEY] = payload[@config.payload_key(:user_id).to_s]
end