OAuth4 (and OAuth4R)

65
Presentation to Singapore Ruby Brigade at SMU, School of Information System 29 November 2007 http://flickr.com/photos/lachlanhardy/1400641336/ Auth Chew Choon Keat sharedcopy.com

Transcript of OAuth4 (and OAuth4R)

Page 1: OAuth4 (and OAuth4R)

Presentation to Singapore Ruby Brigadeat SMU, School of Information System29 November 2007

http://flickr.com/photos/lachlanhardy/1400641336/

AuthChew Choon Keatsharedcopy.com

Page 2: OAuth4 (and OAuth4R)

Why OAuth

• Web 2.0

• APIs

• Mashups

Page 3: OAuth4 (and OAuth4R)

Giving away access

• Mint “an impressive personal finance application”

Page 4: OAuth4 (and OAuth4R)

• Mint Terms of Service

Page 6: OAuth4 (and OAuth4R)

• "Giving your email account password to a social network site so they can look up your friends is the same thing as going to dinner and giving your ATM card and PIN code to the waiter when it’s time to pay."- oauth.net

Giving away access

Page 7: OAuth4 (and OAuth4R)

Alternatives: Hidden Public

• Random URLs

• Security by obscurity

Page 8: OAuth4 (and OAuth4R)

Alternatives: Proprietary

• Google AuthSub

• AOL OpenAuth

• Yahoo BBAuth

• Upcoming API

• Flickr API

• Amazon Web Services API

Page 9: OAuth4 (and OAuth4R)

What is OAuth

• “An open protocol to allow secure API authentication in a simple and standard method from desktop and web applications.”

Page 10: OAuth4 (and OAuth4R)

OAuth Flow

• Registration (server to server)

• Request Token

• Authorization

• Access Token

http://flickr.com/photos/petromyzon/26252991/

Page 11: OAuth4 (and OAuth4R)

End User

http://flickr.com/photos/andreasnilsson1976/433173596/

Page 12: OAuth4 (and OAuth4R)

Protected Resource

http://flickr.com/photos/annettepedrosian/2071523294/

Page 13: OAuth4 (and OAuth4R)

Service Provider

http://flickr.com/photos/spectrasensors/322545693/

Page 14: OAuth4 (and OAuth4R)

Consumer

http://flickr.com/photos/infidelic/147930477/

Page 15: OAuth4 (and OAuth4R)

Tokens

http://flickr.com/photos/kt/364996966/

Page 16: OAuth4 (and OAuth4R)

Service Provider

ConsumerProtected Resources

End User

Page 17: OAuth4 (and OAuth4R)

Consumer

End User

“Let’s work togetherhere are my details”

Protected ResourcesService Provider

http://flickr.com/photos/marcroberts/1484118790/

Consumer Registration

Page 18: OAuth4 (and OAuth4R)

Consumer

End User

Protected Resources Service Provider

Consumer Registration

Page 19: OAuth4 (and OAuth4R)

Consumer

End User

http://flickr.com/photos/9458565@N07/760773574/

Protected Resources

“These are our secrets. Use it every time you talk to me”

Service Provider

Consumer Registration

Page 20: OAuth4 (and OAuth4R)

Consumer

End User

Protected Resources Service Provider

Consumer Registration

Page 21: OAuth4 (and OAuth4R)

Use CaseConsumer

End User

Protected Resources

“Print my pictures from SP”

Service Provider

Page 22: OAuth4 (and OAuth4R)

Get Request TokensConsumer

End User

“I have someonewho needs you”

Protected ResourcesService Provider

Page 23: OAuth4 (and OAuth4R)

Consumer

End User

http://flickr.com/photos/9458565@N07/760773574/

Protected Resources

“Pass this to him, and bring him to me”

Service Provider

Get Request Tokens

Page 24: OAuth4 (and OAuth4R)

Get AuthorizationConsumer

“Go to there. Bring this along”

Protected Resources

End User

Service Provider

Page 25: OAuth4 (and OAuth4R)

Consumer

“Hi, remember me?”

End User

Service ProviderProtected Resources

Get Authorization

Page 26: OAuth4 (and OAuth4R)

Consumer

End User

Protected Resources Service Provider

Get Authorization

“Silver coin! You need Consumer to do things for you?”

Page 27: OAuth4 (and OAuth4R)

Consumer

“Yes”

Protected Resources

End User

Service Provider

Get Authorization

Page 28: OAuth4 (and OAuth4R)

Consumer

End User

Protected Resources

Get Authorization

“Your wish is my command. Return there”

Service Provider

Page 29: OAuth4 (and OAuth4R)

Get Access TokenConsumerProtected Resources

“Its done!”

End User

Service Provider

Page 30: OAuth4 (and OAuth4R)

End User

Consumer

“He said ok? Gimme the keys”

Protected ResourcesService Provider

Get Access Token

Page 31: OAuth4 (and OAuth4R)

Consumer

End User

Protected Resources

“Ignore that silly silver coin... Use this from now and I will

always treat you as he”

Service Provider

http://flickr.com/photos/azuric/150520121/

Get Access Token

Page 32: OAuth4 (and OAuth4R)

End User

Protected Resources ConsumerService Provider

Get Access Token

Page 33: OAuth4 (and OAuth4R)

End User

Protected Resources ConsumerService Provider

Use Access Token

“Gimme MY pictures”

Page 34: OAuth4 (and OAuth4R)

End User

Protected Resources

Using Access TokenConsumerService Provider

Page 35: OAuth4 (and OAuth4R)

• Whenever Consumer calls SP’s API

• GET /photos.xml

• bring consumer key, access token

• sign with consumer secret & access secret

• Service Provider verifies signature

• treats request as End User

Using Access Token

Page 36: OAuth4 (and OAuth4R)

• User at Service Provider website can choose to invalidate the access for Consumer at any time

Using Access Token

Page 37: OAuth4 (and OAuth4R)

Desktop Flow

Page 38: OAuth4 (and OAuth4R)

http://flickr.com/photos/factoryjoe/sets/72157601300877805/detail/

Desktop Flow

Page 39: OAuth4 (and OAuth4R)

http://flickr.com/photos/factoryjoe/sets/72157601300877805/detail/

Desktop Flow

Page 40: OAuth4 (and OAuth4R)

http://flickr.com/photos/factoryjoe/sets/72157601300877805/detail/

Desktop Flow

Page 41: OAuth4 (and OAuth4R)

http://flickr.com/photos/factoryjoe/sets/72157601300877805/detail/

Desktop Flow

Page 42: OAuth4 (and OAuth4R)

Desktop Flow

http://flickr.com/photos/factoryjoe/sets/72157601300877805/detail/

Page 43: OAuth4 (and OAuth4R)

Desktop Flow

http://flickr.com/photos/factoryjoe/sets/72157601300877805/detail/

Page 44: OAuth4 (and OAuth4R)

Desktop Flow

http://flickr.com/photos/factoryjoe/sets/72157601300877805/detail/

Page 45: OAuth4 (and OAuth4R)

Introducing OAuth4R

• Forget the protocol, just fill in the blanks

• Provides code generators to allow Rails website to support OAuth easily

• Generated scaffolds does the OAuth dance out of the box

• Only need developers to link tokens to their Users

Page 46: OAuth4 (and OAuth4R)

OAuth4Rsvn checkout http://oauth4r.googlecode.com/svn/trunk/example_apps

• “Provider” site contains

• users

• users’ contacts

• “Consumer” site contains

• only users

Page 47: OAuth4 (and OAuth4R)

• Users controller at http://localhost:5001/users

• with primitive login implemented

• Users’ Addressbook controller at http://localhost:5001/contacts

• with primitive permissions based on user’s login

OAuth4R: Providercd example_apps/oauth_providerrake db:create:allrake db:migrate./script/server -p 5001

Page 48: OAuth4 (and OAuth4R)

• Users controller at http://localhost:5000/users

• even more primitive login implementation

• For this demo, create a new user, “Tommy”

OAuth4R: Consumercd ../oauth_consumer/rake db:create:allrake db:migrate./script/server -p 5000

Page 49: OAuth4 (and OAuth4R)

OAuth4R: Providercd ../oauth_provider/./script/generate oauth_provider GetContactrake db:migratepatch -p0 < TODO.patch ./script/server -p 5001

• Generate a “scaffold controller”

• Controller does the OAuth dance

• Modify to linkup with your own user models

Page 50: OAuth4 (and OAuth4R)

• Modifying generated OAuth controller

• oauth_user = User.find(session..)

Page 51: OAuth4 (and OAuth4R)

• Modify your User model to has_many oauth_user

• Modify controller guarding Protected Resources to requires_oauth

Page 52: OAuth4 (and OAuth4R)

OAuth4R: Consumercd ../oauth_consumer/./script/generate oauth_consumer UseGetContactrake db:migratepatch -p0 < TODO1.patch./script/server -p 5000

• Generate a “scaffold controller”

• Controller can do OAuth dance with one service provider

• Modify to linkup with your User models

Page 53: OAuth4 (and OAuth4R)

• Modify generated OAuth controller

• oauth_user = User.find(session..)

Page 54: OAuth4 (and OAuth4R)

• Modify user to has_many oauth_user

• Add a link to kick-start OAuth authorization

link_to .. new_use_get_contact_path

Page 55: OAuth4 (and OAuth4R)

• Go to http://localhost:5000/use_get_contacts

• Copy “Callback URL”

Registering Consumer

Page 56: OAuth4 (and OAuth4R)

• http://localhost:5001/get_contacts/new

• Paste “Callback URL” & click Register

• Update config/use_get_contacts.oauth.yml

Registering Consumer

Page 57: OAuth4 (and OAuth4R)

• Go to http://localhost:5000/users

• Click on “Tommy > Show” to login

• Click on "Establish OAuth..."

User Authorization

Page 58: OAuth4 (and OAuth4R)

• Click “Create” and you’ll arrive at provider site (http://localhost:5001) to Login

• Authorization prompt will appear

• Click “Yes” & you’ll be redirected back to consumer site (http://localhost:5000)

User Authorization

Page 59: OAuth4 (and OAuth4R)

All done, then what?

• Scripts accessing APIs on behalf of End User

• This demo uses a simple ActiveResource

Page 60: OAuth4 (and OAuth4R)

• OAuth blocks our unauthenticated access

• We need to modify our API callers slightly

$ ruby script/fetch_contacts.rb /example_apps/oauth_consumer/vendor/rails/activeresource/lib/active_resource/connection.rb:124:in `handle_response': Failed with 500 Internal Server Error (ActiveResource::ServerError)

patch -p0 < TODO2.patch

All done, then what?

Page 61: OAuth4 (and OAuth4R)

• Add acts_as_oauth_resource

• underlying http connection will be automatically padded with OAuth credentials

Modify ActiveResource

Page 62: OAuth4 (and OAuth4R)

• Wrap ActiveResource activity inside with_oauth code blocks

Backend API Access?

Page 63: OAuth4 (and OAuth4R)

$ ruby script/fetch_contacts.rb --- - !ruby/object:Contact attributes: name: Dick updated_at: 2007-11-29 08:11:35 Z id: 1 user_id: 1 created_at: 2007-11-29 08:11:35 Z prefix_options: &id001 {}

- !ruby/object:Contact attributes: name: Harry updated_at: 2007-11-29 08:11:35 Z id: 2 user_id: 1 created_at: 2007-11-29 08:11:35 Z prefix_options: *id001

Done

Page 64: OAuth4 (and OAuth4R)

Ruby Links

• OAuth4Rhttp://oauth4r.googlecode.com/

• OAuth Rails Pluginhttp://oauth-plugin.googlecode.com/http://stakeventures.com/articles/2007/11/26/how-to-turn-your-rails-site-into-an-oauth-provider

• OAuth Gemsudo gem install oauth

• OAuth (was Twitter)http://oauth.googlecode.com/svn/code/ruby/

• Google Group: oauth-rubyhttp://groups.google.com/group/oauth-ruby

Page 65: OAuth4 (and OAuth4R)

Thank you!