Exemplo n.º 1
0
func (s *Account) Create(ctx context.Context, req *account.CreateRequest, rsp *account.CreateResponse) error {
	// validate incoming
	if err := validateAccount(req.Account, "Create"); err != nil {
		return err
	}

	// set a uuid if we dont have one
	if len(req.Account.Id) == 0 {
		req.Account.Id = uuid.NewUUID().String()
	}

	// hash the pass
	salt := db.Salt()
	h, err := bcrypt.GenerateFromPassword([]byte(x+salt+req.Account.ClientSecret), 10)
	if err != nil {
		return errors.InternalServerError("go.micro.srv.auth.Create", err.Error())
	}
	pp := base64.StdEncoding.EncodeToString(h)

	// to lower
	req.Account.ClientId = strings.ToLower(req.Account.ClientId)
	req.Account.Type = strings.ToLower(req.Account.Type)

	if err := db.Create(req.Account, salt, pp); err != nil {
		return errors.InternalServerError("go.micro.srv.auth.Create", err.Error())
	}
	return nil
}
Exemplo n.º 2
0
func (s *Account) Update(ctx context.Context, req *account.UpdateRequest, rsp *account.UpdateResponse) error {
	// validate incoming
	if err := validateAccount(req.Account, "Update"); err != nil {
		return err
	}

	// need an account id for update
	if len(req.Account.Id) == 0 {
		return errors.BadRequest("go.micro.srv.auth.Update", "invalid id")
	}

	// lookup the record and verify it's the same
	acc, err := db.Read(req.Account.Id)
	if err != nil {
		return errors.InternalServerError("go.micro.srv.auth.Update", err.Error())
	}

	// not the same client id
	if req.Account.ClientId != acc.ClientId {
		return errors.BadRequest("go.micro.srv.auth.Update", "invalid client id")
	}

	// hash the pass
	salt := db.Salt()
	h, err := bcrypt.GenerateFromPassword([]byte(x+salt+req.Account.ClientSecret), 10)
	if err != nil {
		return errors.InternalServerError("go.micro.srv.auth.Update", err.Error())
	}
	pp := base64.StdEncoding.EncodeToString(h)

	// to lower
	req.Account.ClientId = strings.ToLower(req.Account.ClientId)
	req.Account.Type = strings.ToLower(req.Account.Type)

	// update
	if err := db.Update(req.Account, salt, pp); err != nil {
		return errors.InternalServerError("go.micro.srv.auth.Update", err.Error())
	}

	return nil
}