· 6 years ago · Oct 22, 2019, 09:14 PM
1import json, os, requests, time
2from base64 import b64encode
3from flask import abort, session
4
5# Session Variables Used in this File
6# accessToken, charID, expire
7# loggedIn, refreshToken
8#
9#
10
11####---- CONFIGURATION VARIABLES ----####
12####---- THESE REFERENCE THE .ENV ----####
13redirect = os.environ['ESI_REDIRECT']
14clientID = os.environ['ESI_CLIENTID']
15secretKey = os.environ['ESI_SECRETKEY']
16appScope = "esi-universe.read_structures.v1 esi-corporations.read_structures.v1 esi-industry.read_character_mining.v1 esi-industry.read_corporation_mining.v1 esi-markets.structure_markets.v1"
17state = "securityCheck"
18tokenURL = "https://login.eveonline.com/oauth/token"
19
20# Create the login URI
21def genLoginURI():
22 return (
23 "https://login.eveonline.com/oauth/authorize?response_type=code&redirect_uri={}&client_id={}&scope={}&state={}".format(
24 redirect,
25 clientID,
26 appScope,
27 state
28 )
29 )
30
31# Create application token for use in the token request step
32def application_token():
33 return b64encode(
34 "{}:{}".format(
35 clientID,
36 secretKey
37 ).encode('utf-8')
38 ).decode('ascii')
39
40# Set or update all required authorization and reauthorization information
41def updateTokens(response):
42 session['accessToken'] = response['access_token']
43 session['refreshToken'] = response['refresh_token']
44 session['expire'] = int(response['expires_in']) / 2 + time.time()
45
46# Sets the headers based on the header type required
47def createHeaders(headerType):
48 # Sets a basic header type, used for login
49 if headerType == "basic":
50 headers = {
51 "Authorization": "Basic {}".format(
52 application_token()
53 ),
54 "Content-Type": "application/x-www-form-urlencoded"
55 }
56 # Sets a bearer header type
57 elif headerType == "bearer":
58 headers = {
59 "Authorization":"Bearer {}".format(
60 session['accessToken']
61 )
62 }
63 else:
64 headers = {}
65 return headers
66
67# Main login routine
68def createSession(auth_code, returnedState):
69 if returnedState != state:
70 # Potential Security Problem
71 abort(500)
72
73 # Sets the headers, including formating client ID/secret key and data
74 headers = createHeaders("basic")
75
76 data = {
77 "grant_type": "authorization_code",
78 "code": auth_code
79 }
80
81 # Get initial access and refresh tokens
82 tokenRequest = requests.post(
83 tokenURL,
84 data=data,
85 headers=headers
86 )
87
88 # Parse to dict
89 response = json.loads(tokenRequest.text)
90
91 # Update session variables, verify token and set logged in character
92 updateTokens(response)
93
94 headers = createHeaders("bearer")
95 tokenRequest = requests.get(
96 "https://login.eveonline.com/oauth/verify",
97 headers=headers
98 )
99
100 # Create and update the charID session variable
101 session['charID'] = json.loads(tokenRequest.text)['CharacterID']
102
103 session['loggedIn'] = True
104
105 return session['loggedIn']
106
107# Use the refresh token to reauthenticate
108def reauthenticate():
109 if session['expire'] <= time.time():
110 headers = createHeaders("basic")
111 tokenRequest = requests.post(
112 tokenURL,
113 headers=headers,
114 data={
115 "grant_type":"refresh_token",
116 "refresh_token":session['refreshToken']
117 }
118 )
119 updateTokens(json.loads(tokenRequest.text))
120 # else no authentication needed