Module: Api::Vendor::V1::Concerns::Authentication

Includes:
IntegrationTestHelpers, EncryptableHelper
Included in:
BaseController, CreateReservation, PackageTypesController, ReservationsController, RestaurantPackagesController, RestaurantsController, VendorReservationsController
Defined in:
app/controllers/api/vendor/v1/concerns/authentication.rb

Instance Method Summary collapse

Methods included from EncryptableHelper

#decrypt, #encrypt, #generate_signature

Instance Method Details

#authentication!Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/controllers/api/vendor/v1/concerns/authentication.rb', line 5

def authentication!
  return true if 

  auth_header = request.headers['Authorization']

  return render_error('Missing authorization header') if auth_header.blank?

  # assume the token is in the 'Authorization' header with format 'hmac <token>'
  hmac, token = auth_header.split
  return render_error('Missing authentication token') unless hmac.downcase == 'hmac'

  # validate token format <KEY>:<TIMESTAMP>:<SIGNATURE>
  return render_error('Invalid authentication token format') unless token.match?(/\A[\w-]+:\d+:[a-f0-9]+\z/)

  api_key, timestamp, client_signature = token.split(':')

  # for vendor integration tests only
  vendor_name_header = integration_test_vendor_name

  return render_error('Vendor api key does not exist') unless valid_vendor_key?(api_key, vendor_name_header)

  return render_error('Vendor booking channel does not exist') if find_vendor_channel.blank?

  return render_error('Invalid authentication token') unless valid_token?(api_key, timestamp, client_signature,
                                                                          vendor_name_header)

  # token is valid, continue with the request
  true
end

#find_vendor_channelObject



35
36
37
# File 'app/controllers/api/vendor/v1/concerns/authentication.rb', line 35

def find_vendor_channel
  @vendor_channel ||= Channel.find_by(oauth_application_id: vendor.id)
end