mirror of
https://github.com/woodchen-ink/Q58Connect.git
synced 2025-07-18 05:51:55 +08:00
feat: Enhance OAuth authorization flow with dynamic redirect and parameter handling
This commit is contained in:
parent
a05bce4e38
commit
d99b9bcc77
18
README.md
18
README.md
@ -39,12 +39,28 @@ Q58论坛网址: https://q58.club
|
||||
|
||||
本项目部署在vercel, 数据库使用Neon.
|
||||
|
||||
## 需要检查的几点
|
||||
## 基础功能的检查
|
||||
|
||||
1. 直接登录本系统
|
||||
2. 未登录本系统, 未登录q58论坛, 检查: 用户在接入应用中登录, 然后登录本系统, 然后登录q58论坛, 正常一直回调到用户应用
|
||||
3. 未登录本系统, 登录了q58论坛, 检查: 用户在接入应用中登录, 然后登录本系统, 正常回调到用户应用
|
||||
|
||||
## 管理员功能检查
|
||||
|
||||
1. 管理员登录本系统
|
||||
2. 管理员在应用管理页面, 检查应用列表, 可以查看应用列表, 可以编辑应用, 可以删除应用
|
||||
3. 管理员在用户管理页面, 可以查看用户列表, 可以编辑用户, 可以删除用户
|
||||
4. 管理员在日志管理页面, 可以查看日志列表
|
||||
|
||||
## 应用控制功能检查
|
||||
|
||||
1. 启用/禁用 应用的功能有效检查
|
||||
2. 限制授权用户 的功能有效检查
|
||||
1. 限制为空时,是否所有用户都能登录
|
||||
2. 新增允许用户, 是否能正常登录
|
||||
3. 删除允许用户, 是否能正常限制
|
||||
4. 当用户从可用变不可用, 或者不可用变可用时, 是否正确进行限制
|
||||
|
||||
## 用户应用接入本系统oauth2.0认证的方式:
|
||||
|
||||
1. 发起授权请求
|
||||
|
@ -12,19 +12,20 @@ const clientSecret = process.env.DISCOURSE_SECRET as string;
|
||||
export async function POST(req: Request) {
|
||||
try {
|
||||
const nonce = WordArray.random(16).toString();
|
||||
let return_url = `${hostUrl}/dashboard`; // 默认重定向到仪表板
|
||||
|
||||
// 设置基本的回调地址
|
||||
const return_url = `${hostUrl}/authorize`;
|
||||
try {
|
||||
const body = await req.json();
|
||||
if (body.oauth_params) {
|
||||
return_url = `${hostUrl}/oauth/authorize?${body.oauth_params}`;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Failed to parse request body:", error);
|
||||
}
|
||||
|
||||
// 构建 SSO 参数
|
||||
const ssoParams = new URLSearchParams();
|
||||
ssoParams.set("nonce", nonce);
|
||||
ssoParams.set("return_sso_url", return_url);
|
||||
|
||||
const sso = btoa(ssoParams.toString());
|
||||
const sso = btoa(`nonce=${nonce}&return_sso_url=${return_url}`);
|
||||
const sig = hmacSHA256(sso, clientSecret).toString(Hex);
|
||||
|
||||
// 保存 nonce 到 cookie
|
||||
cookies().set(AUTH_NONCE, nonce, {
|
||||
maxAge: 60 * 10,
|
||||
path: "/",
|
||||
|
@ -1,7 +1,7 @@
|
||||
"use client";
|
||||
|
||||
import * as React from "react";
|
||||
import { useRouter } from "next/navigation";
|
||||
import { useRouter, useSearchParams } from "next/navigation";
|
||||
import { Loader2, MessageCircleCode } from "lucide-react";
|
||||
|
||||
import { cn } from "@/lib/utils";
|
||||
@ -17,36 +17,59 @@ export function UserAuthForm({
|
||||
...props
|
||||
}: React.HTMLAttributes<HTMLDivElement>) {
|
||||
const [isLoading, setIsLoading] = React.useState<boolean>(false);
|
||||
const router = useRouter();
|
||||
const { toast } = useToast();
|
||||
const searchParams = useSearchParams();
|
||||
|
||||
const signIn = async () => {
|
||||
if (isLoading) return;
|
||||
const signIn = () => {
|
||||
React.startTransition(async () => {
|
||||
try {
|
||||
// 构建请求体,包含 OAuth 参数
|
||||
const body: Record<string, any> = {};
|
||||
const oauthParams = new URLSearchParams();
|
||||
|
||||
setIsLoading(true);
|
||||
try {
|
||||
const response = await fetch("/api/auth/q58", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
});
|
||||
// 收集 OAuth 相关参数
|
||||
[
|
||||
"client_id",
|
||||
"redirect_uri",
|
||||
"response_type",
|
||||
"state",
|
||||
"scope",
|
||||
].forEach((param) => {
|
||||
const value = searchParams.get(param);
|
||||
if (value) {
|
||||
oauthParams.append(param, value);
|
||||
}
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error("登录请求失败");
|
||||
// 如果存在 OAuth 参数,添加到请求体
|
||||
if (oauthParams.toString()) {
|
||||
body.oauth_params = oauthParams.toString();
|
||||
}
|
||||
|
||||
const response = await fetch("/api/auth/q58", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify(body),
|
||||
});
|
||||
|
||||
if (!response.ok || response.status !== 200) {
|
||||
throw new Error(response.statusText || "登录请求失败");
|
||||
}
|
||||
|
||||
const data: DiscourseData = await response.json();
|
||||
router.push(data.sso_url);
|
||||
} catch (error) {
|
||||
setIsLoading(false);
|
||||
toast({
|
||||
variant: "destructive",
|
||||
title: "内部服务异常",
|
||||
description: error instanceof Error ? error.message : "登录请求失败",
|
||||
});
|
||||
}
|
||||
|
||||
const data: DiscourseData = await response.json();
|
||||
// 直接跳转到 SSO 登录页面
|
||||
window.location.href = data.sso_url;
|
||||
} catch (error) {
|
||||
console.error("登录失败:", error);
|
||||
setIsLoading(false);
|
||||
toast({
|
||||
variant: "destructive",
|
||||
title: "登录失败",
|
||||
description: "请求失败,请稍后重试",
|
||||
});
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
return (
|
||||
@ -54,7 +77,10 @@ export function UserAuthForm({
|
||||
<button
|
||||
type="button"
|
||||
className={cn(buttonVariants({ variant: "outline" }))}
|
||||
onClick={signIn}
|
||||
onClick={() => {
|
||||
setIsLoading(true);
|
||||
signIn();
|
||||
}}
|
||||
disabled={isLoading}
|
||||
>
|
||||
{isLoading ? (
|
||||
|
Loading…
x
Reference in New Issue
Block a user