Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(gateway): add api key support (#10)
Signed-off-by: Praveen Yadav <pyadav9678@gmail.com>
- Loading branch information
Showing
25 changed files
with
2,136 additions
and
351 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
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,16 @@ | ||
package apikey | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/google/uuid" | ||
) | ||
|
||
type APIKey struct { | ||
Id uuid.UUID `json:"id"` | ||
Name string `json:"name"` | ||
Value string `json:"value"` | ||
MaskedValue string `json:"masked_value"` | ||
CreatedAt time.Time `json:"created_at"` | ||
LastUsedAt time.Time `json:"last_used_at"` | ||
} |
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,50 @@ | ||
package apikey | ||
|
||
import ( | ||
"context" | ||
) | ||
|
||
type Repository interface { | ||
GetAll(context.Context) ([]APIKey, error) | ||
Create(context.Context, APIKey) (APIKey, error) | ||
Get(context.Context, string) (APIKey, error) | ||
GetByToken(context.Context, string) (APIKey, error) | ||
Update(context.Context, APIKey) (APIKey, error) | ||
DeleteByID(context.Context, string) error | ||
} | ||
|
||
var _ Repository = &Service{} | ||
|
||
type Service struct { | ||
apikeyRepo Repository | ||
} | ||
|
||
func NewService(apikeyRepo Repository) *Service { | ||
return &Service{ | ||
apikeyRepo: apikeyRepo, | ||
} | ||
} | ||
|
||
func (s *Service) GetAll(ctx context.Context) ([]APIKey, error) { | ||
return s.apikeyRepo.GetAll(ctx) | ||
} | ||
|
||
func (s *Service) Create(ctx context.Context, api APIKey) (APIKey, error) { | ||
return s.apikeyRepo.Create(ctx, api) | ||
} | ||
|
||
func (s *Service) Get(ctx context.Context, id string) (APIKey, error) { | ||
return s.apikeyRepo.Get(ctx, id) | ||
} | ||
|
||
func (s *Service) GetByToken(ctx context.Context, id string) (APIKey, error) { | ||
return s.apikeyRepo.GetByToken(ctx, id) | ||
} | ||
|
||
func (s *Service) Update(ctx context.Context, api APIKey) (APIKey, error) { | ||
return s.apikeyRepo.Update(ctx, api) | ||
} | ||
|
||
func (s *Service) DeleteByID(ctx context.Context, id string) error { | ||
return s.apikeyRepo.DeleteByID(ctx, id) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
package v1 | ||
|
||
import ( | ||
"context" | ||
|
||
"connectrpc.com/connect" | ||
"github.com/google/uuid" | ||
"github.com/missingstudio/studio/backend/core/apikey" | ||
"github.com/missingstudio/studio/backend/pkg/utils" | ||
"github.com/missingstudio/studio/common/errors" | ||
llmv1 "github.com/missingstudio/studio/protos/pkg/llm/v1" | ||
"google.golang.org/protobuf/types/known/emptypb" | ||
"google.golang.org/protobuf/types/known/timestamppb" | ||
) | ||
|
||
func (s *V1Handler) ListAPIKeys(ctx context.Context, req *connect.Request[emptypb.Empty]) (*connect.Response[llmv1.ListAPIKeysResponse], error) { | ||
keys, err := s.apikeyService.GetAll(ctx) | ||
if err != nil { | ||
return nil, errors.NewInternalError(err.Error()) | ||
} | ||
|
||
data := []*llmv1.APIKey{} | ||
for _, k := range keys { | ||
data = append(data, &llmv1.APIKey{ | ||
Id: k.Id.String(), | ||
Name: k.Name, | ||
MaskedValue: utils.MaskString(k.Value), | ||
CreatedAt: timestamppb.New(k.CreatedAt), | ||
LastUsedAt: timestamppb.New(k.LastUsedAt), | ||
}) | ||
} | ||
|
||
return connect.NewResponse(&llmv1.ListAPIKeysResponse{ | ||
Keys: data, | ||
}), nil | ||
} | ||
|
||
func (s *V1Handler) CreateAPIKey(ctx context.Context, req *connect.Request[llmv1.CreateAPIKeyRequest]) (*connect.Response[llmv1.CreateAPIKeyResponse], error) { | ||
securekey, err := utils.GenerateSecureAPIKey() | ||
if err != nil { | ||
return nil, errors.New(err) | ||
} | ||
|
||
key := apikey.APIKey{ | ||
Name: req.Msg.Name, | ||
Value: securekey, | ||
} | ||
|
||
newkey, err := s.apikeyService.Create(ctx, key) | ||
if err != nil { | ||
return nil, errors.New(err) | ||
} | ||
|
||
return connect.NewResponse(&llmv1.CreateAPIKeyResponse{ | ||
Key: &llmv1.APIKey{ | ||
Id: newkey.Id.String(), | ||
Name: newkey.Name, | ||
Value: newkey.Value, | ||
CreatedAt: timestamppb.New(newkey.CreatedAt), | ||
LastUsedAt: timestamppb.New(newkey.LastUsedAt), | ||
}, | ||
}), nil | ||
} | ||
|
||
func (s *V1Handler) GetAPIKey(ctx context.Context, req *connect.Request[llmv1.GetAPIKeyRequest]) (*connect.Response[llmv1.GetAPIKeyResponse], error) { | ||
key, err := s.apikeyService.Get(ctx, req.Msg.Id) | ||
if err != nil { | ||
return nil, errors.NewNotFound(err.Error()) | ||
} | ||
|
||
k := &llmv1.APIKey{ | ||
Id: key.Id.String(), | ||
Name: key.Name, | ||
MaskedValue: utils.MaskString(key.Value), | ||
CreatedAt: timestamppb.New(key.CreatedAt), | ||
LastUsedAt: timestamppb.New(key.LastUsedAt), | ||
} | ||
|
||
return connect.NewResponse(&llmv1.GetAPIKeyResponse{ | ||
Key: k, | ||
}), nil | ||
} | ||
|
||
func (s *V1Handler) UpdateAPIKey(ctx context.Context, req *connect.Request[llmv1.UpdateAPIKeyRequest]) (*connect.Response[llmv1.UpdateAPIKeyResponse], error) { | ||
parsedUUID, err := uuid.Parse(req.Msg.Id) | ||
if err != nil { | ||
return nil, errors.New(err) | ||
} | ||
|
||
key := apikey.APIKey{ | ||
Id: parsedUUID, | ||
Name: req.Msg.Name, | ||
} | ||
|
||
updatedkey, err := s.apikeyService.Update(ctx, key) | ||
if err != nil { | ||
return nil, errors.New(err) | ||
} | ||
|
||
return connect.NewResponse(&llmv1.UpdateAPIKeyResponse{ | ||
Key: &llmv1.APIKey{ | ||
Id: updatedkey.Id.String(), | ||
Name: updatedkey.Name, | ||
MaskedValue: utils.MaskString(updatedkey.Value), | ||
CreatedAt: timestamppb.New(updatedkey.CreatedAt), | ||
LastUsedAt: timestamppb.New(updatedkey.LastUsedAt), | ||
}, | ||
}), nil | ||
} | ||
|
||
func (s *V1Handler) DeleteAPIKey(ctx context.Context, req *connect.Request[llmv1.DeleteAPIKeyRequest]) (*connect.Response[emptypb.Empty], error) { | ||
err := s.apikeyService.DeleteByID(ctx, req.Msg.Id) | ||
if err != nil { | ||
return nil, errors.New(err) | ||
} | ||
|
||
return connect.NewResponse(&emptypb.Empty{}), nil | ||
} |
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
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,28 @@ | ||
package postgres | ||
|
||
import ( | ||
"database/sql" | ||
"time" | ||
|
||
"github.com/google/uuid" | ||
"github.com/missingstudio/studio/backend/core/apikey" | ||
) | ||
|
||
type APIKey struct { | ||
ID uuid.UUID `db:"id"` | ||
Name string `db:"name"` | ||
Value []byte `db:"value"` | ||
LastUsedAt sql.NullTime `db:"last_used_at"` | ||
UpdatedAt time.Time `db:"updated_at"` | ||
CreatedAt time.Time `db:"created_at"` | ||
} | ||
|
||
func (c APIKey) ToAPIKey() (apikey.APIKey, error) { | ||
return apikey.APIKey{ | ||
Id: c.ID, | ||
Name: c.Name, | ||
Value: string(c.Value), | ||
LastUsedAt: c.LastUsedAt.Time, | ||
CreatedAt: c.CreatedAt, | ||
}, nil | ||
} |
Oops, something went wrong.