Skip to content

Commit

Permalink
Merge pull request #86 from scramjetorg/fix/refresh_token
Browse files Browse the repository at this point in the history
Changed the way of getting the refresh token
  • Loading branch information
alicja-gruzdz committed Feb 20, 2024
2 parents eb5e36c + 44ca53c commit aa19a29
Show file tree
Hide file tree
Showing 5 changed files with 292 additions and 103 deletions.
2 changes: 1 addition & 1 deletion python/Auth0/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

"auth0_verified_url" : "https://your_tenant.auth0.com/api/v2/users?per_page=5&sort=created_at%3A-1&fields=created_at%2Cnickname%2Cemail%2Cupdated_at&q=user_metadata.newsletter%3D%22true%22",
"auth0_users_url" :"https://your_tenant.auth0.com/api/v2/users?per_page=5&sort=created_at%3A-1",
"api_url" : "<your api url>",
"api_url" : "https://your_tenant.auth0.com/oauth/token",
"request_data" : {"client_id":"<your client id>","client_secret":"<your client secret>","audience":"<your audience>","grant_type":"client_credentials"}

}
97 changes: 65 additions & 32 deletions python/Auth0/main.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,30 @@
import asyncio
from scramjet.streams import Stream
import requests
import requests_async
import json
import enum
import asyncio
from shiftarray import ShiftArray
from scramjet.streams import Stream

provides = {"provides": "pipe", "contentType": "text/plain"}

WAIT_TIME_ON_USER = 5
WAIT_TIME_ERROR = 3

TOKEN_HEADER = {"content-type": "application/json"}

class TokenRefreshResult(enum.Enum):
SUCCESS = 200
FAILURE = 0


class Auth0:
def __init__(self, verified_url, users_url, api_url, request_data, stream):
def __init__(self, verified_url, users_url, api_url, request_data, stream, logger):
self.verified_url = verified_url
self.users_url = users_url
self.api_url = api_url
self.request_data = request_data
self.token_header = {
"content-type": "application/json",
}
self.token_payload = request_data
self.stream = stream
self.logger = logger

def lookahead(self, iterable):
it = iter(iterable)
Expand All @@ -29,63 +34,91 @@ def lookahead(self, iterable):
last = val
yield last, False


async def refresh_token(self):
response = requests.post(
self.api_url, headers=TOKEN_HEADER, data=self.token_payload
)
if response.status_code == TokenRefreshResult.SUCCESS.value:
self.logger.info("Auth0: Token succesfully refreshed!")
return TokenRefreshResult.SUCCESS.value, json.loads(response.text)["access_token"]

self.logger.error("Auth0: Refreshing the token has failed!")
return TokenRefreshResult.FAILURE.value, None


async def get_auth(self):
last_verified = ""
last_user = ""
code, token = await self.refresh_token()

if code != 200:
raise Exception(f"Auth0: Refreshing the token has failed!")

self.logger.info(f"Auth0: Succesfully initiated the token - code: {code}, token: {token}")

last_verified = str()
last_user = str()
buffer_verified = ShiftArray()
buffer_users = ShiftArray()
response = requests.post(
self.api_url, headers=self.token_header, data=self.request_data
)
token = json.loads(response.text)["access_token"]

while True:
headers = {"authorization": f"Bearer {token}"}
verified = await requests_async.get(self.verified_url, headers=headers)
users = await requests_async.get(self.users_url, headers=headers)
if verified.status_code != 200:

try:
verified = requests.get(self.verified_url, headers=headers)
users = requests.get(self.users_url, headers=headers)
except Exception as error:
raise Exception(f"Auth0: Failed to make a request: {error}")


if users.status_code != 200:
await asyncio.sleep(WAIT_TIME_ERROR)
response = requests.post(
self.api_url, headers=self.token_header, data=self.request_data
)
token = json.loads(response.text)["access_token"]
self.logger.info("Auth0: Token's refresh was forced.")

code, token = await self.refresh_token()
continue

verified = verified.json()
users = users.json()
if verified[-1]["email"] != last_verified:
for result, has_more in self.lookahead(verified):
if not buffer_verified.contains(result["email"]):
self.stream.write(
result["email"] + " " + result["nickname"] + " auth0-newsletter"
)
buffer_verified.append(result["email"])
if not has_more:
last_verified = result["email"]


if users[-1]["email"] != last_user:
for result, has_more in self.lookahead(users):
if not buffer_users.contains(result["email"]):
self.logger.info(f"Auth0: New user registered in auth0: {result['email']}")
self.stream.write(
result["email"] + " " + result["nickname"] + " auth0-user"
)
buffer_users.append(result["email"])
if not has_more:
last_user = result["email"]

if verified[-1]["email"] != last_verified:
for result, has_more in self.lookahead(verified):
if not buffer_verified.contains(result["email"]):
self.logger.info(f"Auth0: User allowed the newsletter: {result['email']}")
self.stream.write(
result["email"] + " " + result["nickname"] + " auth0-newsletter"
)
buffer_verified.append(result["email"])
if not has_more:
last_verified = result["email"]
await asyncio.sleep(WAIT_TIME_ON_USER)


async def run(context, input):
config = context.config
stream = Stream()

try:
run.verified_url = config["auth0_verified_url"]
run.users_url = config["auth0_users_url"]
run.api_url = config["api_url"]
run.data = json.dumps(config["request_data"])
except Exception as error:
raise Exception(f"Config not loaded: {error}")
raise Exception(f"Auth0: Config not loaded: {error}")

asyncio.gather(
Auth0(run.verified_url,run.users_url ,run.api_url, run.data, stream).get_auth(),
Auth0(run.verified_url, run.users_url , run.api_url, run.data, stream, context.logger).get_auth(),
return_exceptions=True,
)
return stream.map(lambda x: x + "\n")
3 changes: 1 addition & 2 deletions python/ChimpInsert/config.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"slack_api_url" : "",
"slack_channel_id" : "",
"slack_hook_url" : "",
"mailchimp_api" : "",
"audience_id" : "",
"mailchimp_server" : ""
Expand Down
Loading

0 comments on commit aa19a29

Please sign in to comment.