示例#1
0
文件: account.go 项目: micro/auth-srv
func (s *Account) Read(ctx context.Context, req *account.ReadRequest, rsp *account.ReadResponse) error {
	if len(req.Id) == 0 {
		return errors.BadRequest("go.micro.srv.auth.Read", "id cannot be blank")
	}

	acc, err := db.Read(req.Id)
	if err != nil {
		return errors.InternalServerError("go.micro.srv.auth.Read", err.Error())
	}

	rsp.Account = acc
	return nil
}
示例#2
0
文件: account.go 项目: micro/auth-srv
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
}