Skip to content

Commit

Permalink
Issue #3: pre-commit run --all-files
Browse files Browse the repository at this point in the history
  • Loading branch information
soxofaan committed Dec 11, 2024
1 parent 428562f commit 2a307f8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 35 deletions.
33 changes: 11 additions & 22 deletions oidc-device-flow/helpers.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,19 @@
import os
import requests
import jwt
import time
import json
import webbrowser
import http.server
from urllib.parse import urlencode, parse_qs

import requests


def get_discovery_document(url):
response = requests.get(url)
return response.json()


def request_device_code(discovery_doc, client_credentials):
data={}
data = {}
data.update(client_credentials)
data.update({'scope': 'offline_access' })
response = requests.post(
discovery_doc["device_authorization_endpoint"], data=data
)
data.update({"scope": "offline_access"})
response = requests.post(discovery_doc["device_authorization_endpoint"], data=data)
if response.status_code == 200:
return response.json()
else:
Expand All @@ -27,14 +22,11 @@ def request_device_code(discovery_doc, client_credentials):
return None



def poll_for_access_token(
discovery_doc, client_credentials, device_code, interval, timeout
):
def poll_for_access_token(discovery_doc, client_credentials, device_code, interval, timeout):
payload = {
"grant_type": "urn:ietf:params:oauth:grant-type:device_code",
"device_code": device_code,
"client_id": client_credentials["client_id"]
"client_id": client_credentials["client_id"],
}
start_time = time.time()
print("\nWaiting for device to be authorized ...")
Expand All @@ -59,13 +51,10 @@ def device_login(discovery_doc, client_credentials):
verification_uri_complete = device_code_response.get("verification_uri_complete", None)
webbrowser.open(verification_uri_complete)
if verification_uri_complete:
print(
f"The following URL has been opened in your browser: {verification_uri_complete}"
)
print(f"The following URL has been opened in your browser: {verification_uri_complete}")
else:
print(f"Something went wrong")
print("Something went wrong")


interval = device_code_response["interval"]
timeout = device_code_response["expires_in"]
device_login = poll_for_access_token(
Expand All @@ -83,4 +72,4 @@ def device_login(discovery_doc, client_credentials):
return None
else:
print("Failed to obtain device code")
return None
return None
29 changes: 16 additions & 13 deletions oidc-device-flow/oidc-cli-example.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
from pprint import pprint

from helpers import (
get_discovery_document,
device_login,
)
from helpers import device_login, get_discovery_document

discovery_doc = get_discovery_document("https://auth.apex.esa.int/realms/apex/.well-known/openid-configuration")

client_credentials ={
"client_id": "project-a-catalogue-dev-browser"
}
client_credentials = {"client_id": "project-a-catalogue-dev-browser"}

response = device_login(discovery_doc, client_credentials)

print("\n\nScopes: %s" % response["scope"])

print("\naccess token ( Expires after %s seconds ):\n-------------------------------------------------------------------------------------------------------------------" % response["expires_in"])
print(
"\naccess token ( Expires after %s seconds ):\n-------------------------------------------------------------------------------------------------------------------"
% response["expires_in"]
)
print(response["access_token"])
print("-------------------------------------------------------------------------------------------------------------------")
print(
"-------------------------------------------------------------------------------------------------------------------"
)

print("\nRefresh token ( Expires after %s seconds ):\n-------------------------------------------------------------------------------------------------------------------" % response["refresh_expires_in"])
print(
"\nRefresh token ( Expires after %s seconds ):\n-------------------------------------------------------------------------------------------------------------------"
% response["refresh_expires_in"]
)
print(response["refresh_token"])
print("-------------------------------------------------------------------------------------------------------------------\n")
print(
"-------------------------------------------------------------------------------------------------------------------\n"
)

0 comments on commit 2a307f8

Please sign in to comment.