Fixed openid, finally

This commit is contained in:
Tracker-Friendly 2024-05-04 16:15:40 +01:00
parent 59a4ef0994
commit 94662329e1
1 changed files with 29 additions and 9 deletions

38
main.go
View File

@ -7,6 +7,7 @@ import (
"crypto/x509" "crypto/x509"
"database/sql" "database/sql"
"encoding/base64" "encoding/base64"
"encoding/binary"
"encoding/hex" "encoding/hex"
"encoding/pem" "encoding/pem"
"errors" "errors"
@ -33,14 +34,20 @@ var (
exponent int exponent int
) )
const salt_chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" func Int64ToBase64(num int64) (string, error) {
numBytes := make([]byte, 8)
func addPadding(encodedString string) string { binary.BigEndian.PutUint64(numBytes, uint64(num))
paddingCount := 4 - (len(encodedString) % 4) startIndex := 0
padding := strings.Repeat("=", paddingCount) for startIndex < len(numBytes) && numBytes[startIndex] == 0 {
return encodedString + padding startIndex++
}
trimmedBytes := numBytes[startIndex:]
encoded := base64.StdEncoding.EncodeToString(trimmedBytes)
return encoded, nil
} }
const salt_chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
func genSalt(length int) string { func genSalt(length int) string {
if length <= 0 { if length <= 0 {
log.Println("[ERROR] Known in genSalt() at", strconv.FormatInt(time.Now().Unix(), 10)+":", "Salt length must be at least one.") log.Println("[ERROR] Known in genSalt() at", strconv.FormatInt(time.Now().Unix(), 10)+":", "Salt length must be at least one.")
@ -877,7 +884,7 @@ func main() {
return return
} }
c.JSON(200, gin.H{"access_token": logincode, "token_type": "bearer", "expires_in": 2592000, "id_token": addPadding(openid)}) c.JSON(200, gin.H{"access_token": logincode, "token_type": "bearer", "expires_in": 2592000, "id_token": openid})
}) })
router.POST("/api/deleteauth", func(c *gin.Context) { router.POST("/api/deleteauth", func(c *gin.Context) {
@ -1259,6 +1266,19 @@ func main() {
}) })
router.GET("/.well-known/jwks.json", func(c *gin.Context) { router.GET("/.well-known/jwks.json", func(c *gin.Context) {
mod, err := Int64ToBase64(modulus.Int64())
if err != nil {
log.Println("[ERROR] Unknown in /well-known/jwks.json modulus at", strconv.FormatInt(time.Now().Unix(), 10)+":", err)
c.JSON(500, gin.H{"error": "Unknown error occured"})
return
}
exp, err := Int64ToBase64(int64(exponent))
if err != nil {
log.Println("[ERROR] Unknown in /well-known/jwks.json exponent at", strconv.FormatInt(time.Now().Unix(), 10)+":", err)
c.JSON(500, gin.H{"error": "Unknown error occured"})
return
}
keys := gin.H{ keys := gin.H{
"keys": []gin.H{ "keys": []gin.H{
{ {
@ -1266,8 +1286,8 @@ func main() {
"alg": "RS256", "alg": "RS256",
"use": "sig", "use": "sig",
"kid": "burgerauth", "kid": "burgerauth",
"n": modulus, "n": mod,
"e": exponent, "e": exp,
}, },
}, },
} }