I am coding an application using Phone Gap to connect to a Sharepoint in Office365.
I need to use the REST services in Sharepoint Office 365 to add items to a list, but before that I need to authenticat
This is the javascript code I use to authenticate:
$.ajax({
'url': 'https://login.microsoftonline.com/extSTS.srf',
dataType: 'text',
type:'POST',
'data': '<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:a="http://www.w3.org/2005/08/addressing" xmlns:u="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"><s:Header><a:Action s:mustUnderstand="1">http://schemas.xmlsoap.org/ws/2005/02/trust/RST/Issue</a:Action><a:MessageID>urn:uuid:40c1407d-b2a4-4e05-8248-8a92b71102b6</a:MessageID><a:ReplyTo><a:Address>http://www.w3.org/2005/08/addressing/anonymous</a:Address></a:ReplyTo><a:To s:mustUnderstand="1">https://login.microsoftonline.com/extSTS.srf</a:To><o:Security s:mustUnderstand="1" xmlns:o="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"><u:Timestamp u:Id="_0"><u:Created>2012-07-26T16:13:00.622Z</u:Created><u:Expires>2012-07-26T16:18:00.622Z</u:Expires></u:Timestamp><o:UsernameToken u:Id="uuid-69882db9-2d6b-45d3-b016-c2156cb6c01d-1"><o:Username>admin@somethingonline.onmicrosoft.com</o:Username><o:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">******</o:Password></o:UsernameToken></o:Security></s:Header><s:Body><t:RequestSecurityToken xmlns:t="http://schemas.xmlsoap.org/ws/2005/02/trust"><wsp:AppliesTo xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy"><a:EndpointReference><a:Address>https://somethingonline.sharepoint.com/_forms/default.aspx?wa=wsignin1.0</a:Address></a:EndpointReference></wsp:AppliesTo><t:KeyType>http://schemas.xmlsoap.org/ws/2005/05/identity/NoProofKey</t:KeyType><t:RequestType>http://schemas.xmlsoap.org/ws/2005/02/trust/Issue</t:RequestType><t:TokenType>urn:oasis:names:tc:SAML:1.0:assertion</t:TokenType></t:RequestSecurityToken></s:Body></s:Envelope>',
headers: {
Accept : "application/soap+xml; charset=utf-8"
},
success: function(result, textStatus, jqXHR) {
console.log('done in login ');
console.log('result '+result);
var xmlDoc = $.parseXML( result );
console.log("xmlDoc:"+xmlDoc);
var xml = $( xmlDoc )
console.log("xml:"+xml);
var binToken= xml.find( "wsse\\:BinarySecurityToken" ).text();
console.log("Binary Token:"+binToken);
// alert( localStorage.getItem('BinaryToken') );
wsignin(binToken);
},
error:function (jqXHR, textStatus, errorThrown){
console.log(errorThrown+'error login:' + jqXHR.responseText);
},
complete:function(jqXHR, textStatus) {
console.log('login completed ' + textStatus);
}
});
Which basically sends an <s:Envelope> requesting authentication.
This sends an XML with a <wsse:BinarySecurityToken> which we then use to actually authenticate:
function wsignin(token){
// alert( token );
console.log('start wsignin' );
console.log('wsignin token:'+token );
$.ajax({
'url': 'https://somethingonline.sharepoint.com/_forms/default.aspx?wa=wsignin1.0',
dataType: 'text',
type:'POST',
'data':token,
headers: {
Accept : "application/x-www-form-urlencoded"
},
success: function(result, textStatus, jqXHR) {
console.log('done wsignin ' + result);
// alert( result );
},
error:function (jqXHR, textStatus, errorThrown){
console.log('error wsignin:' + jqXHR.responseText);
},
complete:function(jqXHR, textStatus) {
console.log('complete wsignin ' + textStatus);
}
});
console.log('end wsignin' );
}
All this works perfectly with Explorer 9 (using jQuery.support.cors = true;) but, the exact same code, with Phone Gap, instead of returning in the result the html of the main page inside https://somethingonline.sharepoint.com/ it returns the Office365 login page.
What can I do? What am I doing wrong? Is there some other way to authenticate?
Update: Reading on MSDN found this:
SharePoint Online Authentication Cookies
An important aspect of this process, and the one that makes it difficult but not impossible to use remote authentication for SharePoint Online in client-side applications, is that the FedAuth cookies are written with an HTTPOnly flag. This flag is designed to prevent cross-site scripting (XSS) attacks. In a cross-site scripting attack, a malicious user injects script onto a page that transmits or uses cookies that are available on the current page for some nefarious purpose. The HTTPOnly flag on the cookie prevents Internet Explorer from allowing access to the cookie from client-side script. The Microsoft .NET Framework observes the HTTPOnly flag also, making it impossible to directly retrieve the cookie from the .NET Framework object model.
Maybe this means... that integration with Sharepoint Online in Office365 ¿is plain impossible?