115 lines
3.2 KiB
Go
115 lines
3.2 KiB
Go
package handler
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"git.logidex.ru/fakz9/logidex-id/internal/hydra_client"
|
|
"git.logidex.ru/fakz9/logidex-id/internal/redis"
|
|
"github.com/gofiber/fiber/v2"
|
|
hydraApi "github.com/ory/hydra-client-go"
|
|
)
|
|
|
|
type AuthHandler struct{}
|
|
|
|
func (a AuthHandler) PostAuthConsentAccept(ctx context.Context, request PostAuthConsentAcceptRequestObject) (PostAuthConsentAcceptResponseObject, error) {
|
|
hydraClient := hydra_client.GetClient()
|
|
|
|
hydraRequest := hydraApi.AcceptConsentRequest{}
|
|
hydraRequest.SetGrantScope([]string{"openid"})
|
|
hydraRequest.SetRemember(true)
|
|
hydraRequest.SetRememberFor(3600) // 1 hour
|
|
|
|
hydraResponse, r, err := hydraClient.AdminApi.
|
|
AcceptConsentRequest(ctx).
|
|
ConsentChallenge(request.Body.ConsentChallenge).
|
|
AcceptConsentRequest(hydraRequest).
|
|
Execute()
|
|
if err != nil {
|
|
return PostAuthConsentAccept400JSONResponse{
|
|
RedirectUrl: "",
|
|
Ok: false,
|
|
Message: "Failed to accept consent request",
|
|
}, nil
|
|
}
|
|
fmt.Println(r)
|
|
return PostAuthConsentAccept200JSONResponse{
|
|
RedirectUrl: hydraResponse.RedirectTo,
|
|
Ok: true,
|
|
Message: "Успешно",
|
|
}, nil
|
|
}
|
|
|
|
func (a AuthHandler) PostAuthOtpRequest(ctx context.Context, request PostAuthOtpRequestRequestObject) (PostAuthOtpRequestResponseObject, error) {
|
|
redisClient := redis.GetClient()
|
|
|
|
// TODO implement OTP request logic
|
|
|
|
err := redisClient.Do(ctx, redisClient.B().Set().Key("otp:"+request.Body.PhoneNumber).Value("123456").Build()).Error()
|
|
if err != nil {
|
|
return PostAuthOtpRequest400JSONResponse{
|
|
Message: "Failed to set OTP in Redis",
|
|
Ok: false,
|
|
}, nil
|
|
}
|
|
|
|
return PostAuthOtpRequest200JSONResponse{
|
|
Message: "Код успешно отправлен",
|
|
Ok: true,
|
|
}, nil
|
|
}
|
|
|
|
func (a AuthHandler) PostAuthOtpVerify(ctx context.Context, request PostAuthOtpVerifyRequestObject) (PostAuthOtpVerifyResponseObject, error) {
|
|
redisClient := redis.GetClient()
|
|
hydraClient := hydra_client.GetClient()
|
|
|
|
sentOtp, err := redisClient.Do(ctx, redisClient.B().Get().Key("otp:"+request.Body.PhoneNumber).Build()).ToString()
|
|
if err != nil {
|
|
return PostAuthOtpVerify400JSONResponse{
|
|
RedirectUrl: "",
|
|
Ok: false,
|
|
}, nil
|
|
}
|
|
if sentOtp != request.Body.Otp {
|
|
return PostAuthOtpVerify400JSONResponse{
|
|
RedirectUrl: "",
|
|
Ok: false,
|
|
}, nil
|
|
}
|
|
hydraRequest := hydraApi.AcceptLoginRequest{}
|
|
// TODO read user from database by phone number
|
|
|
|
hydraRequest.SetSubject("some-user-id") // Replace with actual user ID
|
|
hydraRequest.SetRemember(true)
|
|
hydraRequest.SetRememberFor(3600) // 1 hour
|
|
|
|
hydraResponse, r, err := hydraClient.AdminApi.
|
|
AcceptLoginRequest(ctx).
|
|
LoginChallenge(request.Body.LoginChallenge).
|
|
AcceptLoginRequest(hydraRequest).
|
|
Execute()
|
|
fmt.Println(r)
|
|
if err != nil {
|
|
return PostAuthOtpVerify400JSONResponse{
|
|
RedirectUrl: "",
|
|
Ok: false,
|
|
}, nil
|
|
}
|
|
return PostAuthOtpVerify200JSONResponse{
|
|
RedirectUrl: hydraResponse.RedirectTo,
|
|
Ok: true,
|
|
}, nil
|
|
}
|
|
|
|
var _ StrictServerInterface = (*AuthHandler)(nil)
|
|
|
|
func NewAuthHandler() *AuthHandler {
|
|
return &AuthHandler{}
|
|
}
|
|
|
|
func RegisterApp(router fiber.Router) {
|
|
//authGroup := router.Group("/auth")
|
|
server := NewStrictHandler(NewAuthHandler(), nil)
|
|
RegisterHandlers(router, server)
|
|
|
|
}
|