Download the free trial version
Basic4android Video
Features
Tutorials and manuals
Showcase
Screenshots

Go Back   Android Development Forum - Basic4android > Basic4android > Basic4android Getting started & Tutorials
Documentation Wiki Register Members List Windows Mobile Search Today's Posts Mark Forums Read

Basic4android Getting started & Tutorials Android development starts here. Please do not post questions in this sub-forum.

OAuth - How to roll your own (easy).

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-18-2011, 06:25 PM
Newbie
 
Join Date: Jul 2011
Posts: 1
Cool OAuth - How to roll your own (easy).

Hello,

I have seen a few unanswered OAuth questions here and since making an app for an existing site almost always needs OAuth, I decided to check out what was available.

Whilst the twitterlib by Always Busy is cool, it is not usable (for me) for some more obscure OAuth site.

I am of the school 'teach a man how to fish' rather than 'give him a subscription to the fish 'n chips shop', so I will explain but I will not add full blown examples; you learn more by assembling it yourself.

Now, after this lengthy intro, let's start.
-----

First, we need some documentation on this OAuth thingy. Luckily the kind people at the IETF keep track of standards.

Whilst OAuth is not an RFC Standards track document, it is a good starting point.

So, point your browser to RFC 5849 - The OAuth 1.0 Protocol and start reading.

With all the examples in there it is quite easy to see what OAuth does.

The concatenation of strings should also not be a stumbling block, so, in fact, all we really need to be concerned about is the signing.

We could implement our own SHA1 algorithm, but a Mr. AGraham has provided us with all the crypto stuff we need, including a converter.

Next, let's do the HMAC-SHA1 key signing. Wikipedia always has some nice references to check if we are doing things right.

Point your browser to: HMAC - Wikipedia, the free encyclopedia

Then you need to reference the ByteConverter and the Encryption libs by AGraham and you can use the following code to check that the signing for the OAuth really is that simple:

Code:
        Dim MyKeyS As String 'The key as a string
Dim MyKeyB() As Byte 'The key as a byte array

Dim MyString As String 'The string we are going to sign    

MyKeyS=
"key"
MyString=
"The quick brown fox jumps over the lazy dog"

'We need our key in byteformat
Dim ByteEnc As ByteConverter
MyKeyB = ByteEnc.StringToBytes(MyKeyS,
"utf8")


'We abuse the KeyGenerator to get a java.crypto.key object
Dim KeyGen As KeyGenerator
KeyGen.Initialize(
"AES")
KeyGen.KeyFromBytes(MyKeyB)

'The encryption class has a MAC, so things could not be simpler
Dim MyMac As Mac
MyMac.Initialise(
"HMAC-SHA1",KeyGen.Key)
MyMac.Update(ByteEnc.StringToBytes(MyString,
"utf-8"))

'Dump the hex key to the log, so we can verify with the reference
Log("KEYSIGN: " & ByteEnc.HexFromBytes(MyMac.Sign()))
Basically, that's it. Things could be coded in B4A a lot more efficient I guess, but I am a noob at this language (not a noob at coding, that is why I like RFC's).

Well, that wraps it up. Now you should be able to do any OAuth you want.
Reply With Quote
  #2 (permalink)  
Old 07-18-2011, 08:08 PM
agraham's Avatar
Basic4android Veteran
 
Join Date: Jul 2007
Location: Cheshire, UK
Posts: 6,835
Awards Showcase
Innovator medal Beta Tester Forum Contributer 
Total Awards: 3
Default

__________________
Sorry, but I don't answer questions by PM or email. Please post your queries in the forum.

If you have benefitted from my work a contribution would be welcome
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Tutorial Request: OAuth sample ssg Basic4android Updates and Questions 4 07-21-2011 01:04 PM
Orientation - Roll alfcen Basic4android Updates and Questions 3 01-03-2011 02:54 PM
easy question ? tremara1 Basic4android Updates and Questions 4 12-19-2010 08:00 AM
easy conversion to .ICO Mornixuur Code Samples & Tips 0 04-23-2010 01:39 PM
Any chance for an OAuth library? Zaam Questions (Windows Mobile) 1 02-22-2010 11:33 PM


All times are GMT. The time now is 05:27 AM.


Powered by vBulletin® Version 3.6.12
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0