api/auth.go
func addKeyToUser(w http.ResponseWriter, r *http.Request, t auth.Token) (err error) {
	key := repository.Key{
		Body: InputValue(r, "key"),
		Name: InputValue(r, "name"),
	}
	var force bool
	if InputValue(r, "force") == "true" {
		force = true
	}
	allowed := permission.Check(t, permission.PermUserUpdateKeyAdd,
		permission.Context(permTypes.CtxUser, t.GetUserName()),
	)
	if !allowed {
		return permission.ErrUnauthorized
	}
	evt, err := event.New(&event.Opts{
		Target:     userTarget(t.GetUserName()),
		Kind:       permission.PermUserUpdateKeyAdd,
		Owner:      t,
		CustomData: event.FormToCustomData(InputFields(r)),
		Allowed:    event.Allowed(permission.PermUserReadEvents, permission.Context(permTypes.CtxUser, t.GetUserName())),
	})
	if err != nil {
		return err
	}
	defer func() { evt.Done(err) }()
	if key.Body == "" {
		return &errors.HTTP{Code: http.StatusBadRequest, Message: "Missing key content"}
	}
	u, err := auth.ConvertNewUser(t.User())
	if err != nil {
		return err
	}
	err = u.AddKey(key, force)
	if err == authTypes.ErrKeyDisabled || err == repository.ErrUserNotFound {
		return &errors.HTTP{Code: http.StatusBadRequest, Message: err.Error()}
	}
	if err == repository.ErrKeyAlreadyExists {
		return &errors.HTTP{Code: http.StatusConflict, Message: err.Error()}
	}
	return err
}
func addKeyToUser(w http.ResponseWriter, r *http.Request, t auth.Token) (err error) {
	key := repository.Key{
		Body: InputValue(r, "key"),
		Name: InputValue(r, "name"),
	}
	var force bool
	if InputValue(r, "force") == "true" {
		force = true
	}
	allowed := permission.Check(t, permission.PermUserUpdateKeyAdd,
		permission.Context(permTypes.CtxUser, t.GetUserName()),
	)
	if !allowed {
		return permission.ErrUnauthorized
	}
	evt, err := event.New(&event.Opts{
		Target:     userTarget(t.GetUserName()),
		Kind:       permission.PermUserUpdateKeyAdd,
		Owner:      t,
		CustomData: event.FormToCustomData(InputFields(r)),
		Allowed:    event.Allowed(permission.PermUserReadEvents, permission.Context(permTypes.CtxUser, t.GetUserName())),
	})
	if err != nil {
		return err
	}
	defer func() { evt.Done(err) }()
	if key.Body == "" {
		return &errors.HTTP{Code: http.StatusBadRequest, Message: "Missing key content"}
	}
	u, err := auth.ConvertNewUser(t.User())
	if err != nil {
		return err
	}
	err = u.AddKey(key, force)
	if err == authTypes.ErrKeyDisabled || err == repository.ErrUserNotFound {
		return &errors.HTTP{Code: http.StatusBadRequest, Message: err.Error()}
	}
	if err == repository.ErrKeyAlreadyExists {
		return &errors.HTTP{Code: http.StatusConflict, Message: err.Error()}
	}
	return err
}
// title: add key
// path: /users/keys
// method: POST
// consume: application/x-www-form-urlencoded
// responses:
//   200: Ok
//   400: Invalid data
//   401: Unauthorized
//   409: Key already exists