Esempio n. 1
0
// Establish connection to MongoDB and write feedback to collection
func WriteFeedback(name string, email string, text string) int {
	// We like only correct e-mails
	if !govalidator.IsEmail(email) {
		fmt.Printf("'%v' is not an email!\n", email)
		return NOT_AN_EMAIL
	}
	// Length of name can't be less than four letters and more than thirty two letters
	if !govalidator.IsByteLength(name, 4, 32) {
		fmt.Printf("Name '%v' has invalid length!\n", name)
		return INVALID_NAME
	}
	// Length of text should be between 16 and 1024 letters
	if !govalidator.IsByteLength(text, 16, 1024) {
		fmt.Printf("Feedback '%v' has invalid length!\n", text)
		return INVALID_NAME
	}
	url := os.Getenv("DATABASE")
	fmt.Printf("DB_URL is %v\n", url)
	sess, err := mgo.Dial(url)
	if err != nil {
		fmt.Printf("Can't connect to mongo, go error %v\n", err)
		return DB_UNAVAILABLE
	}
	defer sess.Close()
	sess.SetMode(mgo.Monotonic, true)

	c := sess.DB("asaskevich").C("feedback")
	err = c.Insert(&Feedback{name, email, text})
	if err != nil {
		fmt.Printf("Can't write to mongo, go error %v\n", err)
		return CANT_WRITE_TO_DB
	}
	return OK
}
Esempio n. 2
0
// Validate ...
func (lr *LoginRequest) Validate(builder *lib.ValidationErrorBuilder) {
	if !validator.IsByteLength(lr.Email, 1, 255) {
		builder.Add("email", "Email address is missing.")
	}

	if !validator.IsByteLength(lr.Password, 1, 255) {
		builder.Add("password", "Password is missing.")
	}
}
Esempio n. 3
0
// Validate ...
// TODO: unify somehow, to have centralized place where email (or any other field) is validated
func (prr *PasswordRecoveryRequest) Validate(builder *lib.ValidationErrorBuilder) {
	if !validator.IsByteLength(prr.Email, 6, 45) {
		builder.Add("email", "validation_error.email_min_max_wrong")
	} else if !validator.IsEmail(prr.Email) {
		builder.Add("email", "Invalid email address.")
	}
}
Esempio n. 4
0
// Validate ...
func (rr *RegistrationRequest) Validate(builder *lib.ValidationErrorBuilder) {
	if !validator.IsByteLength(rr.Email, 6, 45) {
		builder.Add("email", "validation_error.email_min_max_wrong")
	} else if !validator.IsEmail(rr.Email) {
		builder.Add("email", "Invalid email address.")
	}

	if !validator.IsByteLength(rr.Password, 6, 45) {
		builder.Add("password", "validation_error.password_min_max_wrong")
	}

	if !validator.IsByteLength(rr.FirstName, 1, 45) {
		builder.Add("firstName", "validation_error.firstname_max_len_wrong")
	}

	if !validator.IsByteLength(rr.LastName, 1, 45) {
		builder.Add("lastName", "validation_error.lastname_max_len_wrong")
	}
}
Esempio n. 5
0
// Login -
func (s *harborAuthClient) Login(username string, password string) (string, bool, error) {
	if !govalidator.IsByteLength(password, 8, 128) {
		return "", false, errors.New("Password is either less than the minimum of 8 or over the maximum number of 128 characters")
	}

	in := loginIn{}
	in.Username = username
	in.Password = password
	inBuf, err := json.Marshal(in)

	fullURL := s.url + "/v1/auth/gettoken"
	req, err := http.NewRequest("POST", fullURL, bytes.NewBuffer(inBuf))
	req.Header.Set("Content-Type", "application/json")

	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		fmt.Println(err.Error())
		return "", false, err
	}

	// defer the close until we've read all response body data
	defer resp.Body.Close()

	// anything other than 200 is bad
	status := resp.StatusCode
	if status != 200 {
		return "", false, errors.New("Invalid Status Code: " + resp.Status)
	}

	// read the response body data
	responseBody, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return "", false, errors.New("Error in ReadAll")
	}

	// marshall out the byte array data to something we can return
	var out loginOut
	json.Unmarshal(responseBody, &out)

	// Need to verify output before returning

	return out.Token, out.Success, nil
}