Skip to content

Commit 9de178a

Browse files
committed
wip: disable services
1 parent f74356b commit 9de178a

File tree

15 files changed

+57
-1795
lines changed

15 files changed

+57
-1795
lines changed

api/services/api-key.go

Lines changed: 2 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,10 @@ package services
22

33
import (
44
"context"
5-
"crypto/sha256"
6-
"encoding/hex"
7-
"errors"
85

96
"github.com/shellhub-io/shellhub/pkg/api/requests"
107
"github.com/shellhub-io/shellhub/pkg/api/responses"
11-
"github.com/shellhub-io/shellhub/pkg/clock"
128
"github.com/shellhub-io/shellhub/pkg/models"
13-
"github.com/shellhub-io/shellhub/pkg/uuid"
149
)
1510

1611
type APIKeyService interface {
@@ -31,100 +26,17 @@ type APIKeyService interface {
3126
}
3227

3328
func (s *service) CreateAPIKey(ctx context.Context, req *requests.CreateAPIKey) (*responses.CreateAPIKey, error) {
34-
if _, err := s.store.NamespaceGet(ctx, req.TenantID); err != nil {
35-
return nil, NewErrNamespaceNotFound(req.TenantID, err)
36-
}
37-
38-
expiresIn := int64(0)
39-
switch req.ExpiresAt {
40-
case 30, 60, 90:
41-
expiresIn = clock.Now().AddDate(0, 0, req.ExpiresAt).Unix()
42-
case 365:
43-
expiresIn = clock.Now().AddDate(1, 0, 0).Unix()
44-
case -1:
45-
expiresIn = -1
46-
default:
47-
return nil, NewErrBadRequest(errors.New("experid date to APIKey is invalid"))
48-
}
49-
50-
if req.Key == "" {
51-
req.Key = uuid.Generate()
52-
}
53-
54-
if req.OptRole != "" {
55-
if !req.Role.HasAuthority(req.OptRole) {
56-
return nil, NewErrRoleInvalid()
57-
}
58-
59-
req.Role = req.OptRole
60-
}
61-
62-
// We don't store the plain key, which means we cannot save (because it is the primary key)
63-
// the UUID with a nondeterministic hash (like bcrypt). For this reason, we convert the
64-
// key to a SHA256 hash, which is guaranteed to be the same every time. This way, when
65-
// retrieving the API key by the UUID, we can simply convert the UUID to a SHA256 hash and
66-
// try to match it.
67-
keySum := sha256.Sum256([]byte(req.Key))
68-
hashedKey := hex.EncodeToString(keySum[:])
69-
70-
if conflicts, has, _ := s.store.APIKeyConflicts(ctx, req.TenantID, &models.APIKeyConflicts{ID: hashedKey, Name: req.Name}); has {
71-
return nil, NewErrAPIKeyDuplicated(conflicts)
72-
}
73-
74-
data := &models.APIKey{
75-
ID: hashedKey,
76-
Name: req.Name,
77-
TenantID: req.TenantID,
78-
Role: req.Role,
79-
ExpiresIn: expiresIn,
80-
CreatedBy: req.UserID,
81-
}
82-
83-
if _, err := s.store.APIKeyCreate(ctx, data); err != nil {
84-
return nil, err
85-
}
86-
87-
// As we need to return the plain key in the create service, we temporarily set
88-
// the apiKey.ID to the plain key here.
89-
apiKey, _ := s.store.APIKeyGet(ctx, hashedKey)
90-
apiKey.ID = req.Key
91-
92-
return responses.CreateAPIKeyFromModel(apiKey), nil
29+
return nil, nil
9330
}
9431

9532
func (s *service) ListAPIKeys(ctx context.Context, req *requests.ListAPIKey) ([]models.APIKey, int, error) {
96-
return s.store.APIKeyList(ctx, req.TenantID, req.Paginator, req.Sorter)
33+
return nil, 0, nil
9734
}
9835

9936
func (s *service) UpdateAPIKey(ctx context.Context, req *requests.UpdateAPIKey) error {
100-
ns, err := s.store.NamespaceGet(ctx, req.TenantID)
101-
if err != nil {
102-
return NewErrNamespaceNotFound(req.TenantID, err)
103-
}
104-
105-
// If req.Role is not empty, it must be lower than the user's role.
106-
if req.Role != "" {
107-
if m, ok := ns.FindMember(req.UserID); !ok || !m.Role.HasAuthority(req.Role) {
108-
return NewErrRoleInvalid()
109-
}
110-
}
111-
112-
if conflicts, has, _ := s.store.APIKeyConflicts(ctx, req.TenantID, &models.APIKeyConflicts{Name: req.Name}); has {
113-
return NewErrAPIKeyDuplicated(conflicts)
114-
}
115-
116-
change := &models.APIKeyChanges{Name: req.Name, Role: req.Role}
117-
if err := s.store.APIKeyUpdate(ctx, req.TenantID, req.CurrentName, change); err != nil {
118-
return NewErrAPIKeyNotFound(req.CurrentName, err)
119-
}
120-
12137
return nil
12238
}
12339

12440
func (s *service) DeleteAPIKey(ctx context.Context, req *requests.DeleteAPIKey) error {
125-
if err := s.store.APIKeyDelete(ctx, req.TenantID, req.Name); err != nil {
126-
return NewErrAPIKeyNotFound(req.Name, err)
127-
}
128-
12941
return nil
13042
}

0 commit comments

Comments
 (0)