@@ -27,6 +27,7 @@ import (
27
27
"strings"
28
28
"time"
29
29
30
+ "github.com/google/uuid"
30
31
jsoniter "github.com/json-iterator/go"
31
32
"github.com/rs/zerolog/log"
32
33
api "github.com/tigrisdata/tigris/api/server/v1"
@@ -103,6 +104,17 @@ type UserAppData struct {
103
104
KeyType string `json:"key_type"`
104
105
}
105
106
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
+
106
118
// returns currentSub, creationTime, error.
107
119
func _createAppKey (ctx context.Context , clientId string , clientSecret string , g * gotrue , keyName string , keyDescription string , project string , keyType string ) (string , int64 , error ) {
108
120
currentSub , err := GetCurrentSub (ctx )
@@ -214,12 +226,11 @@ func (g *gotrue) CreateGlobalAppKey(ctx context.Context, req *api.CreateGlobalAp
214
226
}
215
227
216
228
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
221
232
}
222
- return ""
233
+ return fmt . Sprintf ( "%s%s" , clientId , suffix )
223
234
}
224
235
225
236
func _updateAppKey (ctx context.Context , g * gotrue , id string , name string , description string , appKeyType string ) error {
@@ -505,10 +516,8 @@ type appKeyInternal struct {
505
516
}
506
517
507
518
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]" )
512
521
}
513
522
currentSub , err := GetCurrentSub (ctx )
514
523
if err != nil {
@@ -531,7 +540,6 @@ func _listAppKeys(ctx context.Context, g *gotrue, project string, keyType string
531
540
if keyType != "" {
532
541
getUsersUrl = fmt .Sprintf ("%s&keyType=%s" , getUsersUrl , keyType )
533
542
}
534
- log .Info ().Str ("url" , getUsersUrl ).Msg ("Fetching users" )
535
543
client := & http.Client {}
536
544
getUsersReq , err := http .NewRequestWithContext (ctx , http .MethodGet , getUsersUrl , nil )
537
545
if err != nil {
@@ -607,7 +615,6 @@ func _listAppKeys(ctx context.Context, g *gotrue, project string, keyType string
607
615
// parse string time to millis using rfc3339 format
608
616
createdAtMillis = readDate (createdAtStr )
609
617
}
610
- log .Info ().Interface ("metadata" , appMetadata ).Msg ("AppMetadata" )
611
618
appKey := appKeyInternal {
612
619
Id : clientId ,
613
620
Name : appMetadata .Name ,
@@ -617,10 +624,9 @@ func _listAppKeys(ctx context.Context, g *gotrue, project string, keyType string
617
624
CreatedAt : createdAtMillis ,
618
625
Project : appMetadata .Project ,
619
626
}
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
624
630
}
625
631
appKeys [i ] = & appKey
626
632
}
@@ -757,49 +763,39 @@ func (g *gotrue) ValidateApiKey(ctx context.Context, apiKey string, auds []strin
757
763
log .Err (err ).Msg ("Failed to read get user body while validating api key" )
758
764
return nil , errors .Internal ("Failed to validate the api key" )
759
765
}
760
-
766
+ var getUserResp GetUserResp
761
767
// parse JSON response
762
- var getUserJsonMap map [string ]any
763
- err = json .Unmarshal (getUserResBody , & getUserJsonMap )
768
+ err = json .Unmarshal (getUserResBody , & getUserResp )
764
769
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 " )
766
771
return nil , errors .Internal ("Failed to validate the api key" )
767
772
}
768
773
769
774
// 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 {
775
776
return nil , errors .Unauthenticated ("Unsupported api-key" )
776
777
}
777
778
778
779
// validate aud
779
- aud := getUserJsonMap ["aud" ].(string )
780
780
allowedAud := false
781
781
for _ , supportedAud := range auds {
782
- if supportedAud == aud {
782
+ if supportedAud == getUserResp . Aud {
783
783
allowedAud = true
784
784
break
785
785
}
786
786
}
787
787
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" )
789
789
return nil , errors .Unauthenticated ("Unsupported audience" )
790
790
}
791
791
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 )
797
793
798
794
return & types.AccessToken {
799
- Namespace : tigrisNamespaceCode ,
795
+ Namespace : getUserResp . AppMetaData . TigrisNamespace ,
800
796
Sub : sub ,
801
- Project : project ,
802
- Role : role ,
797
+ Project : getUserResp . AppMetaData . Project ,
798
+ Role : getUserResp . Role ,
803
799
}, nil
804
800
}
805
801
0 commit comments