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'
TENANT_SLUG_KEY =

Since:

  • 0.1.0

SUBDOMAIN_KEY
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



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

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



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

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

.current_tenant_id(request) ⇒ Object

Since:

  • 0.1.0



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

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

.current_tenant_slug(request) ⇒ Object

Since:

  • 0.1.0



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

def self.current_tenant_slug(request)
  tenant_slug(request.env)
end

.current_user_id(request) ⇒ Object

Since:

  • 0.1.0



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

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



112
113
114
# File 'lib/rack_jwt_aegis/request_context.rb', line 112

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

.pathname_slugs(env) ⇒ Object

Since:

  • 0.1.0



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

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



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

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

.subdomain(env) ⇒ Object

Since:

  • 0.1.0



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

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



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

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

.tenant_slug(env) ⇒ Object

Since:

  • 0.1.0



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

def self.tenant_slug(env)
  env[TENANT_SLUG_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



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

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



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

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