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)
|
err = con.userAuthDbHandler.CreateUser(ctx, request.Username, request.Password, token.Permissions, token.Email, con.config.Users.DefaultMaxOwnedServers)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctx.AbortWithError(500, err)
|
ctx.AbortWithError(500, err)
|
||||||
return
|
return
|
||||||
@ -125,10 +124,8 @@ type SignInRequest struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (con AuthApi) signIn(ctx *gin.Context) {
|
func (con AuthApi) signIn(ctx *gin.Context) {
|
||||||
|
|
||||||
var request SignInRequest
|
var request SignInRequest
|
||||||
err := json.NewDecoder(ctx.Request.Body).Decode(&request)
|
err := json.NewDecoder(ctx.Request.Body).Decode(&request)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctx.AbortWithError(500, err)
|
ctx.AbortWithError(500, err)
|
||||||
return
|
return
|
||||||
@ -159,13 +156,13 @@ func (con AuthApi) Verify(ctx *gin.Context) {
|
|||||||
claimsPointer, exists := ctx.Get("claims")
|
claimsPointer, exists := ctx.Get("claims")
|
||||||
if !exists {
|
if !exists {
|
||||||
ctx.Status(403)
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
claims, ok := claimsPointer.(*AuthClaims)
|
claims, ok := claimsPointer.(*AuthClaims)
|
||||||
if !ok {
|
if !ok {
|
||||||
ctx.Error(errors.New("Failed to convert claims"))
|
ctx.Error(errors.New("failed to convert claims"))
|
||||||
ctx.Status(500)
|
ctx.Status(500)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -1,11 +1,8 @@
|
|||||||
package factories
|
package factories
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"errors"
|
"errors"
|
||||||
"log"
|
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
|
||||||
|
|
||||||
"git.acooldomain.co/server-manager/backend/dbhandler"
|
"git.acooldomain.co/server-manager/backend/dbhandler"
|
||||||
"git.acooldomain.co/server-manager/backend/dbhandler/mongo"
|
"git.acooldomain.co/server-manager/backend/dbhandler/mongo"
|
||||||
@ -136,21 +133,6 @@ func GetUserPassAuthDbHandler(config models.UserPassAuthConfig) (dbhandler.UserP
|
|||||||
}
|
}
|
||||||
|
|
||||||
userPassAuthDbHandlers[key] = handler
|
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
|
return handler, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
3
main.go
3
main.go
@ -1,7 +1,6 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"git.acooldomain.co/server-manager/backend/auth"
|
"git.acooldomain.co/server-manager/backend/auth"
|
||||||
@ -21,7 +20,7 @@ func main() {
|
|||||||
cors_config := cors.DefaultConfig()
|
cors_config := cors.DefaultConfig()
|
||||||
cors_config.AllowCredentials = true
|
cors_config.AllowCredentials = true
|
||||||
cors_config.ExposeHeaders = []string{"set-cookie"}
|
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 {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
@ -2,8 +2,6 @@ package models
|
|||||||
|
|
||||||
type InitialUserConfig struct {
|
type InitialUserConfig struct {
|
||||||
Email string `yaml:"email"`
|
Email string `yaml:"email"`
|
||||||
Username string `yaml:"username"`
|
|
||||||
Password string `yaml:"password"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type EmailConfig struct {
|
type EmailConfig struct {
|
||||||
|
@ -1,8 +1,12 @@
|
|||||||
package users
|
package users
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"time"
|
||||||
|
|
||||||
"git.acooldomain.co/server-manager/backend/auth"
|
"git.acooldomain.co/server-manager/backend/auth"
|
||||||
"git.acooldomain.co/server-manager/backend/dbhandler"
|
"git.acooldomain.co/server-manager/backend/dbhandler"
|
||||||
@ -28,7 +32,6 @@ type UserResponse struct {
|
|||||||
|
|
||||||
func (con UsersApi) GetUsers(ctx *gin.Context) {
|
func (con UsersApi) GetUsers(ctx *gin.Context) {
|
||||||
users, err := con.userPassAuthHandler.ListUsers(ctx)
|
users, err := con.userPassAuthHandler.ListUsers(ctx)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctx.AbortWithError(500, err)
|
ctx.AbortWithError(500, err)
|
||||||
return
|
return
|
||||||
@ -65,18 +68,24 @@ type InviteUser struct {
|
|||||||
Permissions models.Permission `json:"Permissions"`
|
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) {
|
func (con UsersApi) InviteUser(ctx *gin.Context) {
|
||||||
var request InviteUser
|
var request InviteUser
|
||||||
json.NewDecoder(ctx.Request.Body).Decode(&request)
|
json.NewDecoder(ctx.Request.Body).Decode(&request)
|
||||||
|
err := con.inviteUser(ctx, request.Email, request.Permissions)
|
||||||
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)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctx.AbortWithError(500, err)
|
ctx.AbortWithError(500, err)
|
||||||
return
|
return
|
||||||
@ -107,7 +116,6 @@ func (con UsersApi) DeleteUser(ctx *gin.Context) {
|
|||||||
username := ctx.Param("user_id")
|
username := ctx.Param("user_id")
|
||||||
|
|
||||||
err := con.userPassAuthHandler.RemoveUser(ctx, username)
|
err := con.userPassAuthHandler.RemoveUser(ctx, username)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctx.AbortWithError(500, err)
|
ctx.AbortWithError(500, err)
|
||||||
return
|
return
|
||||||
@ -123,6 +131,9 @@ func LoadGroup(group *gin.RouterGroup, config models.GlobalConfig) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
inviteHandler, err := factories.GetInviteTokenDbHandler(config.Authentication.UserPass.InviteTokenDatabase)
|
inviteHandler, err := factories.GetInviteTokenDbHandler(config.Authentication.UserPass.InviteTokenDatabase)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
mailClient := *mail.NewMailClient(config.Email)
|
mailClient := *mail.NewMailClient(config.Email)
|
||||||
|
|
||||||
@ -133,6 +144,20 @@ func LoadGroup(group *gin.RouterGroup, config models.GlobalConfig) {
|
|||||||
config: &config,
|
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("", auth.AuthorizedTo(0), auth.AuthorizationEnforcer(), connection.GetUsers)
|
||||||
group.GET("/@me", auth.AuthorizedTo(0), auth.AuthorizationEnforcer(), connection.GetUser)
|
group.GET("/@me", auth.AuthorizedTo(0), auth.AuthorizationEnforcer(), connection.GetUser)
|
||||||
group.POST("", auth.AuthorizedTo(models.Admin), auth.AuthorizationEnforcer(), connection.InviteUser)
|
group.POST("", auth.AuthorizedTo(models.Admin), auth.AuthorizationEnforcer(), connection.InviteUser)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user