-
Notifications
You must be signed in to change notification settings - Fork 0
/
database_tokens.go
139 lines (111 loc) · 3.79 KB
/
database_tokens.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package turso
import (
"context"
"encoding/json"
"fmt"
"net/http"
"github.com/xhit/go-str2duration/v2"
)
const (
databaseTokensEndpoint = "v1/organizations/%s/databases/%s/auth/tokens"
FullAccess = "full-access"
ReadOnly = "read-only"
DefaultExpiration = "never"
)
var validAuthorization = []string{FullAccess, ReadOnly}
// DatabaseTokensService is the interface for the Turso API database tokens service
type DatabaseTokensService service
type databaseTokensService interface {
// CreateDatabaseToken creates a new database token
CreateDatabaseToken(ctx context.Context, req CreateDatabaseTokenRequest) (*CreateDatabaseTokenResponse, error)
}
// CreateDatabaseTokenRequest is the struct for the Turso API database token create request
type CreateDatabaseTokenRequest struct {
// DatabaseName is the name of the database
DatabaseName string
// Expiration is the expiration time for the token
Expiration string
// Permissions is the permissions for the token
Authorization string
// ReadAttach permission for the token
AttachPermissions Permissions `json:"permissions"`
}
type Permissions struct {
ReadAttach struct {
Database []string `json:"database"`
} `json:"read_attach"`
}
// CreateDatabaseTokenResponse is the struct for the Turso API database token create response
type CreateDatabaseTokenResponse struct {
JWT string `json:"jwt"`
}
// InvalidateDatabaseTokenRequest is the struct for the Turso API database token invalidate request
type InvalidateDatabaseTokenRequest struct {
// DatabaseName is the name of the database
DatabaseName string `json:"database_name"`
}
// getDatabaseTokensEndpoint returns the endpoint for the Turso API database token service
func getDatabaseTokensEndpoint(baseURL, orgName, dbName string) string {
dbEndpoint := fmt.Sprintf(databaseTokensEndpoint, orgName, dbName)
return fmt.Sprintf("%s/%s", baseURL, dbEndpoint)
}
// CreateDatabaseToken satisfies the databaseTokensService interface
func (s *DatabaseTokensService) CreateDatabaseToken(ctx context.Context, req CreateDatabaseTokenRequest) (*CreateDatabaseTokenResponse, error) {
if err := validateDatabaseTokenRequest(req); err != nil {
return nil, err
}
endpoint := getDatabaseTokensEndpoint(s.client.cfg.BaseURL, s.client.cfg.OrgName, req.DatabaseName)
endpoint = fmt.Sprintf("%s?expiration=%s&authorization=%s", endpoint, req.Expiration, req.Authorization)
resp, err := s.client.DoRequest(ctx, http.MethodPost, endpoint, req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var out CreateDatabaseTokenResponse
if err := json.NewDecoder(resp.Body).Decode(&out); err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, newBadRequestError("database token", "creating", resp.StatusCode)
}
return &out, nil
}
// validateDatabaseTokenRequest ensures the authorization and expiration are valid
// in the given request before making the API call
func validateDatabaseTokenRequest(req CreateDatabaseTokenRequest) error {
if !isValidExpiration(req.Expiration) {
return ErrExpirationInvalid
}
if !isValidAuthorization(req.Authorization) {
return ErrAuthorizationInvalid
}
return nil
}
// IsValidExpiration checks if the expiration is valid
func isValidExpiration(expiration string) bool {
// check for empty fields first
if expiration == "" {
return false
}
if expiration == DefaultExpiration {
return true
}
if _, err := str2duration.ParseDuration(expiration); err != nil {
return false
}
return true
}
// IsValidAuthorization checks if the authorization is valid
func isValidAuthorization(authorization string) bool {
// check for empty fields first
if authorization == "" {
return false
}
// check for valid authorization
for _, v := range validAuthorization {
if v == authorization {
return true
}
}
return false
}