refactored
This commit is contained in:
59
auth/auth.go
59
auth/auth.go
@@ -8,14 +8,14 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.acooldomain.co/server-manager/backend-kubernetes-go/db_handler/mongo"
|
||||
"git.acooldomain.co/server-manager/backend-kubernetes-go/dbhandler"
|
||||
"git.acooldomain.co/server-manager/backend-kubernetes-go/models"
|
||||
"git.acooldomain.co/server-manager/backend/dbhandler"
|
||||
"git.acooldomain.co/server-manager/backend/factories"
|
||||
"git.acooldomain.co/server-manager/backend/models"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/golang-jwt/jwt"
|
||||
)
|
||||
|
||||
type Connection struct {
|
||||
type AuthApi struct {
|
||||
config models.GlobalConfig
|
||||
|
||||
tokenHandler dbhandler.InviteTokenDbHandler
|
||||
@@ -35,7 +35,7 @@ type AuthClaims struct {
|
||||
Claims
|
||||
}
|
||||
|
||||
func (con *Connection) signToken(token Claims) (string, error) {
|
||||
func (con *AuthApi) signToken(token Claims) (string, error) {
|
||||
t := jwt.New(jwt.GetSigningMethod(con.config.Signing.Algorithm))
|
||||
|
||||
t.Claims = &AuthClaims{
|
||||
@@ -70,7 +70,7 @@ func AuthorizedTo(requiredPermissions models.Permission) gin.HandlerFunc {
|
||||
}
|
||||
}
|
||||
|
||||
func (con *Connection) LoggedIn(ctx *gin.Context) {
|
||||
func (con *AuthApi) LoggedIn(ctx *gin.Context) {
|
||||
authCookie, err := ctx.Request.Cookie("auth")
|
||||
if err != nil {
|
||||
ctx.AbortWithError(403, err)
|
||||
@@ -110,7 +110,7 @@ type SignUpRequest struct {
|
||||
Password string
|
||||
}
|
||||
|
||||
func (con Connection) signUp(ctx *gin.Context) {
|
||||
func (con AuthApi) signUp(ctx *gin.Context) {
|
||||
var request SignUpRequest
|
||||
|
||||
err := json.NewDecoder(ctx.Request.Body).Decode(&request)
|
||||
@@ -145,7 +145,7 @@ type SignInRequest struct {
|
||||
Password string
|
||||
}
|
||||
|
||||
func (con Connection) signIn(ctx *gin.Context) {
|
||||
func (con AuthApi) signIn(ctx *gin.Context) {
|
||||
|
||||
var request SignInRequest
|
||||
err := json.NewDecoder(ctx.Request.Body).Decode(&request)
|
||||
@@ -175,7 +175,7 @@ func (con Connection) signIn(ctx *gin.Context) {
|
||||
ctx.IndentedJSON(http.StatusOK, signedToken)
|
||||
}
|
||||
|
||||
func (con Connection) Verify(ctx *gin.Context) {
|
||||
func (con AuthApi) Verify(ctx *gin.Context) {
|
||||
claimsPointer, exists := ctx.Get("claims")
|
||||
if !exists {
|
||||
ctx.Status(403)
|
||||
@@ -215,39 +215,26 @@ func (con Connection) Verify(ctx *gin.Context) {
|
||||
|
||||
ctx.Redirect(303, fmt.Sprintf("http://%s/login", con.config.Domain))
|
||||
}
|
||||
func LoadGroup(group *gin.RouterGroup, client *mongo.Client, config models.GlobalConfig) {
|
||||
var userAuthHandler dbhandler.UserPassAuthanticationDbHandler
|
||||
var inviteHandler dbhandler.InviteTokenDbHandler
|
||||
var serverAuthHandler dbhandler.ServersAuthorizationDbHandler
|
||||
|
||||
var err error
|
||||
|
||||
if config.Authentication.UserPass.Type == models.MONGO {
|
||||
userAuthHandler, err = mongo.NewUserPassAuthHandler(*config.Authentication.UserPass.Mongo)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
func LoadGroup(group *gin.RouterGroup, config models.GlobalConfig) gin.HandlerFunc {
|
||||
userAuthHandler, err := factories.GetUserPassAuthDbHandler(config.Authentication.UserPass)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
if config.Authentication.UserPass.InviteTokenDatabase.Type == models.MONGO {
|
||||
inviteHandler, err = mongo.NewInviteTokenDbHandler(*config.Authentication.UserPass.Mongo)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
inviteHandler, err := factories.GetInviteTokenDbHandler(config.Authentication.UserPass.InviteTokenDatabase)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
mailClient = *mail.NewMailClient(config.Email)
|
||||
|
||||
connection := Connection{
|
||||
userPassAuthHandler: userAuthHandler,
|
||||
tokenHandler: inviteHandler,
|
||||
mailClient: mailClient,
|
||||
config: &config,
|
||||
connection := AuthApi{
|
||||
userAuthDbHandler: userAuthHandler,
|
||||
tokenHandler: inviteHandler,
|
||||
config: config,
|
||||
}
|
||||
|
||||
connection := Connection{DatabaseConnection: client}
|
||||
|
||||
group.POST("/signin", connection.signIn)
|
||||
group.POST("/signup", AuthorizedTo(models.Admin), connection.signUp)
|
||||
group.POST("/signup", connection.LoggedIn, AuthorizedTo(models.Admin), connection.signUp)
|
||||
group.Any("/verify", connection.Verify)
|
||||
|
||||
return connection.LoggedIn
|
||||
}
|
||||
|
@@ -1,4 +1,4 @@
|
||||
module git.acooldomain.co/server-manager/backend-kubernetes-go/auth
|
||||
module git.acooldomain.co/server-manager/backend/auth
|
||||
|
||||
go 1.22.0
|
||||
|
||||
|
Reference in New Issue
Block a user