Class: Booqable::Middleware::Auth::SingleUse
- Defined in:
- lib/booqable/middleware/auth/single_use.rb
Overview
Faraday middleware for single-use JWT token authentication
This middleware generates and adds single-use JWT tokens for authentication. Each token is unique per request and includes request-specific data like method, path, and body hash to prevent replay attacks.
Supports multiple JWT algorithms: HS256 (HMAC), RS256 (RSA), and ES256 (ECDSA).
For more info see: developers.booqable.com/#authentication-request-signing
Constant Summary collapse
- KIND =
Token kind identifier for JWT header
"single_use"- BOOQABLE_DOMAIN =
Default domain for issuer URL construction
"booqable.com"
Instance Method Summary collapse
-
#call(env) ⇒ Faraday::Response
Process the HTTP request and add single-use token authentication.
-
#initialize(app, options = {}) ⇒ SingleUse
constructor
Initialize the single-use token authentication middleware.
Constructor Details
#initialize(app, options = {}) ⇒ SingleUse
Initialize the single-use token authentication middleware
44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/booqable/middleware/auth/single_use.rb', line 44 def initialize(app, = {}) super(app) @kid = .fetch(:single_use_token) @alg = .fetch(:single_use_token_algorithm) || raise(SingleUseTokenAlgorithmRequired) @exp = .fetch(:single_use_token_expiration_period, Time.now.to_i + 10 * 60) @aud = .fetch(:single_use_token_company_id) || raise(SingleUseTokenCompanyIdRequired) @sub = .fetch(:single_use_token_user_id) || raise(SingleUseTokenUserIdRequired) @raw_private_key = .fetch(:single_use_token_private_key) || raise(PrivateKeyOrSecretRequired) @api_endpoint = .fetch(:api_endpoint, nil) @private_key = private_key end |
Instance Method Details
#call(env) ⇒ Faraday::Response
Process the HTTP request and add single-use token authentication
Generates a unique JWT token for this specific request and adds it to the Authorization header. Then passes the request to the next middleware in the stack.
66 67 68 69 70 |
# File 'lib/booqable/middleware/auth/single_use.rb', line 66 def call(env) env.request_headers["Authorization"] ||= "Bearer #{generate_token(env)}" @app.call(env) end |