portal/controller/auth.go

113 lines
2.8 KiB
Go
Raw Permalink Normal View History

package controller
2023-04-29 17:38:21 +00:00
import (
"git.lumeweb.com/LumeWeb/portal/controller/request"
"git.lumeweb.com/LumeWeb/portal/controller/response"
2023-06-15 05:26:36 +00:00
"git.lumeweb.com/LumeWeb/portal/middleware"
"git.lumeweb.com/LumeWeb/portal/service/auth"
2023-04-29 17:38:21 +00:00
"github.com/kataras/iris/v12"
)
type AuthController struct {
Controller
2023-04-29 17:38:21 +00:00
}
// PostLogin handles the POST /api/auth/login request to authenticate a user and return a JWT token.
func (a *AuthController) PostLogin() {
ri, success := tryParseRequest(request.LoginRequest{}, a.Ctx)
if !success {
2023-04-29 17:38:21 +00:00
return
}
r, _ := ri.(*request.LoginRequest)
token, err := auth.LoginWithPassword(r.Email, r.Password)
2023-04-29 17:38:21 +00:00
if err != nil {
if err == auth.ErrFailedGenerateToken {
a.Ctx.StopWithError(iris.StatusInternalServerError, err)
} else {
a.Ctx.StopWithError(iris.StatusUnauthorized, err)
}
2023-04-29 17:38:21 +00:00
return
}
a.respondJSON(&response.LoginResponse{Token: token})
2023-04-29 17:38:21 +00:00
}
2023-04-30 06:46:11 +00:00
// PostChallenge handles the POST /api/auth/pubkey/challenge request to generate a challenge for a user's public key.
func (a *AuthController) PostPubkeyChallenge() {
ri, success := tryParseRequest(request.PubkeyChallengeRequest{}, a.Ctx)
if !success {
2023-04-29 17:38:21 +00:00
return
}
r, _ := (ri).(*request.PubkeyChallengeRequest)
challenge, err := auth.GeneratePubkeyChallenge(r.Pubkey)
2023-04-29 17:38:21 +00:00
if err != nil {
if err == auth.ErrFailedGenerateKeyChallenge {
a.Ctx.StopWithError(iris.StatusInternalServerError, err)
} else {
a.Ctx.StopWithError(iris.StatusUnauthorized, err)
}
2023-04-29 17:38:21 +00:00
return
}
a.respondJSON(&response.ChallengeResponse{Challenge: challenge})
2023-04-29 17:38:21 +00:00
}
2023-04-30 06:46:11 +00:00
// PostKeyLogin handles the POST /api/auth/pubkey/login request to authenticate a user using a public key challenge and return a JWT token.
func (a *AuthController) PostPubkeyLogin() {
ri, success := tryParseRequest(request.PubkeyLoginRequest{}, a.Ctx)
if !success {
2023-04-29 17:38:21 +00:00
return
}
r, _ := ri.(*request.PubkeyLoginRequest)
token, err := auth.LoginWithPubkey(r.Pubkey, r.Challenge, r.Signature)
2023-04-29 17:38:21 +00:00
if err != nil {
if err == auth.ErrFailedGenerateKeyChallenge || err == auth.ErrFailedGenerateToken || err == auth.ErrFailedSaveToken {
a.Ctx.StopWithError(iris.StatusInternalServerError, err)
} else {
a.Ctx.StopWithError(iris.StatusUnauthorized, err)
}
2023-04-29 17:38:21 +00:00
return
}
a.respondJSON(&response.LoginResponse{Token: token})
2023-04-29 17:38:21 +00:00
}
// PostLogout handles the POST /api/auth/logout request to invalidate a JWT token.
func (a *AuthController) PostLogout() {
ri, success := tryParseRequest(request.LogoutRequest{}, a.Ctx)
if !success {
2023-04-29 17:38:21 +00:00
return
}
r, _ := ri.(*request.LogoutRequest)
err := auth.Logout(r.Token)
2023-04-29 17:38:21 +00:00
if err != nil {
a.Ctx.StopWithError(iris.StatusBadRequest, err)
2023-05-19 13:04:47 +00:00
return
2023-04-29 17:38:21 +00:00
}
// Return a success response to the client.
a.Ctx.StatusCode(iris.StatusNoContent)
2023-04-29 17:38:21 +00:00
}
2023-06-15 05:26:36 +00:00
func (a *AuthController) GetStatus() {
middleware.VerifyJwt(a.Ctx)
if a.Ctx.IsStopped() {
return
}
a.respondJSON(&response.AuthStatusResponse{Status: true})
}