fix: ctx needs to be public in AuthService
This commit is contained in:
parent
699e4244e0
commit
a3cfebab30
|
@ -23,7 +23,7 @@ func init() {
|
||||||
}
|
}
|
||||||
|
|
||||||
type AuthService struct {
|
type AuthService struct {
|
||||||
ctx iris.Context
|
Ctx iris.Context
|
||||||
}
|
}
|
||||||
|
|
||||||
type LoginRequest struct {
|
type LoginRequest struct {
|
||||||
|
@ -136,33 +136,33 @@ func (a *AuthService) PostLogin() {
|
||||||
var r LoginRequest
|
var r LoginRequest
|
||||||
|
|
||||||
// Read the login request from the client.
|
// Read the login request from the client.
|
||||||
if err := a.ctx.ReadJSON(&r); err != nil {
|
if err := a.Ctx.ReadJSON(&r); err != nil {
|
||||||
a.ctx.StopWithError(iris.StatusBadRequest, err)
|
a.Ctx.StopWithError(iris.StatusBadRequest, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Retrieve the account for the given email.
|
// Retrieve the account for the given email.
|
||||||
account := model.Account{}
|
account := model.Account{}
|
||||||
if err := db.Get().Where("email = ?", r.Email).First(&account).Error; err != nil {
|
if err := db.Get().Where("email = ?", r.Email).First(&account).Error; err != nil {
|
||||||
a.ctx.StopWithError(iris.StatusBadRequest, errors.New("invalid email or password"))
|
a.Ctx.StopWithError(iris.StatusBadRequest, errors.New("invalid email or password"))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Verify the provided password against the hashed password stored in the database.
|
// Verify the provided password against the hashed password stored in the database.
|
||||||
if err := verifyPassword(*account.Password, r.Password); err != nil {
|
if err := verifyPassword(*account.Password, r.Password); err != nil {
|
||||||
a.ctx.StopWithError(iris.StatusBadRequest, errors.New("invalid email or password"))
|
a.Ctx.StopWithError(iris.StatusBadRequest, errors.New("invalid email or password"))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Generate a JWT token for the authenticated user.
|
// Generate a JWT token for the authenticated user.
|
||||||
token, err := generateAndSaveLoginToken(account.ID, 24*time.Hour)
|
token, err := generateAndSaveLoginToken(account.ID, 24*time.Hour)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
a.ctx.StopWithError(iris.StatusInternalServerError, fmt.Errorf("failed to generate token: %s", err))
|
a.Ctx.StopWithError(iris.StatusInternalServerError, fmt.Errorf("failed to generate token: %s", err))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return the JWT token to the client.
|
// Return the JWT token to the client.
|
||||||
err = a.ctx.JSON(&LoginResponse{Token: token})
|
err = a.Ctx.JSON(&LoginResponse{Token: token})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(fmt.Errorf("Error with login attempt: %s \n", err))
|
panic(fmt.Errorf("Error with login attempt: %s \n", err))
|
||||||
}
|
}
|
||||||
|
@ -173,27 +173,27 @@ func (a *AuthService) PostPubkeyChallenge() {
|
||||||
var r LoginRequest
|
var r LoginRequest
|
||||||
|
|
||||||
// Read the login request from the client.
|
// Read the login request from the client.
|
||||||
if err := a.ctx.ReadJSON(&r); err != nil {
|
if err := a.Ctx.ReadJSON(&r); err != nil {
|
||||||
a.ctx.StopWithError(iris.StatusBadRequest, err)
|
a.Ctx.StopWithError(iris.StatusBadRequest, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Retrieve the account for the given email.
|
// Retrieve the account for the given email.
|
||||||
account := model.Account{}
|
account := model.Account{}
|
||||||
if err := db.Get().Where("email = ?", r.Email).First(&account).Error; err != nil {
|
if err := db.Get().Where("email = ?", r.Email).First(&account).Error; err != nil {
|
||||||
a.ctx.StopWithError(iris.StatusBadRequest, errors.New("invalid email or password"))
|
a.Ctx.StopWithError(iris.StatusBadRequest, errors.New("invalid email or password"))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Generate a random challenge string.
|
// Generate a random challenge string.
|
||||||
challenge, err := generateAndSaveChallengeToken(account.ID, time.Minute)
|
challenge, err := generateAndSaveChallengeToken(account.ID, time.Minute)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
a.ctx.StopWithError(iris.StatusInternalServerError, errors.New("failed to generate challenge"))
|
a.Ctx.StopWithError(iris.StatusInternalServerError, errors.New("failed to generate challenge"))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return the challenge to the client.
|
// Return the challenge to the client.
|
||||||
err = a.ctx.JSON(&ChallengeResponse{Challenge: challenge})
|
err = a.Ctx.JSON(&ChallengeResponse{Challenge: challenge})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(fmt.Errorf("Error with challenge request: %s \n", err))
|
panic(fmt.Errorf("Error with challenge request: %s \n", err))
|
||||||
}
|
}
|
||||||
|
@ -204,33 +204,33 @@ func (a *AuthService) PostPubkeyLogin() {
|
||||||
var r PubkeyLoginRequest
|
var r PubkeyLoginRequest
|
||||||
|
|
||||||
// Read the key login request from the client.
|
// Read the key login request from the client.
|
||||||
if err := a.ctx.ReadJSON(&r); err != nil {
|
if err := a.Ctx.ReadJSON(&r); err != nil {
|
||||||
a.ctx.StopWithError(iris.StatusBadRequest, err)
|
a.Ctx.StopWithError(iris.StatusBadRequest, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Retrieve the key challenge for the given challenge.
|
// Retrieve the key challenge for the given challenge.
|
||||||
challenge := model.KeyChallenge{}
|
challenge := model.KeyChallenge{}
|
||||||
if err := db.Get().Where("challenge = ?", r.Challenge).Preload("Key").First(&challenge).Error; err != nil {
|
if err := db.Get().Where("challenge = ?", r.Challenge).Preload("Key").First(&challenge).Error; err != nil {
|
||||||
a.ctx.StopWithError(iris.StatusBadRequest, errorx.RejectedOperation.New("invalid key challenge"))
|
a.Ctx.StopWithError(iris.StatusBadRequest, errorx.RejectedOperation.New("invalid key challenge"))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
verifiedToken, err := jwt.Verify(jwt.HS256, sharedKey, []byte(r.Challenge), blocklist)
|
verifiedToken, err := jwt.Verify(jwt.HS256, sharedKey, []byte(r.Challenge), blocklist)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
a.ctx.StopWithError(iris.StatusBadRequest, errorx.RejectedOperation.New("invalid key challenge"))
|
a.Ctx.StopWithError(iris.StatusBadRequest, errorx.RejectedOperation.New("invalid key challenge"))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
rawPubKey, err := hex.DecodeString(r.Pubkey)
|
rawPubKey, err := hex.DecodeString(r.Pubkey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
a.ctx.StopWithError(iris.StatusBadRequest, errorx.RejectedOperation.New("invalid pubkey"))
|
a.Ctx.StopWithError(iris.StatusBadRequest, errorx.RejectedOperation.New("invalid pubkey"))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
rawSignature, err := hex.DecodeString(r.Signature)
|
rawSignature, err := hex.DecodeString(r.Signature)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
a.ctx.StopWithError(iris.StatusBadRequest, errorx.RejectedOperation.New("invalid signature"))
|
a.Ctx.StopWithError(iris.StatusBadRequest, errorx.RejectedOperation.New("invalid signature"))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -238,29 +238,29 @@ func (a *AuthService) PostPubkeyLogin() {
|
||||||
|
|
||||||
// Verify the challenge signature.
|
// Verify the challenge signature.
|
||||||
if !ed25519.Verify(publicKeyDecoded, []byte(r.Challenge), rawSignature) {
|
if !ed25519.Verify(publicKeyDecoded, []byte(r.Challenge), rawSignature) {
|
||||||
a.ctx.StopWithError(iris.StatusBadRequest, errorx.RejectedOperation.New("invalid challenge"))
|
a.Ctx.StopWithError(iris.StatusBadRequest, errorx.RejectedOperation.New("invalid challenge"))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Generate a JWT token for the authenticated user.
|
// Generate a JWT token for the authenticated user.
|
||||||
token, err := generateAndSaveLoginToken(challenge.AccountID, 24*time.Hour)
|
token, err := generateAndSaveLoginToken(challenge.AccountID, 24*time.Hour)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
a.ctx.StopWithError(iris.StatusInternalServerError, errorx.RejectedOperation.Wrap(err, "failed to generate token"))
|
a.Ctx.StopWithError(iris.StatusInternalServerError, errorx.RejectedOperation.Wrap(err, "failed to generate token"))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = blocklist.InvalidateToken(verifiedToken.Token, verifiedToken.StandardClaims)
|
err = blocklist.InvalidateToken(verifiedToken.Token, verifiedToken.StandardClaims)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
a.ctx.StopWithError(iris.StatusInternalServerError, errorx.RejectedOperation.Wrap(err, "failed to invalidate token"))
|
a.Ctx.StopWithError(iris.StatusInternalServerError, errorx.RejectedOperation.Wrap(err, "failed to invalidate token"))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := db.Get().Delete(&challenge).Error; err != nil {
|
if err := db.Get().Delete(&challenge).Error; err != nil {
|
||||||
a.ctx.StopWithError(iris.StatusBadRequest, errorx.RejectedOperation.New("failed to delete key challenge"))
|
a.Ctx.StopWithError(iris.StatusBadRequest, errorx.RejectedOperation.New("failed to delete key challenge"))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return the JWT token to the client.
|
// Return the JWT token to the client.
|
||||||
err = a.ctx.JSON(&LoginResponse{Token: token})
|
err = a.Ctx.JSON(&LoginResponse{Token: token})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(fmt.Errorf("Error with login attempt: %s \n", err))
|
panic(fmt.Errorf("Error with login attempt: %s \n", err))
|
||||||
}
|
}
|
||||||
|
@ -272,15 +272,15 @@ func (a *AuthService) PostLogout() {
|
||||||
var r LogoutRequest
|
var r LogoutRequest
|
||||||
|
|
||||||
// Read the logout request from the client.
|
// Read the logout request from the client.
|
||||||
if err := a.ctx.ReadJSON(&r); err != nil {
|
if err := a.Ctx.ReadJSON(&r); err != nil {
|
||||||
a.ctx.StopWithError(iris.StatusBadRequest, err)
|
a.Ctx.StopWithError(iris.StatusBadRequest, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Verify the provided token.
|
// Verify the provided token.
|
||||||
claims, err := jwt.Verify(jwt.HS256, sharedKey, []byte(r.Token), blocklist)
|
claims, err := jwt.Verify(jwt.HS256, sharedKey, []byte(r.Token), blocklist)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
a.ctx.StopWithError(iris.StatusBadRequest, errors.New("invalid token"))
|
a.Ctx.StopWithError(iris.StatusBadRequest, errors.New("invalid token"))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -290,5 +290,5 @@ func (a *AuthService) PostLogout() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return a success response to the client.
|
// Return a success response to the client.
|
||||||
a.ctx.StatusCode(iris.StatusNoContent)
|
a.Ctx.StatusCode(iris.StatusNoContent)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue