Class: Msn::Nexus

Inherits:
Object
  • Object
show all
Defined in:
lib/msn/nexus.rb

Overview

:nodoc:

Constant Summary

Namespaces =
{
  "wsse" => "http://schemas.xmlsoap.org/ws/2003/06/secext",
  "wst" => "http://schemas.xmlsoap.org/ws/2004/04/trust",
  "wsp" => "http://schemas.xmlsoap.org/ws/2002/12/policy",
  "wsa" => "http://schemas.xmlsoap.org/ws/2004/03/addressing",
  "S" => "http://schemas.xmlsoap.org/soap/envelope/",
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(messenger, policy, nonce) ⇒ Nexus

Returns a new instance of Nexus



15
16
17
18
19
# File 'lib/msn/nexus.rb', line 15

def initialize(messenger, policy, nonce)
  @messenger = messenger
  @policy = policy
  @nonce = nonce
end

Instance Attribute Details

#messengerObject (readonly)

Returns the value of attribute messenger



3
4
5
# File 'lib/msn/nexus.rb', line 3

def messenger
  @messenger
end

#nonceObject (readonly)

Returns the value of attribute nonce



5
6
7
# File 'lib/msn/nexus.rb', line 5

def nonce
  @nonce
end

#policyObject (readonly)

Returns the value of attribute policy



4
5
6
# File 'lib/msn/nexus.rb', line 4

def policy
  @policy
end

Instance Method Details

#compute_key(key, hash) ⇒ Object



90
91
92
93
94
95
96
97
# File 'lib/msn/nexus.rb', line 90

def compute_key(key, hash)
  hash1 = sha1_hmac(key, hash)
  hash2 = sha1_hmac(key, "#{hash1}#{hash}")
  hash3 = sha1_hmac(key, hash1)
  hash4 = sha1_hmac(key, "#{hash3}#{hash}")

  "#{hash2}#{hash4[0 ... 4]}"
end

#compute_return_value(binary_secret, iv = Random.new.bytes(8)) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/msn/nexus.rb', line 70

def compute_return_value(binary_secret, iv = Random.new.bytes(8))
  key1 = Base64.decode64 binary_secret

  key2 = compute_key key1, "WS-SecureConversationSESSION KEY HASH"
  key3 = compute_key key1, "WS-SecureConversationSESSION KEY ENCRYPTION"

  hash = sha1_hmac key2, @nonce

  nonce = "#{@nonce}#{8.chr * 8}"

  des = OpenSSL::Cipher::Cipher.new("des-ede3-cbc")
  des.encrypt
  des.iv = iv
  des.key = key3
  encrypted_data = des.update(nonce) + des.final

  final = [28, 1, 0x6603, 0x8004, 8, 20, 72, iv, hash, encrypted_data].pack "L<L<L<L<L<L<L<A8A20A72"
  Base64.strict_encode64 final
end

#fetch_dataObject



36
37
38
39
40
41
42
43
# File 'lib/msn/nexus.rb', line 36

def fetch_data
  return if @fetched_data
  @fetched_data = true

  @sso_token, secret, @ticket_token = get_binary_secret messenger.username, messenger.password

  @secret = compute_return_value secret
end

#get_binary_secret(username, password) ⇒ 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
# File 'lib/msn/nexus.rb', line 45

def get_binary_secret(username, password)
  msn_sso_template_file = File.expand_path('../soap/msn_sso_template.xml', __FILE__)
  msn_sso_template = ERB.new File.read(msn_sso_template_file)
  soap = msn_sso_template.result(binding)

  response = RestClient.post "https://login.live.com/RST.srf", soap

  xml = Nokogiri::XML(response)

  # Check invalid login
  fault = xml.xpath("//S:Fault/faultstring")
  if fault.length > 0
    raise Msn::AuthenticationError.new(fault.first.text)
  end

  rstr = xml.xpath "//wst:RequestSecurityTokenResponse[wsp:AppliesTo/wsa:EndpointReference/wsa:Address='messengerclear.live.com']", Namespaces
  token = rstr.xpath("wst:RequestedSecurityToken/wsse:BinarySecurityToken[@Id='Compact1']", Namespaces).first.text
  secret = rstr.xpath("wst:RequestedProofToken/wst:BinarySecret", Namespaces).first.text

  rstr2 = xml.xpath "//wst:RequestSecurityTokenResponse[wsp:AppliesTo/wsa:EndpointReference/wsa:Address='contacts.msn.com']", Namespaces
  ticket_token = rstr2.xpath("wst:RequestedSecurityToken/wsse:BinarySecurityToken[@Id='Compact2']", Namespaces).first.text

  [token, secret, ticket_token]
end

#secretObject



31
32
33
34
# File 'lib/msn/nexus.rb', line 31

def secret
  fetch_data
  @secret
end

#sha1_hmac(data, key) ⇒ Object



99
100
101
# File 'lib/msn/nexus.rb', line 99

def sha1_hmac(data, key)
  Digest::HMAC.digest(key, data, Digest::SHA1)
end

#sso_tokenObject



21
22
23
24
# File 'lib/msn/nexus.rb', line 21

def sso_token
  fetch_data
  @sso_token
end

#ticket_tokenObject



26
27
28
29
# File 'lib/msn/nexus.rb', line 26

def ticket_token
  fetch_data
  @ticket_token
end