<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Oauth2 on Noetic Nought</title>
    <link>https://punchagan.muse-amuse.in/tags/oauth2/</link>
    <description>Recent content in Oauth2 on Noetic Nought</description>
    <generator>Hugo</generator>
    <language>en-US</language>
    <copyright>© 2006-2026 CC-BY-SA-4.0</copyright>
    <lastBuildDate>Fri, 20 Jun 2014 10:09:51 -0400</lastBuildDate>
    <atom:link href="https://punchagan.muse-amuse.in/tags/oauth2/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>OAuth2 demystified</title>
      <link>https://punchagan.muse-amuse.in/blog/oauth2-demystified/</link>
      <pubDate>Fri, 20 Jun 2014 10:09:51 -0400</pubDate>
      <guid>https://punchagan.muse-amuse.in/blog/oauth2-demystified/</guid>
      <description>&lt;h2 id=&#34;motivation&#34;&gt;Motivation&lt;/h2&gt;&#xA;&lt;p&gt;I was trying to pair on writing a simple app that uses Hacker&#xA;School&amp;rsquo;s OAuth2 API, and hit a roadblock on the first step of&#xA;requesting an authorization from the user.  Once the user authorized&#xA;my app, I would see an error that said, &amp;ldquo;The authorization server&#xA;does not support this response type&amp;rdquo;.  I was using a client library&#xA;that I had &lt;a href=&#34;https://github.com/litl/rauth&#34;&gt;used before&lt;/a&gt;, and the server was using a what seemed like&#xA;a &lt;a href=&#34;https://github.com/doorkeeper-gem/doorkeeper&#34;&gt;popular implementation&lt;/a&gt; for ruby on rails.  Getting weird errors is&#xA;not done!&lt;/p&gt;&#xA;&lt;p&gt;I have used OAuth2 based authentication &lt;a href=&#34;https://github.com/punchagan/statiki/blob/master/statiki.py#L49&#34;&gt;before&lt;/a&gt;, but the thought of&#xA;using it always makes me a little nervous, just because&lt;/p&gt;&#xA;&lt;ol&gt;&#xA;&lt;li&gt;I don&amp;rsquo;t understand it very well.&lt;/li&gt;&#xA;&lt;li&gt;Like almost everything else, there seem to be so many libraries&#xA;for doing this in Python, and I&amp;rsquo;m never sure which one to use, or&#xA;which one I used the last time around.  Not understanding the&#xA;protocol also doesn&amp;rsquo;t let me debug anything that comes up.&lt;/li&gt;&#xA;&lt;/ol&gt;&#xA;&lt;p&gt;To fix this, I set about to read and understand the &lt;a href=&#34;http://tools.ietf.org/html/rfc6749&#34;&gt;OAuth2 protocol&lt;/a&gt;.&#xA;This blog post is an attempt to record it for future reference, and&#xA;possibly act as a reference for others.&lt;/p&gt;&#xA;&lt;h2 id=&#34;why-oauth&#34;&gt;Why OAuth&lt;/h2&gt;&#xA;&lt;p&gt;OAuth is simply a way for an end-user to allow third parties to use&#xA;protected data, without sharing the user&amp;rsquo;s credentials with the&#xA;third-party.&lt;/p&gt;&#xA;&lt;p&gt;For example, an end-user (Jane) can grant a printing service&#xA;(Printo) access to her protected photos stored at a photo-sharing&#xA;service (Picasa), without sharing her username and password with the&#xA;printing service.  Instead, she authenticates directly with a server&#xA;trusted by the photo-sharing service, which issues the printing&#xA;service delegation-specific credentials. (example from the OAuth 2.0&#xA;spec)&lt;/p&gt;&#xA;&lt;h2 id=&#34;protocol-flow&#34;&gt;Protocol Flow&lt;/h2&gt;&#xA;&lt;p&gt;The flow occurs through a sequence of user actions, client requests&#xA;and user-agent (browser) redirects.&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;&#xA;&lt;p&gt;(A) Printo asks Jane to allow using Picasa Data. The request can&#xA;be sent directly to Jane, but is usually routed via&#xA;Picasa/Google.&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;li&gt;&#xA;&lt;p&gt;(B) Printo gets back an authorization grant, which is a&#xA;credential representing Jane&amp;rsquo;s authorization or approval.  The&#xA;type of the actual grant credential depends on the type of&#xA;request that Printo used.&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;li&gt;&#xA;&lt;p&gt;(C, D) Printo gets back to Google with the credentials it obtained&#xA;in the previous step and obtains a token that it can use to talk&#xA;with Picasa.&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;li&gt;&#xA;&lt;p&gt;(E, F) Printo asks for the desired photo with the token it&#xA;obtained previously, and Picasa gives back the photo to print.&#xA;Jane gets her framed photo!&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;p&gt;But before any of this happens, the client needs to register with&#xA;the authorization server and obtain a &lt;code&gt;client_id&lt;/code&gt; and&#xA;&lt;code&gt;client_secret&lt;/code&gt;, that will be used to identify the client making&#xA;the requests.&lt;/p&gt;&#xA;&lt;h2 id=&#34;pythonized-authorization-code-work-flow-dot&#34;&gt;Pythonized &amp;ldquo;authorization code&amp;rdquo; work-flow.&lt;/h2&gt;&#xA;&lt;p&gt;The OAuth2 spec allows the authorization request/grant to be of 4&#xA;different types.  It also allows some flexibility in the token&#xA;type.&lt;/p&gt;&#xA;&lt;p&gt;In my experience, the most common work-flow seems to be using an&#xA;&lt;em&gt;authorization code&lt;/em&gt; as an authorization grant, and using a &lt;em&gt;Bearer&lt;/em&gt;&#xA;type token.  This work-flow is explained in the diagram below (taken&#xA;from the spec document).  This diagram zooms in, onto the steps A-D&#xA;in the diagram above.&lt;/p&gt;&#xA;&lt;p&gt;This &lt;a href=&#34;https://gist.github.com/punchagan/76e8771fc26cd243f3ac&#34;&gt;python code snippet&lt;/a&gt; is a simple implementation of this&#xA;workflow, using the Hacker School API.&lt;/p&gt;&#xA;&lt;script src=&#34;https://gist.github.com/76e8771fc26cd243f3ac.js&#34;&gt;&lt;/script&gt;&#xA;&lt;h2 id=&#34;conclusion&#34;&gt;Conclusion&lt;/h2&gt;&#xA;&lt;p&gt;I think, I understand the OAuth2 spec a lot better now, and hope&#xA;that this will help others understand it, too.  And more&#xA;importantly, I won&amp;rsquo;t get nervous when I have to add it to my&#xA;projects.&lt;/p&gt;&#xA;&lt;p&gt;Also, &lt;a href=&#34;https://github.com/idan/oauthlib&#34;&gt;oauthlib&lt;/a&gt; for Python seems to be a pretty thorough&#xA;implementation of the spec, and &lt;a href=&#34;https://github.com/requests/requests-oauthlib&#34;&gt;requests-oauthlib&lt;/a&gt; seems to wrap it&#xA;for use with requests.  I think I&amp;rsquo;m going to use this in my future&#xA;projects.&lt;/p&gt;&#xA;</description>
    </item>
  </channel>
</rss>
