Skip to content

Commit

Permalink
feat: support GitHub register
Browse files Browse the repository at this point in the history
Signed-off-by: jingfelix <jingfelix@outlook.com>
  • Loading branch information
jingfelix committed Dec 29, 2023
1 parent 920bf22 commit f3f68bb
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 6 deletions.
14 changes: 12 additions & 2 deletions pdm.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ dependencies = [
"pymysql>=1.1.0",
"click>=8.1.7",
"bson>=0.5.10",
"jwt>=1.3.1",
"urllib3>=2.1.0",
]
requires-python = ">=3.10"
readme = "README.md"
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,5 @@ six==1.16.0
sniffio==1.3.0
sqlalchemy==2.0.23
typing-extensions==4.9.0
urllib3==2.1.0
Werkzeug==3.0.1
29 changes: 29 additions & 0 deletions server/routes/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@

@bp.route("/install", methods=["GET"])
def github_install():
"""Install GitHub App.
If not `installation_id`, redirect to install page.
If `installation_id`, get installation token.
If `code`, register by code.
"""
installation_id = request.args.get("installation_id", None)

if installation_id is None:
Expand Down Expand Up @@ -46,4 +53,26 @@ def github_install():
return "Success!"


@bp.route("/register", methods=["GET"])
def github_register():
"""GitHub OAuth register.
If not `code`, redirect to GitHub OAuth page.
If `code`, register by code.
"""
code = request.args.get("code", None)
if code is None:
return redirect(
f"https://github.com/login/oauth/authorize?client_id={os.environ.get('GITHUB_CLIENT_ID')}"
)

print(f"code: {code}")
user_token = register_by_code(code)
if user_token is None:
return "Failed to register by code."

print(f"user_token: {user_token}")
return user_token


app.register_blueprint(bp)
11 changes: 7 additions & 4 deletions server/utils/github.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import time
from urllib.parse import parse_qs

import httpx
from jwt import JWT, jwk_from_pem
Expand Down Expand Up @@ -79,14 +80,16 @@ def register_by_code(code: str) -> str | None:
response = client.post(
"https://github.com/login/oauth/access_token",
params={
"client_id": os.environ.get("CLIENT_ID"),
"client_secret": os.environ.get("CLIENT_SECRET"),
"client_id": os.environ.get("GITHUB_CLIENT_ID"),
"client_secret": os.environ.get("GITHUB_CLIENT_SECRET"),
"code": code,
},
)
if response.status_code == 200:
if response.status_code != 200:
return None

return response.text
access_token = parse_qs(response.text).get("access_token", None)
if access_token is not None:
return access_token[0]

return None

0 comments on commit f3f68bb

Please sign in to comment.