blob: 5440bf90f2232e27c03d1a01132c282f5fd2c476 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
|
"""
Python 3 Web Application OAuth 2.0 Example.
This script demonstrates how to perform the OAuth 2.0 authorization code flow
for a web-based application using the EVE Online SSO.
Prerequisites:
* Create an SSO application at https://developers.eveonline.com with the
scope "esi-characters.read_blueprints.v1" and the callback URL
"https://localhost/callback/". Note: Never use localhost as a callback
in production applications.
* Have a Python 3 environment available (possibly by using a virtual
environment: https://virtualenv.pypa.io/en/stable/).
* Install the required packages by running:
pip install -r requirements.txt
To run this example, make sure you have completed the prerequisites and then
run the following command:
>>> python esi_oauth_web.py
Then follow the prompts.
"""
import base64
import os
from typing import Optional
from dotenv import load_dotenv
from shared_flow import (
handle_sso_token_response,
print_auth_url,
send_token_request,
)
def main() -> None:
"""
Guides you through a local example of the OAuth 2.0 web flow.
"""
print(
"This program will take you through an example OAuth 2.0 flow "
"that you should be using if you are building a web-based "
"application. Follow the prompts and enter the information requested."
)
client_id: Optional[str] = os.getenv('CLIENT_ID')
if not client_id:
raise ValueError("CLIENT_ID not found in environment variables.")
print_auth_url(client_id)
auth_code: str = input('Copy the "code" query parameter and enter it here: ')
# Basic auth can be handled by the requests library with the auth parameter,
# but we're constructing it manually for educational purposes.
secret_key: Optional[str] = os.getenv('SECRET_KEY')
if not secret_key:
raise ValueError("SECRET_KEY not found in environment variables.")
user_pass: str = f"{client_id}:{secret_key}"
basic_auth: str = base64.urlsafe_b64encode(user_pass.encode('utf-8')).decode()
auth_header: str = f"Basic {basic_auth}"
form_values: dict[str, str] = {
"grant_type": "authorization_code",
"code": auth_code,
}
headers: dict[str, str] = {"Authorization": auth_header}
print(
"\nThe following request uses basic authentication, as a web-based "
"app you should do the same."
)
input("\nPress Enter to continue...")
res = send_token_request(form_values, add_headers=headers)
handle_sso_token_response(res)
if __name__ == "__main__":
load_dotenv()
main()
|