Files
YYeTsBot/yyetsweb/databases/oauth.py

60 lines
1.7 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env python3
# coding: utf-8
from hashlib import sha256
from common.utils import ts_date
from databases.base import Mongo
class OAuthRegister(Mongo):
def add_user(self, username, ip, browser, uid, source: "str"):
uid = str(uid)
# username = "Benny"
user = self.db["users"].find_one({"uid": uid, "source": source})
if user and user.get("password"):
# 直接注册的用户
return {"status": "fail", "message": "第三方登录失败,用户名已存在"}
elif user:
# 已存在的oauth用户
return {"status": "success", "message": "欢迎回来,即将跳转首页", "username": username}
else:
# 第一次oauth登录假定一定会成功
# TODO GitHub可以改用户名的但是uid不会变也许需要加unique index
self.db["users"].insert_one(
{
"username": username,
"date": ts_date(),
"ip": ip,
"browser": browser,
"oldUser": True,
"source": source,
"uid": uid,
"hash": sha256(username.encode("u8")).hexdigest(),
}
)
return {
"status": "success",
"message": "第三方登录成功,即将跳转首页",
"username": username,
}
class GitHubOAuth2Login(OAuthRegister):
pass
class MSOAuth2Login(OAuthRegister):
pass
class GoogleOAuth2Login(OAuthRegister):
pass
class TwitterOAuth2Login(OAuthRegister):
pass
class FacebookAuth2Login(OAuthRegister):
pass