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

     1  // Package public adds some public routes that can be used to give information
     2  // to anonymous users, or to the not yet authentified cozy owner on its login
     3  // page.
     4  package public
     5  
     6  import (
     7  	"net/http"
     8  	"strings"
     9  	"time"
    10  
    11  	"github.com/cozy/cozy-stack/model/bitwarden/settings"
    12  	csettings "github.com/cozy/cozy-stack/model/settings"
    13  	"github.com/cozy/cozy-stack/pkg/assets"
    14  	"github.com/cozy/cozy-stack/pkg/config/config"
    15  	"github.com/cozy/cozy-stack/web/middlewares"
    16  	"github.com/cozy/cozy-stack/web/statik"
    17  	"github.com/labstack/echo/v4"
    18  )
    19  
    20  // Avatar returns the default avatar currently.
    21  func Avatar(c echo.Context) error {
    22  	inst := middlewares.GetInstance(c)
    23  	switch c.QueryParam("fallback") {
    24  	case "404":
    25  		// Nothing
    26  	case "initials":
    27  		publicName, err := csettings.PublicName(inst)
    28  		if err != nil {
    29  			publicName = strings.Split(inst.Domain, ".")[0]
    30  		}
    31  		img, mime, err := config.Avatars().GenerateInitials(publicName)
    32  		if err == nil {
    33  			return c.Blob(http.StatusOK, mime, img)
    34  		}
    35  	default:
    36  		f, ok := assets.Get("/images/default-avatar.png", inst.ContextName)
    37  		if ok {
    38  			handler := statik.NewHandler()
    39  			handler.ServeFile(c.Response(), c.Request(), f, true)
    40  			return nil
    41  		}
    42  	}
    43  	return echo.NewHTTPError(http.StatusNotFound, "Page not found")
    44  }
    45  
    46  // Prelogin returns information that could be useful to show a login page (like
    47  // in the flagship app).
    48  func Prelogin(c echo.Context) error {
    49  	inst := middlewares.GetInstance(c)
    50  	if !inst.OnboardingFinished {
    51  		return c.JSON(http.StatusPreconditionFailed, echo.Map{
    52  			"error": "the instance has not been onboarded",
    53  		})
    54  	}
    55  
    56  	publicName, err := csettings.PublicName(inst)
    57  	if err != nil {
    58  		publicName = ""
    59  	}
    60  	setting, err := settings.Get(inst)
    61  	if err != nil {
    62  		return err
    63  	}
    64  	_, oidc := config.GetOIDC(inst.ContextName)
    65  	franceConnect := inst.FranceConnectID != ""
    66  	return c.JSON(http.StatusOK, echo.Map{
    67  		"Kdf":           setting.PassphraseKdf,
    68  		"KdfIterations": setting.PassphraseKdfIterations,
    69  		"OIDC":          oidc,
    70  		"FranceConnect": franceConnect,
    71  		"magic_link":    inst.MagicLink,
    72  		"locale":        inst.Locale,
    73  		"name":          publicName,
    74  	})
    75  }
    76  
    77  // Routes sets the routing for the public service
    78  func Routes(router *echo.Group) {
    79  	cacheControl := middlewares.CacheControl(middlewares.CacheOptions{
    80  		MaxAge: 24 * time.Hour,
    81  	})
    82  	router.GET("/avatar", Avatar, cacheControl)
    83  	router.GET("/prelogin", Prelogin)
    84  }