github.com/cozy/cozy-stack@v0.0.0-20240603063001-31110fa4cae1/web/oauth/clients.go (about)

     1  package oauth
     2  
     3  import (
     4  	"encoding/json"
     5  	"net/http"
     6  
     7  	"github.com/cozy/cozy-stack/model/instance/lifecycle"
     8  	"github.com/cozy/cozy-stack/model/oauth"
     9  	"github.com/cozy/cozy-stack/pkg/consts"
    10  	"github.com/cozy/cozy-stack/pkg/couchdb"
    11  	"github.com/labstack/echo/v4"
    12  )
    13  
    14  func deleteClients(c echo.Context) error {
    15  	domain := c.Param("domain")
    16  	inst, err := lifecycle.GetInstance(domain)
    17  	if err != nil {
    18  		return err
    19  	}
    20  
    21  	kind := c.QueryParam("Kind")
    22  	var clients []couchdb.Doc
    23  	err = couchdb.ForeachDocs(inst, consts.OAuthClients, func(id string, doc json.RawMessage) error {
    24  		client := &oauth.Client{}
    25  		if err := json.Unmarshal(doc, client); err != nil {
    26  			return err
    27  		}
    28  		if kind == "" || client.ClientKind == kind {
    29  			clients = append(clients, client)
    30  		}
    31  		return nil
    32  	})
    33  	if err != nil {
    34  		return err
    35  	}
    36  
    37  	if len(clients) > 0 {
    38  		if err := couchdb.BulkDeleteDocs(inst, consts.OAuthClients, clients); err != nil {
    39  			return err
    40  		}
    41  	}
    42  
    43  	return c.JSON(http.StatusOK, echo.Map{"count": len(clients)})
    44  }
    45  
    46  // Routes sets the routing for the oauth clients (admin)
    47  func Routes(router *echo.Group) {
    48  	router.DELETE("/:domain/clients", deleteClients)
    49  }