2023-05-21 12:37:59 -04:00
|
|
|
package db
|
2021-02-27 18:58:09 -05:00
|
|
|
|
|
|
|
import (
|
2021-06-24 09:44:19 -04:00
|
|
|
"errors"
|
2024-04-12 09:57:43 -04:00
|
|
|
"fmt"
|
2021-02-27 18:58:09 -05:00
|
|
|
|
2023-05-21 12:37:59 -04:00
|
|
|
"github.com/juanfont/headscale/hscontrol/types"
|
2023-05-11 03:09:18 -04:00
|
|
|
"github.com/juanfont/headscale/hscontrol/util"
|
2021-06-24 09:44:19 -04:00
|
|
|
"gorm.io/gorm"
|
2021-02-27 18:58:09 -05:00
|
|
|
)
|
|
|
|
|
2023-05-11 03:09:18 -04:00
|
|
|
var (
|
|
|
|
ErrUserExists = errors.New("user already exists")
|
|
|
|
ErrUserNotFound = errors.New("user not found")
|
|
|
|
ErrUserStillHasNodes = errors.New("user not empty: node(s) found")
|
2021-11-04 18:15:17 -04:00
|
|
|
)
|
2021-05-09 11:12:05 -04:00
|
|
|
|
2023-05-21 12:37:59 -04:00
|
|
|
func (hsdb *HSDatabase) CreateUser(name string) (*types.User, error) {
|
2024-02-08 11:28:19 -05:00
|
|
|
return Write(hsdb.DB, func(tx *gorm.DB) (*types.User, error) {
|
|
|
|
return CreateUser(tx, name)
|
|
|
|
})
|
|
|
|
}
|
2023-07-17 07:35:05 -04:00
|
|
|
|
2024-02-08 11:28:19 -05:00
|
|
|
// CreateUser creates a new User. Returns error if could not be created
|
|
|
|
// or another user already exists.
|
|
|
|
func CreateUser(tx *gorm.DB, name string) (*types.User, error) {
|
2023-05-21 12:37:59 -04:00
|
|
|
err := util.CheckForFQDNRules(name)
|
2022-02-22 06:45:50 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-05-21 12:37:59 -04:00
|
|
|
user := types.User{}
|
2024-02-08 11:28:19 -05:00
|
|
|
if err := tx.Where("name = ?", name).First(&user).Error; err == nil {
|
2023-01-17 11:43:44 -05:00
|
|
|
return nil, ErrUserExists
|
2021-02-27 18:58:09 -05:00
|
|
|
}
|
2023-01-17 11:43:44 -05:00
|
|
|
user.Name = name
|
2024-02-08 11:28:19 -05:00
|
|
|
if err := tx.Create(&user).Error; err != nil {
|
2024-04-12 09:57:43 -04:00
|
|
|
return nil, fmt.Errorf("creating user: %w", err)
|
2021-02-27 18:58:09 -05:00
|
|
|
}
|
2021-11-14 10:46:09 -05:00
|
|
|
|
2023-01-17 11:43:44 -05:00
|
|
|
return &user, nil
|
2021-02-27 18:58:09 -05:00
|
|
|
}
|
|
|
|
|
2023-05-11 03:09:18 -04:00
|
|
|
func (hsdb *HSDatabase) DestroyUser(name string) error {
|
2024-02-08 11:28:19 -05:00
|
|
|
return hsdb.Write(func(tx *gorm.DB) error {
|
|
|
|
return DestroyUser(tx, name)
|
|
|
|
})
|
|
|
|
}
|
2023-07-17 07:35:05 -04:00
|
|
|
|
2024-02-08 11:28:19 -05:00
|
|
|
// DestroyUser destroys a User. Returns error if the User does
|
|
|
|
// not exist or if there are nodes associated with it.
|
|
|
|
func DestroyUser(tx *gorm.DB, name string) error {
|
Redo OIDC configuration (#2020)
expand user, add claims to user
This commit expands the user table with additional fields that
can be retrieved from OIDC providers (and other places) and
uses this data in various tailscale response objects if it is
available.
This is the beginning of implementing
https://docs.google.com/document/d/1X85PMxIaVWDF6T_UPji3OeeUqVBcGj_uHRM5CI-AwlY/edit
trying to make OIDC more coherant and maintainable in addition
to giving the user a better experience and integration with a
provider.
remove usernames in magic dns, normalisation of emails
this commit removes the option to have usernames as part of MagicDNS
domains and headscale will now align with Tailscale, where there is a
root domain, and the machine name.
In addition, the various normalisation functions for dns names has been
made lighter not caring about username and special character that wont
occur.
Email are no longer normalised as part of the policy processing.
untagle oidc and regcache, use typed cache
This commits stops reusing the registration cache for oidc
purposes and switches the cache to be types and not use any
allowing the removal of a bunch of casting.
try to make reauth/register branches clearer in oidc
Currently there was a function that did a bunch of stuff,
finding the machine key, trying to find the node, reauthing
the node, returning some status, and it was called validate
which was very confusing.
This commit tries to split this into what to do if the node
exists, if it needs to register etc.
Signed-off-by: Kristoffer Dalby <kristoffer@tailscale.com>
2024-10-02 08:50:17 -04:00
|
|
|
user, err := GetUserByUsername(tx, name)
|
2021-05-09 11:12:05 -04:00
|
|
|
if err != nil {
|
2023-01-17 11:43:44 -05:00
|
|
|
return ErrUserNotFound
|
2021-05-09 11:12:05 -04:00
|
|
|
}
|
|
|
|
|
2024-02-08 11:28:19 -05:00
|
|
|
nodes, err := ListNodesByUser(tx, name)
|
2021-05-09 11:12:05 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-09-24 07:42:05 -04:00
|
|
|
if len(nodes) > 0 {
|
2023-01-17 11:43:44 -05:00
|
|
|
return ErrUserStillHasNodes
|
2021-11-13 14:01:05 -05:00
|
|
|
}
|
|
|
|
|
2024-02-08 11:28:19 -05:00
|
|
|
keys, err := ListPreAuthKeys(tx, name)
|
2021-11-13 14:01:05 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-11-14 14:32:03 -05:00
|
|
|
for _, key := range keys {
|
2024-02-08 11:28:19 -05:00
|
|
|
err = DestroyPreAuthKey(tx, key)
|
2021-11-13 15:24:32 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-05-09 11:12:05 -04:00
|
|
|
}
|
|
|
|
|
2024-02-08 11:28:19 -05:00
|
|
|
if result := tx.Unscoped().Delete(&user); result.Error != nil {
|
2021-10-16 11:14:37 -04:00
|
|
|
return result.Error
|
2021-05-09 11:12:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-05-11 03:09:18 -04:00
|
|
|
func (hsdb *HSDatabase) RenameUser(oldName, newName string) error {
|
2024-02-08 11:28:19 -05:00
|
|
|
return hsdb.Write(func(tx *gorm.DB) error {
|
|
|
|
return RenameUser(tx, oldName, newName)
|
|
|
|
})
|
|
|
|
}
|
2023-07-17 07:35:05 -04:00
|
|
|
|
2024-02-08 11:28:19 -05:00
|
|
|
// RenameUser renames a User. Returns error if the User does
|
|
|
|
// not exist or if another User exists with the new name.
|
|
|
|
func RenameUser(tx *gorm.DB, oldName, newName string) error {
|
2022-02-22 06:45:50 -05:00
|
|
|
var err error
|
Redo OIDC configuration (#2020)
expand user, add claims to user
This commit expands the user table with additional fields that
can be retrieved from OIDC providers (and other places) and
uses this data in various tailscale response objects if it is
available.
This is the beginning of implementing
https://docs.google.com/document/d/1X85PMxIaVWDF6T_UPji3OeeUqVBcGj_uHRM5CI-AwlY/edit
trying to make OIDC more coherant and maintainable in addition
to giving the user a better experience and integration with a
provider.
remove usernames in magic dns, normalisation of emails
this commit removes the option to have usernames as part of MagicDNS
domains and headscale will now align with Tailscale, where there is a
root domain, and the machine name.
In addition, the various normalisation functions for dns names has been
made lighter not caring about username and special character that wont
occur.
Email are no longer normalised as part of the policy processing.
untagle oidc and regcache, use typed cache
This commits stops reusing the registration cache for oidc
purposes and switches the cache to be types and not use any
allowing the removal of a bunch of casting.
try to make reauth/register branches clearer in oidc
Currently there was a function that did a bunch of stuff,
finding the machine key, trying to find the node, reauthing
the node, returning some status, and it was called validate
which was very confusing.
This commit tries to split this into what to do if the node
exists, if it needs to register etc.
Signed-off-by: Kristoffer Dalby <kristoffer@tailscale.com>
2024-10-02 08:50:17 -04:00
|
|
|
oldUser, err := GetUserByUsername(tx, oldName)
|
2021-10-16 11:20:06 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-05-21 12:37:59 -04:00
|
|
|
err = util.CheckForFQDNRules(newName)
|
2022-02-22 06:45:50 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
Redo OIDC configuration (#2020)
expand user, add claims to user
This commit expands the user table with additional fields that
can be retrieved from OIDC providers (and other places) and
uses this data in various tailscale response objects if it is
available.
This is the beginning of implementing
https://docs.google.com/document/d/1X85PMxIaVWDF6T_UPji3OeeUqVBcGj_uHRM5CI-AwlY/edit
trying to make OIDC more coherant and maintainable in addition
to giving the user a better experience and integration with a
provider.
remove usernames in magic dns, normalisation of emails
this commit removes the option to have usernames as part of MagicDNS
domains and headscale will now align with Tailscale, where there is a
root domain, and the machine name.
In addition, the various normalisation functions for dns names has been
made lighter not caring about username and special character that wont
occur.
Email are no longer normalised as part of the policy processing.
untagle oidc and regcache, use typed cache
This commits stops reusing the registration cache for oidc
purposes and switches the cache to be types and not use any
allowing the removal of a bunch of casting.
try to make reauth/register branches clearer in oidc
Currently there was a function that did a bunch of stuff,
finding the machine key, trying to find the node, reauthing
the node, returning some status, and it was called validate
which was very confusing.
This commit tries to split this into what to do if the node
exists, if it needs to register etc.
Signed-off-by: Kristoffer Dalby <kristoffer@tailscale.com>
2024-10-02 08:50:17 -04:00
|
|
|
_, err = GetUserByUsername(tx, newName)
|
2021-10-16 11:20:06 -04:00
|
|
|
if err == nil {
|
2023-01-17 11:43:44 -05:00
|
|
|
return ErrUserExists
|
2021-10-16 11:20:06 -04:00
|
|
|
}
|
2023-01-17 11:43:44 -05:00
|
|
|
if !errors.Is(err, ErrUserNotFound) {
|
2021-10-16 11:20:06 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-01-17 11:43:44 -05:00
|
|
|
oldUser.Name = newName
|
2021-10-16 11:20:06 -04:00
|
|
|
|
2024-02-08 11:28:19 -05:00
|
|
|
if result := tx.Save(&oldUser); result.Error != nil {
|
2021-10-16 11:20:06 -04:00
|
|
|
return result.Error
|
|
|
|
}
|
|
|
|
|
2021-05-09 11:12:05 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
Redo OIDC configuration (#2020)
expand user, add claims to user
This commit expands the user table with additional fields that
can be retrieved from OIDC providers (and other places) and
uses this data in various tailscale response objects if it is
available.
This is the beginning of implementing
https://docs.google.com/document/d/1X85PMxIaVWDF6T_UPji3OeeUqVBcGj_uHRM5CI-AwlY/edit
trying to make OIDC more coherant and maintainable in addition
to giving the user a better experience and integration with a
provider.
remove usernames in magic dns, normalisation of emails
this commit removes the option to have usernames as part of MagicDNS
domains and headscale will now align with Tailscale, where there is a
root domain, and the machine name.
In addition, the various normalisation functions for dns names has been
made lighter not caring about username and special character that wont
occur.
Email are no longer normalised as part of the policy processing.
untagle oidc and regcache, use typed cache
This commits stops reusing the registration cache for oidc
purposes and switches the cache to be types and not use any
allowing the removal of a bunch of casting.
try to make reauth/register branches clearer in oidc
Currently there was a function that did a bunch of stuff,
finding the machine key, trying to find the node, reauthing
the node, returning some status, and it was called validate
which was very confusing.
This commit tries to split this into what to do if the node
exists, if it needs to register etc.
Signed-off-by: Kristoffer Dalby <kristoffer@tailscale.com>
2024-10-02 08:50:17 -04:00
|
|
|
func (hsdb *HSDatabase) GetUserByName(name string) (*types.User, error) {
|
2024-02-08 11:28:19 -05:00
|
|
|
return Read(hsdb.DB, func(rx *gorm.DB) (*types.User, error) {
|
Redo OIDC configuration (#2020)
expand user, add claims to user
This commit expands the user table with additional fields that
can be retrieved from OIDC providers (and other places) and
uses this data in various tailscale response objects if it is
available.
This is the beginning of implementing
https://docs.google.com/document/d/1X85PMxIaVWDF6T_UPji3OeeUqVBcGj_uHRM5CI-AwlY/edit
trying to make OIDC more coherant and maintainable in addition
to giving the user a better experience and integration with a
provider.
remove usernames in magic dns, normalisation of emails
this commit removes the option to have usernames as part of MagicDNS
domains and headscale will now align with Tailscale, where there is a
root domain, and the machine name.
In addition, the various normalisation functions for dns names has been
made lighter not caring about username and special character that wont
occur.
Email are no longer normalised as part of the policy processing.
untagle oidc and regcache, use typed cache
This commits stops reusing the registration cache for oidc
purposes and switches the cache to be types and not use any
allowing the removal of a bunch of casting.
try to make reauth/register branches clearer in oidc
Currently there was a function that did a bunch of stuff,
finding the machine key, trying to find the node, reauthing
the node, returning some status, and it was called validate
which was very confusing.
This commit tries to split this into what to do if the node
exists, if it needs to register etc.
Signed-off-by: Kristoffer Dalby <kristoffer@tailscale.com>
2024-10-02 08:50:17 -04:00
|
|
|
return GetUserByUsername(rx, name)
|
2024-02-08 11:28:19 -05:00
|
|
|
})
|
2023-07-17 07:35:05 -04:00
|
|
|
}
|
|
|
|
|
Redo OIDC configuration (#2020)
expand user, add claims to user
This commit expands the user table with additional fields that
can be retrieved from OIDC providers (and other places) and
uses this data in various tailscale response objects if it is
available.
This is the beginning of implementing
https://docs.google.com/document/d/1X85PMxIaVWDF6T_UPji3OeeUqVBcGj_uHRM5CI-AwlY/edit
trying to make OIDC more coherant and maintainable in addition
to giving the user a better experience and integration with a
provider.
remove usernames in magic dns, normalisation of emails
this commit removes the option to have usernames as part of MagicDNS
domains and headscale will now align with Tailscale, where there is a
root domain, and the machine name.
In addition, the various normalisation functions for dns names has been
made lighter not caring about username and special character that wont
occur.
Email are no longer normalised as part of the policy processing.
untagle oidc and regcache, use typed cache
This commits stops reusing the registration cache for oidc
purposes and switches the cache to be types and not use any
allowing the removal of a bunch of casting.
try to make reauth/register branches clearer in oidc
Currently there was a function that did a bunch of stuff,
finding the machine key, trying to find the node, reauthing
the node, returning some status, and it was called validate
which was very confusing.
This commit tries to split this into what to do if the node
exists, if it needs to register etc.
Signed-off-by: Kristoffer Dalby <kristoffer@tailscale.com>
2024-10-02 08:50:17 -04:00
|
|
|
func GetUserByUsername(tx *gorm.DB, name string) (*types.User, error) {
|
2023-05-21 12:37:59 -04:00
|
|
|
user := types.User{}
|
2024-02-08 11:28:19 -05:00
|
|
|
if result := tx.First(&user, "name = ?", name); errors.Is(
|
2021-11-13 03:36:45 -05:00
|
|
|
result.Error,
|
|
|
|
gorm.ErrRecordNotFound,
|
|
|
|
) {
|
2023-01-17 11:43:44 -05:00
|
|
|
return nil, ErrUserNotFound
|
2021-02-27 18:58:09 -05:00
|
|
|
}
|
2021-11-14 10:46:09 -05:00
|
|
|
|
2023-01-17 11:43:44 -05:00
|
|
|
return &user, nil
|
2021-02-27 18:58:09 -05:00
|
|
|
}
|
|
|
|
|
Redo OIDC configuration (#2020)
expand user, add claims to user
This commit expands the user table with additional fields that
can be retrieved from OIDC providers (and other places) and
uses this data in various tailscale response objects if it is
available.
This is the beginning of implementing
https://docs.google.com/document/d/1X85PMxIaVWDF6T_UPji3OeeUqVBcGj_uHRM5CI-AwlY/edit
trying to make OIDC more coherant and maintainable in addition
to giving the user a better experience and integration with a
provider.
remove usernames in magic dns, normalisation of emails
this commit removes the option to have usernames as part of MagicDNS
domains and headscale will now align with Tailscale, where there is a
root domain, and the machine name.
In addition, the various normalisation functions for dns names has been
made lighter not caring about username and special character that wont
occur.
Email are no longer normalised as part of the policy processing.
untagle oidc and regcache, use typed cache
This commits stops reusing the registration cache for oidc
purposes and switches the cache to be types and not use any
allowing the removal of a bunch of casting.
try to make reauth/register branches clearer in oidc
Currently there was a function that did a bunch of stuff,
finding the machine key, trying to find the node, reauthing
the node, returning some status, and it was called validate
which was very confusing.
This commit tries to split this into what to do if the node
exists, if it needs to register etc.
Signed-off-by: Kristoffer Dalby <kristoffer@tailscale.com>
2024-10-02 08:50:17 -04:00
|
|
|
func (hsdb *HSDatabase) GetUserByID(id types.UserID) (*types.User, error) {
|
|
|
|
return Read(hsdb.DB, func(rx *gorm.DB) (*types.User, error) {
|
|
|
|
return GetUserByID(rx, id)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetUserByID(tx *gorm.DB, id types.UserID) (*types.User, error) {
|
|
|
|
user := types.User{}
|
|
|
|
if result := tx.First(&user, "id = ?", id); errors.Is(
|
|
|
|
result.Error,
|
|
|
|
gorm.ErrRecordNotFound,
|
|
|
|
) {
|
|
|
|
return nil, ErrUserNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
return &user, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (hsdb *HSDatabase) GetUserByOIDCIdentifier(id string) (*types.User, error) {
|
|
|
|
return Read(hsdb.DB, func(rx *gorm.DB) (*types.User, error) {
|
|
|
|
return GetUserByOIDCIdentifier(rx, id)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetUserByOIDCIdentifier(tx *gorm.DB, id string) (*types.User, error) {
|
|
|
|
user := types.User{}
|
|
|
|
if result := tx.First(&user, "provider_identifier = ?", id); errors.Is(
|
|
|
|
result.Error,
|
|
|
|
gorm.ErrRecordNotFound,
|
|
|
|
) {
|
|
|
|
return nil, ErrUserNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
return &user, nil
|
|
|
|
}
|
|
|
|
|
2023-05-21 12:37:59 -04:00
|
|
|
func (hsdb *HSDatabase) ListUsers() ([]types.User, error) {
|
2024-02-08 11:28:19 -05:00
|
|
|
return Read(hsdb.DB, func(rx *gorm.DB) ([]types.User, error) {
|
|
|
|
return ListUsers(rx)
|
|
|
|
})
|
2023-07-17 07:35:05 -04:00
|
|
|
}
|
|
|
|
|
2024-02-08 11:28:19 -05:00
|
|
|
// ListUsers gets all the existing users.
|
|
|
|
func ListUsers(tx *gorm.DB) ([]types.User, error) {
|
2023-05-21 12:37:59 -04:00
|
|
|
users := []types.User{}
|
2024-02-08 11:28:19 -05:00
|
|
|
if err := tx.Find(&users).Error; err != nil {
|
2021-02-27 18:58:09 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
2021-11-14 10:46:09 -05:00
|
|
|
|
2023-01-17 11:43:44 -05:00
|
|
|
return users, nil
|
2021-02-27 18:58:09 -05:00
|
|
|
}
|
|
|
|
|
2023-09-24 07:42:05 -04:00
|
|
|
// ListNodesByUser gets all the nodes in a given user.
|
2024-02-08 11:28:19 -05:00
|
|
|
func ListNodesByUser(tx *gorm.DB, name string) (types.Nodes, error) {
|
2023-05-21 12:37:59 -04:00
|
|
|
err := util.CheckForFQDNRules(name)
|
2022-02-22 06:45:50 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
Redo OIDC configuration (#2020)
expand user, add claims to user
This commit expands the user table with additional fields that
can be retrieved from OIDC providers (and other places) and
uses this data in various tailscale response objects if it is
available.
This is the beginning of implementing
https://docs.google.com/document/d/1X85PMxIaVWDF6T_UPji3OeeUqVBcGj_uHRM5CI-AwlY/edit
trying to make OIDC more coherant and maintainable in addition
to giving the user a better experience and integration with a
provider.
remove usernames in magic dns, normalisation of emails
this commit removes the option to have usernames as part of MagicDNS
domains and headscale will now align with Tailscale, where there is a
root domain, and the machine name.
In addition, the various normalisation functions for dns names has been
made lighter not caring about username and special character that wont
occur.
Email are no longer normalised as part of the policy processing.
untagle oidc and regcache, use typed cache
This commits stops reusing the registration cache for oidc
purposes and switches the cache to be types and not use any
allowing the removal of a bunch of casting.
try to make reauth/register branches clearer in oidc
Currently there was a function that did a bunch of stuff,
finding the machine key, trying to find the node, reauthing
the node, returning some status, and it was called validate
which was very confusing.
This commit tries to split this into what to do if the node
exists, if it needs to register etc.
Signed-off-by: Kristoffer Dalby <kristoffer@tailscale.com>
2024-10-02 08:50:17 -04:00
|
|
|
user, err := GetUserByUsername(tx, name)
|
2021-02-27 18:58:09 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-09-24 07:42:05 -04:00
|
|
|
nodes := types.Nodes{}
|
2024-02-08 11:28:19 -05:00
|
|
|
if err := tx.Preload("AuthKey").Preload("AuthKey.User").Preload("User").Where(&types.Node{UserID: user.ID}).Find(&nodes).Error; err != nil {
|
2021-02-27 18:58:09 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
2021-11-14 10:46:09 -05:00
|
|
|
|
2023-09-24 07:42:05 -04:00
|
|
|
return nodes, nil
|
2021-02-27 18:58:09 -05:00
|
|
|
}
|
|
|
|
|
2023-09-24 07:42:05 -04:00
|
|
|
func (hsdb *HSDatabase) AssignNodeToUser(node *types.Node, username string) error {
|
2024-02-08 11:28:19 -05:00
|
|
|
return hsdb.Write(func(tx *gorm.DB) error {
|
|
|
|
return AssignNodeToUser(tx, node, username)
|
|
|
|
})
|
|
|
|
}
|
2023-07-17 07:35:05 -04:00
|
|
|
|
2024-02-08 11:28:19 -05:00
|
|
|
// AssignNodeToUser assigns a Node to a user.
|
|
|
|
func AssignNodeToUser(tx *gorm.DB, node *types.Node, username string) error {
|
2023-05-21 12:37:59 -04:00
|
|
|
err := util.CheckForFQDNRules(username)
|
2022-02-22 06:45:50 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
Redo OIDC configuration (#2020)
expand user, add claims to user
This commit expands the user table with additional fields that
can be retrieved from OIDC providers (and other places) and
uses this data in various tailscale response objects if it is
available.
This is the beginning of implementing
https://docs.google.com/document/d/1X85PMxIaVWDF6T_UPji3OeeUqVBcGj_uHRM5CI-AwlY/edit
trying to make OIDC more coherant and maintainable in addition
to giving the user a better experience and integration with a
provider.
remove usernames in magic dns, normalisation of emails
this commit removes the option to have usernames as part of MagicDNS
domains and headscale will now align with Tailscale, where there is a
root domain, and the machine name.
In addition, the various normalisation functions for dns names has been
made lighter not caring about username and special character that wont
occur.
Email are no longer normalised as part of the policy processing.
untagle oidc and regcache, use typed cache
This commits stops reusing the registration cache for oidc
purposes and switches the cache to be types and not use any
allowing the removal of a bunch of casting.
try to make reauth/register branches clearer in oidc
Currently there was a function that did a bunch of stuff,
finding the machine key, trying to find the node, reauthing
the node, returning some status, and it was called validate
which was very confusing.
This commit tries to split this into what to do if the node
exists, if it needs to register etc.
Signed-off-by: Kristoffer Dalby <kristoffer@tailscale.com>
2024-10-02 08:50:17 -04:00
|
|
|
user, err := GetUserByUsername(tx, username)
|
2021-02-27 18:58:09 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-09-24 07:42:05 -04:00
|
|
|
node.User = *user
|
2024-02-08 11:28:19 -05:00
|
|
|
if result := tx.Save(&node); result.Error != nil {
|
2022-05-02 05:47:21 -04:00
|
|
|
return result.Error
|
|
|
|
}
|
2021-11-14 10:46:09 -05:00
|
|
|
|
2021-02-27 18:58:09 -05:00
|
|
|
return nil
|
|
|
|
}
|