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.



26
27
28
29
30
31
32
33
34
# File 'lib/smaak/auth_message.rb', line 26

def initialize(identifier, route_info, nonce, expires, psk, recipient_public_key, encrypt)
  set_and_validate_identifier(identifier)
  set_and_validate_route_info(route_info)
  set_and_validate_nonce(nonce)
  set_and_validate_expires(expires)
  set_recipient(recipient_public_key)
  set_psk(psk)
  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)


72
73
74
# File 'lib/smaak/auth_message.rb', line 72

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

#intended_for_recipient?(pubkey) ⇒ Boolean

Returns:

  • (Boolean)


82
83
84
85
86
# File 'lib/smaak/auth_message.rb', line 82

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

#psk_match?(psk) ⇒ Boolean

Returns:

  • (Boolean)


76
77
78
79
80
# File 'lib/smaak/auth_message.rb', line 76

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

#set_and_validate_expires(expires) ⇒ Object

Raises:

  • (ArgumentError)


54
55
56
57
# File 'lib/smaak/auth_message.rb', line 54

def set_and_validate_expires(expires)
  raise ArgumentError.new("Message must have a valid expiry set") unless validate_expiry(expires)
  @expires = expires
end

#set_and_validate_identifier(identifier) ⇒ Object

Raises:

  • (ArgumentError)


36
37
38
39
40
# File 'lib/smaak/auth_message.rb', line 36

def set_and_validate_identifier(identifier)
  raise ArgumentError.new("Message must have a valid identifier set") if identifier.nil? or identifier.empty?      
  @identifier = identifier
  @identifier.freeze
end

#set_and_validate_nonce(nonce) ⇒ Object

Raises:

  • (ArgumentError)


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

def set_and_validate_nonce(nonce)
  raise ArgumentError.new("Message must have a valid nonce set") unless validate_nonce(nonce)
  @nonce = nonce
  @nonce.freeze
end

#set_and_validate_route_info(route_info) ⇒ Object

Raises:

  • (ArgumentError)


42
43
44
45
46
# File 'lib/smaak/auth_message.rb', line 42

def set_and_validate_route_info(route_info)
  raise ArgumentError.new("Message must have a valid route information set") if route_info.nil?
  @route_info = route_info
  @route_info.freeze
end

#set_encrypt(encrypt) ⇒ Object



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

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

#set_psk(psk) ⇒ Object



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

def set_psk(psk)
  @psk = psk
end

#set_recipient(recipient_public_key) ⇒ Object



59
60
61
# File 'lib/smaak/auth_message.rb', line 59

def set_recipient(recipient_public_key)
  @recipient = recipient_public_key
end

#verify(psk) ⇒ Object



88
89
90
91
# File 'lib/smaak/auth_message.rb', line 88

def verify(psk)
  return false unless psk_match?(psk)
  true
end