Ejemplo n.º 1
0
func CreateUserSQL(db *sql.DB) gin.HandlerFunc {
	return func(c *gin.Context) {

		user := User{}
		err := c.BindJSON(&user)
		if err != nil {
			gumrest.ErrorResponse(
				c,
				http.StatusNotAcceptable,
				err,
			)
			return
		}

		user.IsActive = true
		user.LastLogin = time.Now()
		user.Password = NewSha512Password(user.Password)

		err = SQLNewUser(db, &user)
		if err != nil {
			gumrest.ErrorResponse(
				c,
				http.StatusNotAcceptable,
				err,
			)
			return
		}

		c.JSON(http.StatusCreated, user)
	}

}
Ejemplo n.º 2
0
func Signer(c *gin.Context, s s2tore.SessionStore, u UserStore) {
	nameParam := c.Params.ByName(NameRequestField)
	tmp, err := base64.StdEncoding.DecodeString(nameParam)
	if err != nil {
		gumrest.ErrorResponse(c, http.StatusNotAcceptable, err)
		return
	}
	name := string(tmp)
	passParam := c.Params.ByName(PassRequestField)
	tmp, err = base64.StdEncoding.DecodeString(passParam)
	if err != nil {
		gumrest.ErrorResponse(c, http.StatusNotAcceptable, err)
		return
	}
	pass := string(tmp)

	user, err := u.FindUser(name)
	if err != nil {
		gumrest.ErrorResponse(c, http.StatusUnauthorized, err)
		return
	}

	if !user.ValidPassword(pass) {
		gumrest.ErrorResponse(c, http.StatusUnauthorized, SignInErr)
		return
	}

	expire := time.Now().Add(24 * time.Hour)
	session, err := s.NewSession(user.ID(), expire)
	if err != nil {
		gumrest.ErrorResponse(c, http.StatusNotAcceptable, err)
		return
	}

	c.Set(SessionKey, session)

	cookie := http.Cookie{
		Name:    XSRFCookieName,
		Value:   session.Token(),
		Expires: session.Expires(),
		// Setze Path auf / ansonsten kann angularjs
		// diese Cookie nicht finden und in späteren
		// Request nicht mitsenden.
		Path: "/",
	}
	http.SetCookie(c.Writer, &cookie)

	c.JSON(http.StatusAccepted, Session{
		Token:   session.Token(),
		UserID:  session.UserID(),
		Expires: session.Expires(),
	})
}
Ejemplo n.º 3
0
func ReadAll(ginCtx *gin.Context, db *gorp.DbMap) {
	userID, err := ReadUserID(ginCtx)
	if err != nil {
		gumrest.ErrorResponse(ginCtx, http.StatusBadRequest, err)
		return
	}

	tasks, err := ReadAllTasksUser(db, userID)
	if err != nil {
		gumrest.ErrorResponse(ginCtx, http.StatusNotFound, err)
		return
	}

	ginCtx.JSON(200, tasks)
}
Ejemplo n.º 4
0
func Update(ginCtx *gin.Context, db *gorp.DbMap) {
	tmp := ginCtx.Params.ByName("id")
	taskID, err := strconv.ParseInt(tmp, 10, 64)
	if err != nil {
		gumrest.ErrorResponse(ginCtx, http.StatusBadRequest, err)
		return
	}

	newTask := Task{}
	err = ginCtx.BindJSON(&newTask)
	newTask.ID = taskID

	_, err = db.Update(&newTask)
	if err != nil {
		gumrest.ErrorResponse(ginCtx, http.StatusBadRequest, err)
		return
	}

	ginCtx.JSON(200, newTask)

}
Ejemplo n.º 5
0
func Create(ginCtx *gin.Context, db *gorp.DbMap) {
	userID, err := ReadUserID(ginCtx)
	if err != nil {
		gumrest.ErrorResponse(ginCtx, http.StatusBadRequest, err)
		return
	}

	task := &Task{}
	err = ginCtx.BindJSON(task)
	if err != nil {
		gumrest.ErrorResponse(ginCtx, http.StatusBadRequest, err)
		return
	}

	err = CreateTaskUser(db, task, userID)
	if err != nil {
		gumrest.ErrorResponse(ginCtx, http.StatusNotFound, err)
		return
	}

	ginCtx.JSON(http.StatusCreated, task)
}
Ejemplo n.º 6
0
func Delete(ginCtx *gin.Context, db *gorp.DbMap) {
	userID, err := ReadUserID(ginCtx)
	if err != nil {
		gumrest.ErrorResponse(ginCtx, http.StatusBadRequest, err)
		return
	}

	tmp := ginCtx.Params.ByName("id")
	taskID, err := strconv.ParseInt(tmp, 10, 64)
	if err != nil {
		gumrest.ErrorResponse(ginCtx, http.StatusBadRequest, err)
		return
	}

	err = DeleteTaskUser(db, &Task{ID: taskID}, userID)
	if err != nil {
		gumrest.ErrorResponse(ginCtx, http.StatusBadRequest, err)
		return
	}

	ginCtx.JSON(http.StatusOK, nil)
}
Ejemplo n.º 7
0
func ReadOne(ginCtx *gin.Context, db *gorp.DbMap) {
	tmp := ginCtx.Param("id")
	taskID, err := strconv.ParseInt(tmp, 10, 64)
	if err != nil {
		gumrest.ErrorResponse(ginCtx, http.StatusBadRequest, err)
		return
	}

	userID, err := ReadUserID(ginCtx)
	if err != nil {
		gumrest.ErrorResponse(ginCtx, http.StatusBadRequest, err)
		return
	}

	task, err := ReadOneTaskUser(db, taskID, userID)
	if err != nil {
		gumrest.ErrorResponse(ginCtx, http.StatusNotFound, err)
		return
	}

	ginCtx.JSON(http.StatusOK, task)

}
Ejemplo n.º 8
0
func Bouncer(c *gin.Context, s s2tore.SessionStore) bool {
	token := c.Request.Header.Get(TokenHeaderField)
	if token == "" {
		cookie, err := c.Request.Cookie(XSRFCookieName)
		if err != nil {
			gumrest.ErrorResponse(
				c,
				http.StatusUnauthorized,
				CookieNotFoundErr,
			)
			return false
		}
		token = cookie.Value
		if token == "" {
			gumrest.ErrorResponse(
				c,
				http.StatusUnauthorized,
				HeaderNotFoundErr,
			)
		}
	}

	session, ok := s.ReadSession(token)
	if !ok {
		gumrest.ErrorResponse(
			c,
			http.StatusUnauthorized,
			SessionNotFoundErr,
		)
		return false

	}

	c.Set(SessionKey, session)
	return true
}