自定义用户系统接入文档
1. 系统架构概述
项目采用了抽象的SSO接口设计,支持多种认证服务的接入。通过实现统一的 SSOInterface 接口,可以轻松集成自定义的用户系统。当前已支持两种认证服务:
- Casdoor :开源的身份和访问管理平台
- Paraview :一款闭源符合国标的身份认证系统
2. 核心接口定义
系统中所有SSO服务都必须实现 SSOInterface 接口,定义在 builder/rpc/sso.go 文件中:
// SSO接口定义了所有SSO服务必须实现的方法
type SSOInterface interface {
UpdateUserInfo(ctx context.Context, userInfo *SSOUpdateUserInfo) error
GetUserInfo(ctx context.Context, accessToken string) (*SSOUserInfo, error)
GetOAuthToken(ctx context.Context, code, state string) (*oauth2.Token, error)
DeleteUser(ctx context.Context, uuid string) error
IsExistByName(ctx context.Context, name string) (bool, error)
IsExistByEmail(ctx context.Context, email string) (bool, error)
IsExistByPhone(ctx context.Context, phone string) (bool, error)
CreateInvitation(ctx context.Context, code string) error
GetInvitationCode(ctx context.Context, userUUID string) (string, error)
}
3. 用户系统接入步骤
步骤1: 创建自定义SSO客户端实现
首先,创建一个结构体来实现 SSOInterface 接口。参考现有的 casdoorClientImpl 或 paraviewClientImpl 实现。
package rpc
import (
"context"
"fmt"
// 引入其他必要的包
)
// 自定义SSO客户端实现
type customSSOClientImpl struct {
// 自定义配置字段
endpoint string
clientID string
clientSecret string
// 其他需要的字段
}
// 确保实现了SSOInterface接口
var (
_ SSOInterface = (*customSSOClientImpl)(nil)
)
步骤2: 实现接口方法
为自定义客户端实现 SSOInterface 中定义的所有方法:
// 获取OAuth令牌
func (c *customSSOClientImpl) GetOAuthToken(ctx context.Context, code, state string) (*oauth2.Token, error) {
// 实现从自定义SSO服务获取令牌的逻辑
// ...
return token, nil
}
// 获取用户信息
func (c *customSSOClientImpl) GetUserInfo(ctx context.Context, accessToken string) (*SSOUserInfo, error) {
// 实现使用访问令牌获取用户信息的逻辑
// ...
return &SSOUserInfo{
Name: "用户名",
Email: "邮箱",
UUID: "用户唯一标识",
RegProvider: "自定义SSO类型",
// 设置其他用户信息字段
}, nil
}
// 实现其他接口方法...
func (c *customSSOClientImpl) UpdateUserInfo(ctx context.Context, userInfo *SSOUpdateUserInfo) error { /* ... */ }
func (c *customSSOClientImpl) DeleteUser(ctx context.Context, uuid string) error { /* ... */ }
func (c *customSSOClientImpl) IsExistByName(ctx context.Context, name string) (bool, error) { /* ... */ }
func (c *customSSOClientImpl) IsExistByEmail(ctx context.Context, email string) (bool, error) { /* ... */ }
func (c *customSSOClientImpl) IsExistByPhone(ctx context.Context, phone string) (bool, error) { /* ... */ }
func (c *customSSOClientImpl) CreateInvitation(ctx context.Context, code string) error { /* ... */ }
func (c *customSSOClientImpl) GetInvitationCode(ctx context.Context, userUUID string) (string, error) { /* ... */ }