Ole Henrik
Posts: 0
Joined: Fri Feb 17, 2012 5:54 pm

Basic authentication for Delphi XE2 datasnap REST service

Hi!

How can I send basic authentication to Embarcadero data snap REST service? I have tried with username:password @servername syntax but this does not work. I have a documentation from Embarcadero how this could be done from a Javascript client. I think they are using username and password in the header with a base64 string. The wiki is located here: http://docwiki.embarcadero.com/RADStu...

Best regards Ole (Timeflex Systemer AS, Norway)

maxkatz
Posts: 0
Joined: Fri Aug 13, 2010 3:24 pm

Basic authentication for Delphi XE2 datasnap REST service

Here is one way you can do it.

-Create Authorization request parameter and mark it as header
-For input mapping, run this code to convert it to base64 string:
code
var bytes = Crypto.charenc.Binary.stringToBytes('username:password');
var base64 = Crypto.util.bytesToBase64(bytes);
return "Basic " + base64;
/code

Or, you can run this script once and hard code the value.

Let me know if this helps...

Ole Henrik
Posts: 0
Joined: Fri Feb 17, 2012 5:54 pm

Basic authentication for Delphi XE2 datasnap REST service

Thanks for a very quick answer :-)

I get the message:

ReferenceError Can't find variable: Crypto

Maybe the library isn't avail?

I've shared the project if you need.

Best regards Ole

maxkatz
Posts: 0
Joined: Fri Aug 13, 2010 3:24 pm

Basic authentication for Delphi XE2 datasnap REST service

Sorry, you need to add this:
code
http://crypto-js.googlecode.com/files/2.5.3-crypto-min.js
/code

There are a couple of ways to add it:
1) Via Create New... JavaScript
2) Project Profile External resources

Ole Henrik
Posts: 0
Joined: Fri Feb 17, 2012 5:54 pm

Basic authentication for Delphi XE2 datasnap REST service

This works! You are a true genious!!!
I tried first making a variable/input parameter with name "auth". But this didn't work. I then used fiddler2 from http://www.fiddler2.com and I saw that "auth" was placed in the header but under Miscellaneous with the string Auth: Basic dXNlcm5hbWU6cGF3Q=.

Then I renamed the the input parametere to "Authorzation" (rfc2616) and now i t works.

my goal is to make a REST api for Timeflex Timerecording in Nordic countries (http://www.timeflex.no english version: http://www.timeflex.com) and at the same time make a solution for Iphone and android. I dont know if I will succeed but you sure have helped me a lot on my way. Thanks !!!

Ole Henrik
Posts: 0
Joined: Fri Feb 17, 2012 5:54 pm

Basic authentication for Delphi XE2 datasnap REST service

sorry-missspelled "Authorzation" should be "Authorization"

Return to “Issues”