class GrapeTokenAuth::AuthenticationHeader

Attributes

data[R]
request_start[R]
resource[R]

Public Class Methods

build_auth_headers(token, uid) click to toggle source
# File lib/grape_token_auth/authentication_header.rb, line 17
def self.build_auth_headers(token, uid)
  {
    'access-token' => token.to_s,
    'expiry' => token.expiry.to_s,
    'client' => token.client_id.to_s,
    'token-type' => 'Bearer',
    'uid' => uid.to_s
  }
end
new(data, start_time) click to toggle source
# File lib/grape_token_auth/authentication_header.rb, line 6
def initialize(data, start_time)
  @resource = data.first_authenticated_resource
  @request_start = start_time
  @data = data
end

Public Instance Methods

headers() click to toggle source
# File lib/grape_token_auth/authentication_header.rb, line 12
def headers
  return {} unless resource && resource.valid? && client_id && !skip_auth_headers
  auth_headers_from_resource
end

Private Instance Methods

auth_headers_from_resource() click to toggle source
# File lib/grape_token_auth/authentication_header.rb, line 33
def auth_headers_from_resource
  auth_headers = {}
  resource.while_record_locked do
    if was_not_authenticated_with_token
      auth_headers = resource.create_new_auth_token
    elsif !GrapeTokenAuth.change_headers_on_each_request
      auth_headers = resource.extend_batch_buffer(token, client_id)
    elsif batch_request?
      resource.extend_batch_buffer(token, client_id)
    else
      auth_headers = resource.create_new_auth_token(client_id)
    end
  end
  coerce_headers_to_strings(auth_headers)
end
batch_request?() click to toggle source
# File lib/grape_token_auth/authentication_header.rb, line 57
def batch_request?
  @batch_request ||= resource.tokens[client_id] &&
                     resource.tokens[client_id]['updated_at'] &&
                     within_batch_request_window?
end
coerce_headers_to_strings(auth_headers) click to toggle source
# File lib/grape_token_auth/authentication_header.rb, line 53
def coerce_headers_to_strings(auth_headers)
  auth_headers.each { |k, v|  auth_headers[k] = v.to_s }
end
was_not_authenticated_with_token() click to toggle source
# File lib/grape_token_auth/authentication_header.rb, line 49
def was_not_authenticated_with_token
  !data.authed_with_token
end
within_batch_request_window?() click to toggle source
# File lib/grape_token_auth/authentication_header.rb, line 63
def within_batch_request_window?
  end_of_window = Time.parse(resource.tokens[client_id]['updated_at']) +
                  GrapeTokenAuth.batch_request_buffer_throttle

  request_start < end_of_window
end