Exemplo n.º 1
0
// SetCurrent injects the user into the context.
func SetCurrent() gin.HandlerFunc {
	return func(c *gin.Context) {
		var (
			record *model.User
		)

		parsed, err := token.Parse(
			c.Request,
			func(t *token.Token) ([]byte, error) {
				var (
					res *gorm.DB
				)

				record, res = store.GetUser(
					c,
					t.Text,
				)

				signingKey, _ := base32.StdEncoding.DecodeString(record.Hash)
				return signingKey, res.Error
			},
		)

		if err == nil {
			c.Set(TokenContextKey, parsed)
			c.Set(CurrentContextKey, record)
		}

		c.Next()
	}
}
Exemplo n.º 2
0
// AuthVerify is a handler to verify an JWT token.
func AuthVerify(c *gin.Context) {
	var (
		record *model.User
	)

	_, err := token.Direct(
		c.Param("token"),
		func(t *token.Token) ([]byte, error) {
			var (
				res *gorm.DB
			)

			record, res = store.GetUser(
				c,
				t.Text,
			)

			signingKey, _ := base32.StdEncoding.DecodeString(record.Hash)
			return signingKey, res.Error
		},
	)

	if err != nil {
		c.JSON(
			http.StatusOK,
			gin.H{
				"error": "Invalid token provided",
			},
		)
	} else {
		c.JSON(
			http.StatusOK,
			gin.H{
				"valid":      "Valid token provided",
				"name":       record.Username,
				"created_at": record.CreatedAt,
			},
		)
	}
}
Exemplo n.º 3
0
// SetUser injects the user into the context.
func SetUser() gin.HandlerFunc {
	return func(c *gin.Context) {
		record, res := store.GetUser(
			c,
			c.Param("user"),
		)

		if res.Error != nil || res.RecordNotFound() {
			c.JSON(
				http.StatusNotFound,
				gin.H{
					"status":  http.StatusNotFound,
					"message": "Failed to find user",
				},
			)

			c.Abort()
		} else {
			c.Set(UserContextKey, record)
			c.Next()
		}
	}
}
Exemplo n.º 4
0
// AuthLogin represents the login handler.
func AuthLogin(c *gin.Context) {
	auth := &model.Auth{}

	if err := c.BindJSON(&auth); err != nil {
		logrus.Warn("Failed to bind login data. %s", err)

		c.JSON(
			http.StatusPreconditionFailed,
			gin.H{
				"status":  http.StatusPreconditionFailed,
				"message": "Failed to bind login data",
			},
		)

		c.Abort()
		return
	}

	user, res := store.GetUser(
		c,
		auth.Username,
	)

	if res.Error != nil || res.RecordNotFound() {
		logrus.Warnf("Failed to fetch requested user. %s", res.Error)

		c.JSON(
			http.StatusUnauthorized,
			gin.H{
				"status":  http.StatusUnauthorized,
				"message": "Wrong username or password",
			},
		)

		c.Abort()
		return
	}

	if err := user.MatchPassword(auth.Password); err != nil {
		logrus.Warnf("Failed to match passwords. %s", err)

		c.JSON(
			http.StatusUnauthorized,
			gin.H{
				"status":  http.StatusUnauthorized,
				"message": "Wrong username or password",
			},
		)

		c.Abort()
		return
	}

	token := token.New(token.SessToken, user.Username)
	result, err := token.SignExpiring(user.Hash, config.Session.Expire)

	if err != nil {
		logrus.Warnf("Failed to generate token. %s", err)

		c.JSON(
			http.StatusUnauthorized,
			gin.H{
				"status":  http.StatusUnauthorized,
				"message": "Wrong username or password",
			},
		)

		c.Abort()
		return
	}

	c.JSON(
		http.StatusOK,
		result,
	)
}