Faraday::RackBuilder 方法
Faraday::RackBuilder methods
为了使用 faraday 文档中的自定义中间件,我看到我必须使用 use
方法。在我的用例中,我的自定义构建器只需在 header:
中添加一个 jwt 身份验证令牌
Faraday.new(url: wsconfig.base_url) do |builder|
builder.use CustomMiddlewares::JwtAuthentication
builder.request :url_encoded
builder.response :json
builder.adapter :net_http
end
jwt_authentication.rb
require 'jwt'
module CustomMiddlewares
class JwtAuthentication < Faraday::Middleware
def call(env)
payload = RequestStore.store[:jwt_claims].to_h.merge({method: env.method, path: env.url.request_uri})
token = jwt(payload)
Rails.logger.debug { " with token: #{token}" }
env[:request_headers]["Authorization"] = "Token: #{token}"
@app.call(env)
rescue StandardError => e
raise "problem in JwtAuthentication Middleware"
end
private
def jwt(payload, expiration = 1.minute.from_now)
payload = payload.dup
payload['exp'] = expiration.to_i
payload['iss'] = 'cgp'
JWT.encode(payload, key, 'RS256')
end
def key
OpenSSL::PKey::RSA.new(Rails.configuration.x.secrets.ws_config.jwt_private_key)
end
end
end
CustomMiddlewares::JwtAuthentication
只应在请求阶段使用,例如 url_encoded
中间件,它是通过 request
方法添加的。我想知道为什么我不能对我的做同样的事情:
builder.request CustomMiddlewares::JwtAuthentication
我得到了:
CustomMiddlewares::VerbosingPseudonymizationWs is not registered on Faraday::Request (Faraday::Error)
如果你想使用builder.request
你首先需要像这样注册中间件:
Faraday::Request.register_middleware jwt: -> { CustomMiddlewares::JwtAuthentication }
之后您应该可以调用 builder.request :jwt
。这是因为 Faraday::RackBuilder#request
本质上是以 Faraday::Request.lookup_middleware(key)
作为第一个参数调用 Faraday::RackBuilder#use
。
见https://github.com/lostisland/faraday/blob/master/lib/faraday/rack_builder.rb#L92
和 https://github.com/lostisland/faraday/blob/master/lib/faraday/rack_builder.rb#L228
这也意味着builder.request :jwt
和builder.use CustomMiddlewares::JwtAuthentication
没有区别。
请求和响应中间件之间的区别在于响应中间件应该继承自 Faraday::Response::Middleware
,这确保它们只在响应 (on_complete
) 上执行。参见 https://github.com/lostisland/faraday/blob/master/lib/faraday/response.rb#L8
即,即使通过 builder.request
注册中间件,如果它实现了 on_complete
回调,它仍然可以对响应进行操作。相反,如果您不实现回调,则不会为响应执行任何代码。
为了使用 faraday 文档中的自定义中间件,我看到我必须使用 use
方法。在我的用例中,我的自定义构建器只需在 header:
Faraday.new(url: wsconfig.base_url) do |builder|
builder.use CustomMiddlewares::JwtAuthentication
builder.request :url_encoded
builder.response :json
builder.adapter :net_http
end
jwt_authentication.rb
require 'jwt'
module CustomMiddlewares
class JwtAuthentication < Faraday::Middleware
def call(env)
payload = RequestStore.store[:jwt_claims].to_h.merge({method: env.method, path: env.url.request_uri})
token = jwt(payload)
Rails.logger.debug { " with token: #{token}" }
env[:request_headers]["Authorization"] = "Token: #{token}"
@app.call(env)
rescue StandardError => e
raise "problem in JwtAuthentication Middleware"
end
private
def jwt(payload, expiration = 1.minute.from_now)
payload = payload.dup
payload['exp'] = expiration.to_i
payload['iss'] = 'cgp'
JWT.encode(payload, key, 'RS256')
end
def key
OpenSSL::PKey::RSA.new(Rails.configuration.x.secrets.ws_config.jwt_private_key)
end
end
end
CustomMiddlewares::JwtAuthentication
只应在请求阶段使用,例如 url_encoded
中间件,它是通过 request
方法添加的。我想知道为什么我不能对我的做同样的事情:
builder.request CustomMiddlewares::JwtAuthentication
我得到了:
CustomMiddlewares::VerbosingPseudonymizationWs is not registered on Faraday::Request (Faraday::Error)
如果你想使用builder.request
你首先需要像这样注册中间件:
Faraday::Request.register_middleware jwt: -> { CustomMiddlewares::JwtAuthentication }
之后您应该可以调用 builder.request :jwt
。这是因为 Faraday::RackBuilder#request
本质上是以 Faraday::Request.lookup_middleware(key)
作为第一个参数调用 Faraday::RackBuilder#use
。
见https://github.com/lostisland/faraday/blob/master/lib/faraday/rack_builder.rb#L92 和 https://github.com/lostisland/faraday/blob/master/lib/faraday/rack_builder.rb#L228
这也意味着builder.request :jwt
和builder.use CustomMiddlewares::JwtAuthentication
没有区别。
请求和响应中间件之间的区别在于响应中间件应该继承自 Faraday::Response::Middleware
,这确保它们只在响应 (on_complete
) 上执行。参见 https://github.com/lostisland/faraday/blob/master/lib/faraday/response.rb#L8
即,即使通过 builder.request
注册中间件,如果它实现了 on_complete
回调,它仍然可以对响应进行操作。相反,如果您不实现回调,则不会为响应执行任何代码。