Skip to content

Commit 1ac0526

Browse files
committed
getting closer
1 parent 844faba commit 1ac0526

File tree

7 files changed

+64
-21
lines changed

7 files changed

+64
-21
lines changed

ide/auth.py

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,28 @@
3030

3131
AUTHORIZATION_SCOPE ='openid email profile'
3232

33-
AUTH_REDIRECT_URI = os.environ.get("FN_AUTH_REDIRECT_URI", default=False)
3433
BASE_URI = os.environ.get("FN_BASE_URI", default=False)
3534

3635
def get_project_name():
3736
web_setting = models.Setting.get('google_project_name')
3837
return web_setting.value
3938

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
4044
#
4145
# Robust way to check for running locally. Also easy to modify.
4246
#
4347
GRL = os.environ.get("GLOWSCRIPT_RUNNING_LOCALLY")
4448
GRL = GRL and GRL.lower() # let's keep it case insenstive
4549
GRL = GRL not in (None, 'false') # Anything but None or 'false'
4650

47-
class ModuleCache:
51+
class SecretCache:
52+
"""
53+
We need to cache the client_id and the client_secret.
54+
"""
4855

4956
def __init__(self):
5057
self.cache = {}
@@ -76,7 +83,7 @@ def fillCache(self):
7683
CLIENT_SECRET = ''
7784

7885
self.cache['CLIENT_ID'] = CLIENT_ID
79-
self.cache['CLINET_SECRET'] = CLIENT_SECRET
86+
self.cache['CLIENT_SECRET'] = CLIENT_SECRET
8087

8188
def getID(self):
8289
theID = self.cache.get('CLIENT_ID')
@@ -86,13 +93,13 @@ def getID(self):
8693
return theID
8794

8895
def getSecret(self):
89-
theSecret = self.cache.get('CLINET_SECRET')
96+
theSecret = self.cache.get('CLIENT_SECRET')
9097
if theSecret is None:
9198
self.fillCache()
92-
theSecret = self.cache.get('CLINET_SECRET')
99+
theSecret = self.cache.get('CLIENT_SECRET')
93100
return theSecret
94101

95-
module_cache = ModuleCache()
102+
secret_cache = SecretCache()
96103

97104
AUTH_TOKEN_KEY = 'auth_token'
98105
AUTH_STATE_KEY = 'auth_state'
@@ -109,8 +116,8 @@ def build_credentials():
109116
return google.oauth2.credentials.Credentials(
110117
oauth2_tokens['access_token'],
111118
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(),
114121
token_uri=ACCESS_TOKEN_URI)
115122

116123
def get_user_info():
@@ -123,7 +130,7 @@ def get_user_info():
123130
oauth2_client = googleapiclient.discovery.build(
124131
'oauth2', 'v2',
125132
credentials=credentials)
126-
return oauth2_client.userinfo().get().execute()
133+
return oauth2_client.userinfo().get().execute() # pylint: disable=maybe-no-member"
127134

128135

129136
def no_cache(view):
@@ -142,9 +149,9 @@ def no_cache_impl(*args, **kwargs):
142149
@no_cache
143150
def google_login():
144151

145-
session = OAuth2Session(module_cache.getID(), module_cache.getSecret(),
152+
session = OAuth2Session(secret_cache.getID(), secret_cache.getSecret(),
146153
scope=AUTHORIZATION_SCOPE,
147-
redirect_uri=AUTH_REDIRECT_URI)
154+
redirect_uri=get_redirect_uri())
148155

149156
uri, state = session.create_authorization_url(AUTHORIZATION_URL)
150157

@@ -168,18 +175,18 @@ def google_auth_redirect():
168175
"""
169176
return flask.redirect('/index')
170177

171-
session = OAuth2Session(module_cache.getID(), module_cache.getSecret(),
178+
session = OAuth2Session(secret_cache.getID(), secret_cache.getSecret(),
172179
scope=AUTHORIZATION_SCOPE,
173180
state=flask.session.get(AUTH_STATE_KEY),
174-
redirect_uri=AUTH_REDIRECT_URI)
181+
redirect_uri=get_redirect_uri())
175182

176183
oauth2_tokens = session.fetch_access_token(
177184
ACCESS_TOKEN_URI,
178185
authorization_response=flask.request.url)
179186

180187
flask.session[AUTH_TOKEN_KEY] = oauth2_tokens
181188

182-
return flask.redirect(BASE_URI, code=302)
189+
return flask.redirect(get_base_url(), code=302)
183190

184191
@app.route('/google/logout')
185192
@no_cache
@@ -188,5 +195,4 @@ def google_logout():
188195
flask.session.pop(AUTH_TOKEN_KEY, None)
189196
flask.session.pop(AUTH_STATE_KEY, None)
190197

191-
return flask.redirect(BASE_URI, code=302)
192-
198+
return flask.redirect(get_base_url(), code=302)

ide/dumpSettings.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# pipe this into "flask shell" to update all users in the datastore
2+
3+
wc(modDBFunctions.DumpSettings, Setting=Setting)
4+

ide/dumpUsers.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# pipe this into "flask shell" to update all users in the datastore
2+
3+
wc(modDBFunctions.DumpUsers, User=User)
4+

ide/modDBFunctions.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,17 @@ def UpdateUsers(**kwargs):
2525
count += 1
2626

2727

28+
def DumpSettings(**kwargs):
29+
for s in kwargs['Setting'].query():
30+
print (s)
31+
32+
def DumpUsers(**kwargs):
33+
for s in kwargs['User'].query():
34+
print (s)
35+
36+
37+
def SetSetting(name, value, **kwargs):
38+
Setting = kwargs['Setting']
39+
s = Setting(id=name, value=value)
40+
s.put()
41+

ide/models.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,24 @@ class Setting(ndb.Model):
2929
"""A setting value"""
3030
# No parent
3131
# Key is the setting name
32+
# we're going to cache values since these will be
33+
# static configuration values
34+
#
35+
3236
value = ndb.StringProperty()
37+
cache = {}
3338

3439
@staticmethod
3540
def get(name):
3641
NOT_SET_VALUE = "NOT SET"
37-
ndb_setting = ndb.Key("Setting",name).get()
42+
ndb_setting = Setting.cache.get(name)
43+
3844
if not ndb_setting:
39-
ndb_setting = Setting(id=name, value=NOT_SET_VALUE)
40-
ndb_setting.put()
45+
ndb_setting = ndb.Key("Setting",name).get()
46+
if ndb_setting:
47+
Setting.cache[name] = ndb_setting
48+
else:
49+
ndb_setting = Setting(id=name, value=NOT_SET_VALUE)
50+
ndb_setting.put()
51+
4152
return ndb_setting

ide/saveSetting.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
wc(modDBFunctions.SetSetting, name='foo', value='baz', Setting=Setting)

main.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
from ide import app, models, routes
2+
from ide import app, models, routes, modDBFunctions
33
from google.cloud import ndb
44

55
@app.shell_context_processor
@@ -13,6 +13,8 @@ def wc(func, **args):
1313
with client.context():
1414
func(**args)
1515

16-
return {'app': app, 'User': models.User, 'Folder':models.Folder, 'Program':models.Program, 'ndb':ndb, 'wc':wc}
16+
return {'app': app, 'User': models.User, 'Folder':models.Folder, 'Program':models.Program, 'Setting':models.Setting,
17+
'ndb':ndb, 'wc':wc, 'models':models, 'routes':routes,
18+
'project':project, 'client':client, 'modDBFunctions':modDBFunctions}
1719

1820

0 commit comments

Comments
 (0)