16
16
17
17
from . import app
18
18
from . import routes
19
+ from . import models
19
20
20
21
try :
21
22
from . import secret
32
33
AUTH_REDIRECT_URI = os .environ .get ("FN_AUTH_REDIRECT_URI" , default = False )
33
34
BASE_URI = os .environ .get ("FN_BASE_URI" , default = False )
34
35
36
+ def get_project_name ():
37
+ web_setting = models .Setting .get ('google_project_name' )
38
+ return web_setting .value
39
+
35
40
#
36
41
# Robust way to check for running locally. Also easy to modify.
37
42
#
38
43
GRL = os .environ .get ("GLOWSCRIPT_RUNNING_LOCALLY" )
39
44
GRL = GRL and GRL .lower () # let's keep it case insenstive
40
45
GRL = GRL not in (None , 'false' ) # Anything but None or 'false'
41
46
42
- if (not GRL ):
43
- #
44
- # If we're not running locally, we should get the secrets from the secret manager.
45
- #
46
-
47
- GOOGLE_PROJECT_ID = os .environ .get ('GOOGLE_PROJECT_ID' ) or 'glowscript-py38'
48
- CLIENT_SECRET_VERSION = os .environ .get ('CLIENT_SECRET_VERSION' ) or '1'
49
- from google .cloud import secretmanager
50
- secrets = secretmanager .SecretManagerServiceClient ()
51
- secret_path = f"projects/{ GOOGLE_PROJECT_ID } /secrets/OAUTH_CLIENT_SECRETS/versions/{ CLIENT_SECRET_VERSION } "
52
- theSecret = secrets .access_secret_version (secret_path ).payload .data .decode ("utf-8" )
53
- client_secrets = json .loads (theSecret )
54
- CLIENT_ID = client_secrets .get ("FN_CLIENT_ID" )
55
- CLIENT_SECRET = client_secrets .get ("FN_CLIENT_SECRET" )
56
- if CLIENT_ID is None :
57
- raise RuntimeError ("We are not running locally, but CLIENT_ID is not set. Dang. Did you mean to set GLOWSCRIPT_RUNNING_LOCALLY?" )
58
- else :
59
- CLIENT_ID = ''
60
- CLIENT_SECRET = ''
47
+ class ModuleCache :
48
+
49
+ def __init__ (self ):
50
+ self .cache = {}
51
+
52
+ def fillCache (self ):
53
+ if (not GRL ):
54
+ #
55
+ # If we're not running locally, we should get the secrets from the secret manager.
56
+ #
57
+ # this got much more complicated by storing the project id in the datastore.
58
+ # we cannot accesss the datastore unless we are in application context but that
59
+ # means it's got to be cached somehow if we don't want to have to keep pulling
60
+ # it from the datastore on every request. Bleah.
61
+ #
62
+
63
+ GOOGLE_PROJECT_ID = get_project_name ()
64
+ CLIENT_SECRET_VERSION = os .environ .get ('CLIENT_SECRET_VERSION' ) or '1'
65
+ from google .cloud import secretmanager
66
+ secrets = secretmanager .SecretManagerServiceClient ()
67
+ secret_path = f"projects/{ GOOGLE_PROJECT_ID } /secrets/OAUTH_CLIENT_SECRETS/versions/{ CLIENT_SECRET_VERSION } "
68
+ theSecret = secrets .access_secret_version (secret_path ).payload .data .decode ("utf-8" )
69
+ client_secrets = json .loads (theSecret )
70
+ CLIENT_ID = client_secrets .get ("FN_CLIENT_ID" )
71
+ CLIENT_SECRET = client_secrets .get ("FN_CLIENT_SECRET" )
72
+ if CLIENT_ID is None :
73
+ raise RuntimeError ("We are not running locally, but CLIENT_ID is not set. Dang. Did you mean to set GLOWSCRIPT_RUNNING_LOCALLY?" )
74
+ else :
75
+ CLIENT_ID = ''
76
+ CLIENT_SECRET = ''
77
+
78
+ self .cache ['CLIENT_ID' ] = CLIENT_ID
79
+ self .cache ['CLINET_SECRET' ] = CLIENT_SECRET
80
+
81
+ def getID (self ):
82
+ theID = self .cache .get ('CLIENT_ID' )
83
+ if theID is None :
84
+ self .fillCache ()
85
+ theID = self .cache .get ('CLIENT_ID' )
86
+ return theID
87
+
88
+ def getSecret (self ):
89
+ theSecret = self .cache .get ('CLINET_SECRET' )
90
+ if theSecret is None :
91
+ self .fillCache ()
92
+ theSecret = self .cache .get ('CLINET_SECRET' )
93
+ return theSecret
94
+
95
+ module_cache = ModuleCache ()
61
96
62
97
AUTH_TOKEN_KEY = 'auth_token'
63
98
AUTH_STATE_KEY = 'auth_state'
@@ -74,8 +109,8 @@ def build_credentials():
74
109
return google .oauth2 .credentials .Credentials (
75
110
oauth2_tokens ['access_token' ],
76
111
refresh_token = oauth2_tokens ['refresh_token' ],
77
- client_id = CLIENT_ID ,
78
- client_secret = CLIENT_SECRET ,
112
+ client_id = module_cache . getID () ,
113
+ client_secret = module_cache . getSecret () ,
79
114
token_uri = ACCESS_TOKEN_URI )
80
115
81
116
def get_user_info ():
@@ -107,7 +142,7 @@ def no_cache_impl(*args, **kwargs):
107
142
@no_cache
108
143
def google_login ():
109
144
110
- session = OAuth2Session (CLIENT_ID , CLIENT_SECRET ,
145
+ session = OAuth2Session (module_cache . getID (), module_cache . getSecret () ,
111
146
scope = AUTHORIZATION_SCOPE ,
112
147
redirect_uri = AUTH_REDIRECT_URI )
113
148
@@ -133,7 +168,7 @@ def google_auth_redirect():
133
168
"""
134
169
return flask .redirect ('/index' )
135
170
136
- session = OAuth2Session (CLIENT_ID , CLIENT_SECRET ,
171
+ session = OAuth2Session (module_cache . getID (), module_cache . getSecret () ,
137
172
scope = AUTHORIZATION_SCOPE ,
138
173
state = flask .session .get (AUTH_STATE_KEY ),
139
174
redirect_uri = AUTH_REDIRECT_URI )
0 commit comments