Skip to content

Commit

Permalink
fix zk wallet login
Browse files Browse the repository at this point in the history
  • Loading branch information
Rushikeshnimkar committed May 13, 2024
1 parent 214bd08 commit 421aa98
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions api/v1/authenticate/authenticate.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func ApplyRoutes(r *gin.RouterGroup) {
g := r.Group("/authenticate")
{
g.POST("", authenticate)
g.POST("/NonSign", authenticateNonSignature)
g.Use(paseto.PASETO(false))
g.GET("", authenticateToken)
}
Expand Down Expand Up @@ -168,3 +169,64 @@ func authenticateToken(c *gin.Context) {
}
httpo.NewSuccessResponseP(200, "Token verifies successfully", payload).SendD(c)
}
func authenticateNonSignature(c *gin.Context) {
db := dbconfig.GetDb()
//TODO remove flow id if 200
var req AuthenticateRequestNoSign
err := c.BindJSON(&req)
if err != nil {
httpo.NewErrorResponse(http.StatusBadRequest, fmt.Sprintf("payload is invalid: %s", err)).SendD(c)
return
}

//Get flowid type
var flowIdData models.FlowId
err = db.Model(&models.FlowId{}).Where("flow_id = ?", req.FlowId).First(&flowIdData).Error
if err != nil {
logwrapper.Errorf("failed to get flowId, error %v", err)
httpo.NewErrorResponse(http.StatusNotFound, "flow id not found").SendD(c)
return
}

if flowIdData.FlowIdType != models.AUTH {
httpo.NewErrorResponse(http.StatusBadRequest, "flow id not created for auth").SendD(c)
return
}
if req.WalletAddress != flowIdData.WalletAddress {
httpo.NewErrorResponse(http.StatusBadRequest, "WalletAddress incorrect").SendD(c)
return
}

// update wallet address for that user_id
err = db.Model(&models.User{}).Where("user_id = ?", flowIdData.UserId).Update("wallet_address", flowIdData.WalletAddress).Error
if err != nil {
httpo.NewErrorResponse(http.StatusInternalServerError, "Unexpected error occured").SendD(c)
logwrapper.Errorf("failed to update wallet address, error %v", err.Error())
return
}

customClaims := claims.NewWithWallet(flowIdData.UserId, &flowIdData.WalletAddress)
pvKey, err := hex.DecodeString(envconfig.EnvVars.PASETO_PRIVATE_KEY[2:])
if err != nil {
httpo.NewErrorResponse(http.StatusInternalServerError, "Unexpected error occured").SendD(c)
logwrapper.Errorf("failed to generate token, error %v", err.Error())
return
}
pasetoToken, err := auth.GenerateToken(customClaims, pvKey)
if err != nil {
httpo.NewErrorResponse(http.StatusInternalServerError, "Unexpected error occured").SendD(c)
logwrapper.Errorf("failed to generate token, error %v", err.Error())
return
}
err = db.Where("flow_id = ?", req.FlowId).Delete(&models.FlowId{}).Error
if err != nil {
httpo.NewErrorResponse(http.StatusInternalServerError, "Unexpected error occured").SendD(c)
logwrapper.Errorf("failed to delete flowId, error %v", err.Error())
return
}
payload := AuthenticatePayload{
Token: pasetoToken,
UserId: flowIdData.UserId,
}
httpo.NewSuccessResponseP(200, "Token generated successfully", payload).SendD(c)
}

0 comments on commit 421aa98

Please sign in to comment.