30
30
31
31
AUTHORIZATION_SCOPE = 'openid email profile'
32
32
33
- AUTH_REDIRECT_URI = os .environ .get ("FN_AUTH_REDIRECT_URI" , default = False )
34
33
BASE_URI = os .environ .get ("FN_BASE_URI" , default = False )
35
34
36
35
def get_project_name ():
37
36
web_setting = models .Setting .get ('google_project_name' )
38
37
return web_setting .value
39
38
39
+ def get_redirect_uri ():
40
+ return get_base_url () + '/google/auth'
41
+
42
+ def get_base_url ():
43
+ return models .Setting .get ('auth_base_url' ).value
40
44
#
41
45
# Robust way to check for running locally. Also easy to modify.
42
46
#
43
47
GRL = os .environ .get ("GLOWSCRIPT_RUNNING_LOCALLY" )
44
48
GRL = GRL and GRL .lower () # let's keep it case insenstive
45
49
GRL = GRL not in (None , 'false' ) # Anything but None or 'false'
46
50
47
- class ModuleCache :
51
+ class SecretCache :
52
+ """
53
+ We need to cache the client_id and the client_secret.
54
+ """
48
55
49
56
def __init__ (self ):
50
57
self .cache = {}
@@ -76,7 +83,7 @@ def fillCache(self):
76
83
CLIENT_SECRET = ''
77
84
78
85
self .cache ['CLIENT_ID' ] = CLIENT_ID
79
- self .cache ['CLINET_SECRET ' ] = CLIENT_SECRET
86
+ self .cache ['CLIENT_SECRET ' ] = CLIENT_SECRET
80
87
81
88
def getID (self ):
82
89
theID = self .cache .get ('CLIENT_ID' )
@@ -86,13 +93,13 @@ def getID(self):
86
93
return theID
87
94
88
95
def getSecret (self ):
89
- theSecret = self .cache .get ('CLINET_SECRET ' )
96
+ theSecret = self .cache .get ('CLIENT_SECRET ' )
90
97
if theSecret is None :
91
98
self .fillCache ()
92
- theSecret = self .cache .get ('CLINET_SECRET ' )
99
+ theSecret = self .cache .get ('CLIENT_SECRET ' )
93
100
return theSecret
94
101
95
- module_cache = ModuleCache ()
102
+ secret_cache = SecretCache ()
96
103
97
104
AUTH_TOKEN_KEY = 'auth_token'
98
105
AUTH_STATE_KEY = 'auth_state'
@@ -109,8 +116,8 @@ def build_credentials():
109
116
return google .oauth2 .credentials .Credentials (
110
117
oauth2_tokens ['access_token' ],
111
118
refresh_token = oauth2_tokens ['refresh_token' ],
112
- client_id = module_cache .getID (),
113
- client_secret = module_cache .getSecret (),
119
+ client_id = secret_cache .getID (),
120
+ client_secret = secret_cache .getSecret (),
114
121
token_uri = ACCESS_TOKEN_URI )
115
122
116
123
def get_user_info ():
@@ -123,7 +130,7 @@ def get_user_info():
123
130
oauth2_client = googleapiclient .discovery .build (
124
131
'oauth2' , 'v2' ,
125
132
credentials = credentials )
126
- return oauth2_client .userinfo ().get ().execute ()
133
+ return oauth2_client .userinfo ().get ().execute () # pylint: disable=maybe-no-member"
127
134
128
135
129
136
def no_cache (view ):
@@ -142,9 +149,9 @@ def no_cache_impl(*args, **kwargs):
142
149
@no_cache
143
150
def google_login ():
144
151
145
- session = OAuth2Session (module_cache .getID (), module_cache .getSecret (),
152
+ session = OAuth2Session (secret_cache .getID (), secret_cache .getSecret (),
146
153
scope = AUTHORIZATION_SCOPE ,
147
- redirect_uri = AUTH_REDIRECT_URI )
154
+ redirect_uri = get_redirect_uri () )
148
155
149
156
uri , state = session .create_authorization_url (AUTHORIZATION_URL )
150
157
@@ -168,18 +175,18 @@ def google_auth_redirect():
168
175
"""
169
176
return flask .redirect ('/index' )
170
177
171
- session = OAuth2Session (module_cache .getID (), module_cache .getSecret (),
178
+ session = OAuth2Session (secret_cache .getID (), secret_cache .getSecret (),
172
179
scope = AUTHORIZATION_SCOPE ,
173
180
state = flask .session .get (AUTH_STATE_KEY ),
174
- redirect_uri = AUTH_REDIRECT_URI )
181
+ redirect_uri = get_redirect_uri () )
175
182
176
183
oauth2_tokens = session .fetch_access_token (
177
184
ACCESS_TOKEN_URI ,
178
185
authorization_response = flask .request .url )
179
186
180
187
flask .session [AUTH_TOKEN_KEY ] = oauth2_tokens
181
188
182
- return flask .redirect (BASE_URI , code = 302 )
189
+ return flask .redirect (get_base_url () , code = 302 )
183
190
184
191
@app .route ('/google/logout' )
185
192
@no_cache
@@ -188,5 +195,4 @@ def google_logout():
188
195
flask .session .pop (AUTH_TOKEN_KEY , None )
189
196
flask .session .pop (AUTH_STATE_KEY , None )
190
197
191
- return flask .redirect (BASE_URI , code = 302 )
192
-
198
+ return flask .redirect (get_base_url (), code = 302 )
0 commit comments