예제 #1
0
func generateArticleContent(r *app.Request) (string, string, models.Category, []byte) {
	articleJson := articleJsonBody{}

	r.DecodeJsonPayload(&articleJson)

	// params
	markdown := articleJson.Markdown
	title := articleJson.Title
	category_name := articleJson.Category
	unsafe := articleJson.Content

	// find category
	category := models.Category{}
	if category_name != "" {
		category.FindByName(category_name)
		if category.ID == 0 {
			category.Name = category_name
			category.Create()
		}
	}

	// HTML sanitizer
	html := bluemonday.UGCPolicy().SanitizeBytes([]byte(unsafe))

	return title, markdown, category, html
}
예제 #2
0
func (self *authMiddware) LoginController(w app.ResponseWriter, r *app.Request) {
	var tokenString string

	data := jsonLoginDate{}

	// Decode Json from request
	err := r.DecodeJsonPayload(&data)

	if err != nil {
		logging.Logger.Error(fmt.Sprintf("Error: %s", err))
		w.WriteHeader(500)
		w.WriteJson(map[string]string{"error": fmt.Sprintf("Error: %s", err)})
		return
	}

	// extract
	email := data.Email
	password := data.Password

	// use email get user
	user := models.User{}
	user.Find(email)

	// validate password generate jwt tokenString
	// user jwt we can ignore CRSF
	if user.Validate(password) {
		user.Last_seen = time.Now().UTC()
		user.Save()
		token := jwt.New(jwt.SigningMethodHS256)
		token.Claims["email"] = user.Email
		token.Claims["role"] = user.Role
		token.Claims["exp"] = time.Now().Add(time.Hour * 6).UTC().Unix()
		tokenString, err = token.SignedString(self.signingKey)
		if err != nil {
			logging.Logger.Error(fmt.Sprintf("Error: %s", err))
			w.WriteHeader(500)
			w.WriteJson(map[string]string{"error": fmt.Sprintf("Error: %s", err)})
		}
		w.WriteJson(responseUserData{user.Nickname, tokenString, user.Role})

	} else {
		w.WriteHeader(400)
		w.WriteJson(map[string]string{"error": "email or password incorrect"})
	}
}