Skip to content

Commit

Permalink
feat: add oauth invite token
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhouhaoJiang committed Sep 2, 2024
1 parent 7dbe8b5 commit 2eee6f2
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
5 changes: 2 additions & 3 deletions api/controllers/console/auth/oauth.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,15 @@ def get_oauth_providers():


class OAuthLogin(Resource):
def get(self, provider: str):
invite_token = request.args.get("invite_token") or None
def get(self, provider: str, invite_toke: Optional[str] = None):
OAUTH_PROVIDERS = get_oauth_providers()
with current_app.app_context():
oauth_provider = OAUTH_PROVIDERS.get(provider)
print(vars(oauth_provider))
if not oauth_provider:
return {"error": "Invalid provider"}, 400

auth_url = oauth_provider.get_authorization_url(invite_token=invite_token)
auth_url = oauth_provider.get_authorization_url(invite_toke)
return redirect(auth_url)


Expand Down
9 changes: 7 additions & 2 deletions api/libs/oauth.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import urllib.parse
from dataclasses import dataclass
from typing import Optional

import requests

Expand Down Expand Up @@ -40,12 +41,14 @@ class GitHubOAuth(OAuth):
_USER_INFO_URL = "https://api.github.com/user"
_EMAIL_INFO_URL = "https://api.github.com/user/emails"

def get_authorization_url(self):
def get_authorization_url(self, invite_token: Optional[str] = None):
params = {
"client_id": self.client_id,
"redirect_uri": self.redirect_uri,
"scope": "user:email", # Request only basic user information
}
if invite_token:
params["state"] = invite_token
return f"{self._AUTH_URL}?{urllib.parse.urlencode(params)}"

def get_access_token(self, code: str):
Expand Down Expand Up @@ -90,13 +93,15 @@ class GoogleOAuth(OAuth):
_TOKEN_URL = "https://oauth2.googleapis.com/token"
_USER_INFO_URL = "https://www.googleapis.com/oauth2/v3/userinfo"

def get_authorization_url(self):
def get_authorization_url(self, invite_token: Optional[str] = None):
params = {
"client_id": self.client_id,
"response_type": "code",
"redirect_uri": self.redirect_uri,
"scope": "openid email",
}
if invite_token:
params["state"] = invite_token
return f"{self._AUTH_URL}?{urllib.parse.urlencode(params)}"

def get_access_token(self, code: str):
Expand Down

0 comments on commit 2eee6f2

Please sign in to comment.