2023-05-22 00:37:59 +08:00
|
|
|
package db
|
2022-01-26 06:11:05 +08:00
|
|
|
|
|
|
|
import (
|
2023-05-11 15:09:18 +08:00
|
|
|
"errors"
|
2022-01-26 06:11:05 +08:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2023-05-22 00:37:59 +08:00
|
|
|
"github.com/juanfont/headscale/hscontrol/types"
|
2023-05-11 15:09:18 +08:00
|
|
|
"github.com/juanfont/headscale/hscontrol/util"
|
2022-01-26 06:11:05 +08:00
|
|
|
"golang.org/x/crypto/bcrypt"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
apiPrefixLength = 7
|
|
|
|
apiKeyLength = 32
|
|
|
|
)
|
|
|
|
|
2023-05-11 15:09:18 +08:00
|
|
|
var ErrAPIKeyFailedToParse = errors.New("failed to parse ApiKey")
|
|
|
|
|
2023-01-18 00:43:44 +08:00
|
|
|
// CreateAPIKey creates a new ApiKey in a user, and returns it.
|
2023-05-11 15:09:18 +08:00
|
|
|
func (hsdb *HSDatabase) CreateAPIKey(
|
2022-01-26 06:11:05 +08:00
|
|
|
expiration *time.Time,
|
2023-05-22 00:37:59 +08:00
|
|
|
) (string, *types.APIKey, error) {
|
2023-05-11 15:09:18 +08:00
|
|
|
prefix, err := util.GenerateRandomStringURLSafe(apiPrefixLength)
|
2022-01-26 06:11:05 +08:00
|
|
|
if err != nil {
|
|
|
|
return "", nil, err
|
|
|
|
}
|
|
|
|
|
2023-05-11 15:09:18 +08:00
|
|
|
toBeHashed, err := util.GenerateRandomStringURLSafe(apiKeyLength)
|
2022-01-26 06:11:05 +08:00
|
|
|
if err != nil {
|
|
|
|
return "", nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Key to return to user, this will only be visible _once_
|
|
|
|
keyStr := prefix + "." + toBeHashed
|
|
|
|
|
|
|
|
hash, err := bcrypt.GenerateFromPassword([]byte(toBeHashed), bcrypt.DefaultCost)
|
|
|
|
if err != nil {
|
|
|
|
return "", nil, err
|
|
|
|
}
|
|
|
|
|
2023-05-22 00:37:59 +08:00
|
|
|
key := types.APIKey{
|
2022-01-26 06:11:05 +08:00
|
|
|
Prefix: prefix,
|
|
|
|
Hash: hash,
|
|
|
|
Expiration: expiration,
|
|
|
|
}
|
2022-05-30 21:31:06 +08:00
|
|
|
|
2024-02-09 00:28:19 +08:00
|
|
|
if err := hsdb.DB.Save(&key).Error; err != nil {
|
2022-05-30 21:31:06 +08:00
|
|
|
return "", nil, fmt.Errorf("failed to save API key to database: %w", err)
|
|
|
|
}
|
2022-01-26 06:11:05 +08:00
|
|
|
|
|
|
|
return keyStr, &key, nil
|
|
|
|
}
|
|
|
|
|
2023-01-18 00:43:44 +08:00
|
|
|
// ListAPIKeys returns the list of ApiKeys for a user.
|
2023-05-22 00:37:59 +08:00
|
|
|
func (hsdb *HSDatabase) ListAPIKeys() ([]types.APIKey, error) {
|
|
|
|
keys := []types.APIKey{}
|
2024-02-09 00:28:19 +08:00
|
|
|
if err := hsdb.DB.Find(&keys).Error; err != nil {
|
2022-01-26 06:11:05 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return keys, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetAPIKey returns a ApiKey for a given key.
|
2023-05-22 00:37:59 +08:00
|
|
|
func (hsdb *HSDatabase) GetAPIKey(prefix string) (*types.APIKey, error) {
|
|
|
|
key := types.APIKey{}
|
2024-02-09 00:28:19 +08:00
|
|
|
if result := hsdb.DB.First(&key, "prefix = ?", prefix); result.Error != nil {
|
2022-01-26 06:11:05 +08:00
|
|
|
return nil, result.Error
|
|
|
|
}
|
|
|
|
|
|
|
|
return &key, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetAPIKeyByID returns a ApiKey for a given id.
|
2023-05-22 00:37:59 +08:00
|
|
|
func (hsdb *HSDatabase) GetAPIKeyByID(id uint64) (*types.APIKey, error) {
|
|
|
|
key := types.APIKey{}
|
2024-02-09 00:28:19 +08:00
|
|
|
if result := hsdb.DB.Find(&types.APIKey{ID: id}).First(&key); result.Error != nil {
|
2022-01-26 06:11:05 +08:00
|
|
|
return nil, result.Error
|
|
|
|
}
|
|
|
|
|
|
|
|
return &key, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DestroyAPIKey destroys a ApiKey. Returns error if the ApiKey
|
|
|
|
// does not exist.
|
2023-05-22 00:37:59 +08:00
|
|
|
func (hsdb *HSDatabase) DestroyAPIKey(key types.APIKey) error {
|
2024-02-09 00:28:19 +08:00
|
|
|
if result := hsdb.DB.Unscoped().Delete(key); result.Error != nil {
|
2022-01-26 06:11:05 +08:00
|
|
|
return result.Error
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ExpireAPIKey marks a ApiKey as expired.
|
2023-05-22 00:37:59 +08:00
|
|
|
func (hsdb *HSDatabase) ExpireAPIKey(key *types.APIKey) error {
|
2024-02-09 00:28:19 +08:00
|
|
|
if err := hsdb.DB.Model(&key).Update("Expiration", time.Now()).Error; err != nil {
|
2022-01-26 06:11:05 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-05-11 15:09:18 +08:00
|
|
|
func (hsdb *HSDatabase) ValidateAPIKey(keyStr string) (bool, error) {
|
2022-06-09 00:09:11 +08:00
|
|
|
prefix, hash, found := strings.Cut(keyStr, ".")
|
|
|
|
if !found {
|
2022-07-29 23:35:21 +08:00
|
|
|
return false, ErrAPIKeyFailedToParse
|
2022-01-26 06:11:05 +08:00
|
|
|
}
|
|
|
|
|
2023-05-11 15:09:18 +08:00
|
|
|
key, err := hsdb.GetAPIKey(prefix)
|
2022-01-26 06:11:05 +08:00
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf("failed to validate api key: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if key.Expiration.Before(time.Now()) {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := bcrypt.CompareHashAndPassword(key.Hash, []byte(hash)); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}
|