2023-05-10 11:07:56 +00:00
|
|
|
package controller
|
2023-04-29 17:38:21 +00:00
|
|
|
|
|
|
|
import (
|
2023-06-07 17:04:38 +00:00
|
|
|
"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"
|
2023-06-09 08:03:29 +00:00
|
|
|
"git.lumeweb.com/LumeWeb/portal/service/auth"
|
2023-04-29 17:38:21 +00:00
|
|
|
"github.com/kataras/iris/v12"
|
|
|
|
)
|
|
|
|
|
2023-05-10 11:07:56 +00:00
|
|
|
type AuthController struct {
|
2023-06-09 08:03:29 +00:00
|
|
|
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.
|
2023-05-10 11:07:56 +00:00
|
|
|
func (a *AuthController) PostLogin() {
|
2023-06-07 17:04:38 +00:00
|
|
|
ri, success := tryParseRequest(request.LoginRequest{}, a.Ctx)
|
|
|
|
if !success {
|
2023-04-29 17:38:21 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-06-07 17:04:38 +00:00
|
|
|
r, _ := ri.(*request.LoginRequest)
|
|
|
|
|
2023-06-09 08:03:29 +00:00
|
|
|
token, err := auth.LoginWithPassword(r.Email, r.Password)
|
2023-04-29 17:38:21 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2023-06-09 08:03:29 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-06-09 08:03:29 +00:00
|
|
|
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.
|
2023-05-10 11:07:56 +00:00
|
|
|
func (a *AuthController) PostPubkeyChallenge() {
|
2023-06-07 17:04:38 +00:00
|
|
|
ri, success := tryParseRequest(request.PubkeyChallengeRequest{}, a.Ctx)
|
|
|
|
if !success {
|
2023-04-29 17:38:21 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-06-07 17:04:38 +00:00
|
|
|
r, _ := (ri).(*request.PubkeyChallengeRequest)
|
|
|
|
|
2023-06-09 08:03:29 +00:00
|
|
|
challenge, err := auth.GeneratePubkeyChallenge(r.Pubkey)
|
2023-04-29 17:38:21 +00:00
|
|
|
if err != nil {
|
2023-06-09 08:03:29 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-06-09 08:03:29 +00:00
|
|
|
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.
|
2023-05-10 11:07:56 +00:00
|
|
|
func (a *AuthController) PostPubkeyLogin() {
|
2023-06-07 17:04:38 +00:00
|
|
|
ri, success := tryParseRequest(request.PubkeyLoginRequest{}, a.Ctx)
|
|
|
|
if !success {
|
2023-04-29 17:38:21 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-06-07 17:04:38 +00:00
|
|
|
r, _ := ri.(*request.PubkeyLoginRequest)
|
|
|
|
|
2023-06-09 08:03:29 +00:00
|
|
|
token, err := auth.LoginWithPubkey(r.Pubkey, r.Challenge, r.Signature)
|
2023-04-29 17:38:21 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2023-06-09 08:03:29 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-06-09 08:03:29 +00:00
|
|
|
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.
|
2023-05-10 11:07:56 +00:00
|
|
|
func (a *AuthController) PostLogout() {
|
2023-06-07 17:04:38 +00:00
|
|
|
ri, success := tryParseRequest(request.LogoutRequest{}, a.Ctx)
|
|
|
|
if !success {
|
2023-04-29 17:38:21 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-06-07 17:04:38 +00:00
|
|
|
r, _ := ri.(*request.LogoutRequest)
|
|
|
|
|
2023-06-09 08:03:29 +00:00
|
|
|
err := auth.Logout(r.Token)
|
2023-04-29 17:38:21 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2023-06-09 08:03:29 +00:00
|
|
|
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.
|
2023-04-30 08:49:19 +00:00
|
|
|
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})
|
|
|
|
}
|