3636
3737
3838class PublicClientAuth (Auth ):
39- scopes : List [str ] = ["api:read-data" , "api:write-data" , "offline_access" ]
40-
4139 """
4240 Client for Public Client OAuth-authenticated Ontology applications.
4341 Runs a background thread to periodically refresh access token.
44-
4542 :param client_id: OAuth client id to be used by the application.
4643 :param client_secret: OAuth client secret to be used by the application.
4744 :param hostname: Hostname for authentication and ontology endpoints.
4845 """
4946
5047 def __init__ (
51- self , client_id : str , redirect_url : str , hostname : str , should_refresh : bool = False
48+ self ,
49+ client_id : str ,
50+ redirect_url : str ,
51+ hostname : str ,
52+ scopes : Optional [List [str ]] = None ,
53+ should_refresh : bool = False ,
5254 ) -> None :
5355 self ._client_id = client_id
5456 self ._redirect_url = redirect_url
@@ -58,7 +60,7 @@ def __init__(
5860 self ._stop_refresh_event = threading .Event ()
5961 self ._hostname = hostname
6062 self ._server_oauth_flow_provider = PublicClientOAuthFlowProvider (
61- client_id = client_id , redirect_url = redirect_url , url = self .url , scopes = self . scopes
63+ client_id = client_id , redirect_url = redirect_url , url = self .url , scopes = scopes
6264 )
6365 self ._auth_request : Optional [AuthorizeRequest ] = None
6466
@@ -81,9 +83,11 @@ def run_with_token(self, func: Callable[[OAuthToken], T]) -> None:
8183 self .sign_out ()
8284 raise e
8385
84- def _refresh_token (self ):
85- if self ._token is None :
86- raise Exception ("" )
86+ def _refresh_token (self ) -> None :
87+ if not self ._token :
88+ raise RuntimeError ("must have token to refresh" )
89+ if not self ._token .refresh_token :
90+ raise RuntimeError ("no refresh token provided" )
8791
8892 self ._token = self ._server_oauth_flow_provider .refresh_token (
8993 refresh_token = self ._token .refresh_token
@@ -92,30 +96,29 @@ def _refresh_token(self):
9296 def _run_with_attempted_refresh (self , func : Callable [[OAuthToken ], T ]) -> T :
9397 """
9498 Attempt to run func, and if it fails with a 401, refresh the token and try again.
95-
9699 If it fails with a 401 again, raise the exception.
97100 """
98101 try :
99102 return func (self .get_token ())
100103 except requests .HTTPError as e :
101- if e .response is not None and e . response .status_code == 401 :
104+ if e .response .status_code == 401 :
102105 self ._refresh_token ()
103106 return func (self .get_token ())
104107 else :
105108 raise e
106109
107110 @property
108- def url (self ):
111+ def url (self ) -> str :
109112 return remove_prefixes (self ._hostname , ["https://" , "http://" ])
110113
111- def sign_in (self ) -> None :
114+ def sign_in (self ) -> str :
112115 self ._auth_request = self ._server_oauth_flow_provider .generate_auth_request ()
113- webbrowser . open ( self ._auth_request .url )
116+ return self ._auth_request .url
114117
115- def _start_auto_refresh (self ):
116- def _auto_refresh_token ():
118+ def _start_auto_refresh (self ) -> None :
119+ def _auto_refresh_token () -> None :
117120 while not self ._stop_refresh_event .is_set ():
118- if self ._token :
121+ if self ._token and self . _token . refresh_token :
119122 # Sleep for (expires_in - 60) seconds to refresh the token 1 minute before it expires
120123 time .sleep (self ._token .expires_in - 60 )
121124 self ._token = self ._server_oauth_flow_provider .refresh_token (
@@ -129,9 +132,10 @@ def _auto_refresh_token():
129132 refresh_thread .start ()
130133
131134 def set_token (self , code : str , state : str ) -> None :
132- if self ._auth_request is None or state != self ._auth_request .state :
133- raise RuntimeError ("Unable to verify the state" )
134-
135+ if not self ._auth_request :
136+ raise RuntimeError ("Must sign in prior to setting token" )
137+ if state != self ._auth_request .state :
138+ raise RuntimeError ("Unable to verify state" )
135139 self ._token = self ._server_oauth_flow_provider .get_token (
136140 code = code , code_verifier = self ._auth_request .code_verifier
137141 )
0 commit comments