-
Notifications
You must be signed in to change notification settings - Fork 529
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
42 changed files
with
1,095 additions
and
844 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package server | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"github.com/gofiber/fiber/v2" | ||
"github.com/unkeyed/unkey/apps/api/pkg/database" | ||
"net/http" | ||
) | ||
|
||
type GetKeyRequest struct { | ||
KeyId string `validate:"required"` | ||
} | ||
|
||
type GetKeyResponse = keyResponse | ||
|
||
func (s *Server) getKey(c *fiber.Ctx) error { | ||
ctx, span := s.tracer.Start(c.UserContext(), "server.getKey") | ||
defer span.End() | ||
req := GetKeyRequest{ | ||
KeyId: c.Params("keyId"), | ||
} | ||
|
||
err := s.validator.Struct(req) | ||
if err != nil { | ||
return c.Status(http.StatusBadRequest).JSON(ErrorResponse{ | ||
Code: BAD_REQUEST, | ||
Error: fmt.Sprintf("unable to validate request: %s", err.Error()), | ||
}) | ||
} | ||
|
||
authHash, err := getKeyHash(c.Get("Authorization")) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
authKey, err := s.db.GetKeyByHash(ctx, authHash) | ||
if err != nil { | ||
return c.Status(http.StatusInternalServerError).JSON(ErrorResponse{ | ||
Code: INTERNAL_SERVER_ERROR, | ||
Error: fmt.Sprintf("unable to find key: %s", err.Error()), | ||
}) | ||
} | ||
|
||
if authKey.ForWorkspaceId == "" { | ||
return c.Status(http.StatusBadRequest).JSON(ErrorResponse{ | ||
Code: BAD_REQUEST, | ||
Error: "wrong key type", | ||
}) | ||
} | ||
|
||
key, err := s.db.GetKeyById(ctx, req.KeyId) | ||
if err != nil { | ||
if errors.Is(err, database.ErrNotFound) { | ||
return c.Status(http.StatusNotFound).JSON(ErrorResponse{ | ||
Code: NOT_FOUND, | ||
Error: fmt.Sprintf("unable to find key: %s", req.KeyId), | ||
}) | ||
} | ||
return c.Status(http.StatusInternalServerError).JSON(ErrorResponse{ | ||
Code: INTERNAL_SERVER_ERROR, | ||
Error: fmt.Sprintf("unable to find key: %s", err.Error()), | ||
}) | ||
} | ||
if key.WorkspaceId != authKey.ForWorkspaceId { | ||
return c.Status(http.StatusUnauthorized).JSON(ErrorResponse{ | ||
Code: UNAUTHORIZED, | ||
Error: "access to workspace denied", | ||
}) | ||
} | ||
|
||
res := GetKeyResponse{ | ||
Id: key.Id, | ||
ApiId: key.ApiId, | ||
WorkspaceId: key.WorkspaceId, | ||
Start: key.Start, | ||
OwnerId: key.OwnerId, | ||
Meta: key.Meta, | ||
CreatedAt: key.CreatedAt.UnixMilli(), | ||
ForWorkspaceId: key.ForWorkspaceId, | ||
} | ||
if !key.Expires.IsZero() { | ||
res.Expires = key.Expires.UnixMilli() | ||
} | ||
if key.Ratelimit != nil { | ||
res.Ratelimit = &ratelimitSettng{ | ||
Type: key.Ratelimit.Type, | ||
Limit: key.Ratelimit.Limit, | ||
RefillRate: key.Ratelimit.RefillRate, | ||
RefillInterval: key.Ratelimit.RefillInterval, | ||
} | ||
} | ||
if key.Remaining.Enabled { | ||
res.Remaining = &key.Remaining.Remaining | ||
} | ||
|
||
return c.JSON(res) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package server | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"net/http/httptest" | ||
"os" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
"github.com/unkeyed/unkey/apps/api/pkg/cache" | ||
"github.com/unkeyed/unkey/apps/api/pkg/database" | ||
"github.com/unkeyed/unkey/apps/api/pkg/entities" | ||
"github.com/unkeyed/unkey/apps/api/pkg/hash" | ||
"github.com/unkeyed/unkey/apps/api/pkg/logging" | ||
"github.com/unkeyed/unkey/apps/api/pkg/testutil" | ||
"github.com/unkeyed/unkey/apps/api/pkg/tracing" | ||
"github.com/unkeyed/unkey/apps/api/pkg/uid" | ||
) | ||
|
||
func KeyGetKey_Simple(t *testing.T) { | ||
ctx := context.Background() | ||
resources := testutil.SetupResources(t) | ||
|
||
db, err := database.New(database.Config{ | ||
Logger: logging.NewNoopLogger(), | ||
|
||
PrimaryUs: os.Getenv("DATABASE_DSN"), | ||
}) | ||
require.NoError(t, err) | ||
|
||
srv := New(Config{ | ||
Logger: logging.NewNoopLogger(), | ||
KeyCache: cache.NewNoopCache[entities.Key](), | ||
ApiCache: cache.NewNoopCache[entities.Api](), | ||
Database: db, | ||
Tracer: tracing.NewNoop(), | ||
}) | ||
|
||
key := entities.Key{ | ||
Id: uid.Key(), | ||
ApiId: resources.UserApi.Id, | ||
WorkspaceId: resources.UserWorkspace.Id, | ||
Hash: hash.Sha256(uid.New(16, "test")), | ||
CreatedAt: time.Now(), | ||
} | ||
err = db.CreateKey(ctx, key) | ||
require.NoError(t, err) | ||
|
||
req := httptest.NewRequest("GET", fmt.Sprintf("/v1/keys/%s", key.Id), nil) | ||
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", resources.UnkeyKey)) | ||
|
||
res, err := srv.app.Test(req) | ||
require.NoError(t, err) | ||
defer res.Body.Close() | ||
|
||
body, err := io.ReadAll(res.Body) | ||
require.NoError(t, err) | ||
|
||
require.Equal(t, 200, res.StatusCode) | ||
|
||
successResponse := GetKeyResponse{} | ||
err = json.Unmarshal(body, &successResponse) | ||
require.NoError(t, err) | ||
|
||
require.Equal(t, key, successResponse) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.