You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
OneAuth/oauth/utils.go

50 lines
1.1 KiB
Go

3 months ago
//
// Copyright (C) 2024 veypi <i@veypi.com>
// 2025-07-24 15:27:31
// Distributed under terms of the MIT license.
//
package oauth
import (
"crypto/rand"
"encoding/hex"
"net/url"
)
// generateRandomString 生成指定长度的随机字符串
func generateRandomString(length int) (string, error) {
bytes := make([]byte, length)
if _, err := rand.Read(bytes); err != nil {
return "", err
}
return hex.EncodeToString(bytes)[:length], nil
}
// BuildRedirectURI 构建成功重定向URI
func BuildRedirectURI(baseURI, code, state string) string {
u, _ := url.Parse(baseURI)
q := u.Query()
q.Set("code", code)
if state != "" {
q.Set("state", state)
}
u.RawQuery = q.Encode()
return u.String()
}
// BuildErrorRedirectURI 构建错误重定向URI
func BuildErrorRedirectURI(baseURI, errorCode, errorDesc, state string) string {
u, _ := url.Parse(baseURI)
q := u.Query()
q.Set("error", errorCode)
if errorDesc != "" {
q.Set("error_description", errorDesc)
}
if state != "" {
q.Set("state", state)
}
u.RawQuery = q.Encode()
return u.String()
}