4
4
from authlib .integrations .flask_client import OAuth
5
5
from flask import Blueprint , flash , redirect , request , session , url_for
6
6
7
- from redash import models
7
+ from redash import models , settings
8
8
from redash .authentication import (
9
9
create_and_login_user ,
10
10
get_next_path ,
@@ -29,40 +29,64 @@ def verify_profile(org, profile):
29
29
return False
30
30
31
31
32
+ def get_user_profile (access_token , logger ):
33
+ headers = {"Authorization" : f"OAuth { access_token } " }
34
+ response = requests .get ("https://www.googleapis.com/oauth2/v1/userinfo" , headers = headers )
35
+
36
+ if response .status_code == 401 :
37
+ logger .warning ("Failed getting user profile (response code 401)." )
38
+ return None
39
+
40
+ return response .json ()
41
+
42
+
43
+ def build_redirect_uri ():
44
+ scheme = settings .GOOGLE_OAUTH_SCHEME_OVERRIDE or None
45
+ return url_for (".callback" , _external = True , _scheme = scheme )
46
+
47
+
48
+ def build_next_path (org_slug = None ):
49
+ next_path = request .args .get ("next" )
50
+ if not next_path :
51
+ if org_slug is None :
52
+ org_slug = session .get ("org_slug" )
53
+
54
+ scheme = None
55
+ if settings .GOOGLE_OAUTH_SCHEME_OVERRIDE :
56
+ scheme = settings .GOOGLE_OAUTH_SCHEME_OVERRIDE
57
+
58
+ next_path = url_for (
59
+ "redash.index" ,
60
+ org_slug = org_slug ,
61
+ _external = True ,
62
+ _scheme = scheme ,
63
+ )
64
+ return next_path
65
+
66
+
32
67
def create_google_oauth_blueprint (app ):
33
68
oauth = OAuth (app )
34
69
35
70
logger = logging .getLogger ("google_oauth" )
36
71
blueprint = Blueprint ("google_oauth" , __name__ )
37
72
38
73
CONF_URL = "https://accounts.google.com/.well-known/openid-configuration"
39
- oauth = OAuth (app )
40
74
oauth .register (
41
75
name = "google" ,
42
76
server_metadata_url = CONF_URL ,
43
77
client_kwargs = {"scope" : "openid email profile" },
44
78
)
45
79
46
- def get_user_profile (access_token ):
47
- headers = {"Authorization" : "OAuth {}" .format (access_token )}
48
- response = requests .get ("https://www.googleapis.com/oauth2/v1/userinfo" , headers = headers )
49
-
50
- if response .status_code == 401 :
51
- logger .warning ("Failed getting user profile (response code 401)." )
52
- return None
53
-
54
- return response .json ()
55
-
56
80
@blueprint .route ("/<org_slug>/oauth/google" , endpoint = "authorize_org" )
57
81
def org_login (org_slug ):
58
82
session ["org_slug" ] = current_org .slug
59
83
return redirect (url_for (".authorize" , next = request .args .get ("next" , None )))
60
84
61
85
@blueprint .route ("/oauth/google" , endpoint = "authorize" )
62
86
def login ():
63
- redirect_uri = url_for ( ".callback" , _external = True )
87
+ redirect_uri = build_redirect_uri ( )
64
88
65
- next_path = request . args . get ( "next" , url_for ( "redash.index" , org_slug = session . get ( "org_slug" )) )
89
+ next_path = build_next_path ( )
66
90
logger .debug ("Callback url: %s" , redirect_uri )
67
91
logger .debug ("Next is: %s" , next_path )
68
92
@@ -86,7 +110,7 @@ def authorized():
86
110
flash ("Validation error. Please retry." )
87
111
return redirect (url_for ("redash.login" ))
88
112
89
- profile = get_user_profile (access_token )
113
+ profile = get_user_profile (access_token , logger )
90
114
if profile is None :
91
115
flash ("Validation error. Please retry." )
92
116
return redirect (url_for ("redash.login" ))
@@ -110,7 +134,9 @@ def authorized():
110
134
if user is None :
111
135
return logout_and_redirect_to_index ()
112
136
113
- unsafe_next_path = session .get ("next_url" ) or url_for ("redash.index" , org_slug = org .slug )
137
+ unsafe_next_path = session .get ("next_url" )
138
+ if not unsafe_next_path :
139
+ unsafe_next_path = build_next_path (org .slug )
114
140
next_path = get_next_path (unsafe_next_path )
115
141
116
142
return redirect (next_path )
0 commit comments