Class: Smaak::AuthMessage

Inherits:
Object
  • Object
show all
Defined in:
lib/smaak/auth_message.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(identifier, route_info, nonce, expires, psk, recipient_public_key, encrypt) ⇒ AuthMessage

Returns a new instance of AuthMessage.

Raises:

  • (ArgumentError)


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/smaak/auth_message.rb', line 26

def initialize(identifier, route_info, nonce, expires, psk, recipient_public_key, encrypt)
  raise ArgumentError.new("Message must have a valid identifier set") if identifier.nil? or identifier.empty?      
  raise ArgumentError.new("Message must have a valid route information set") if route_info.nil?
  @identifier = identifier
  @identifier.freeze
  @route_info = route_info

  @route_info.freeze

  raise ArgumentError.new("Message must have a valid nonce set") if not validate_nonce(nonce)
  @nonce = nonce
  @nonce.freeze

  @recipient = recipient_public_key
  @psk = psk

  raise ArgumentError.new("Message must have a valid expiry set") if not validate_expiry(expires)
  @expires = expires
  set_encrypt(encrypt)
end

Instance Attribute Details

#encryptObject (readonly)

Returns the value of attribute encrypt.



11
12
13
# File 'lib/smaak/auth_message.rb', line 11

def encrypt
  @encrypt
end

#expiresObject (readonly)

Returns the value of attribute expires.



10
11
12
# File 'lib/smaak/auth_message.rb', line 10

def expires
  @expires
end

#identifierObject (readonly)

Returns the value of attribute identifier.



5
6
7
# File 'lib/smaak/auth_message.rb', line 5

def identifier
  @identifier
end

#nonceObject (readonly)

Returns the value of attribute nonce.



7
8
9
# File 'lib/smaak/auth_message.rb', line 7

def nonce
  @nonce
end

#pskObject (readonly)

Returns the value of attribute psk.



9
10
11
# File 'lib/smaak/auth_message.rb', line 9

def psk
  @psk
end

#recipientObject (readonly)

Returns the value of attribute recipient.



8
9
10
# File 'lib/smaak/auth_message.rb', line 8

def recipient
  @recipient
end

#route_infoObject (readonly)

Returns the value of attribute route_info.



6
7
8
# File 'lib/smaak/auth_message.rb', line 6

def route_info
  @route_info
end

Class Method Details

.build(recipient_public_key, psk, expires, identifier, route_info, nonce, encrypt = false) ⇒ Object



21
22
23
24
# File 'lib/smaak/auth_message.rb', line 21

def self.build(recipient_public_key, psk, expires, identifier, route_info, nonce, encrypt = false)
  #No need to obfuscate PSK. Off the wire we should always expect an obfuscated PSK
  AuthMessage.new(identifier, route_info, nonce, expires, psk, recipient_public_key, encrypt)
end

.create(recipient_public_key, psk, token_life, identifier, route_info = "", encrypt = false) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/smaak/auth_message.rb', line 13

def self.create(recipient_public_key, psk, token_life, identifier, route_info = "", encrypt = false)
  nonce = Smaak::Crypto::generate_nonce
  expires = Time.now.to_i + token_life
  #Must obfuscate PSK. AuthMessage must always have an obfuscated PSK
  psk = Smaak::Crypto::obfuscate_psk(psk)
  AuthMessage::build(recipient_public_key, psk, expires, identifier, route_info, nonce, encrypt)
end

Instance Method Details

#expired?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/smaak/auth_message.rb', line 52

def expired?
  @expires.to_i < Time.now.to_i
end

#intended_for_recipient?(pubkey) ⇒ Boolean

Returns:

  • (Boolean)


62
63
64
65
66
# File 'lib/smaak/auth_message.rb', line 62

def intended_for_recipient?(pubkey)
  return false if pubkey.nil?
  return false if @recipient.nil?
  @recipient == pubkey
end

#psk_match?(psk) ⇒ Boolean

Returns:

  • (Boolean)


56
57
58
59
60
# File 'lib/smaak/auth_message.rb', line 56

def psk_match?(psk)
  return false if psk.nil?
  return false if @psk.nil?
  @psk == Smaak::Crypto::obfuscate_psk(psk)
end

#set_encrypt(encrypt) ⇒ Object



47
48
49
50
# File 'lib/smaak/auth_message.rb', line 47

def set_encrypt(encrypt)
  @encrypt = false
  @encrypt = true if encrypt == "true" or encrypt == true
end

#verify(psk) ⇒ Object



68
69
70
71
# File 'lib/smaak/auth_message.rb', line 68

def verify(psk)
  return false if not psk_match?(psk)
  true
end