Skip to content

Commit 3352161

Browse files
committed
fix testing issues, add creator ref
1 parent 151aa9c commit 3352161

File tree

7 files changed

+126
-87
lines changed

7 files changed

+126
-87
lines changed

packages/api/internal/api/spec.gen.go

+63-63
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/api/internal/api/types.gen.go

+3-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/api/internal/auth/accesstoken.go

+9
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@ import (
44
"crypto/rand"
55
"encoding/hex"
66
"strings"
7+
8+
"golang.org/x/crypto/argon2"
79
)
810

11+
const accessTokenSalt = "e2b_access_token_hash_salt"
12+
913
func MaskAccessToken(key string) string {
1014
prefixLength := len(AccessTokenPrefix)
1115
firstFour := key[:prefixLength]
@@ -14,6 +18,11 @@ func MaskAccessToken(key string) string {
1418
return firstFour + stars + lastFour
1519
}
1620

21+
func HashAccessToken(key string) string {
22+
hashBytes := argon2.IDKey([]byte(key), []byte(accessTokenSalt), 1, 64*1024, 4, 32)
23+
return hex.EncodeToString(hashBytes)
24+
}
25+
1726
func GenerateAccessToken() string {
1827
randomBytes := make([]byte, 20)
1928
_, err := rand.Read(randomBytes)

packages/api/internal/auth/team_apikey.go

+9
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@ import (
44
"crypto/rand"
55
"encoding/hex"
66
"strings"
7+
8+
"golang.org/x/crypto/argon2"
79
)
810

11+
const apiKeySalt = "e2b_api_key_hash_salt"
12+
913
func MaskAPIKey(key string) string {
1014
prefixLength := len(ApiKeyPrefix)
1115
firstFour := key[:prefixLength]
@@ -14,6 +18,11 @@ func MaskAPIKey(key string) string {
1418
return firstFour + stars + lastFour
1519
}
1620

21+
func HashAPIKey(key string) string {
22+
hashBytes := argon2.IDKey([]byte(key), []byte(apiKeySalt), 1, 64*1024, 4, 32)
23+
return hex.EncodeToString(hashBytes)
24+
}
25+
1726
func GenerateTeamAPIKey() string {
1827
randomBytes := make([]byte, 20)
1928
_, err := rand.Read(randomBytes)

packages/api/internal/handlers/accesstoken.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,11 @@ func (a *APIStore) PostAccesstokens(c *gin.Context) {
3333
accessToken := auth.GenerateAccessToken()
3434
accessTokenDB, err := a.db.Client.AccessToken.
3535
Create().
36-
SetID(accessToken).
36+
SetUniqueID(uuid.New()).
3737
SetUserID(userID).
38+
SetID(accessToken).
3839
SetAccessTokenMask(auth.MaskAccessToken(accessToken)).
40+
SetAccessTokenHash(auth.HashAccessToken(accessToken)).
3941
SetCreatedAt(time.Now()).
4042
SetName(body.Name).
4143
Save(ctx)
@@ -48,7 +50,7 @@ func (a *APIStore) PostAccesstokens(c *gin.Context) {
4850
}
4951

5052
c.JSON(http.StatusCreated, api.CreatedAccessToken{
51-
Id: accessTokenDB.ID,
53+
Id: accessTokenDB.UniqueID,
5254
Token: accessToken,
5355
TokenMask: auth.MaskAccessToken(accessToken),
5456
Name: accessTokenDB.Name,

packages/api/internal/handlers/team_apikey.go

+33-9
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/e2b-dev/infra/packages/api/internal/api"
1313
"github.com/e2b-dev/infra/packages/api/internal/auth"
1414
"github.com/e2b-dev/infra/packages/api/internal/utils"
15+
"github.com/e2b-dev/infra/packages/shared/pkg/models/teamapikey"
1516
"github.com/e2b-dev/infra/packages/shared/pkg/telemetry"
1617
)
1718

@@ -53,7 +54,11 @@ func (a *APIStore) GetApikeys(c *gin.Context) {
5354

5455
teamID := a.GetTeamInfo(c).Team.ID
5556

56-
apiKeysDB, err := a.db.GetTeamAPIKeys(ctx, teamID)
57+
apiKeysDB, err := a.db.Client.TeamAPIKey.
58+
Query().
59+
Where(teamapikey.TeamID(teamID)).
60+
WithCreator().
61+
All(ctx)
5762
if err != nil {
5863
log.Println("Error when getting team API keys: ", err)
5964
c.JSON(http.StatusInternalServerError, "Error when getting team API keys")
@@ -63,12 +68,20 @@ func (a *APIStore) GetApikeys(c *gin.Context) {
6368

6469
teamAPIKeys := make([]api.TeamAPIKey, len(apiKeysDB))
6570
for i, apiKey := range apiKeysDB {
71+
var createdBy *api.TeamUser
72+
if apiKey.Edges.Creator != nil {
73+
createdBy = &api.TeamUser{
74+
Email: apiKey.Edges.Creator.Email,
75+
Id: apiKey.Edges.Creator.ID,
76+
}
77+
}
78+
6679
teamAPIKeys[i] = api.TeamAPIKey{
6780
Id: apiKey.ID,
6881
Name: apiKey.Name,
6982
KeyMask: auth.MaskAPIKey(apiKey.APIKey),
7083
CreatedAt: apiKey.CreatedAt,
71-
CreatedBy: apiKey.CreatedBy,
84+
CreatedBy: createdBy,
7285
LastUsed: apiKey.LastUsed,
7386
}
7487
}
@@ -119,28 +132,39 @@ func (a *APIStore) PostApikeys(c *gin.Context) {
119132
apiKey, err := a.db.Client.TeamAPIKey.
120133
Create().
121134
SetTeamID(teamID).
122-
SetAPIKey(teamApiKey).
123135
SetCreatedBy(userID).
124136
SetLastUsed(time.Now()).
125137
SetUpdatedAt(time.Now()).
138+
SetAPIKey(teamApiKey).
126139
SetAPIKeyMask(auth.MaskAPIKey(teamApiKey)).
140+
SetAPIKeyHash(auth.HashAPIKey(teamApiKey)).
127141
SetCreatedAt(time.Now()).
128142
SetName(body.Name).
129143
Save(ctx)
130-
131144
if err != nil {
132145
a.sendAPIStoreError(c, http.StatusInternalServerError, fmt.Sprintf("Error when creating API key: %s", err))
133146

134147
errMsg := fmt.Errorf("error when creating API key: %w", err)
135148
telemetry.ReportCriticalError(ctx, errMsg)
136149
}
137150

151+
user, err := a.db.Client.User.Get(ctx, userID)
152+
if err != nil {
153+
a.sendAPIStoreError(c, http.StatusInternalServerError, fmt.Sprintf("Error when getting user: %s", err))
154+
155+
errMsg := fmt.Errorf("error when getting user: %w", err)
156+
telemetry.ReportCriticalError(ctx, errMsg)
157+
}
158+
138159
c.JSON(http.StatusCreated, api.CreatedTeamAPIKey{
139-
Id: apiKey.ID,
140-
Key: teamApiKey,
141-
KeyMask: auth.MaskAPIKey(teamApiKey),
142-
Name: apiKey.Name,
143-
CreatedBy: apiKey.CreatedBy,
160+
Id: apiKey.ID,
161+
Key: teamApiKey,
162+
KeyMask: auth.MaskAPIKey(teamApiKey),
163+
Name: apiKey.Name,
164+
CreatedBy: &api.TeamUser{
165+
Id: user.ID,
166+
Email: user.Email,
167+
},
144168
CreatedAt: apiKey.CreatedAt,
145169
LastUsed: apiKey.LastUsed,
146170
})

spec/openapi.yml

+5-6
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,7 @@ components:
523523
properties:
524524
id:
525525
type: string
526+
format: uuid
526527
description: Identifier of the access token
527528
name:
528529
type: string
@@ -570,9 +571,8 @@ components:
570571
format: date-time
571572
description: Timestamp of API key creation
572573
createdBy:
573-
type: string
574-
format: uuid
575-
description: Identifier of the user who created the API key
574+
allOf:
575+
- $ref: "#/components/schemas/TeamUser"
576576
nullable: true
577577
lastUsed:
578578
type: string
@@ -608,9 +608,8 @@ components:
608608
format: date-time
609609
description: Timestamp of API key creation
610610
createdBy:
611-
type: string
612-
format: uuid
613-
description: Identifier of the user who created the API key
611+
allOf:
612+
- $ref: "#/components/schemas/TeamUser"
614613
nullable: true
615614
lastUsed:
616615
type: string

0 commit comments

Comments
 (0)