Skip to content

Commit 8757dc9

Browse files
committed
refactor: minor refactor based on code review comments
1 parent 5039c1c commit 8757dc9

File tree

1 file changed

+31
-33
lines changed

1 file changed

+31
-33
lines changed

server/services/v1/auth/gotrue.go

+31-33
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"strings"
2828
"time"
2929

30+
"github.com/google/uuid"
3031
jsoniter "github.com/json-iterator/go"
3132
"github.com/rs/zerolog/log"
3233
api "github.com/tigrisdata/tigris/api/server/v1"
@@ -103,6 +104,17 @@ type UserAppData struct {
103104
KeyType string `json:"key_type"`
104105
}
105106

107+
type GetUserResp struct {
108+
InstanceID uuid.UUID `json:"instance_id"`
109+
ID uuid.UUID `json:"id"`
110+
Aud string `json:"aud"`
111+
Role string `json:"role"`
112+
Email string `json:"email"`
113+
EncryptedPassword string `json:"encrypted_password"`
114+
115+
AppMetaData *UserAppData `json:"app_metadata" db:"app_metadata"`
116+
}
117+
106118
// returns currentSub, creationTime, error.
107119
func _createAppKey(ctx context.Context, clientId string, clientSecret string, g *gotrue, keyName string, keyDescription string, project string, keyType string) (string, int64, error) {
108120
currentSub, err := GetCurrentSub(ctx)
@@ -214,12 +226,11 @@ func (g *gotrue) CreateGlobalAppKey(ctx context.Context, req *api.CreateGlobalAp
214226
}
215227

216228
func _getEmail(clientId string, appKeyType string, g *gotrue) string {
217-
if appKeyType == "" || appKeyType == AppKeyTypeCredentials {
218-
return fmt.Sprintf("%s%s", clientId, g.AuthConfig.Gotrue.UsernameSuffix)
219-
} else if appKeyType == AppKeyTypeApiKey {
220-
return fmt.Sprintf("%s%s", clientId, g.AuthConfig.ApiKeys.EmailSuffix)
229+
suffix := g.AuthConfig.Gotrue.UsernameSuffix
230+
if appKeyType == AppKeyTypeApiKey {
231+
suffix = g.AuthConfig.ApiKeys.EmailSuffix
221232
}
222-
return ""
233+
return fmt.Sprintf("%s%s", suffix)
223234
}
224235

225236
func _updateAppKey(ctx context.Context, g *gotrue, id string, name string, description string, appKeyType string) error {
@@ -505,10 +516,8 @@ type appKeyInternal struct {
505516
}
506517

507518
func _listAppKeys(ctx context.Context, g *gotrue, project string, keyType string) ([]*appKeyInternal, error) {
508-
if keyType != "" {
509-
if !(keyType == AppKeyTypeApiKey || keyType == AppKeyTypeCredentials) {
510-
return nil, errors.InvalidArgument("Invalid keytype. Supported values are [credentials, api_key]")
511-
}
519+
if keyType != "" && !(keyType == AppKeyTypeApiKey || keyType == AppKeyTypeCredentials) {
520+
return nil, errors.InvalidArgument("Invalid key_type. Supported values are [credentials, api_key]")
512521
}
513522
currentSub, err := GetCurrentSub(ctx)
514523
if err != nil {
@@ -617,10 +626,9 @@ func _listAppKeys(ctx context.Context, g *gotrue, project string, keyType string
617626
CreatedAt: createdAtMillis,
618627
Project: appMetadata.Project,
619628
}
620-
if appMetadata.KeyType == "" || appMetadata.KeyType == AppKeyTypeCredentials {
621-
appKey.KeyType = AppKeyTypeCredentials
622-
} else if appMetadata.KeyType == AppKeyTypeApiKey {
623-
appKey.KeyType = AppKeyTypeApiKey
629+
appKey.KeyType = AppKeyTypeCredentials
630+
if appMetadata.KeyType != "" {
631+
appKey.KeyType = appMetadata.KeyType
624632
}
625633
appKeys[i] = &appKey
626634
}
@@ -757,49 +765,39 @@ func (g *gotrue) ValidateApiKey(ctx context.Context, apiKey string, auds []strin
757765
log.Err(err).Msg("Failed to read get user body while validating api key")
758766
return nil, errors.Internal("Failed to validate the api key")
759767
}
760-
768+
var getUserResp GetUserResp
761769
// parse JSON response
762-
var getUserJsonMap map[string]any
763-
err = json.Unmarshal(getUserResBody, &getUserJsonMap)
770+
err = json.Unmarshal(getUserResBody, &getUserResp)
764771
if err != nil {
765-
log.Err(err).Msg("Failed to deserialize response into JSON")
772+
log.Err(err).Msg("Failed to deserialize response into GetUserResp type")
766773
return nil, errors.Internal("Failed to validate the api key")
767774
}
768775

769776
// validate password
770-
password := getUserJsonMap["encrypted_password"].(string)
771-
log.Error().Str("stored_password", password).
772-
Str("input_password", config.DefaultConfig.Auth.ApiKeys.UserPassword).
773-
Msg("Do password match")
774-
if config.DefaultConfig.Auth.ApiKeys.UserPassword != password {
777+
if config.DefaultConfig.Auth.ApiKeys.UserPassword != getUserResp.EncryptedPassword {
775778
return nil, errors.Unauthenticated("Unsupported api-key")
776779
}
777780

778781
// validate aud
779-
aud := getUserJsonMap["aud"].(string)
780782
allowedAud := false
781783
for _, supportedAud := range auds {
782-
if supportedAud == aud {
784+
if supportedAud == getUserResp.Aud {
783785
allowedAud = true
784786
break
785787
}
786788
}
787789
if !allowedAud {
788-
log.Error().Str("api_key_aud", aud).Strs("supported_auds", auds).Msg("Audience is not supported")
790+
log.Error().Str("api_key_aud", getUserResp.Aud).Strs("supported_auds", auds).Msg("Audience is not supported")
789791
return nil, errors.Unauthenticated("Unsupported audience")
790792
}
791793

792-
role := getUserJsonMap["role"].(string)
793-
metadata := getUserJsonMap["app_metadata"].(map[string]any)
794-
tigrisNamespaceCode := metadata["tigris_namespace"].(string)
795-
project := metadata["tigris_project"].(string)
796-
sub := fmt.Sprintf("gt_key|%s", getUserJsonMap["id"])
794+
sub := fmt.Sprintf("gt_key|%s", getUserResp.ID)
797795

798796
return &types.AccessToken{
799-
Namespace: tigrisNamespaceCode,
797+
Namespace: getUserResp.AppMetaData.TigrisNamespace,
800798
Sub: sub,
801-
Project: project,
802-
Role: role,
799+
Project: getUserResp.AppMetaData.Project,
800+
Role: getUserResp.Role,
803801
}, nil
804802
}
805803

0 commit comments

Comments
 (0)