示例#1
0
func (m *Authentication) ChangeUserPassword(usr, pw string) error {
	// validate password
	err := auth.CheckPassword(pw)
	if err != nil {
		return err
	}

	// get collection
	col := m.getCollection()

	encrypt_pw, err := auth.PasswordEncrypt(pw)
	if err != nil {
		msg := fmt.Sprintf("user %s couldn't be created:\n%s", usr, err)
		return errors.New(msg)
	}

	// build query
	q := map[string]interface{}{"username": usr}
	uq := bson.M{"$set": bson.M{"password": encrypt_pw}}
	err = col.Update(q, uq)
	if err != nil {
		msg := fmt.Sprintf("user %s password couldn't be created:\n%s", usr, err)
		return errors.New(msg)
	}

	return nil
}
示例#2
0
func (m *Authentication) NewUser(usr, pw string, root bool) error {
	// usernames are lowercase
	usr = strings.ToLower(usr)

	// check username and password
	err := auth.CheckUsername(usr)
	if err != nil {
		return err
	}
	err = auth.CheckPassword(pw)
	if err != nil {
		return err
	}

	// check if username available
	// get collection
	col := m.getCollection()

	// build query
	q := map[string]interface{}{"username": usr}
	count, e2 := col.Find(q).Count()
	if e2 != nil {
		msg := fmt.Sprintf("user %s couldn't be created:\n%s", usr, e2)
		return errors.New(msg)
	}
	if count > 0 {
		msg := fmt.Sprintf("user %s already exists", usr)
		return errors.New(msg)
	}

	encrypt_pw, err := auth.PasswordEncrypt(pw)
	if err != nil {
		msg := fmt.Sprintf("user %s couldn't be created:\n%s", usr, err)
		return errors.New(msg)
	}

	// create token
	_token := authToken{
		usr,
		string(encrypt_pw),
		true,
		[]string{},
		root,
	}
	e4 := col.Insert(&_token)
	if err != nil {
		msg := fmt.Sprintf("user %s couldn't be created:\n%s", usr, e4)
		return errors.New(msg)
	}

	return nil
}