Skip to content

Commit 9544b17

Browse files
committed
Slightly better factored version. Moved stuff out of the viewmodels into msal_builder.
1 parent 07cacfa commit 9544b17

File tree

3 files changed

+24
-22
lines changed

3 files changed

+24
-22
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import uuid
2+
3+
import msal
4+
5+
from pypi_org.configs import app_config
6+
7+
8+
def build_auth_url(authority=None, scopes=None, state=None):
9+
return build_msal_app(authority=authority).get_authorization_request_url(
10+
scopes or [],
11+
state=state or str(uuid.uuid4()),
12+
redirect_uri="http://localhost:5006/account/auth")
13+
14+
15+
def build_msal_app(cache=None, authority=None):
16+
return msal.ConfidentialClientApplication(
17+
app_config.CLIENT_ID, authority=authority or app_config.AUTHORITY,
18+
client_credential=app_config.CLIENT_SECRET, token_cache=cache)

app/ch15_deploy/final/pypi_org/viewmodels/shared/viewmodelbase.py

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,7 @@ def __init__(self):
1818
self.user_id: Optional[int] = cookie_auth.get_user_id_via_auth_cookie(self.request)
1919

2020
def to_dict(self):
21-
d = self.__dict__
22-
d['build_auth_url'] = self.build_auth_url
23-
24-
return d
25-
26-
# noinspection PyMethodMayBeStatic
27-
def build_auth_url(self, authority=None, scopes=None, state=None):
28-
return self.build_msal_app(authority=authority).get_authorization_request_url(
29-
scopes or [],
30-
state=state or str(uuid.uuid4()),
31-
redirect_uri="http://localhost:5006/account/auth")
32-
33-
# noinspection PyMethodMayBeStatic
34-
def build_msal_app(self, cache=None, authority=None):
35-
return msal.ConfidentialClientApplication(
36-
app_config.CLIENT_ID, authority=authority or app_config.AUTHORITY,
37-
client_credential=app_config.CLIENT_SECRET, token_cache=cache)
21+
return self.__dict__
22+
23+
3824

app/ch15_deploy/final/pypi_org/views/account_views.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import pypi_org.infrastructure.cookie_auth as cookie_auth
77
from pypi_org.configs import app_config
8-
from pypi_org.infrastructure import session_cache
8+
from pypi_org.infrastructure import session_cache, msal_builder
99
from pypi_org.infrastructure.view_modifiers import response
1010
from pypi_org.services import user_service
1111
from pypi_org.viewmodels.account.index_viewmodel import IndexViewModel
@@ -21,7 +21,6 @@
2121

2222
@blueprint.route('/account/auth')
2323
def auth():
24-
vm = ViewModelBase()
2524
args = flask.request.args
2625

2726
if flask.request.args.get('state') != session.get("state"):
@@ -31,7 +30,7 @@ def auth():
3130
return f"There was an error logging in: Error: {args.get('error')}, details: {args.get('error_description')}."
3231
if flask.request.args.get('code'):
3332
cache = session_cache.load_cache()
34-
result = vm.build_msal_app(cache=cache).acquire_token_by_authorization_code(
33+
result = msal_builder.build_msal_app(cache=cache).acquire_token_by_authorization_code(
3534
flask.request.args['code'],
3635
scopes=app_config.SCOPE, # Misspelled scope would cause an HTTP 400 error here
3736
redirect_uri='http://localhost:5006/account/auth')
@@ -59,11 +58,10 @@ def auth():
5958

6059
@blueprint.route('/account/begin_auth')
6160
def begin_auth():
62-
vm = ViewModelBase()
6361
state = str(uuid.uuid4())
6462
session["state"] = state
6563

66-
return flask.redirect(vm.build_auth_url(state=state))
64+
return flask.redirect(msal_builder.build_auth_url(state=state))
6765

6866

6967
# ################### INDEX #################################

0 commit comments

Comments
 (0)