Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Enode authentication class #81

Merged
merged 29 commits into from
Apr 21, 2023
Merged
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
cdc0b8f
Create enode_auth.py
jackypark9852 Apr 11, 2023
0ddd7fb
Define basic auth function
jackypark9852 Apr 13, 2023
181592a
Get api key from env
jackypark9852 Apr 13, 2023
78938ad
Finish part 1
jackypark9852 Apr 13, 2023
553cc31
Create httpx auth class
jackypark9852 Apr 14, 2023
c803632
Add commnet
jackypark9852 Apr 14, 2023
806c37a
Remove test end point
jackypark9852 Apr 14, 2023
4f663ae
clean up
jackypark9852 Apr 14, 2023
ed84375
Remove print messages
jackypark9852 Apr 14, 2023
97fa169
poetry lock && install
jackypark9852 Apr 14, 2023
1ac31c0
Make getenv set variable to str
jackypark9852 Apr 14, 2023
233c7ed
make format
jackypark9852 Apr 14, 2023
0849563
Comment out enode client in main
jackypark9852 Apr 14, 2023
d43a9e7
lint and format
jackypark9852 Apr 14, 2023
69ee29c
Remove unecessary check
jackypark9852 Apr 14, 2023
47e497e
Remove test files
jackypark9852 Apr 14, 2023
742def9
Use env var for enode token url
jackypark9852 Apr 14, 2023
96e87e5
Change get_enode_access_token to return request
jackypark9852 Apr 18, 2023
8cd01ca
Refactor get_enode_access_token() into EnodeAuth class
jackypark9852 Apr 18, 2023
4aae6b3
change 404 to 401
jackypark9852 Apr 18, 2023
c016e9c
format
jackypark9852 Apr 18, 2023
7e1b845
Remove test code
jackypark9852 Apr 18, 2023
5877f5d
Remove print
jackypark9852 Apr 18, 2023
b6f5252
Remove print
jackypark9852 Apr 18, 2023
bff3d4c
Merge branch 'h4i/enode-auth' of https://github.com/openclimatefix/pv…
jackypark9852 Apr 18, 2023
03a2785
Address PR changes
jackypark9852 Apr 20, 2023
643ecb4
Update enode constructor to take in client_id, secret, and url
jackypark9852 Apr 20, 2023
6fe62ca
Add type hints
jackypark9852 Apr 20, 2023
c931e79
Fix poetry lock
jackypark9852 Apr 20, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 37 additions & 0 deletions pv_site_api/enode_auth.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import os

import httpx


class EnodeAuth(httpx.Auth):
def __init__(self, access_token):
self.access_token = access_token
jackypark9852 marked this conversation as resolved.
Show resolved Hide resolved

def auth_flow(self, request):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a type hint

Suggested change
def auth_flow(self, request):
def auth_flow(self, request: httpx.Request):

# Add the Authorization header to the request using the current access token
request.headers["Authorization"] = f"Bearer {self.access_token}"
response = yield request

if response.status_code == 404:
jackypark9852 marked this conversation as resolved.
Show resolved Hide resolved
# The access token is no longer valid, refresh it
token_response = yield self._build_refresh_request()
self._update_access_token(token_response)
# Update the request's Authorization header with the new access token
request.headers["Authorization"] = f"Bearer {self.access_token}"
# Resend the request with the new access token
response = yield request
return response

def _build_refresh_request(self):
# TODO: Add environment variables
client_id = os.getenv("CLIENT_ID")
client_secret = os.getenv("CLIENT_SECRET")
basic_auth = httpx.BasicAuth(client_id, client_secret)
url = os.getenv("ENODE_TOKEN_URL")
data = {"grant_type": "client_credentials"}
request = next(basic_auth.auth_flow(httpx.Request("POST", url, data=data)))
return request
jackypark9852 marked this conversation as resolved.
Show resolved Hide resolved

def _update_access_token(self, response):
response.read()
self.access_token = response.json()["access_token"]