facebook/tornado — pull request #667 by shiroyuki (2013-01-14 04:25:45+00:00)
Title: Add the support for GitHub OAuth2 Authentication

This is a simple implementation to support GitHub OAuth2 Authentication.

For example,

``` python
from tornado.web import asynchronous
from tornado.auth import GitHubMixin

class GitHubAuthentication(Controller, GitHubMixin):
xsite_token = 'github'
client_id = 'cid'
client_secret = 'cse'

@asynchronous
def get(self):
params = {
'redirect_uri': 'http://shiroyuki.com/login',
'client_id': self.client_id,
'state': self.xsite_token
}

if self.session.get('user'):
return self.redirect('/')

code = self.get_argument('code', None)

# Seek the authorization
if code:
# For security reason, the state value (cross-site token) will be
# retrieved from the query string.
params.update({
'client_secret': self.client_secret,
'success_callback': self._on_login,
'error_callback': self._on_error,
'code': code,
'state': self.get_argument('state', None)
})

self.get_authenticated_user(**params)

return

# Redirect for user authentication
self.get_authenticated_user(**params)

def _on_login(self, user, access_token=None):
self.session.set('user', user)

self.redirect('/')

def _on_error(self, code, body=None, error=None):
self.write('

HTTP {}

'.format(code))

if body:
self.write('

Content: {}

'.format(body))

if error:
self.write('

Error: {}

'.format(error))

self.finish()
```

Please note that this PR does not have tests included. I would like some help on updating the tests.