Skip to content

Instantly share code, notes, and snippets.

@blakebarnett
Created February 16, 2017 19:52
Show Gist options
  • Save blakebarnett/44009b7fc7f7f3f81fe9bfb4e1ebcf46 to your computer and use it in GitHub Desktop.
Save blakebarnett/44009b7fc7f7f3f81fe9bfb4e1ebcf46 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import sys
import re
import requests
from requests_oauthlib import OAuth2Session
def main():
redirect_uri = 'http://127.0.0.1:5555/callback'
client_id = 'kubernetes'
client_secret = '<secret>'
oauth = OAuth2Session(
client_id=client_id,
scope=['openid', 'profile', 'email', 'offline_access'],
redirect_uri=redirect_uri)
auth_code = authenticate(auth_url(oauth))
print(auth_code)
token = oauth.fetch_token(dex_url('/token'), code=auth_code, client_secret=client_secret, verify=False)
print(token)
def dex_url(string):
return ('https://dex.example.com:5556' + string)
def auth_url(oauth):
url, _ = oauth.authorization_url(dex_url('/auth'))
response = requests.get(url, verify=False)
match = re.search(r"([/]auth[/]ldap)[?]req=(\w+)", response.text)
print(match.groups(1))
return dex_url(match.group())
def authenticate(auth_uri):
requests.get(auth_uri, verify=False)
response = requests.post(
auth_uri, data=dict(login='test', password='pass'),
allow_redirects=False, verify=False)
if 'Location' not in response.headers:
print('Invalid Login!')
sys.exit(1)
print(response.headers['Location'])
response = requests.get(
dex_url(response.headers['Location']),
allow_redirects=False, verify=False)
match = re.search(r"[/]callback[?]code=(\w+)", response.text)
return match.group(1)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment