Prefixed session data with discord to allow users to have their own other OAuth sessions

This commit is contained in:
thecosmos
2019-05-19 17:45:04 +05:30
parent bf49390a4c
commit f91bdca7d9
2 changed files with 6 additions and 6 deletions

View File

@@ -32,7 +32,7 @@ class DiscordOAuth2HttpClient(abc.ABC):
@staticmethod @staticmethod
def _token_updater(token): def _token_updater(token):
session["oauth2_token"] = token session["discord_oauth2_token"] = token
def _make_session(self, token: str = None, state: str = None, scope: list = None) -> OAuth2Session: def _make_session(self, token: str = None, state: str = None, scope: list = None) -> OAuth2Session:
"""A low level method used for creating OAuth2 session. """A low level method used for creating OAuth2 session.
@@ -55,7 +55,7 @@ class DiscordOAuth2HttpClient(abc.ABC):
""" """
return OAuth2Session( return OAuth2Session(
client_id=self.client_id, client_id=self.client_id,
token=token or session.get("oauth2_token"), token=token or session.get("discord_oauth2_token"),
state=state, state=state,
scope=scope, scope=scope,
redirect_uri=self.redirect_uri, redirect_uri=self.redirect_uri,
@@ -86,7 +86,7 @@ class DiscordOAuth2HttpClient(abc.ABC):
return self._make_session().get(configs.API_BASE_URL + route).json() return self._make_session().get(configs.API_BASE_URL + route).json()
def get_json(self): def get_json(self):
discord_session = self._make_session(token=session.get("oauth2_token")) discord_session = self._make_session(token=session.get("discord_oauth2_token"))
user = discord_session.get(configs.API_BASE_URL + '/users/@me').json() user = discord_session.get(configs.API_BASE_URL + '/users/@me').json()
guilds = discord_session.get(configs.API_BASE_URL + '/users/@me/guilds').json() guilds = discord_session.get(configs.API_BASE_URL + '/users/@me/guilds').json()
connections = discord_session.get(configs.API_BASE_URL + '/users/@me/connections').json() connections = discord_session.get(configs.API_BASE_URL + '/users/@me/connections').json()

View File

@@ -39,7 +39,7 @@ class DiscordOAuth2Session(_http.DiscordOAuth2HttpClient):
scope = scope or request.args.get("scope", str()).split() or configs.DEFAULT_SCOPES scope = scope or request.args.get("scope", str()).split() or configs.DEFAULT_SCOPES
discord_session = self._make_session(scope=scope) discord_session = self._make_session(scope=scope)
authorization_url, state = discord_session.authorization_url(configs.AUTHORIZATION_BASE_URL) authorization_url, state = discord_session.authorization_url(configs.AUTHORIZATION_BASE_URL)
session["oauth2_state"] = state session["discord_oauth2_state"] = state
return redirect(authorization_url) return redirect(authorization_url)
def callback(self): def callback(self):
@@ -51,13 +51,13 @@ class DiscordOAuth2Session(_http.DiscordOAuth2HttpClient):
""" """
if request.values.get("error"): if request.values.get("error"):
return request.values["error"] return request.values["error"]
discord = self._make_session(state=session.get("oauth2_state")) discord = self._make_session(state=session.get("discord_oauth2_state"))
token = discord.fetch_token( token = discord.fetch_token(
configs.TOKEN_URL, configs.TOKEN_URL,
client_secret=self.client_secret, client_secret=self.client_secret,
authorization_response=request.url authorization_response=request.url
) )
session["oauth2_token"] = token session["discord_oauth2_token"] = token
def fetch_user(self): def fetch_user(self):
return models.User(self.get("/users/@me")) return models.User(self.get("/users/@me"))