Class: LiveContacts

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

Overview

Goto msm.live.com/app/default.aspx to configure your application

For testin purposes in my example me.diary.com is a virtual host in the /etc/hosts file pointing at 127.0.0.1

The APP_DETAILS hash has most of all the information I have registered my application with

Example rails controller code:

require ‘live_contacts’

protect_from_forgery :except => :windows_live_authentication

APP_DETAILS =

:application_name => 'RLiveContacts',
:app_id => '0016BFFD80013411',
:secret => 'b623d64e8f1d18bfe8b281d385ad461c1e8bbebb',
:security_algorithm => 'wsignin1.0',
:return_url => 'http://me.diary.com:3000/home/windows_live_authentication',
:privacy_policy_url => "http://me.diary.com:3000/privacy",
:application_verifier_required => false

def windows_live_authentication

lc = LiveContacts.new(APP_DETAILS)
if request.post?
  lc.process_consent_params(params)
  xml = lc.retrieve_address_book_xml
  render :xml => xml and return
else
  redirect_to lc.generate_delegation_url and return
end

end

Constant Summary collapse

VERSION =
'0.0.6'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app_details) ⇒ LiveContacts

Step 1 - Initialize the object with the corrent details



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/live_contacts.rb', line 53

def initialize(app_details)
  self.application_name = app_details[:application_name]
  self.app_id = app_details[:app_id]
  self.secret = app_details[:secret]
  self.security_algorithm = app_details[:security_algorithm]
  self.return_url = app_details[:return_url]
  self.privacy_policy_url = app_details[:privacy_policy_url]
  self.application_verifier_required = app_details[:application_verifier_required]
  
  # mainly used for testing purposes to check the signature generated
  self.timestamp = app_details[:timestamp]
end

Instance Attribute Details

#app_idObject

Live application attibutes



47
48
49
# File 'lib/live_contacts.rb', line 47

def app_id
  @app_id
end

#application_nameObject

Live application attibutes



47
48
49
# File 'lib/live_contacts.rb', line 47

def application_name
  @application_name
end

#application_verifier_requiredObject

Live application attibutes



47
48
49
# File 'lib/live_contacts.rb', line 47

def application_verifier_required
  @application_verifier_required
end

delegated authentication attributes



50
51
52
# File 'lib/live_contacts.rb', line 50

def consent_token
  @consent_token
end

#cryptkeyObject (readonly)

delegated authentication attributes



50
51
52
# File 'lib/live_contacts.rb', line 50

def cryptkey
  @cryptkey
end

#decrypted_tokenObject (readonly)

delegated authentication attributes



50
51
52
# File 'lib/live_contacts.rb', line 50

def decrypted_token
  @decrypted_token
end

#deltObject (readonly)

delegated authentication attributes



50
51
52
# File 'lib/live_contacts.rb', line 50

def delt
  @delt
end

#eactObject (readonly)

delegated authentication attributes



50
51
52
# File 'lib/live_contacts.rb', line 50

def eact
  @eact
end

#int_lidObject (readonly)

delegated authentication attributes



50
51
52
# File 'lib/live_contacts.rb', line 50

def int_lid
  @int_lid
end

#lidObject (readonly)

delegated authentication attributes



50
51
52
# File 'lib/live_contacts.rb', line 50

def lid
  @lid
end

#parsed_decrypted_tokenObject (readonly)

delegated authentication attributes



50
51
52
# File 'lib/live_contacts.rb', line 50

def parsed_decrypted_token
  @parsed_decrypted_token
end

#parsed_tokenObject (readonly)

delegated authentication attributes



50
51
52
# File 'lib/live_contacts.rb', line 50

def parsed_token
  @parsed_token
end

#privacy_policy_urlObject

Live application attibutes



47
48
49
# File 'lib/live_contacts.rb', line 47

def privacy_policy_url
  @privacy_policy_url
end

#return_urlObject

Live application attibutes



47
48
49
# File 'lib/live_contacts.rb', line 47

def return_url
  @return_url
end

#secretObject

Live application attibutes



47
48
49
# File 'lib/live_contacts.rb', line 47

def secret
  @secret
end

#security_algorithmObject

Live application attibutes



47
48
49
# File 'lib/live_contacts.rb', line 47

def security_algorithm
  @security_algorithm
end

#timestampObject

Live application attibutes



47
48
49
# File 'lib/live_contacts.rb', line 47

def timestamp
  @timestamp
end

Instance Method Details

#generate_delegation_urlObject

Step 2 - Get authentication consent from Live.com



67
68
69
70
71
# File 'lib/live_contacts.rb', line 67

def generate_delegation_url
  url = "https://consent.live.com/Delegation.aspx?RU=#{self.return_url}&ps=Contacts.View&pl=#{self.privacy_policy_url}"
  url += "&app=#{generate_app_verifier}" if self.application_verifier_required
  url
end

Step 3 - Process what was returned from Step 2, so we are ready to talk to the API



74
75
76
77
78
79
80
# File 'lib/live_contacts.rb', line 74

def process_consent_params(params)
  return false unless params['ResponseCode'] == 'RequestApproved'
  return false unless params['ConsentToken']
  @consent_successful = false
  @consent_successful = true if process_consent_token(params['ConsentToken'])
  @consent_successful
end

#retrieve_address_book_xmlObject

Step 4 - Actually request some information from the API, at the moment, simply just getting all contacts from the address book TODO timeout, error responses



84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/live_contacts.rb', line 84

def retrieve_address_book_xml
  return unless @consent_successful
  url = URI.parse("https://livecontacts.services.live.com" + "/users/@C@" + self.int_lid.to_s + "/REST/LiveContacts/Contacts")

  req = Net::HTTP::Get.new(url.path)
  req.add_field('Authorization', "DelegatedToken dt=\"" + self.delt + "\"")
  req.set_content_type('application/xml', :charset => 'utf-8')

  con = Net::HTTP.new(url.host, url.port)
  con.use_ssl = true

  res = con.start { |http| http.request(req) }
  res.body
end