sync authserver sso integrations

This commit is contained in:
mac
2026-07-14 17:37:08 +08:00
parent aa06c05d88
commit 8c7a1ae9ea
12 changed files with 490 additions and 43 deletions
+46 -1
View File
@@ -1,6 +1,7 @@
package auth
import (
"crypto/rsa"
"errors"
"time"
@@ -19,6 +20,18 @@ type OAuthCodeClaims struct {
UserID uint64 `json:"uid"`
ClientID string `json:"client_id"`
RedirectURI string `json:"redirect_uri"`
Scope string `json:"scope"`
Nonce string `json:"nonce,omitempty"`
jwt.RegisteredClaims
}
type IDTokenClaims struct {
UserID uint64 `json:"uid"`
Username string `json:"preferred_username"`
Email string `json:"email"`
EmailVerified bool `json:"email_verified"`
Name string `json:"name"`
Nonce string `json:"nonce,omitempty"`
jwt.RegisteredClaims
}
@@ -45,7 +58,7 @@ func Sign(secret, issuer string, ttl time.Duration, userID uint64, username, ema
return signed, tokenID, expiresAt, err
}
func SignOAuthCode(secret, issuer string, ttl time.Duration, userID uint64, clientID, redirectURI string) (string, string, time.Time, error) {
func SignOAuthCode(secret, issuer string, ttl time.Duration, userID uint64, clientID, redirectURI, scope, nonce string) (string, string, time.Time, error) {
now := time.Now()
expiresAt := now.Add(ttl)
codeID := randomID()
@@ -53,6 +66,8 @@ func SignOAuthCode(secret, issuer string, ttl time.Duration, userID uint64, clie
UserID: userID,
ClientID: clientID,
RedirectURI: redirectURI,
Scope: scope,
Nonce: nonce,
RegisteredClaims: jwt.RegisteredClaims{
ID: codeID,
Issuer: issuer,
@@ -67,6 +82,36 @@ func SignOAuthCode(secret, issuer string, ttl time.Duration, userID uint64, clie
return signed, codeID, expiresAt, err
}
func SignIDToken(privateKey *rsa.PrivateKey, keyID, issuer, clientID string, ttl time.Duration, userID uint64, username, email, name, nonce string) (string, time.Time, error) {
now := time.Now()
expiresAt := now.Add(ttl)
subject := email
if subject == "" {
subject = username
}
claims := IDTokenClaims{
UserID: userID,
Username: username,
Email: email,
EmailVerified: email != "",
Name: name,
Nonce: nonce,
RegisteredClaims: jwt.RegisteredClaims{
Issuer: issuer,
Subject: subject,
Audience: []string{clientID},
IssuedAt: jwt.NewNumericDate(now),
ExpiresAt: jwt.NewNumericDate(expiresAt),
},
}
token := jwt.NewWithClaims(jwt.SigningMethodRS256, claims)
if keyID != "" {
token.Header["kid"] = keyID
}
signed, err := token.SignedString(privateKey)
return signed, expiresAt, err
}
func Parse(secret string, tokenValue string) (*Claims, error) {
token, err := jwt.ParseWithClaims(tokenValue, &Claims{}, func(token *jwt.Token) (any, error) {
if token.Method != jwt.SigningMethodHS256 {
+64
View File
@@ -0,0 +1,64 @@
package auth
import (
"crypto/rsa"
"crypto/sha256"
"crypto/x509"
"encoding/base64"
"encoding/pem"
"errors"
"math/big"
"os"
)
type JWK struct {
KeyType string `json:"kty"`
Use string `json:"use"`
KeyID string `json:"kid"`
Algorithm string `json:"alg"`
Modulus string `json:"n"`
Exponent string `json:"e"`
}
func LoadRSAPrivateKey(path string) (*rsa.PrivateKey, error) {
if path == "" {
return nil, errors.New("rsa private key file is required")
}
data, err := os.ReadFile(path)
if err != nil {
return nil, err
}
block, _ := pem.Decode(data)
if block == nil {
return nil, errors.New("rsa private key file has no PEM block")
}
if key, err := x509.ParsePKCS1PrivateKey(block.Bytes); err == nil {
return key, nil
}
parsed, err := x509.ParsePKCS8PrivateKey(block.Bytes)
if err != nil {
return nil, err
}
key, ok := parsed.(*rsa.PrivateKey)
if !ok {
return nil, errors.New("private key is not an RSA private key")
}
return key, nil
}
func PublicJWKFromKey(key *rsa.PrivateKey) JWK {
publicKey := key.PublicKey
return JWK{
KeyType: "RSA",
Use: "sig",
KeyID: RSAKeyID(&publicKey),
Algorithm: "RS256",
Modulus: base64.RawURLEncoding.EncodeToString(publicKey.N.Bytes()),
Exponent: base64.RawURLEncoding.EncodeToString(big.NewInt(int64(publicKey.E)).Bytes()),
}
}
func RSAKeyID(publicKey *rsa.PublicKey) string {
hash := sha256.Sum256(publicKey.N.Bytes())
return base64.RawURLEncoding.EncodeToString(hash[:8])
}