fixed initial user
Some checks failed
Build and Push Docker Image / Build image (push) Has been cancelled
Some checks failed
Build and Push Docker Image / Build image (push) Has been cancelled
This commit is contained in:
parent
8747c308e5
commit
8ac960102a
@ -110,7 +110,6 @@ func (con AuthApi) signUp(ctx *gin.Context) {
|
||||
}
|
||||
|
||||
err = con.userAuthDbHandler.CreateUser(ctx, request.Username, request.Password, token.Permissions, token.Email, con.config.Users.DefaultMaxOwnedServers)
|
||||
|
||||
if err != nil {
|
||||
ctx.AbortWithError(500, err)
|
||||
return
|
||||
@ -125,10 +124,8 @@ type SignInRequest struct {
|
||||
}
|
||||
|
||||
func (con AuthApi) signIn(ctx *gin.Context) {
|
||||
|
||||
var request SignInRequest
|
||||
err := json.NewDecoder(ctx.Request.Body).Decode(&request)
|
||||
|
||||
if err != nil {
|
||||
ctx.AbortWithError(500, err)
|
||||
return
|
||||
@ -159,13 +156,13 @@ func (con AuthApi) Verify(ctx *gin.Context) {
|
||||
claimsPointer, exists := ctx.Get("claims")
|
||||
if !exists {
|
||||
ctx.Status(403)
|
||||
ctx.Error(errors.New("Failed to get claims, not logged in"))
|
||||
ctx.Error(errors.New("failed to get claims, not logged in"))
|
||||
return
|
||||
}
|
||||
|
||||
claims, ok := claimsPointer.(*AuthClaims)
|
||||
if !ok {
|
||||
ctx.Error(errors.New("Failed to convert claims"))
|
||||
ctx.Error(errors.New("failed to convert claims"))
|
||||
ctx.Status(500)
|
||||
return
|
||||
}
|
||||
|
@ -1,11 +1,8 @@
|
||||
package factories
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"log"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"git.acooldomain.co/server-manager/backend/dbhandler"
|
||||
"git.acooldomain.co/server-manager/backend/dbhandler/mongo"
|
||||
@ -136,21 +133,6 @@ func GetUserPassAuthDbHandler(config models.UserPassAuthConfig) (dbhandler.UserP
|
||||
}
|
||||
|
||||
userPassAuthDbHandlers[key] = handler
|
||||
|
||||
ctx, cancel := context.WithTimeoutCause(context.Background(), 5*time.Second, errors.New("Timeout"))
|
||||
defer cancel()
|
||||
if config.InitialUser == nil {
|
||||
return handler, nil
|
||||
}
|
||||
|
||||
count, _ := handler.CountUsers(ctx)
|
||||
if count == 0 {
|
||||
log.Printf("Trying to create user %#v\n", config.InitialUser)
|
||||
err := handler.CreateUser(ctx, config.InitialUser.Username, config.InitialUser.Password, models.Admin, config.InitialUser.Email, 10)
|
||||
if err != nil {
|
||||
log.Printf("Failed to create initial user %e\n", err)
|
||||
}
|
||||
}
|
||||
return handler, nil
|
||||
}
|
||||
|
||||
|
3
main.go
3
main.go
@ -1,7 +1,6 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"git.acooldomain.co/server-manager/backend/auth"
|
||||
@ -21,7 +20,7 @@ func main() {
|
||||
cors_config := cors.DefaultConfig()
|
||||
cors_config.AllowCredentials = true
|
||||
cors_config.ExposeHeaders = []string{"set-cookie"}
|
||||
file, err := os.Open(fmt.Sprintf("%s", os.Getenv(CONFIG_PATH)))
|
||||
file, err := os.Open(os.Getenv(CONFIG_PATH))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
@ -1,9 +1,7 @@
|
||||
package models
|
||||
|
||||
type InitialUserConfig struct {
|
||||
Email string `yaml:"email"`
|
||||
Username string `yaml:"username"`
|
||||
Password string `yaml:"password"`
|
||||
Email string `yaml:"email"`
|
||||
}
|
||||
|
||||
type EmailConfig struct {
|
||||
|
@ -1,8 +1,12 @@
|
||||
package users
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"log"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"git.acooldomain.co/server-manager/backend/auth"
|
||||
"git.acooldomain.co/server-manager/backend/dbhandler"
|
||||
@ -28,7 +32,6 @@ type UserResponse struct {
|
||||
|
||||
func (con UsersApi) GetUsers(ctx *gin.Context) {
|
||||
users, err := con.userPassAuthHandler.ListUsers(ctx)
|
||||
|
||||
if err != nil {
|
||||
ctx.AbortWithError(500, err)
|
||||
return
|
||||
@ -65,18 +68,24 @@ type InviteUser struct {
|
||||
Permissions models.Permission `json:"Permissions"`
|
||||
}
|
||||
|
||||
func (con *UsersApi) inviteUser(ctx context.Context, email string, permissions models.Permission) error {
|
||||
token, err := con.tokenHandler.SaveInviteToken(ctx, email, permissions)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = con.mailClient.SendMail(email, "You've been invited to join", "please open this link https://games.acooldomain.co/signup?token="+token)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (con UsersApi) InviteUser(ctx *gin.Context) {
|
||||
var request InviteUser
|
||||
json.NewDecoder(ctx.Request.Body).Decode(&request)
|
||||
|
||||
token, err := con.tokenHandler.SaveInviteToken(ctx, request.Email, request.Permissions)
|
||||
|
||||
if err != nil {
|
||||
ctx.AbortWithError(500, err)
|
||||
return
|
||||
}
|
||||
|
||||
err = con.mailClient.SendMail(request.Email, "You've been invited to join", "please open this link https://games.acooldomain.co/signup?token="+token)
|
||||
err := con.inviteUser(ctx, request.Email, request.Permissions)
|
||||
if err != nil {
|
||||
ctx.AbortWithError(500, err)
|
||||
return
|
||||
@ -107,7 +116,6 @@ func (con UsersApi) DeleteUser(ctx *gin.Context) {
|
||||
username := ctx.Param("user_id")
|
||||
|
||||
err := con.userPassAuthHandler.RemoveUser(ctx, username)
|
||||
|
||||
if err != nil {
|
||||
ctx.AbortWithError(500, err)
|
||||
return
|
||||
@ -123,6 +131,9 @@ func LoadGroup(group *gin.RouterGroup, config models.GlobalConfig) {
|
||||
}
|
||||
|
||||
inviteHandler, err := factories.GetInviteTokenDbHandler(config.Authentication.UserPass.InviteTokenDatabase)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
mailClient := *mail.NewMailClient(config.Email)
|
||||
|
||||
@ -133,6 +144,20 @@ func LoadGroup(group *gin.RouterGroup, config models.GlobalConfig) {
|
||||
config: &config,
|
||||
}
|
||||
|
||||
if config.Authentication.Type == models.UserPass && config.Authentication.UserPass.InitialUser != nil {
|
||||
ctx, cancel := context.WithTimeoutCause(context.Background(), 5*time.Second, errors.New("Timeout"))
|
||||
defer cancel()
|
||||
|
||||
count, _ := connection.userPassAuthHandler.CountUsers(ctx)
|
||||
if count == 0 {
|
||||
log.Printf("Trying to create user %#v\n", config.Authentication.UserPass.InitialUser)
|
||||
err := connection.inviteUser(ctx, config.Authentication.UserPass.InitialUser.Email, models.Admin)
|
||||
if err != nil {
|
||||
log.Printf("Failed to create initial user %e\n", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
group.GET("", auth.AuthorizedTo(0), auth.AuthorizationEnforcer(), connection.GetUsers)
|
||||
group.GET("/@me", auth.AuthorizedTo(0), auth.AuthorizationEnforcer(), connection.GetUser)
|
||||
group.POST("", auth.AuthorizedTo(models.Admin), auth.AuthorizationEnforcer(), connection.InviteUser)
|
||||
|
Loading…
x
Reference in New Issue
Block a user