Module: ApiAuthenticator

Extended by:
ActiveSupport::Concern
Defined in:
lib/api_authenticator.rb,
lib/api_authenticator/errors.rb,
lib/api_authenticator/version.rb,
lib/api_authenticator/configuration.rb,
lib/api_authenticator/api_authenticator.rb,
lib/api_authenticator/authenticated_request.rb

Defined Under Namespace

Classes: BaseError, InvalidTimeError, InvalidTokenError

Constant Summary collapse

VERSION =
"0.3.0"
URL_REQUEST_TYPE =
:url
PATH_REQUEST_TYPE =
:path
REQUEST_TYPES =
[URL_REQUEST_TYPE, PATH_REQUEST_TYPE]
@@logger =
nil
@@request_type =
URL_REQUEST_TYPE

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.authenticated_request?(request) ⇒ Boolean

authenticated_request?

Returns: True or False

Returns:

  • (Boolean)


8
9
10
11
12
13
14
15
16
17
# File 'lib/api_authenticator/authenticated_request.rb', line 8

def self.authenticated_request?(request)
  time = nil
  token = request.headers['API-Token']
  begin
    time = DateTime.parse(request.headers['API-Time'])
  rescue ArgumentError, TypeError
  end
  valid_api_time?(time)
  valid_api_token?(originating_request(request), time, token)
end

.configure {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



8
9
10
# File 'lib/api_authenticator/configuration.rb', line 8

def self.configure
  yield self
end

.loggerObject



47
48
49
# File 'lib/api_authenticator/configuration.rb', line 47

def self.logger
  @@logger || Logger.new($stdout)
end

.logger=(logger) ⇒ Object



43
44
45
# File 'lib/api_authenticator/configuration.rb', line 43

def self.logger=(logger)
  @@logger = logger || Logger.new($stdout)
end

.report_unauthenticated_requests=(report) ⇒ Object



39
40
41
# File 'lib/api_authenticator/configuration.rb', line 39

def self.report_unauthenticated_requests=(report)
  @@report_unauthenticated_requests = report || false
end

.request_typeObject



20
21
22
# File 'lib/api_authenticator/configuration.rb', line 20

def self.request_type
  @@request_type
end

.request_type=(request_type) ⇒ Object



24
25
26
27
28
29
# File 'lib/api_authenticator/configuration.rb', line 24

def self.request_type=(request_type)
  unless REQUEST_TYPES.include?(request_type)
    raise ArgumentError.new("Request types must be one of the following #{REQUEST_TYPES.join(', ')}}")
  end
  @@request_type = request_type
end

.shared_secret_keysObject



16
17
18
# File 'lib/api_authenticator/configuration.rb', line 16

def self.shared_secret_keys
  @@shared_secret_keys
end

.shared_secret_keys=(shared_secret_keys) ⇒ Object



12
13
14
# File 'lib/api_authenticator/configuration.rb', line 12

def self.shared_secret_keys=(shared_secret_keys)
  @@shared_secret_keys = shared_secret_keys
end

.time_thresholdObject



35
36
37
# File 'lib/api_authenticator/configuration.rb', line 35

def self.time_threshold
  @@time_threshold
end

.time_threshold=(time_threshold) ⇒ Object



31
32
33
# File 'lib/api_authenticator/configuration.rb', line 31

def self.time_threshold=(time_threshold)
  @@time_threshold = time_threshold
end

Instance Method Details

#api_authenticatorObject

Before filter



9
10
11
12
13
14
15
16
# File 'lib/api_authenticator/api_authenticator.rb', line 9

def api_authenticator
  begin
    ApiAuthenticator.authenticated_request?(request)
  rescue BaseError => e
    report_unauthenticated_requests(e)
    render( status: 401, nothing: true ) and return false
  end
end