<commit_before>package registry

import (
	"net/http"

	"github.com/cozy/cozy-stack/pkg/permissions"
	"github.com/cozy/cozy-stack/pkg/registry"
	"github.com/cozy/cozy-stack/web/middlewares"
	webpermissions "github.com/cozy/cozy-stack/web/permissions"
	"github.com/labstack/echo"
)

func proxyReq(cacheControl registry.CacheControl) echo.HandlerFunc {
	return func(c echo.Context) error {
		i := middlewares.GetInstance(c)
		pdoc, err := webpermissions.GetPermission(c)
		if err != nil || pdoc.Type != permissions.TypeWebapp {
			return echo.NewHTTPError(http.StatusForbidden)
		}
		registries, err := i.Registries()
		if err != nil {
			return err
		}
		req := c.Request()
		resp, err := registry.Proxy(req, registries, cacheControl)
		if err != nil {
			return err
		}
		defer resp.Body.Close()
		contentType := resp.Header.Get("content-type")
		return c.Stream(resp.StatusCode, contentType, resp.Body)
	}
}

func proxyListReq(c echo.Context) error {
	i := middlewares.GetInstance(c)
	pdoc, err := webpermissions.GetPermission(c)
	if err != nil || pdoc.Type != permissions.TypeWebapp {
		return echo.NewHTTPError(http.StatusForbidden)
	}
	registries, err := i.Registries()
	if err != nil {
		return err
	}
	req := c.Request()
	list, err := registry.ProxyList(req, registries)
	if err != nil {
		return err
	}
	return c.JSON(http.StatusOK, list)
}

// Routes sets the routing for the registry
func Routes(router *echo.Group) {
	router.GET("", proxyListReq)
	router.GET("/", proxyListReq)
	router.GET("/:app", proxyReq(registry.WithCache))
	router.GET("/:app/icon", proxyReq(registry.NoCache))
	router.GET("/:app/screenshots/:filename", proxyReq(registry.NoCache))
	router.GET("/:app/:version", proxyReq(registry.WithCache))
	router.GET("/:app/:channel/latest", proxyReq(registry.WithCache))
}<commit_msg>Allow fetching assets without permissions<commit_after>package registry

import (
	"net/http"

	"github.com/cozy/cozy-stack/pkg/permissions"
	"github.com/cozy/cozy-stack/pkg/registry"
	"github.com/cozy/cozy-stack/web/middlewares"
	webpermissions "github.com/cozy/cozy-stack/web/permissions"
	"github.com/labstack/echo"
)

type authType int

const (
	authed authType = iota
	perms
)

func proxyReq(auth authType, cacheControl registry.CacheControl) echo.HandlerFunc {
	return func(c echo.Context) error {
		i := middlewares.GetInstance(c)
		switch auth {
		case authed:
			if !middlewares.IsLoggedIn(c) {
				return echo.NewHTTPError(http.StatusForbidden)
			}
		case perms:
			pdoc, err := webpermissions.GetPermission(c)
			if err != nil || pdoc.Type != permissions.TypeWebapp {
				return echo.NewHTTPError(http.StatusForbidden)
			}
		default:
			panic("unknown authType")
		}
		registries, err := i.Registries()
		if err != nil {
			return err
		}
		req := c.Request()
		resp, err := registry.Proxy(req, registries, cacheControl)
		if err != nil {
			return err
		}
		defer resp.Body.Close()
		contentType := resp.Header.Get("content-type")
		return c.Stream(resp.StatusCode, contentType, resp.Body)
	}
}

func proxyListReq(c echo.Context) error {
	i := middlewares.GetInstance(c)
	pdoc, err := webpermissions.GetPermission(c)
	if err != nil || pdoc.Type != permissions.TypeWebapp {
		return echo.NewHTTPError(http.StatusForbidden)
	}
	registries, err := i.Registries()
	if err != nil {
		return err
	}
	req := c.Request()
	list, err := registry.ProxyList(req, registries)
	if err != nil {
		return err
	}
	return c.JSON(http.StatusOK, list)
}

// Routes sets the routing for the registry
func Routes(router *echo.Group) {
	router.GET("", proxyListReq)
	router.GET("/", proxyListReq)
	router.GET("/:app", proxyReq(perms, registry.WithCache))
	router.GET("/:app/icon", proxyReq(authed, registry.NoCache))
	router.GET("/:app/screenshots/:filename", proxyReq(authed, registry.NoCache))
	router.GET("/:app/:version", proxyReq(perms, registry.WithCache))
	router.GET("/:app/:channel/latest", proxyReq(perms, registry.WithCache))
}
{'fraction_non_alphanumeric': 0.14645308924485126, 'fraction_numerical': 0.0, 'mean_word_length': 4.433701657458563, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2194401', 'n_tokens_mistral': 1460, 'n_tokens_neox': 1407, 'n_words': 272}