Class: Krn::Auth

Inherits:
Object
  • Object
show all
Defined in:
lib/krn/auth.rb

Overview

KRN Auth Class

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ Auth

Your code goes here…



14
15
16
17
18
19
20
# File 'lib/krn/auth.rb', line 14

def initialize(opts)
  @name = opts[:name]
  @crypt_key = opts[:crypt_key]
  @hmac_secret = opts[:hmac_secret]
  @rest_key = opts[:rest_key]
  @rsa_key  = opts[:rsa_key]
end

Instance Attribute Details

#crypt_keyObject

Returns the value of attribute crypt_key.



11
12
13
# File 'lib/krn/auth.rb', line 11

def crypt_key
  @crypt_key
end

#hmac_secretObject

Returns the value of attribute hmac_secret.



11
12
13
# File 'lib/krn/auth.rb', line 11

def hmac_secret
  @hmac_secret
end

#nameObject

Returns the value of attribute name.



11
12
13
# File 'lib/krn/auth.rb', line 11

def name
  @name
end

#rest_keyObject

Returns the value of attribute rest_key.



11
12
13
# File 'lib/krn/auth.rb', line 11

def rest_key
  @rest_key
end

#rsa_keyObject

Returns the value of attribute rsa_key.



11
12
13
# File 'lib/krn/auth.rb', line 11

def rsa_key
  @rsa_key
end

Instance Method Details

#decrypt(data) ⇒ Object



81
82
83
84
85
86
87
88
89
90
# File 'lib/krn/auth.rb', line 81

def decrypt(data)
  secretdata = Base64.decode64(data)
  decipher = OpenSSL::Cipher.new('aes-256-cbc')
  iv = 'x' * decipher.iv_len
  decipher.decrypt
  decipher.key = @crypt_key
  f = decipher.update(secretdata) + decipher.final
  f = f[decipher.iv_len..-1]
  JSON.parse(f)
end

#deep_validate(passport: '') ⇒ Object



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

def deep_validate(passport: '')
  uri = URI("#{trinity_url}/deep-validate?token=#{passport}")
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  req = Net::HTTP::Post.new(uri.path, 'Content-Type' => 'application/json')
  res = http.request(req)
  JSON.parse(res.body)
rescue StandardError
  false
end

#send_request(method: '', path: '', headers: [], body: '') ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/krn/auth.rb', line 45

def send_request(method: '', path: '', headers: [], body: '')
  uri = URI("#{trinity_url}#{path}")
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = false
  m = Net::HTTP::Post
  m = Net::HTTP::Get if method == 'GET'
  req = m.new(uri.path, 'Content-Type' => 'application/json')

  req['KRN-PARTNER-KEY'] = @rest_key
  req['Date'] = Time.now.getutc
  req['KRN-SIGN-URL'] = uri

  req.body = body

  req = sign_request(req)
  req['Authorization'] = nil

  res = http.request(req)
  JSON.parse(res.body)
rescue StandardError => e
  false
end

#sign_request(req) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/krn/auth.rb', line 68

def sign_request(req)
  $context = HttpSignatures::Context.new(
    keys: { 'KMM_KEY' => {
      private_key: @rsa_key
    } },
    headers: %w[KRN-SIGN-URL KRN-PARTNER-KEY Date],
    algorithm: 'rsa-sha256'
  )
  $context.signer.sign(req)

  req
end

#trinity_urlObject



92
93
94
# File 'lib/krn/auth.rb', line 92

def trinity_url
  ENV['KRN_HOST_PREFIX'] ? 'http://' + ENV['KRN_HOST_PREFIX'] + 'trinity.krn.krone.at' : 'https://trinity.krone.at'
end

#validate(passport: '') ⇒ Object



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

def validate(passport: '')
  token_parts = passport.split(':')
  raise 'Validation Failed' if token_parts.first != @name

  begin
    decoded_token = JWT.decode token_parts.last, @hmac_secret
    decrypt(decoded_token.first['payload'])
  rescue StandardError => e
    false
  end
end