satellite/console: support reading apikeys by name and project id

When performing re-authorizations for OAuth, we need to pull up an
APIKey using it's project id and name. This change also updates the
APIKeyInfo struct to return the head value associated with an API
key.

Change-Id: I4b40f7f13fb9b58a1927dd283b42a39015ea550e
This commit is contained in:
Mya 2022-02-14 12:20:12 -06:00
parent 7609bb9515
commit 75be1c0a28
3 changed files with 27 additions and 0 deletions

View File

@ -37,6 +37,7 @@ type APIKeyInfo struct {
PartnerID uuid.UUID `json:"partnerId"` PartnerID uuid.UUID `json:"partnerId"`
UserAgent []byte `json:"userAgent"` UserAgent []byte `json:"userAgent"`
Name string `json:"name"` Name string `json:"name"`
Head []byte `json:"-"`
Secret []byte `json:"-"` Secret []byte `json:"-"`
CreatedAt time.Time `json:"createdAt"` CreatedAt time.Time `json:"createdAt"`
} }

View File

@ -1487,6 +1487,31 @@ func (s *Service) CreateAPIKey(ctx context.Context, projectID uuid.UUID, name st
return info, key, nil return info, key, nil
} }
// GetAPIKeyInfoByName retrieves an api key by its name and project id.
func (s *Service) GetAPIKeyInfoByName(ctx context.Context, projectID uuid.UUID, name string) (_ *APIKeyInfo, err error) {
defer mon.Task()(&ctx)(&err)
auth, err := s.getAuthAndAuditLog(ctx, "get api key info",
zap.String("projectID", projectID.String()),
zap.String("name", name))
if err != nil {
return nil, err
}
key, err := s.store.APIKeys().GetByNameAndProjectID(ctx, name, projectID)
if err != nil {
return nil, Error.Wrap(err)
}
_, err = s.isProjectMember(ctx, auth.User.ID, key.ProjectID)
if err != nil {
return nil, Error.Wrap(err)
}
return key, nil
}
// GetAPIKeyInfo retrieves api key by id. // GetAPIKeyInfo retrieves api key by id.
func (s *Service) GetAPIKeyInfo(ctx context.Context, id uuid.UUID) (_ *APIKeyInfo, err error) { func (s *Service) GetAPIKeyInfo(ctx context.Context, id uuid.UUID) (_ *APIKeyInfo, err error) {
defer mon.Task()(&ctx)(&err) defer mon.Task()(&ctx)(&err)

View File

@ -234,6 +234,7 @@ func fromDBXAPIKey(ctx context.Context, key *dbx.ApiKey) (_ *console.APIKeyInfo,
ProjectID: projectID, ProjectID: projectID,
Name: key.Name, Name: key.Name,
CreatedAt: key.CreatedAt, CreatedAt: key.CreatedAt,
Head: key.Head,
Secret: key.Secret, Secret: key.Secret,
} }