mirror of
https://github.com/hexolan/panels.git
synced 2026-03-26 12:40:21 +00:00
init gateway-service
This commit is contained in:
70
services/gateway-service/internal/api/v1/auth.go
Normal file
70
services/gateway-service/internal/api/v1/auth.go
Normal file
@@ -0,0 +1,70 @@
|
||||
package v1
|
||||
|
||||
import (
|
||||
"time"
|
||||
"context"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
|
||||
"github.com/hexolan/panels/gateway-service/internal/rpc"
|
||||
"github.com/hexolan/panels/gateway-service/internal/rpc/authv1"
|
||||
)
|
||||
|
||||
type userLoginForm struct {
|
||||
Username string
|
||||
Password string
|
||||
}
|
||||
|
||||
func setAuthMethod(userId string, password string) error {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
defer cancel()
|
||||
_, err := rpc.Svcs.GetAuthSvc().SetPasswordAuth(
|
||||
ctx,
|
||||
&authv1.SetPasswordAuthMethod{
|
||||
UserId: userId,
|
||||
Password: password,
|
||||
},
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
||||
func authWithPassword(userId string, password string) (*authv1.AuthToken, error) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
defer cancel()
|
||||
token, err := rpc.Svcs.GetAuthSvc().AuthWithPassword(
|
||||
ctx,
|
||||
&authv1.PasswordAuthRequest{
|
||||
UserId: userId,
|
||||
Password: password,
|
||||
},
|
||||
)
|
||||
return token, err
|
||||
}
|
||||
|
||||
func LoginWithPassword(c *fiber.Ctx) error {
|
||||
// Parse the body data
|
||||
form := new(userLoginForm)
|
||||
if err := c.BodyParser(form); err != nil {
|
||||
fiber.NewError(fiber.StatusBadRequest, "malformed request")
|
||||
}
|
||||
|
||||
// username -> user ID
|
||||
user, err := getUserByUsername(form.Username)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// attempt to auth
|
||||
token, err := authWithPassword(user.Id, form.Password)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return c.JSON(fiber.Map{
|
||||
"status": "success",
|
||||
"data": fiber.Map{
|
||||
"user": user,
|
||||
"token": token,
|
||||
},
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user