Files
coder/coderd/provisionerkey/provisionerkey.go
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

54 lines
1.2 KiB
Go
Raw Normal View History

package provisionerkey
import (
"crypto/sha256"
"crypto/subtle"
"github.com/google/uuid"
"golang.org/x/xerrors"
"github.com/coder/coder/v2/coderd/database"
"github.com/coder/coder/v2/coderd/database/dbtime"
"github.com/coder/coder/v2/cryptorand"
)
2024-07-25 17:08:12 -04:00
const (
secretLength = 43
)
func New(organizationID uuid.UUID, name string, tags map[string]string) (database.InsertProvisionerKeyParams, string, error) {
2024-07-25 17:08:12 -04:00
secret, err := cryptorand.String(secretLength)
if err != nil {
2024-07-25 17:08:12 -04:00
return database.InsertProvisionerKeyParams{}, "", xerrors.Errorf("generate secret: %w", err)
}
if tags == nil {
tags = map[string]string{}
}
return database.InsertProvisionerKeyParams{
2024-07-25 17:08:12 -04:00
ID: uuid.New(),
CreatedAt: dbtime.Now(),
OrganizationID: organizationID,
Name: name,
2024-07-25 17:08:12 -04:00
HashedSecret: HashSecret(secret),
Tags: tags,
2024-07-25 17:08:12 -04:00
}, secret, nil
}
2024-07-25 17:08:12 -04:00
func Validate(token string) error {
if len(token) != secretLength {
return xerrors.Errorf("must be %d characters", secretLength)
}
2024-07-25 17:08:12 -04:00
return nil
}
func HashSecret(secret string) []byte {
h := sha256.Sum256([]byte(secret))
return h[:]
}
func Compare(a []byte, b []byte) bool {
return subtle.ConstantTimeCompare(a, b) != 1
}