33# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/api/08_oauth.ipynb.
44
55# %% auto 0
6- __all__ = ['http_patterns' , 'GoogleAppClient' , 'GitHubAppClient' , 'HuggingFaceClient' , 'DiscordAppClient' , 'redir_url ' ,
7- 'url_match' , 'OAuth' ]
6+ __all__ = ['http_patterns' , 'GoogleAppClient' , 'GitHubAppClient' , 'HuggingFaceClient' , 'DiscordAppClient' , 'Auth0AppClient ' ,
7+ 'redir_url' , ' url_match' , 'OAuth' ]
88
99# %% ../nbs/api/08_oauth.ipynb
1010from .common import *
1414
1515# %% ../nbs/api/08_oauth.ipynb
1616class _AppClient (WebApplicationClient ):
17+ id_key = 'sub'
1718 def __init__ (self , client_id , client_secret , code = None , scope = None , ** kwargs ):
1819 super ().__init__ (client_id , code = code , scope = scope , ** kwargs )
1920 self .client_secret = client_secret
@@ -22,9 +23,8 @@ def __init__(self, client_id, client_secret, code=None, scope=None, **kwargs):
2223class GoogleAppClient (_AppClient ):
2324 "A `WebApplicationClient` for Google oauth2"
2425 base_url = "https://accounts.google.com/o/oauth2/v2/auth"
25- token_url = "https://www.googleapis.com/oauth2/v4/token"
26- info_url = "https://www.googleapis.com/oauth2/v3/userinfo"
27- id_key = 'sub'
26+ token_url = "https://oauth2.googleapis.com/token"
27+ info_url = "https://openidconnect.googleapis.com/v1/userinfo"
2828
2929 def __init__ (self , client_id , client_secret , code = None , scope = None , ** kwargs ):
3030 scope_pre = "https://www.googleapis.com/auth/userinfo"
@@ -39,8 +39,9 @@ def from_file(cls, fname, code=None, scope=None, **kwargs):
3939# %% ../nbs/api/08_oauth.ipynb
4040class GitHubAppClient (_AppClient ):
4141 "A `WebApplicationClient` for GitHub oauth2"
42- base_url = "https://github.com/login/oauth/authorize"
43- token_url = "https://github.com/login/oauth/access_token"
42+ prefix = "https://github.com/login/oauth/"
43+ base_url = f"{ prefix } authorize"
44+ token_url = f"{ prefix } access_token"
4445 info_url = "https://api.github.com/user"
4546 id_key = 'id'
4647
@@ -50,11 +51,10 @@ def __init__(self, client_id, client_secret, code=None, scope=None, **kwargs):
5051# %% ../nbs/api/08_oauth.ipynb
5152class HuggingFaceClient (_AppClient ):
5253 "A `WebApplicationClient` for HuggingFace oauth2"
53-
54- base_url = "https://huggingface.co/oauth/authorize"
55- token_url = "https://huggingface.co/oauth/token"
56- info_url = "https://huggingface.co/oauth/userinfo"
57- id_key = 'sub'
54+ prefix = "https://huggingface.co/oauth/"
55+ base_url = f"{ prefix } authorize"
56+ token_url = f"{ prefix } token"
57+ info_url = f"{ prefix } userinfo"
5858
5959 def __init__ (self , client_id , client_secret , code = None , scope = None , state = None , ** kwargs ):
6060 if not scope : scope = ["openid" ,"profile" ]
@@ -87,6 +87,26 @@ def parse_response(self, code):
8787 r .raise_for_status ()
8888 self .parse_request_body_response (r .text )
8989
90+ # %% ../nbs/api/08_oauth.ipynb
91+ class Auth0AppClient (_AppClient ):
92+ "A `WebApplicationClient` for Auth0 OAuth2"
93+ def __init__ (self , domain , client_id , client_secret , code = None , scope = None , https = True , redirect_uri = "" , ** kwargs ):
94+ self .redirect_uri ,self .https ,self .domain = redirect_uri ,https ,domain
95+ config = self ._fetch_openid_config ()
96+ self .base_url ,self .token_url ,self .info_url = config ["authorization_endpoint" ],config ["token_endpoint" ],config ["userinfo_endpoint" ]
97+ super ().__init__ (client_id , client_secret , code = code , scope = scope , https = https , redirect_uri = redirect_uri , ** kwargs )
98+
99+ def _fetch_openid_config (self ):
100+ r = httpx .get (f"https://{ self .domain } /.well-known/openid-configuration" )
101+ r .raise_for_status ()
102+ return r .json ()
103+
104+ def redir_url (self , req ): return redir_url (req , self .redirect_uri , "https" if self .https else "http" )
105+
106+ def login_link (self , req ):
107+ d = dict (response_type = "code" , client_id = self .client_id , scope = self .scope , redirect_uri = self .redir_url (req ))
108+ return f"{ self .base_url } ?{ urlencode (d )} "
109+
90110# %% ../nbs/api/08_oauth.ipynb
91111@patch
92112def login_link (self :WebApplicationClient , redirect_uri , scope = None , state = None ):
@@ -96,8 +116,9 @@ def login_link(self:WebApplicationClient, redirect_uri, scope=None, state=None):
96116 return self .prepare_request_uri (self .base_url , redirect_uri , scope , state = state )
97117
98118# %% ../nbs/api/08_oauth.ipynb
99- def redir_url (request , redir_path , scheme = 'https' ):
119+ def redir_url (request , redir_path , scheme = None ):
100120 "Get the redir url for the host in `request`"
121+ scheme = 'http' if request .url .hostname == 'localhost' else 'https'
101122 return f"{ scheme } ://{ request .url .netloc } { redir_path } "
102123
103124# %% ../nbs/api/08_oauth.ipynb
0 commit comments