Skip to content

Commit

Permalink
[refactor] Refactor to match the new auto-import
Browse files Browse the repository at this point in the history
  • Loading branch information
BernieHuang2008 committed Feb 17, 2024
1 parent 7979c6c commit ab2a644
Show file tree
Hide file tree
Showing 4 changed files with 189 additions and 113 deletions.
30 changes: 30 additions & 0 deletions services/verify/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"""
"""

from fastapi import APIRouter
from dynaconf import Dynaconf

from . import router

router.init(APIRouter(prefix="/verify"))


def entrypoint(settings: Dynaconf):
return {
"Author": "Bernie H.",
"Version": "1.0.0",
"Describe": "验证微服务,主要用于验证用户的邮箱等。",
"Router": router.router,
"Init": lambda: init(settings),
}


g_settings = None


def init(settings: Dynaconf):
print("Verify service is starting...")
global g_settings
g_settings = settings

109 changes: 0 additions & 109 deletions services/verify/main.py

This file was deleted.

155 changes: 155 additions & 0 deletions services/verify/router.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
"""
[service] Verify Service
* this: 本服务
* s1: 调用本服务的服务
* client: 用户的客户端
1. [s1] 请求内网api "/newVerify" 来进行一个新的验证 (需提供一个回调地址、一个 session ID)
2. [client] 通过路由请求内部api "/verifyOutsideCode" 进行验证
3. [this] 将 [client] 重定向到 "<CALLBACK> ?s= <SUCCESS> &v= <INSIDE-CODE>" (`SUCCESS` 为 `0/1`, 代表验证失败/成功, `INSIDE-CODE` 为内部验证码)
4. [s1] 请求内网api "/verifyInsideCode" 验证 `INSIDE-CODE`
5. [s1] 根据返回字段 `ok` 判断验证是否成功, `sess` 来恢复会话
"""

import fastapi
from fastapi import Request, Response, redirect

import verify
import send


router = None


def init(r: fastapi.APIRouter):
global router

r.add_route("/newVerify", newVerify, methods=["GET"])
r.add_route("/verifyOutsideCode", verifyOutsideCode, methods=["GET"])
r.add_route("/verifyInsideCode", verifyInsideCode, methods=["GET"])

router = r


def _getVerifyUri(code: str):
"""
[Tool] 从验证码生成验证链接
"""
return f"https://openteens.org/verify?code={code}"

def newVerify():
"""
[内部] 进行一个新的验证
Request:
method: str (one of [email])
target: str (e.g. "[email protected]")
session: str
callbackURI: str (建议使用带https的绝对路径)
Response:
..codeblock:: json
{
code: int
}
"""
request = Request()

method = request.args.get("method")
target = request.args.get("target")
session = request.args.get("session")
callbackURI = request.args.get("callbackURI")

code = verify.genOutsideCode(session, callbackURI)

if method == "email":
send.sendEmail(target, _getVerifyUri(code))
else:
return {"code": -1}

return {"code": 0}


def newVerifyCode():
"""
[内部] 进行一个新的验证(返回验证码)
Request:
session: str
callbackURI: str (建议使用带https的绝对路径)
Response:
..codeblock:: json
{
code: int,
verifyUri: str
}
"""
request = Request()

session = request.args.get("session")
callbackURI = request.args.get("callbackURI")

code = verify.genOutsideCode(session, callbackURI)

return {"code": 0, "verifyUri": _getVerifyUri(code)}


def verifyOutsideCode():
"""
[外部] 验证外部验证码
Request:
code: str (外部验证码)
Response:
<Redirect> (重定向到回调地址)
"""
request = Request()

code = request.args.get("code")

result = verify.verifyOutsideCode(code)
if result:
sess = result["session"]
callbackURI = result["callbackURI"]

insideCode = verify.genInsideCode(sess)
redirectURI = f"{callbackURI}?s=1&v={insideCode}"
else:
redirectURI = "ERROR: Invalid Verification Link"

return redirect(redirectURI)


def verifyInsideCode():
"""
[内部] 验证内部验证码
Request:
code: str (内部验证码)
Response:
..codeblock:: json
{
code: int,
ok: bool,
sess: str
}
"""
request = Request()

code = request.args.get("code")

result = verify.verifyInsideCode(code)
if result:
return {"code": 0, "ok": True, "sess": result["session"]}
else:
return {"code": -1}


if __name__ == "__main__":
init(fastapi.APIRouter())
router.run("localhost", 5003)
8 changes: 4 additions & 4 deletions services/verify/send.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
def sendEmail(target, code):
def sendEmail(target, uri):
"""
发送验证邮件
"""
Expand Down Expand Up @@ -128,13 +128,13 @@ def sendEmail(target, code):
您正在进行邮箱验证,请点击以下链接完成验证:
<div id="verificationCode">
<a href="https://api.openteens.org/userVerify?code={}"><button class="button">验证邮箱</button></a>
<a href="{}"><button class="button">验证邮箱</button></a>
</div>
若不是您在操作,请忽略此邮件。
<div id="content_bottom">
<small>
如果您无法点击以上链接,请将此链接复制到浏览器地址栏中访问。
<a href="https://api.openteens.org/userVerify?code={}">https://api.openteens.org/userVerify?code={}</a>
<a href="{}">{}</a>
</small>
</div>
</div>
Expand All @@ -156,6 +156,6 @@ def sendEmail(target, code):
title = "OpenTeens 邮箱验证"
from_ = "OpenTeens <[email protected]>"
to = target
content = template.format(code, code, code)
content = template.format(uri, uri, uri)


0 comments on commit ab2a644

Please sign in to comment.