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.



171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/better_auth/endpoint.rb', line 171

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.



159
160
161
# File 'lib/better_auth/endpoint.rb', line 159

def body
  @body
end

#contextObject

Returns the value of attribute context.



159
160
161
# File 'lib/better_auth/endpoint.rb', line 159

def context
  @context
end

#headersObject

Returns the value of attribute headers.



159
160
161
# File 'lib/better_auth/endpoint.rb', line 159

def headers
  @headers
end

#methodObject

Returns the value of attribute method.



159
160
161
# File 'lib/better_auth/endpoint.rb', line 159

def method
  @method
end

#paramsObject

Returns the value of attribute params.



159
160
161
# File 'lib/better_auth/endpoint.rb', line 159

def params
  @params
end

#pathObject

Returns the value of attribute path.



159
160
161
# File 'lib/better_auth/endpoint.rb', line 159

def path
  @path
end

#queryObject

Returns the value of attribute query.



159
160
161
# File 'lib/better_auth/endpoint.rb', line 159

def query
  @query
end

#requestObject

Returns the value of attribute request.



159
160
161
# File 'lib/better_auth/endpoint.rb', line 159

def request
  @request
end

#response_headersObject

Returns the value of attribute response_headers.



159
160
161
# File 'lib/better_auth/endpoint.rb', line 159

def response_headers
  @response_headers
end

#returnedObject

Returns the value of attribute returned.



159
160
161
# File 'lib/better_auth/endpoint.rb', line 159

def returned
  @returned
end

#statusObject

Returns the value of attribute status.



159
160
161
# File 'lib/better_auth/endpoint.rb', line 159

def status
  @status
end

Instance Method Details

#cookiesObject



209
210
211
# File 'lib/better_auth/endpoint.rb', line 209

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

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



234
235
236
# File 'lib/better_auth/endpoint.rb', line 234

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


205
206
207
# File 'lib/better_auth/endpoint.rb', line 205

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


218
219
220
221
222
223
224
225
226
# File 'lib/better_auth/endpoint.rb', line 218

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



228
229
230
231
232
# File 'lib/better_auth/endpoint.rb', line 228

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



243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/better_auth/endpoint.rb', line 243

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



238
239
240
241
# File 'lib/better_auth/endpoint.rb', line 238

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


199
200
201
202
203
# File 'lib/better_auth/endpoint.rb', line 199

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



189
190
191
192
193
194
195
196
197
# File 'lib/better_auth/endpoint.rb', line 189

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


213
214
215
216
# File 'lib/better_auth/endpoint.rb', line 213

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



185
186
187
# File 'lib/better_auth/endpoint.rb', line 185

def set_status(value)
  @status = value
end