a17cd9aa3e
Change-Id: I840cd0fdbd8dca884eefbd111f21fd3990c11e68
65 lines
1.4 KiB
Go
65 lines
1.4 KiB
Go
// Copyright (C) 2020 Storj Labs, Inc.
|
|
// See LICENSE for copying information.
|
|
|
|
package apikeys
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/spacemonkeygo/monkit/v3"
|
|
"github.com/zeebo/errs"
|
|
)
|
|
|
|
var (
|
|
// ErrService defines secret service error.
|
|
ErrService = errs.Class("secret service error")
|
|
|
|
mon = monkit.Package()
|
|
)
|
|
|
|
// Service responsible for operations with storagenode's uniq secret.
|
|
//
|
|
// architecture: Service
|
|
type Service struct {
|
|
store DB
|
|
}
|
|
|
|
// NewService is a constructor for service.
|
|
func NewService(db DB) *Service {
|
|
return &Service{store: db}
|
|
}
|
|
|
|
// Issue generates new api key and stores it into db.
|
|
func (service *Service) Issue(ctx context.Context) (apiKey APIKey, err error) {
|
|
defer mon.Task()(&ctx)(&err)
|
|
secret, err := NewSecret()
|
|
if err != nil {
|
|
return APIKey{}, ErrService.Wrap(err)
|
|
}
|
|
|
|
apiKey.Secret = secret
|
|
apiKey.CreatedAt = time.Now()
|
|
|
|
err = service.store.Store(ctx, apiKey)
|
|
if err != nil {
|
|
return APIKey{}, ErrService.Wrap(err)
|
|
}
|
|
|
|
return apiKey, nil
|
|
}
|
|
|
|
// Check returns error if api key does not exists.
|
|
func (service *Service) Check(ctx context.Context, secret Secret) (err error) {
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
return service.store.Check(ctx, secret)
|
|
}
|
|
|
|
// Remove revokes apikey, deletes it from db.
|
|
func (service *Service) Remove(ctx context.Context, secret Secret) (err error) {
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
return ErrService.Wrap(service.store.Revoke(ctx, secret))
|
|
}
|