[server][auth] Auto create accounts from certain domains

This commit is contained in:
Abhishek Shroff
2025-06-26 11:48:00 +05:30
parent 38b39dbc03
commit fd42fbfbea
4 changed files with 36 additions and 6 deletions
+15 -3
View File
@@ -5,6 +5,7 @@ import (
"encoding/base64"
"errors"
"net/http"
"strings"
"time"
"codeberg.org/shroff/phylum/server/internal/auth/crypt"
@@ -53,13 +54,24 @@ func Init(cfg Config, log zerolog.Logger) error {
return nil
}
func VerifyUserPassword(db db.Handler, email, password string) (core.User, error) {
if b, err := passwordBackend.VerifyUserPassword(db, email, password); err != nil {
func VerifyUserPassword(d db.Handler, email, password string) (core.User, error) {
if b, err := passwordBackend.VerifyUserPassword(d, email, password); err != nil {
return core.User{}, err
} else if !b {
return core.User{}, ErrCredentialsInvalid
}
return core.UserByEmail(db, email)
user, err := core.UserByEmail(d, email)
if errors.Is(err, core.ErrUserNotFound) {
for _, domain := range cfg.AutoCreateDomains {
if strings.HasSuffix(strings.ToLower(email), "@"+strings.ToLower(domain)) {
err = d.RunInTx(func(db db.TxHandler) error {
user, err = core.CreateUser(db, email, "", false)
return err
})
}
}
}
return user, err
}
func CreateAccessToken(db db.TxHandler, email, password string) (core.User, string, error) {
+2 -1
View File
@@ -6,7 +6,8 @@ import (
)
type Config struct {
Password PasswordConfig `koanf:"password"`
AutoCreateDomains []string `koanf:"auto_create_domains"`
Password PasswordConfig `koanf:"password"`
}
type PasswordConfig struct {
+17
View File
@@ -4,6 +4,7 @@ import (
"context"
"embed"
"errors"
"fmt"
"os"
"path"
"strings"
@@ -150,6 +151,7 @@ func SetupCommand() {
fs.SetupCommand(),
user.SetupCommand(),
serve.SetupCommand(),
createLDAPTestCommand(),
)
cmd.AddGroup(&cobra.Group{ID: "misc", Title: "Misc"})
cmd.SetHelpCommandGroupID("misc")
@@ -157,6 +159,21 @@ func SetupCommand() {
cmd.Execute()
}
func createLDAPTestCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "ldap",
}
cmd.Run = func(c *cobra.Command, args []string) {
if u, err := auth.VerifyUserPassword(db.Get(context.Background()), "ldaptest@kudosoft.net", "asdfasdf"); err != nil {
fmt.Println(err.Error())
} else {
fmt.Printf("%+v\n", u)
}
}
return cmd
}
func isCmd(cmd *cobra.Command, s string) bool {
for c := cmd; c != nil; c = c.Parent() {
if cmd.Name() == s {
+2 -2
View File
@@ -17,8 +17,8 @@ core:
permission: 0x10 # Invite users
auth:
auto_create:
- "@example.com"
auto_create_domains:
- example.com
password:
backend: crypt
crypt: