Class: BetterAuth::Endpoint::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/better_auth/endpoint.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path:, method:, query:, body:, params:, headers:, context:, request: nil) ⇒ Context

Returns a new instance of Context.



258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/better_auth/endpoint.rb', line 258

def initialize(path:, method:, query:, body:, params:, headers:, context:, request: nil)
  @path = path
  @method = method.to_s.upcase
  @query = query || {}
  @body = body || {}
  @params = params || {}
  @headers = normalize_headers(headers || {})
  @context = context
  @request = request
  @status = 200
  @response_headers = {}
  @returned = nil
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



246
247
248
# File 'lib/better_auth/endpoint.rb', line 246

def body
  @body
end

#contextObject

Returns the value of attribute context.



246
247
248
# File 'lib/better_auth/endpoint.rb', line 246

def context
  @context
end

#headersObject

Returns the value of attribute headers.



246
247
248
# File 'lib/better_auth/endpoint.rb', line 246

def headers
  @headers
end

#methodObject

Returns the value of attribute method.



246
247
248
# File 'lib/better_auth/endpoint.rb', line 246

def method
  @method
end

#paramsObject

Returns the value of attribute params.



246
247
248
# File 'lib/better_auth/endpoint.rb', line 246

def params
  @params
end

#pathObject

Returns the value of attribute path.



246
247
248
# File 'lib/better_auth/endpoint.rb', line 246

def path
  @path
end

#queryObject

Returns the value of attribute query.



246
247
248
# File 'lib/better_auth/endpoint.rb', line 246

def query
  @query
end

#requestObject

Returns the value of attribute request.



246
247
248
# File 'lib/better_auth/endpoint.rb', line 246

def request
  @request
end

#response_headersObject

Returns the value of attribute response_headers.



246
247
248
# File 'lib/better_auth/endpoint.rb', line 246

def response_headers
  @response_headers
end

#returnedObject

Returns the value of attribute returned.



246
247
248
# File 'lib/better_auth/endpoint.rb', line 246

def returned
  @returned
end

#statusObject

Returns the value of attribute status.



246
247
248
# File 'lib/better_auth/endpoint.rb', line 246

def status
  @status
end

Instance Method Details

#cookiesObject



296
297
298
# File 'lib/better_auth/endpoint.rb', line 296

def cookies
  BetterAuth::Cookies.parse_cookies(headers["cookie"])
end

#error(status, message: nil, headers: {}) ⇒ Object



321
322
323
# File 'lib/better_auth/endpoint.rb', line 321

def error(status, message: nil, headers: {})
  APIError.new(status, message: message, headers: headers)
end


292
293
294
# File 'lib/better_auth/endpoint.rb', line 292

def get_cookie(name)
  cookies[name.to_s]
end


305
306
307
308
309
310
311
312
313
# File 'lib/better_auth/endpoint.rb', line 305

def get_signed_cookie(name, secret)
  value = get_cookie(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



315
316
317
318
319
# File 'lib/better_auth/endpoint.rb', line 315

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



330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
# File 'lib/better_auth/endpoint.rb', line 330

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



325
326
327
328
# File 'lib/better_auth/endpoint.rb', line 325

def redirect(location, status: 302)
  code = (status == 302) ? "FOUND" : status
  APIError.new(code, message: "Redirect", headers: {"location" => location})
end


286
287
288
289
290
# File 'lib/better_auth/endpoint.rb', line 286

def set_cookie(name, value, options = {})
  attributes = cookie_attributes(options)
  cookie = (["#{name}=#{value}"] + attributes).join("; ")
  set_header("set-cookie", cookie)
end

#set_header(key, value) ⇒ Object



276
277
278
279
280
281
282
283
284
# File 'lib/better_auth/endpoint.rb', line 276

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


300
301
302
303
# File 'lib/better_auth/endpoint.rb', line 300

def set_signed_cookie(name, value, secret, options = {})
  signature = BetterAuth::Crypto.hmac_signature(value, secret, encoding: :base64url)
  set_cookie(name, "#{value}.#{signature}", options)
end

#set_status(value) ⇒ Object



272
273
274
# File 'lib/better_auth/endpoint.rb', line 272

def set_status(value)
  @status = value
end