Skip to content

Commit 1ad5748

Browse files
committed
refactor: minor refactor based on code review comments
1 parent 25ed15e commit 1ad5748

File tree

2 files changed

+32
-36
lines changed

2 files changed

+32
-36
lines changed

api/proto

Submodule proto updated from dd1d4c7 to 2f9df79

server/services/v1/auth/gotrue.go

+31-35
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", clientId, 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 {
@@ -531,7 +540,6 @@ func _listAppKeys(ctx context.Context, g *gotrue, project string, keyType string
531540
if keyType != "" {
532541
getUsersUrl = fmt.Sprintf("%s&keyType=%s", getUsersUrl, keyType)
533542
}
534-
log.Info().Str("url", getUsersUrl).Msg("Fetching users")
535543
client := &http.Client{}
536544
getUsersReq, err := http.NewRequestWithContext(ctx, http.MethodGet, getUsersUrl, nil)
537545
if err != nil {
@@ -607,7 +615,6 @@ func _listAppKeys(ctx context.Context, g *gotrue, project string, keyType string
607615
// parse string time to millis using rfc3339 format
608616
createdAtMillis = readDate(createdAtStr)
609617
}
610-
log.Info().Interface("metadata", appMetadata).Msg("AppMetadata")
611618
appKey := appKeyInternal{
612619
Id: clientId,
613620
Name: appMetadata.Name,
@@ -617,10 +624,9 @@ func _listAppKeys(ctx context.Context, g *gotrue, project string, keyType string
617624
CreatedAt: createdAtMillis,
618625
Project: appMetadata.Project,
619626
}
620-
if appMetadata.KeyType == "" || appMetadata.KeyType == AppKeyTypeCredentials {
621-
appKey.KeyType = AppKeyTypeCredentials
622-
} else if appMetadata.KeyType == AppKeyTypeApiKey {
623-
appKey.KeyType = AppKeyTypeApiKey
627+
appKey.KeyType = AppKeyTypeCredentials
628+
if appMetadata.KeyType != "" {
629+
appKey.KeyType = appMetadata.KeyType
624630
}
625631
appKeys[i] = &appKey
626632
}
@@ -757,49 +763,39 @@ func (g *gotrue) ValidateApiKey(ctx context.Context, apiKey string, auds []strin
757763
log.Err(err).Msg("Failed to read get user body while validating api key")
758764
return nil, errors.Internal("Failed to validate the api key")
759765
}
760-
766+
var getUserResp GetUserResp
761767
// parse JSON response
762-
var getUserJsonMap map[string]any
763-
err = json.Unmarshal(getUserResBody, &getUserJsonMap)
768+
err = json.Unmarshal(getUserResBody, &getUserResp)
764769
if err != nil {
765-
log.Err(err).Msg("Failed to deserialize response into JSON")
770+
log.Err(err).Msg("Failed to deserialize response into GetUserResp type")
766771
return nil, errors.Internal("Failed to validate the api key")
767772
}
768773

769774
// 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 {
775+
if config.DefaultConfig.Auth.ApiKeys.UserPassword != getUserResp.EncryptedPassword {
775776
return nil, errors.Unauthenticated("Unsupported api-key")
776777
}
777778

778779
// validate aud
779-
aud := getUserJsonMap["aud"].(string)
780780
allowedAud := false
781781
for _, supportedAud := range auds {
782-
if supportedAud == aud {
782+
if supportedAud == getUserResp.Aud {
783783
allowedAud = true
784784
break
785785
}
786786
}
787787
if !allowedAud {
788-
log.Error().Str("api_key_aud", aud).Strs("supported_auds", auds).Msg("Audience is not supported")
788+
log.Error().Str("api_key_aud", getUserResp.Aud).Strs("supported_auds", auds).Msg("Audience is not supported")
789789
return nil, errors.Unauthenticated("Unsupported audience")
790790
}
791791

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"])
792+
sub := fmt.Sprintf("gt_key|%s", getUserResp.ID)
797793

798794
return &types.AccessToken{
799-
Namespace: tigrisNamespaceCode,
795+
Namespace: getUserResp.AppMetaData.TigrisNamespace,
800796
Sub: sub,
801-
Project: project,
802-
Role: role,
797+
Project: getUserResp.AppMetaData.Project,
798+
Role: getUserResp.Role,
803799
}, nil
804800
}
805801

0 commit comments

Comments
 (0)