Class: Etna::TestAuth

Inherits:
Auth
  • Object
show all
Defined in:
lib/etna/test_auth.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Auth

#call, #initialize

Constructor Details

This class inherits a constructor from Etna::Auth

Class Method Details

.hmac_header(signature) ⇒ Object



18
19
20
# File 'lib/etna/test_auth.rb', line 18

def self.hmac_header(signature)
  return [ Etna::Auth.etna_url_param(:signature).to_s, signature ]
end

.hmac_params(params) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/etna/test_auth.rb', line 22

def self.hmac_params(params)
  return {
    expiration: params.delete(:expiration) || DateTime.now.iso8601,
    id: params.delete(:id) || 'etna',
    nonce: 'nonce',
    signature: params.delete(:signature) || 'invalid',
    headers: params.keys.join(',')
  }.merge(params).map do |item, value|
    [ Etna::Auth.etna_url_param(item).to_s, value ]
  end.to_h
end

.token_header(params) ⇒ Object



8
9
10
11
# File 'lib/etna/test_auth.rb', line 8

def self.token_header(params)
  token = Base64.strict_encode64(params.to_json)
  return [ 'Authorization', "Etna #{token}" ]
end

.token_param(params) ⇒ Object



13
14
15
16
# File 'lib/etna/test_auth.rb', line 13

def self.token_param(params)
  token = Base64.strict_encode64(params.to_json)
  return [ Etna::Auth.etna_url_param(:authorization).to_s, "Etna #{token}" ]
end

Instance Method Details

#approve_hmac(request) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/etna/test_auth.rb', line 45

def approve_hmac(request)
  hmac_signature = etna_param(request, :signature) || 'invalid'

  headers = (etna_param(request, :headers)&.split(/,/) || []).map do |header|
    [ header.to_sym, etna_param(request, header) ]
  end.to_h

  hmac_params = {
    method: request.request_method,
    host: request.host,
    path: request.path,

    expiration: etna_param(request, :expiration) || DateTime.now.iso8601,
    id: etna_param(request, :id) || 'etna',
    nonce: etna_param(request, :nonce) || 'nonce',
    headers: headers,
    test_signature: hmac_signature
  }

  hmac = Etna::TestHmac.new(application, hmac_params)

  request.env['etna.hmac'] = hmac

  return nil unless hmac.valid?

  params(request).update(headers)

  return true
end

#approve_user(request) ⇒ Object



34
35
36
37
38
39
40
41
42
43
# File 'lib/etna/test_auth.rb', line 34

def approve_user(request)
  token = auth(request,:etna)

  return false unless token

  # here we simply base64-encode our user hash and pass it through
  payload = JSON.parse(Base64.decode64(token))

  request.env['etna.user'] = Etna::User.new(payload.map{|k,v| [k.to_sym, v]}.to_h, token)
end