Last active
June 14, 2017 21:12
-
-
Save woloski/8149412 to your computer and use it in GitHub Desktop.
this shows a very simple bare bones example of authenticating with Auth0 using standard python libraries
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import webapp2 | |
import urllib2 | |
import urllib | |
import json | |
## CHANGE THIS | |
CLIENT_ID = "vYPeq7LGf1utg2dbDlGKCwGKgy94lPH0" | |
CLIENT_SECRET = "DLQ5dWkNMwPlUWo2jqVkbG1PFyeMvV60HEJaW0FioeI4ZxGaAW73BiqRBZmRk29v" | |
DOMAIN = "contoso.auth0.com" | |
CALLBACK_URL = "http://localhost:8080/callback" | |
MAIN_PAGE_HTML = """\ | |
<html> | |
<body> | |
<script src="https://cdn.auth0.com/w2/auth0-widget-2.6.min.js"></script> | |
<script type="text/javascript"> | |
var widget = new Auth0Widget({ | |
domain: '%s', | |
clientID: '%s', | |
callbackURL: '%s' | |
}); | |
</script> | |
<button onclick="widget.signin()">Login</button> | |
</body> | |
</html> | |
""" % (DOMAIN, CLIENT_ID, CALLBACK_URL) | |
class MainPage(webapp2.RequestHandler): | |
def get(self): | |
self.response.write(MAIN_PAGE_HTML) | |
class LoginCallback(webapp2.RequestHandler): | |
def get(self): | |
code = self.request.get("code") | |
base_url = "https://{domain}".format(domain=DOMAIN) | |
data = urllib.urlencode([('client_id', CLIENT_ID), | |
('redirect_uri', CALLBACK_URL), | |
('client_secret', CLIENT_SECRET), | |
('code', code), | |
('grant_type', 'authorization_code')]) | |
req = urllib2.Request(base_url + "/oauth/token", data) | |
response = urllib2.urlopen(req) | |
oauth = json.loads(response.read()) | |
userinfo = base_url + "/userinfo?access_token=" + oauth['access_token'] | |
response = urllib2.urlopen(userinfo) | |
data = response.read() | |
## print user data | |
self.response.write(data) | |
application = webapp2.WSGIApplication([ | |
('/', MainPage), | |
('/callback', LoginCallback) | |
], debug=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment