"""auth provides authentication helper classes to support bearer and OAuth token usage"""fromdatetimeimportdatetimefromtypingimportDictimportrequestsfrompydanticimportBaseModel,Fieldfromrequests.authimportAuthBase
[docs]classOAuthToken(BaseModel):"""Handles OAuth tokens for the runZero platform"""access_token:str=Field(...)token_type:str=Field(...)expires_in:int=Field(...)created_at:datetime=datetime.now()
[docs]defis_expired(self)->bool:""" Determines if the oauth token is expired or will expire within a minute :returns: Returns a bool of whether the token is expired or about to """delta=self.created_at-datetime.now()returnnotdelta.total_seconds()<=(self.expires_in+60)
[docs]classBearerToken(AuthBase):"""Implements bearer token authentication scheme"""def__init__(self,token:str):self._token:str=tokendef__call__(self,r:requests.PreparedRequest)->requests.PreparedRequest:""" Attaches the bearer token to the request headers :param r: the calling requests object - which is a requests.PreparedRequest :returns: the calling object """r.headers["Authorization"]=f"Bearer {self._token}"returnr
[docs]classRegisteredAPIClient(AuthBase):"""Handles the runZero API client registration to retrieve a bearer token"""def__init__(self,client_id:str,client_secret:str):self._client_id:str=client_idself._client_secret:str=client_secretdef__call__(self,r:requests.PreparedRequest)->requests.PreparedRequest:""" Attach appropriate headers to the request for api client registration :param r: the calling requests object - which is a requests.PreparedRequest :returns: the calling object """r.headers["Content-Type"]="application/x-www-form-urlencoded"returnr
[docs]defregister(self)->Dict[str,str]:""" Uses the provided OAuth credentials to construct the url for requesting the OAuth bearer token. :returns: dict containing the required components to be urlencoded for OAuth authentication """return{"grant_type":"client_credentials","client_id":self._client_id,"client_secret":self._client_secret}