Skip to content

Commit

Permalink
Use flask.g instead of _app_ctx_stack
Browse files Browse the repository at this point in the history
  • Loading branch information
coopfeathy committed Sep 25, 2022
1 parent a0b64f5 commit 8a45ad8
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 12 deletions.
9 changes: 3 additions & 6 deletions authlib/integrations/flask_client/apps.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from flask import redirect, request, session
from flask import _app_ctx_stack
from flask import g, redirect, request, session
from ..requests_client import OAuth1Session, OAuth2Session
from ..base_client import (
BaseApp, OAuthError,
Expand All @@ -10,9 +9,8 @@
class FlaskAppMixin(object):
@property
def token(self):
ctx = _app_ctx_stack.top
attr = '_oauth_token_{}'.format(self.name)
token = getattr(ctx, attr, None)
token = g.get(attr)
if token:
return token
if self._fetch_token:
Expand All @@ -22,9 +20,8 @@ def token(self):

@token.setter
def token(self, token):
ctx = _app_ctx_stack.top
attr = '_oauth_token_{}'.format(self.name)
setattr(ctx, attr, token)
setattr(g, attr, token)

def _get_requested_token(self, *args, **kwargs):
return self.token
Expand Down
9 changes: 3 additions & 6 deletions authlib/integrations/flask_oauth1/resource_protector.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import functools
from flask import json, Response
from flask import g, json, Response
from flask import request as _req
from flask import _app_ctx_stack
from werkzeug.local import LocalProxy
from authlib.consts import default_json_headers
from authlib.oauth1 import ResourceProtector as _ResourceProtector
Expand Down Expand Up @@ -86,8 +85,7 @@ def acquire_credential(self):
_req.form.to_dict(flat=True),
_req.headers
)
ctx = _app_ctx_stack.top
ctx.authlib_server_oauth1_credential = req.credential
g.authlib_server_oauth1_credential = req.credential
return req.credential

def __call__(self, scope=None):
Expand All @@ -109,8 +107,7 @@ def decorated(*args, **kwargs):


def _get_current_credential():
ctx = _app_ctx_stack.top
return getattr(ctx, 'authlib_server_oauth1_credential', None)
return g.get('authlib_server_oauth1_credential')


current_credential = LocalProxy(_get_current_credential)

0 comments on commit 8a45ad8

Please sign in to comment.