当前位置: 首页 > news >正文

模拟开发授权平台

这次只是实现应用的curd和公私钥的校验以及第三方的通知dmeo项目,大家可以拓开视野来编写

进入主题

项目链接:桌角的眼镜/develop_auth_platform

直接下拉并运行就行 回调应用代码在test包中

回调应用测试代码

package mainimport ("encoding/json""fmt""io""log""net/http""time"
)func main() {// 设置路由http.HandleFunc("/callback", callbackHandler)http.HandleFunc("/", homeHandler)// 配置服务器server := &http.Server{Addr:         ":8089",ReadTimeout:  5 * time.Second,WriteTimeout: 10 * time.Second,}// 启动服务器fmt.Printf("🚀 服务已启动,监听端口 8089\n")fmt.Printf("👉 测试接口: curl -X POST http://localhost:8089/callback -d '{\"message\":\"test\"}'\n")if err := server.ListenAndServe(); err != nil {log.Fatalf("❌ 服务器启动失败: %v", err)}
}// 回调接口处理器
func callbackHandler(w http.ResponseWriter, r *http.Request) {// 打印请求基本信息fmt.Printf("\n=== 收到回调请求 ===\n")fmt.Printf("时间: %s\n", time.Now().Format(time.RFC3339))fmt.Printf("方法: %s\n", r.Method)fmt.Printf("来源IP: %s\n", r.RemoteAddr)fmt.Printf("请求头: %v\n", r.Header)// 根据Content-Type处理不同格式的请求体contentType := r.Header.Get("Content-Type")var body interface{}switch contentType {case "application/json":var jsonBody map[string]interface{}if err := json.NewDecoder(r.Body).Decode(&jsonBody); err != nil {http.Error(w, "无效的JSON数据", http.StatusBadRequest)return}body = jsonBodydefault:// 其他类型直接读取原始数据data, err := io.ReadAll(r.Body)if err != nil {http.Error(w, "读取请求体失败", http.StatusBadRequest)return}body = string(data)}// 打印请求体fmt.Printf("请求体: %+v\n", body)fmt.Printf("===================\n")// 返回成功响应w.Header().Set("Content-Type", "application/json")json.NewEncoder(w).Encode(map[string]interface{}{"status":  "success","message": "回调已接收","data":    body,})
}// 首页处理器
func homeHandler(w http.ResponseWriter, r *http.Request) {if r.URL.Path != "/" {http.NotFound(w, r)return}w.Header().Set("Content-Type", "text/plain")fmt.Fprintf(w, "回调服务运行中\n请访问 /callback 接口")
}

 项目的简易结构

核心部分 

Application的curd

/api/v1/下的application.go

package v1import ("github.com/gin-gonic/gin""github.com/spectacleCase/develop_auth_platform/global""github.com/spectacleCase/develop_auth_platform/models"request "github.com/spectacleCase/develop_auth_platform/models/request"
)type Application struct{}func (Application) Create() gin.HandlerFunc {return func(c *gin.Context) {var NewApp request.Createif err := c.ShouldBind(&NewApp); err != nil {models.FailWithMessage("参数有误", c)return}id, err := models.GenerateID()if err != nil {models.FailWithMessage(err.Error(), c)return}// 得到公私钥prKey, puKey, err := models.GetDefaultKeyPair()if err != nil {models.FailWithMessage(err.Error(), c)return}global.ApplicationList = append(global.ApplicationList, &models.Application{Id:          id,Name:        NewApp.Name,CallbackUrl: NewApp.CallbackUrl,PrKey:       prKey,PuKey:       puKey,})models.Ok(c)return}
}func (Application) Get() gin.HandlerFunc {return func(c *gin.Context) {models.OkWithData(global.ApplicationList, c)return}
}func (Application) Update() gin.HandlerFunc {return func(c *gin.Context) {var updateApp request.Updateif err := c.ShouldBind(&updateApp); err != nil {models.FailWithMessage("参数有误", c)return}for index, app := range global.ApplicationList {if app == nil {continue // 跳过 nil 指针}if app.Id == updateApp.Id {app.Name = updateApp.Nameapp.CallbackUrl = updateApp.CallbackUrlglobal.ApplicationList[index] = app}models.Ok(c)return}models.FailWithMessage("错误的参数", c)return}
}func (Application) Delete() gin.HandlerFunc {return func(c *gin.Context) {var delApp request.Deleteif err := c.ShouldBind(&delApp); err != nil {models.FailWithMessage("参数有误", c)return}for index, app := range global.ApplicationList {if app == nil {continue // 跳过 nil 指针}if app.Id == delApp.Id {global.ApplicationList[index] = nil}models.Ok(c)return}models.FailWithMessage("错误的参数", c)return}
}

/models下的application.go 

package modelsimport ("crypto/rand""crypto/rsa""crypto/x509""encoding/base64""encoding/pem""fmt"
)type Application struct {Id          string `json:"id"`Name        string `json:"name"`PrKey       string `json:"-"`     // 私钥(JSON 序列化时忽略)PuKey       string `json:"puKey"` // 公钥SerPuKey    string `json:"serPuKey"`CallbackUrl string `json:"callbackUrl"` // 回调地址
}// GenerateID 生成唯一应用ID (UUID简化版)
func GenerateID() (string, error) {const length = 16 // 16字节 = 128位b := make([]byte, length)if _, err := rand.Read(b); err != nil {return "", fmt.Errorf("生成ID失败: %v", err)}return base64.URLEncoding.EncodeToString(b), nil
}// GenerateKeyPair 生成RSA公私钥对
func GenerateKeyPair(bits int) (prKey, puKey string, err error) {privateKey, err := rsa.GenerateKey(rand.Reader, bits)if err != nil {return "", "", fmt.Errorf("密钥生成失败: %v", err)}// 编码私钥privateKeyBytes := x509.MarshalPKCS1PrivateKey(privateKey)privateKeyPEM := pem.EncodeToMemory(&pem.Block{Type:  "RSA PRIVATE KEY",Bytes: privateKeyBytes,})// 编码公钥publicKeyBytes, err := x509.MarshalPKIXPublicKey(&privateKey.PublicKey)if err != nil {return "", "", fmt.Errorf("公钥编码失败: %v", err)}publicKeyPEM := pem.EncodeToMemory(&pem.Block{Type:  "PUBLIC KEY",Bytes: publicKeyBytes,})return string(privateKeyPEM), string(publicKeyPEM), nil
}// GetDefaultKeyPair 获取默认强度的密钥对 (2048位)
func GetDefaultKeyPair() (string, string, error) {return GenerateKeyPair(2048)
}// VerifyKeyPair 验证公私钥是否匹配
func VerifyKeyPair(prKey, puKey string) bool {block, _ := pem.Decode([]byte(prKey))if block == nil {return false}privateKey, err := x509.ParsePKCS1PrivateKey(block.Bytes)if err != nil {return false}pubBlock, _ := pem.Decode([]byte(puKey))if pubBlock == nil {return false}pubKey, err := x509.ParsePKIXPublicKey(pubBlock.Bytes)if err != nil {return false}rsaPubKey, ok := pubKey.(*rsa.PublicKey)if !ok {return false}return privateKey.PublicKey.Equal(rsaPubKey)
}

测试截图

 

相关文章:

  • Flutter BottomNavigationBar 详解
  • 定制开发开源AI智能名片S2B2C商城小程序驱动的无界零售基础设施变革研究——基于京东模式的技术解构与商业重构
  • 单链表操作(single list)
  • Unity 与 Lua 交互详解
  • (转)角色与动画的性能优化 | UnrealFest演讲干货
  • 第 7 篇:跳表 (Skip List):简单务实的概率性选手
  • MATLAB图像加密案例
  • 城市智控 | 废弃物分类可视化管理平台
  • MySQL 索引不生效的情况
  • python 桌面程序开发简述及示例
  • TS 常用类型
  • Redis宣布再次开源
  • 从原理到实战讲解回归算法!!!
  • ESP-ADF esp_dispatcher组件之audio_service子模块状态控制函数详解
  • pytest——参数化
  • 【dify—10】工作流实战——文生图工具
  • 精益数据分析(37/126):深度剖析SaaS模式下的参与度与流失率指标
  • 游戏引擎学习第254天:重新启用性能分析
  • C++析构函数详解
  • Synthesis的分类
  • 传奇落幕!波波维奇卸任马刺队主教练,转型全职球队总裁
  • 王毅在金砖正式成员和伙伴国外长会上的发言
  • 购车补贴、“谷子”消费、特色产品,这些活动亮相五五购物节
  • 乌美签署矿产协议
  • 空调+零食助顶级赛马备战,上海环球马术冠军赛即将焕新登场
  • 国台办:相关优化离境退税政策适用于来大陆的台湾同胞