Class: BetterAuth::Endpoint::Context
- Inherits:
-
Object
- Object
- BetterAuth::Endpoint::Context
- Defined in:
- lib/better_auth/endpoint.rb
Instance Attribute Summary collapse
-
#body ⇒ Object
Returns the value of attribute body.
-
#context ⇒ Object
Returns the value of attribute context.
-
#headers ⇒ Object
Returns the value of attribute headers.
-
#method ⇒ Object
Returns the value of attribute method.
-
#params ⇒ Object
Returns the value of attribute params.
-
#path ⇒ Object
Returns the value of attribute path.
-
#query ⇒ Object
Returns the value of attribute query.
-
#raw_body ⇒ Object
Returns the value of attribute raw_body.
-
#request ⇒ Object
Returns the value of attribute request.
-
#response_headers ⇒ Object
Returns the value of attribute response_headers.
-
#returned ⇒ Object
Returns the value of attribute returned.
-
#status ⇒ Object
Returns the value of attribute status.
Instance Method Summary collapse
- #cookies ⇒ Object
- #error(status, message: nil, headers: {}) ⇒ Object
- #get_cookie(name) ⇒ Object
- #get_signed_cookie(name, secret) ⇒ Object
-
#initialize(path:, method:, query:, body:, params:, headers:, context:, request: nil, raw_body: nil) ⇒ Context
constructor
A new instance of Context.
- #json(value, status: nil, headers: {}) ⇒ Object
- #merge_context!(data) ⇒ Object
- #redirect(location, status: 302) ⇒ Object
- #set_cookie(name, value, options = {}) ⇒ Object
- #set_header(key, value) ⇒ Object
- #set_signed_cookie(name, value, secret, options = {}) ⇒ Object
- #set_status(value) ⇒ Object
Constructor Details
#initialize(path:, method:, query:, body:, params:, headers:, context:, request: nil, raw_body: nil) ⇒ Context
Returns a new instance of Context.
262 263 264 265 266 267 268 269 270 271 272 273 274 275 |
# File 'lib/better_auth/endpoint.rb', line 262 def initialize(path:, method:, query:, body:, params:, headers:, context:, request: nil, raw_body: nil) @path = path @method = method.to_s.upcase @query = query || {} @body = body || {} @params = params || {} @headers = normalize_headers(headers || {}) @raw_body = raw_body @context = context @request = request @status = 200 @response_headers = {} @returned = nil end |
Instance Attribute Details
#body ⇒ Object
Returns the value of attribute body.
249 250 251 |
# File 'lib/better_auth/endpoint.rb', line 249 def body @body end |
#context ⇒ Object
Returns the value of attribute context.
249 250 251 |
# File 'lib/better_auth/endpoint.rb', line 249 def context @context end |
#headers ⇒ Object
Returns the value of attribute headers.
249 250 251 |
# File 'lib/better_auth/endpoint.rb', line 249 def headers @headers end |
#method ⇒ Object
Returns the value of attribute method.
249 250 251 |
# File 'lib/better_auth/endpoint.rb', line 249 def method @method end |
#params ⇒ Object
Returns the value of attribute params.
249 250 251 |
# File 'lib/better_auth/endpoint.rb', line 249 def params @params end |
#path ⇒ Object
Returns the value of attribute path.
249 250 251 |
# File 'lib/better_auth/endpoint.rb', line 249 def path @path end |
#query ⇒ Object
Returns the value of attribute query.
249 250 251 |
# File 'lib/better_auth/endpoint.rb', line 249 def query @query end |
#raw_body ⇒ Object
Returns the value of attribute raw_body.
249 250 251 |
# File 'lib/better_auth/endpoint.rb', line 249 def raw_body @raw_body end |
#request ⇒ Object
Returns the value of attribute request.
249 250 251 |
# File 'lib/better_auth/endpoint.rb', line 249 def request @request end |
#response_headers ⇒ Object
Returns the value of attribute response_headers.
249 250 251 |
# File 'lib/better_auth/endpoint.rb', line 249 def response_headers @response_headers end |
#returned ⇒ Object
Returns the value of attribute returned.
249 250 251 |
# File 'lib/better_auth/endpoint.rb', line 249 def returned @returned end |
#status ⇒ Object
Returns the value of attribute status.
249 250 251 |
# File 'lib/better_auth/endpoint.rb', line 249 def status @status end |
Instance Method Details
#cookies ⇒ Object
301 302 303 |
# File 'lib/better_auth/endpoint.rb', line 301 def BetterAuth::Cookies.(headers["cookie"]) end |
#error(status, message: nil, headers: {}) ⇒ Object
326 327 328 |
# File 'lib/better_auth/endpoint.rb', line 326 def error(status, message: nil, headers: {}) APIError.new(status, message: , headers: headers) end |
#get_cookie(name) ⇒ Object
297 298 299 |
# File 'lib/better_auth/endpoint.rb', line 297 def (name) [name.to_s] end |
#get_signed_cookie(name, secret) ⇒ Object
310 311 312 313 314 315 316 317 318 |
# File 'lib/better_auth/endpoint.rb', line 310 def (name, secret) value = (name) return nil unless value payload, signature = value.rpartition(".").values_at(0, 2) return nil if payload.empty? || signature.empty? BetterAuth::Crypto.verify_hmac_signature(payload, signature, secret, encoding: :base64url) ? payload : nil end |
#json(value, status: nil, headers: {}) ⇒ Object
320 321 322 323 324 |
# File 'lib/better_auth/endpoint.rb', line 320 def json(value, status: nil, headers: {}) set_status(status) if status headers.each { |key, header_value| set_header(key, header_value) } Result.new(response: value, status: self.status, headers: response_headers) end |
#merge_context!(data) ⇒ Object
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 |
# File 'lib/better_auth/endpoint.rb', line 335 def merge_context!(data) data.each do |key, value| case key.to_sym when :query @query = deep_merge(query, value) when :body @body = deep_merge(body, value) when :params @params = deep_merge(params, value) when :headers @headers = normalize_headers(deep_merge(headers, value)) else public_send("#{key}=", value) if respond_to?("#{key}=") end end end |
#redirect(location, status: 302) ⇒ Object
330 331 332 333 |
# File 'lib/better_auth/endpoint.rb', line 330 def redirect(location, status: 302) code = (status == 302) ? "FOUND" : status APIError.new(code, message: "Redirect", headers: {"location" => location}) end |
#set_cookie(name, value, options = {}) ⇒ Object
291 292 293 294 295 |
# File 'lib/better_auth/endpoint.rb', line 291 def (name, value, = {}) attributes = () = (["#{name}=#{value}"] + attributes).join("; ") set_header("set-cookie", ) end |
#set_header(key, value) ⇒ Object
281 282 283 284 285 286 287 288 289 |
# File 'lib/better_auth/endpoint.rb', line 281 def set_header(key, value) normalized = safe_header_name(key) safe_value = safe_header_value(value) response_headers[normalized] = if normalized == "set-cookie" && response_headers[normalized] [response_headers[normalized], safe_value].join("\n") else safe_value end end |
#set_signed_cookie(name, value, secret, options = {}) ⇒ Object
305 306 307 308 |
# File 'lib/better_auth/endpoint.rb', line 305 def (name, value, secret, = {}) signature = BetterAuth::Crypto.hmac_signature(value, secret, encoding: :base64url) (name, "#{value}.#{signature}", ) end |
#set_status(value) ⇒ Object
277 278 279 |
# File 'lib/better_auth/endpoint.rb', line 277 def set_status(value) @status = value end |