Example #1
0
// PUT /developers/{token}/reset, Edit password
func PasswordEditHandler(rw http.ResponseWriter, req *http.Request) {
	if err := req.ParseForm(); err != nil {
		renderer.JSON(rw, http.StatusBadRequest, map[string]string{
			"status": requests.StatusFailed,
			"error":  err.Error(),
		})
		return
	}

	id := req.FormValue("id")
	u, err := db.GetDeveloperById(id)
	if err != nil {
		renderer.JSON(rw, http.StatusBadRequest, map[string]string{
			"status": requests.StatusFailed,
			"error":  err.Error(),
		})
		return
	}

	query := map[string]interface{}{"token": mux.Vars(req)["token"]}
	update := map[string]interface{}{"password": util.HashPassword(req.FormValue("new"), u.Salt)}
	if err := db.UpdateDeveloper(query, update); err != nil {
		renderer.JSON(rw, http.StatusBadRequest, map[string]string{
			"status": requests.StatusFailed,
			"error":  err.Error(),
		})
		return
	}

	renderer.JSON(rw, http.StatusOK, map[string]interface{}{
		"status": requests.StatusSuccess,
		"user":   u,
	})
}
Example #2
0
// GET /session/{id}, Gets user by ID. If their license has expired it attempts
// to charge them again. It is called everytime crosby is run.
func SessionInfoHandler(rw http.ResponseWriter, req *http.Request) {
	id := mux.Vars(req)["id"]
	fmt.Println("Getting user by id", id)
	u, err := db.GetDeveloperById(id)
	if err != nil {
		renderer.JSON(rw, http.StatusBadRequest, map[string]string{
			"status": requests.StatusFailed,
			"error":  err.Error(),
		})
		return
	}

	if u.Expiration.After(time.Now()) {
		renderer.JSON(rw, http.StatusOK, map[string]interface{}{
			"status":    requests.StatusFound,
			"developer": u,
		})
		return
	}

	if u.StripeToken == "" {
		renderer.JSON(rw, http.StatusOK, map[string]interface{}{
			"status":    requests.StatusExpired,
			"developer": u,
		})
		return
	}

	// Charge them, update expiration, & respond with found.
	// Charge Stripe Customer
	chargeParams := stripe.ChargeParams{
		Desc:     "Crosby Annual License",
		Amount:   2500,
		Currency: "usd",
		Customer: u.StripeToken,
	}
	_, err = stripe.Charges.Create(&chargeParams)
	if err != nil {
		renderer.JSON(rw, http.StatusBadRequest, map[string]string{
			"status": requests.StatusFailed,
			"error":  err.Error(),
		})
		return
	}
	u.Expiration = time.Now()
	if err := db.Save(u); err != nil { // not actually a save, but an update. fix
		renderer.JSON(rw, http.StatusBadRequest, map[string]string{
			"status": requests.StatusFailed,
			"error":  err.Error(),
		})
		return
	}

	renderer.JSON(rw, http.StatusOK, map[string]interface{}{
		"status": requests.StatusFound,
		"user":   u,
	})
}
Example #3
0
// GET /developers/{token}/reset/{id}, Serves from where users can reset their password.
func ResetHandler(rw http.ResponseWriter, req *http.Request) {
	id := mux.Vars(req)["id"]
	token := mux.Vars(req)["token"]

	u, err := db.GetDeveloperById(id)
	if err != nil {
		RenderTemplate(rw, "error", map[string]string{"Error": err.Error()})
		return
	}

	if token != u.Token {
		RenderTemplate(rw, "error", map[string]string{"Error": "Invalid Token"})
		return
	}

	if err := RenderTemplate(rw, "password_reset", map[string]interface{}{
		"Token": u.Token,
		"ID":    u.ID.Hex(),
	}); err != nil {
		RenderTemplate(rw, "error", map[string]string{"Error": err.Error()})
	}
}
Example #4
0
// GET /developers/{id}, return public info for a developer
func GetDeveloperByIDHandler(rw http.ResponseWriter, req *http.Request) {
	id := mux.Vars(req)["id"]
	token := req.FormValue("token")
	if token == "" {
		renderer.JSON(rw, http.StatusBadRequest, map[string]string{
			"status": requests.StatusFailed,
			"error":  "Valid token required.",
		})
		return
	}

	dev, err := db.GetDeveloperById(id)
	if err != nil {
		renderer.JSON(rw, http.StatusInternalServerError, map[string]string{
			"status": requests.StatusFailed,
			"error":  err.Error(),
		})
		return
	}

	// If the developer doing the request is not the dev found, only send
	// minimal information.
	if dev.Token != token {
		dev = &schemas.Developer{
			Email:               dev.Email,
			Name:                dev.Name,
			Version:             dev.Version,
			IntegrationEngineer: dev.IntegrationEngineer,
		}
	}

	renderer.JSON(rw, http.StatusOK, map[string]interface{}{
		"status":    requests.StatusFound,
		"developer": dev,
	})
}