2023-05-21 16:37:59 +00:00
|
|
|
package db
|
2022-01-25 22:11:05 +00:00
|
|
|
|
|
|
|
import (
|
2023-05-11 07:09:18 +00:00
|
|
|
"errors"
|
2022-01-25 22:11:05 +00:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2023-05-21 16:37:59 +00:00
|
|
|
"github.com/juanfont/headscale/hscontrol/types"
|
2023-05-11 07:09:18 +00:00
|
|
|
"github.com/juanfont/headscale/hscontrol/util"
|
2022-01-25 22:11:05 +00:00
|
|
|
"golang.org/x/crypto/bcrypt"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
apiPrefixLength = 7
|
|
|
|
apiKeyLength = 32
|
|
|
|
)
|
|
|
|
|
2023-05-11 07:09:18 +00:00
|
|
|
var ErrAPIKeyFailedToParse = errors.New("failed to parse ApiKey")
|
|
|
|
|
2023-01-17 16:43:44 +00:00
|
|
|
// CreateAPIKey creates a new ApiKey in a user, and returns it.
|
2023-05-11 07:09:18 +00:00
|
|
|
func (hsdb *HSDatabase) CreateAPIKey(
|
2022-01-25 22:11:05 +00:00
|
|
|
expiration *time.Time,
|
2023-05-21 16:37:59 +00:00
|
|
|
) (string, *types.APIKey, error) {
|
2023-07-17 11:35:05 +00:00
|
|
|
hsdb.mu.Lock()
|
|
|
|
defer hsdb.mu.Unlock()
|
|
|
|
|
2023-05-11 07:09:18 +00:00
|
|
|
prefix, err := util.GenerateRandomStringURLSafe(apiPrefixLength)
|
2022-01-25 22:11:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", nil, err
|
|
|
|
}
|
|
|
|
|
2023-05-11 07:09:18 +00:00
|
|
|
toBeHashed, err := util.GenerateRandomStringURLSafe(apiKeyLength)
|
2022-01-25 22:11:05 +00: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-21 16:37:59 +00:00
|
|
|
key := types.APIKey{
|
2022-01-25 22:11:05 +00:00
|
|
|
Prefix: prefix,
|
|
|
|
Hash: hash,
|
|
|
|
Expiration: expiration,
|
|
|
|
}
|
2022-05-30 13:31:06 +00:00
|
|
|
|
2023-05-11 07:09:18 +00:00
|
|
|
if err := hsdb.db.Save(&key).Error; err != nil {
|
2022-05-30 13:31:06 +00:00
|
|
|
return "", nil, fmt.Errorf("failed to save API key to database: %w", err)
|
|
|
|
}
|
2022-01-25 22:11:05 +00:00
|
|
|
|
|
|
|
return keyStr, &key, nil
|
|
|
|
}
|
|
|
|
|
2023-01-17 16:43:44 +00:00
|
|
|
// ListAPIKeys returns the list of ApiKeys for a user.
|
2023-05-21 16:37:59 +00:00
|
|
|
func (hsdb *HSDatabase) ListAPIKeys() ([]types.APIKey, error) {
|
2023-07-17 11:35:05 +00:00
|
|
|
hsdb.mu.RLock()
|
|
|
|
defer hsdb.mu.RUnlock()
|
|
|
|
|
2023-05-21 16:37:59 +00:00
|
|
|
keys := []types.APIKey{}
|
2023-05-11 07:09:18 +00:00
|
|
|
if err := hsdb.db.Find(&keys).Error; err != nil {
|
2022-01-25 22:11:05 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return keys, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetAPIKey returns a ApiKey for a given key.
|
2023-05-21 16:37:59 +00:00
|
|
|
func (hsdb *HSDatabase) GetAPIKey(prefix string) (*types.APIKey, error) {
|
2023-07-17 11:35:05 +00:00
|
|
|
hsdb.mu.RLock()
|
|
|
|
defer hsdb.mu.RUnlock()
|
|
|
|
|
2023-05-21 16:37:59 +00:00
|
|
|
key := types.APIKey{}
|
2023-05-11 07:09:18 +00:00
|
|
|
if result := hsdb.db.First(&key, "prefix = ?", prefix); result.Error != nil {
|
2022-01-25 22:11:05 +00:00
|
|
|
return nil, result.Error
|
|
|
|
}
|
|
|
|
|
|
|
|
return &key, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetAPIKeyByID returns a ApiKey for a given id.
|
2023-05-21 16:37:59 +00:00
|
|
|
func (hsdb *HSDatabase) GetAPIKeyByID(id uint64) (*types.APIKey, error) {
|
2023-07-17 11:35:05 +00:00
|
|
|
hsdb.mu.RLock()
|
|
|
|
defer hsdb.mu.RUnlock()
|
|
|
|
|
2023-05-21 16:37:59 +00:00
|
|
|
key := types.APIKey{}
|
|
|
|
if result := hsdb.db.Find(&types.APIKey{ID: id}).First(&key); result.Error != nil {
|
2022-01-25 22:11:05 +00:00
|
|
|
return nil, result.Error
|
|
|
|
}
|
|
|
|
|
|
|
|
return &key, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DestroyAPIKey destroys a ApiKey. Returns error if the ApiKey
|
|
|
|
// does not exist.
|
2023-05-21 16:37:59 +00:00
|
|
|
func (hsdb *HSDatabase) DestroyAPIKey(key types.APIKey) error {
|
2023-07-17 11:35:05 +00:00
|
|
|
hsdb.mu.Lock()
|
|
|
|
defer hsdb.mu.Unlock()
|
|
|
|
|
2023-05-11 07:09:18 +00:00
|
|
|
if result := hsdb.db.Unscoped().Delete(key); result.Error != nil {
|
2022-01-25 22:11:05 +00:00
|
|
|
return result.Error
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ExpireAPIKey marks a ApiKey as expired.
|
2023-05-21 16:37:59 +00:00
|
|
|
func (hsdb *HSDatabase) ExpireAPIKey(key *types.APIKey) error {
|
2023-07-17 11:35:05 +00:00
|
|
|
hsdb.mu.Lock()
|
|
|
|
defer hsdb.mu.Unlock()
|
|
|
|
|
2023-05-11 07:09:18 +00:00
|
|
|
if err := hsdb.db.Model(&key).Update("Expiration", time.Now()).Error; err != nil {
|
2022-01-25 22:11:05 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-05-11 07:09:18 +00:00
|
|
|
func (hsdb *HSDatabase) ValidateAPIKey(keyStr string) (bool, error) {
|
2023-07-17 11:35:05 +00:00
|
|
|
hsdb.mu.RLock()
|
|
|
|
defer hsdb.mu.RUnlock()
|
|
|
|
|
2022-06-08 16:09:11 +00:00
|
|
|
prefix, hash, found := strings.Cut(keyStr, ".")
|
|
|
|
if !found {
|
2022-07-29 15:35:21 +00:00
|
|
|
return false, ErrAPIKeyFailedToParse
|
2022-01-25 22:11:05 +00:00
|
|
|
}
|
|
|
|
|
2023-05-11 07:09:18 +00:00
|
|
|
key, err := hsdb.GetAPIKey(prefix)
|
2022-01-25 22:11:05 +00: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
|
|
|
|
}
|