-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.py
More file actions
59 lines (48 loc) · 1.56 KB
/
auth.py
File metadata and controls
59 lines (48 loc) · 1.56 KB
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Dec 27 13:32:15 2025
@author: jennaeverard
"""
import requests
from config import LOGIN_URL, REFRESH_URL, USERNAME, PASSWORD, TIMEOUT
class AuthClient:
# keep track of access and refresh token
def __init__(self):
self.access_token = None
self.refresh_token = None
# login and get tokens
# first time logging in
def login(self):
# store username and password in secure file!!
r = requests.post(
LOGIN_URL,
json={"UserName": USERNAME, "Password": PASSWORD},
timeout=TIMEOUT
)
r.raise_for_status() # check for success
data = r.json()
# retrieve access and refresh token
self.access_token = data["AccessToken"]
self.refresh_token = data["RefreshToken"]
# login and get tokens
# renewig access token after first time
def refresh(self):
r = requests.post(
REFRESH_URL,
json={
"UserName": USERNAME,
"RefreshToken": self.refresh_token
},
timeout=TIMEOUT
)
r.raise_for_status() # check for success
data = r.json()
self.access_token = data["AccessToken"]
self.refresh_token = data["RefreshToken"]
# get headers to send with url request
# won't work otherwise!!
def headers(self):
if not self.access_token:
self.login()
return {"Authorization": f"Bearer {self.access_token}"}