| 
									
										
										
										
											2021-06-23 11:36:09 +02:00
										 |  |  | package auth | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"errors" | 
					
						
							|  |  |  | 	"net/http" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/gin-gonic/gin" | 
					
						
							|  |  |  | 	"github.com/google/uuid" | 
					
						
							|  |  |  | 	"gorm.io/gorm" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"dev.sum7.eu/genofire/golang-lib/web" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // PasswordWithForgetCode - JSON Request to set password without login
 | 
					
						
							|  |  |  | type PasswordWithForgetCode struct { | 
					
						
							|  |  |  | 	ForgetCode uuid.UUID `json:"forget_code"` | 
					
						
							|  |  |  | 	Password   string    `json:"password"` | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // @Summary Change Password with ForgetCode
 | 
					
						
							|  |  |  | // @Description Change Password of any user by generated forget code
 | 
					
						
							| 
									
										
										
										
											2021-06-30 15:47:24 +02:00
										 |  |  | // @Tags auth
 | 
					
						
							| 
									
										
										
										
											2021-06-23 11:36:09 +02:00
										 |  |  | // @Accept json
 | 
					
						
							|  |  |  | // @Produce  json
 | 
					
						
							|  |  |  | // @Success 200 {object} string "username of changed password (e.g. `"admin"`)"
 | 
					
						
							|  |  |  | // @Failure 400 {object} web.HTTPError
 | 
					
						
							|  |  |  | // @Failure 401 {object} web.HTTPError
 | 
					
						
							|  |  |  | // @Failure 500 {object} web.HTTPError
 | 
					
						
							|  |  |  | // @Router /api/v1/auth/password/code [post]
 | 
					
						
							|  |  |  | // @Param body body PasswordWithForgetCode false "new password and forget code"
 | 
					
						
							| 
									
										
										
										
											2021-07-19 17:59:08 +02:00
										 |  |  | func apiPasswordCode(r *gin.Engine, ws *web.Service) { | 
					
						
							|  |  |  | 	r.POST("/api/v1/auth/password/code", func(c *gin.Context) { | 
					
						
							|  |  |  | 		var req PasswordWithForgetCode | 
					
						
							|  |  |  | 		if err := c.BindJSON(&req); err != nil { | 
					
						
							|  |  |  | 			c.JSON(http.StatusBadRequest, web.HTTPError{ | 
					
						
							| 
									
										
										
										
											2021-07-22 18:16:05 +02:00
										 |  |  | 				Message: web.ErrAPIInvalidRequestFormat.Error(), | 
					
						
							| 
									
										
										
										
											2021-07-19 17:59:08 +02:00
										 |  |  | 				Error:   err.Error(), | 
					
						
							|  |  |  | 			}) | 
					
						
							|  |  |  | 			return | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		d := User{} | 
					
						
							|  |  |  | 		if err := ws.DB.Where("forget_code", req.ForgetCode).First(&d).Error; err != nil { | 
					
						
							|  |  |  | 			if errors.Is(err, gorm.ErrRecordNotFound) { | 
					
						
							| 
									
										
										
										
											2021-06-23 11:36:09 +02:00
										 |  |  | 				c.JSON(http.StatusBadRequest, web.HTTPError{ | 
					
						
							| 
									
										
										
										
											2021-07-22 18:16:05 +02:00
										 |  |  | 					Message: ErrAPIUserNotFound.Error(), | 
					
						
							| 
									
										
										
										
											2021-06-23 11:36:09 +02:00
										 |  |  | 					Error:   err.Error(), | 
					
						
							|  |  |  | 				}) | 
					
						
							|  |  |  | 				return | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2021-07-19 17:59:08 +02:00
										 |  |  | 			c.JSON(http.StatusInternalServerError, web.HTTPError{ | 
					
						
							| 
									
										
										
										
											2021-07-22 18:16:05 +02:00
										 |  |  | 				Message: ErrAPICreatePassword.Error(), | 
					
						
							| 
									
										
										
										
											2021-07-19 17:59:08 +02:00
										 |  |  | 				Error:   err.Error(), | 
					
						
							|  |  |  | 			}) | 
					
						
							|  |  |  | 			return | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if err := d.SetPassword(req.Password); err != nil { | 
					
						
							|  |  |  | 			c.JSON(http.StatusInternalServerError, web.HTTPError{ | 
					
						
							| 
									
										
										
										
											2021-07-22 18:16:05 +02:00
										 |  |  | 				Message: ErrAPICreatePassword.Error(), | 
					
						
							| 
									
										
										
										
											2021-07-19 17:59:08 +02:00
										 |  |  | 				Error:   err.Error(), | 
					
						
							|  |  |  | 			}) | 
					
						
							|  |  |  | 			return | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		d.ForgetCode = nil | 
					
						
							| 
									
										
										
										
											2021-06-23 11:36:09 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-19 17:59:08 +02:00
										 |  |  | 		if err := ws.DB.Save(&d).Error; err != nil { | 
					
						
							|  |  |  | 			c.JSON(http.StatusInternalServerError, web.HTTPError{ | 
					
						
							| 
									
										
										
										
											2021-07-22 18:16:05 +02:00
										 |  |  | 				Message: web.ErrAPIInternalDatabase.Error(), | 
					
						
							| 
									
										
										
										
											2021-07-19 17:59:08 +02:00
										 |  |  | 				Error:   err.Error(), | 
					
						
							|  |  |  | 			}) | 
					
						
							|  |  |  | 			return | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		c.JSON(http.StatusOK, d.Username) | 
					
						
							| 
									
										
										
										
											2021-06-23 11:36:09 +02:00
										 |  |  | 	}) | 
					
						
							|  |  |  | } |