Skip to content

Instantly share code, notes, and snippets.

@Humbedooh
Created July 19, 2019 07:03
Show Gist options
  • Star 59 You must be signed in to star a gist
  • Fork 29 You must be signed in to fork a gist
  • Save Humbedooh/612af6038a9ae1c9d5f3b5aee6dbc83a to your computer and use it in GitHub Desktop.
Save Humbedooh/612af6038a9ae1c9d5f3b5aee6dbc83a to your computer and use it in GitHub Desktop.
ASF Oauth Examples
#!/usr/bin/env python3
""" ASF OAuth example in Python 3"""
import cgi
import os
import requests
import urllib
import uuid
def init_oauth():
""" This is what is used to initiate an OAuth request """
# Make a state object where you can store things for later.
state_id = str(uuid.uuid4())
state_object = {
'stuff': 'mystuff',
'id': state_id
}
# Save the state using whatever you wish, pseudo-call below!
save_state(state_object)
# Figure out where our own callback URL is, and what parameters we'd
# like passed along, such as the state ID
callback_url = "https://foo.apache.org/mycallback?state=%s" % state_id
# Set the OAuth gateway URL
oauth_gateway = "https://oauth.apache.org/auth"
# Construct the full redirect URL we are about to pass to browser
redirect_url = "%s?state=%s&redirect_uri=%s" % (
oauth_gateway,
state_id,
urllib.parse.quote(callback_url)
)
# Redirect the browser!
print("Status: 302 Found")
print("Location: %s" %redirect_url)
print("Content-Type: text/plain")
print("")
print("Moved to: %s" % redirect_url)
def callback():
""" This is our callback after the OAuth system has processed login """
params = cgi.FieldStorage()
# Get state ID and OAuth token
state_id = params.getvalue('state')
code = params.getvalue('code')
# Validate state ID and code if need be
validate_parameters_somehow(state_id, code)
# Fetch our state object from wherever we stored it (pseudo-call!)
state_object = load_state(state_id)
# Call up OAuth system and get results!
rv = requests.get("https://oauth.apache.org/token?code=%s" % code).json()
# Check that the token is valid and login worked
if rv.status_code != 200:
bork("Something went wrong!")
# If all good, fetch data and load the JSON into python
else:
credentials = rv.json()
# Validate that our state ID matches the one in the credentials
if credentials['state'] != state_id:
bork("This isn't the data I was hoping for!")
else:
# All good, do your stuff!
do_stuff_with_credentials(credentials)
def main():
""" Simple CGI that derives an action from the URL """
action = os.environ.get('SCRIPT_NAME', '/auth')
# Init OAuth session?
if action == '/auth':
init_oauth()
# Callback??
elif action == '/mycallback':
callback()
else:
bork("I dunno what to do")
if __name__ == '__main__':
main()
@Upinstuff
Copy link

Thank you.

@liuchengwei555
Copy link

Thank

@guillermo-ai
Copy link

You

@jite4780
Copy link

jite4780 commented Sep 3, 2023

Code colour yellow

@Mpmhed
Copy link

Mpmhed commented Dec 6, 2023

@Sarrac3873
Copy link

Tnks

@Maitevt1
Copy link

  1. [](url)

@Asainegams
Copy link

I'm thanaphon vhonghachak lookin wapsi back in London setback I'm Thanaphon vhonghachak to global express for the THA im birthday 11/11/1994

Uploading 2024-03-14-195905214.mp4…

@Khmashwl
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment