Skip to content

Commit be2365f

Browse files
committed
feat: add optional encrypted return param to GetUser
1 parent 9172cf3 commit be2365f

2 files changed

Lines changed: 66 additions & 9 deletions

File tree

db-connector.go

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"net/http"
1616
"net/url"
1717
"os"
18+
"regexp"
1819
"strconv"
1920

2021
"crypto/sha256"
@@ -5728,8 +5729,9 @@ func FindUser(ctx context.Context, username string) ([]User, error) {
57285729
return newUsers, nil
57295730
}
57305731

5731-
func GetUser(ctx context.Context, username string) (*User, error) {
5732+
func GetUser(ctx context.Context, username string, returnEncrypted ...bool) (*User, error) {
57325733
curUser := &User{}
5734+
uuidRegex := regexp.MustCompile(`^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$`)
57335735

57345736
parsedKey := strings.ToLower(username)
57355737
cacheKey := fmt.Sprintf("user_%s", parsedKey)
@@ -5739,6 +5741,21 @@ func GetUser(ctx context.Context, username string) (*User, error) {
57395741
cacheData := []byte(cache.([]uint8))
57405742
err = json.Unmarshal(cacheData, &curUser)
57415743
if err == nil {
5744+
// uuid or not
5745+
if len(returnEncrypted) == 0 || !returnEncrypted[0] {
5746+
if len(curUser.ApiKey) > 0 && !uuidRegex.MatchString(curUser.ApiKey) {
5747+
decryptedApiKey, decErr := HandleKeyDecryption([]byte(curUser.ApiKey), "apikey")
5748+
if decErr == nil {
5749+
curUser.ApiKey = string(decryptedApiKey)
5750+
}
5751+
}
5752+
if len(curUser.Session) > 0 && !uuidRegex.MatchString(curUser.Session) {
5753+
decryptedSession, decErr := HandleKeyDecryption([]byte(curUser.Session), "session")
5754+
if decErr == nil {
5755+
curUser.Session = string(decryptedSession)
5756+
}
5757+
}
5758+
}
57425759
return curUser, nil
57435760
}
57445761
} else {
@@ -5805,6 +5822,19 @@ func GetUser(ctx context.Context, username string) (*User, error) {
58055822
data, err := json.Marshal(curUser)
58065823
if err != nil {
58075824
log.Printf("[WARNING] Failed marshalling user: %s", err)
5825+
// uuid check
5826+
if len(returnEncrypted) == 0 || !returnEncrypted[0] {
5827+
if len(curUser.ApiKey) > 0 && !uuidRegex.MatchString(curUser.ApiKey) {
5828+
if decrypted, decErr := HandleKeyDecryption([]byte(curUser.ApiKey), "apikey"); decErr == nil {
5829+
curUser.ApiKey = string(decrypted)
5830+
}
5831+
}
5832+
if len(curUser.Session) > 0 && !uuidRegex.MatchString(curUser.Session) {
5833+
if decrypted, decErr := HandleKeyDecryption([]byte(curUser.Session), "session"); decErr == nil {
5834+
curUser.Session = string(decrypted)
5835+
}
5836+
}
5837+
}
58085838
return curUser, nil
58095839
}
58105840

@@ -5814,6 +5844,21 @@ func GetUser(ctx context.Context, username string) (*User, error) {
58145844
}
58155845
}
58165846

5847+
if len(returnEncrypted) == 0 || !returnEncrypted[0] {
5848+
if len(curUser.ApiKey) > 0 && !uuidRegex.MatchString(curUser.ApiKey) {
5849+
decryptedApiKey, err := HandleKeyDecryption([]byte(curUser.ApiKey), "apikey")
5850+
if err == nil {
5851+
curUser.ApiKey = string(decryptedApiKey)
5852+
}
5853+
}
5854+
if len(curUser.Session) > 0 && !uuidRegex.MatchString(curUser.Session) {
5855+
decryptedSession, err := HandleKeyDecryption([]byte(curUser.Session), "session")
5856+
if err == nil {
5857+
curUser.Session = string(decryptedSession)
5858+
}
5859+
}
5860+
}
5861+
58175862
return curUser, nil
58185863
}
58195864

shared.go

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3562,10 +3562,22 @@ func HandleApiAuthentication(resp http.ResponseWriter, request *http.Request) (U
35623562

35633563
uuidRegex := regexp.MustCompile(`^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$`)
35643564

3565-
// Encrypt API key if it's plain UUID
3566-
if uuidRegex.MatchString(user.ApiKey) {
3567-
log.Printf("[AUDIT] API key is a UUID: %s", user.ApiKey)
3565+
// since GetSessionNew returns encrypted values
3566+
plainApiKey := user.ApiKey
3567+
if len(plainApiKey) > 0 && !uuidRegex.MatchString(plainApiKey) {
3568+
if decrypted, err := HandleKeyDecryption([]byte(plainApiKey), "apikey"); err == nil {
3569+
plainApiKey = string(decrypted)
3570+
}
3571+
}
3572+
plainSession := user.Session
3573+
if len(plainSession) > 0 && !uuidRegex.MatchString(plainSession) {
3574+
if decrypted, err := HandleKeyDecryption([]byte(plainSession), "session"); err == nil {
3575+
plainSession = string(decrypted)
3576+
}
3577+
}
35683578

3579+
// save encrypted to just db
3580+
if uuidRegex.MatchString(user.ApiKey) {
35693581
encryptedKey, err := HandleKeyEncryption([]byte(user.ApiKey), "apikey", true)
35703582
if err == nil {
35713583
user.ApiKey = string(encryptedKey)
@@ -3574,19 +3586,19 @@ func HandleApiAuthentication(resp http.ResponseWriter, request *http.Request) (U
35743586
}
35753587
}
35763588

3577-
// Encrypt session if matched on plain
35783589
if user.Session == sessionToken && uuidRegex.MatchString(sessionToken) {
3579-
log.Printf("[AUDIT] Encrypting session")
35803590
encryptedSession, err := HandleKeyEncryption([]byte(sessionToken), "session", true)
35813591
if err == nil {
35823592
user.Session = string(encryptedSession)
35833593
SetSession(ctx, user, user.Session)
3584-
} else {
3585-
log.Printf("[ERROR] Failed to encrypt session: %v", err)
35863594
}
35873595
}
35883596

3589-
// Means session exists, but
3597+
// we use user.Apikey throughout the codebase
3598+
// from the returned function value. Trying to keep the usage consistent.
3599+
user.ApiKey = plainApiKey
3600+
user.Session = plainSession
3601+
35903602
return user, nil
35913603
}
35923604

0 commit comments

Comments
 (0)