@@ -2,8 +2,7 @@ package tenant
2
2
3
3
import (
4
4
"context"
5
- "net/http"
6
- "time"
5
+ "strings"
7
6
8
7
"github.com/go-errors/errors"
9
8
"github.com/supabase/cli/internal/utils"
@@ -32,16 +31,41 @@ func NewApiKey(resp []api.ApiKeyResponse) ApiKey {
32
31
if err != nil {
33
32
continue
34
33
}
34
+ if t , err := key .Type .Get (); err == nil {
35
+ switch t {
36
+ case api .ApiKeyResponseTypePublishable :
37
+ result .Anon = value
38
+ continue
39
+ case api .ApiKeyResponseTypeSecret :
40
+ if isServiceRole (key ) {
41
+ result .ServiceRole = value
42
+ }
43
+ continue
44
+ }
45
+ }
35
46
switch key .Name {
36
47
case "anon" :
37
- result .Anon = value
48
+ if len (result .Anon ) == 0 {
49
+ result .Anon = value
50
+ }
38
51
case "service_role" :
39
- result .ServiceRole = value
52
+ if len (result .ServiceRole ) == 0 {
53
+ result .ServiceRole = value
54
+ }
40
55
}
41
56
}
42
57
return result
43
58
}
44
59
60
+ func isServiceRole (key api.ApiKeyResponse ) bool {
61
+ if tmpl , err := key .SecretJwtTemplate .Get (); err == nil {
62
+ if role , ok := tmpl ["role" ].(string ); ok {
63
+ return strings .EqualFold (role , "service_role" )
64
+ }
65
+ }
66
+ return false
67
+ }
68
+
45
69
func GetApiKeys (ctx context.Context , projectRef string ) (ApiKey , error ) {
46
70
resp , err := utils .GetSupabase ().V1GetProjectApiKeysWithResponse (ctx , projectRef , & api.V1GetProjectApiKeysParams {})
47
71
if err != nil {
@@ -62,19 +86,9 @@ type TenantAPI struct {
62
86
}
63
87
64
88
func NewTenantAPI (ctx context.Context , projectRef , anonKey string ) TenantAPI {
65
- server := "https://" + utils .GetSupabaseHost (projectRef )
66
- client := & http.Client {
67
- Timeout : 10 * time .Second ,
68
- }
69
- header := func (req * http.Request ) {
70
- req .Header .Add ("apikey" , anonKey )
71
- }
72
- api := TenantAPI {Fetcher : fetcher .NewFetcher (
73
- server ,
74
- fetcher .WithHTTPClient (client ),
75
- fetcher .WithRequestEditor (header ),
89
+ return TenantAPI {Fetcher : fetcher .NewServiceGateway (
90
+ "https://" + utils .GetSupabaseHost (projectRef ),
91
+ anonKey ,
76
92
fetcher .WithUserAgent ("SupabaseCLI/" + utils .Version ),
77
- fetcher .WithExpectedStatus (http .StatusOK ),
78
93
)}
79
- return api
80
94
}
0 commit comments