mirror of
https://github.com/coder/coder.git
synced 2025-07-06 15:41:45 +00:00
32 lines
825 B
Go
32 lines
825 B
Go
package provisionerkey
|
|
|
|
import (
|
|
"crypto/sha256"
|
|
"fmt"
|
|
|
|
"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"
|
|
)
|
|
|
|
func New(organizationID uuid.UUID, name string) (database.InsertProvisionerKeyParams, string, error) {
|
|
id := uuid.New()
|
|
secret, err := cryptorand.HexString(64)
|
|
if err != nil {
|
|
return database.InsertProvisionerKeyParams{}, "", xerrors.Errorf("generate token: %w", err)
|
|
}
|
|
hashedSecret := sha256.Sum256([]byte(secret))
|
|
token := fmt.Sprintf("%s:%s", id, secret)
|
|
|
|
return database.InsertProvisionerKeyParams{
|
|
ID: id,
|
|
CreatedAt: dbtime.Now(),
|
|
OrganizationID: organizationID,
|
|
Name: name,
|
|
HashedSecret: hashedSecret[:],
|
|
}, token, nil
|
|
}
|