diff --git a/CHANGELOG.md b/CHANGELOG.md index 0708020..b2ca59c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [v0.1.22] +### Added +- Endpoint to get the encryption type of share + ## [v0.1.21] ### Fixed - Register Share encryption validation switch diff --git a/internal/adapters/handlers/rest/server.go b/internal/adapters/handlers/rest/server.go index c9699a7..b44894e 100644 --- a/internal/adapters/handlers/rest/server.go +++ b/internal/adapters/handlers/rest/server.go @@ -76,6 +76,7 @@ func (s *Server) Start(ctx context.Context) error { u := r.PathPrefix("/shares").Subrouter() u.Use(authMdw.AuthenticateUser) u.HandleFunc("", shareHdl.GetShare).Methods(http.MethodGet) + u.HandleFunc("/encryption", shareHdl.GetShareEncryption).Methods(http.MethodGet) u.HandleFunc("", shareHdl.RegisterShare).Methods(http.MethodPost) u.HandleFunc("", shareHdl.DeleteShare).Methods(http.MethodDelete) u.HandleFunc("", shareHdl.UpdateShare).Methods(http.MethodPut) diff --git a/internal/adapters/handlers/rest/sharehdl/handler.go b/internal/adapters/handlers/rest/sharehdl/handler.go index 255505c..c98737f 100644 --- a/internal/adapters/handlers/rest/sharehdl/handler.go +++ b/internal/adapters/handlers/rest/sharehdl/handler.go @@ -220,3 +220,38 @@ func (h *Handler) GetShare(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) _, _ = w.Write(resp) } + +// GetShareEncryption gets the encryption of a share +// @Summary Get share encryption +// @Description Get the encryption of a share for the user +// @Tags Share +// @Accept json +// @Produce json +// @Param X-API-Key header string true "API Key" +// @Param Authorization header string true "Bearer token" +// @Param X-Auth-Provider header string true "Auth Provider" +// @Param X-Openfort-Provider header string false "Openfort Provider" +// @Param X-Openfort-Token-Type header string false "Openfort Token Type" +// @Success 200 {object} GetShareEncryptionResponse "Successful response" +// @Failure 404 "Description: Not Found" +// @Failure 500 "Description: Internal Server Error" +// @Router /shares/encryption [get] +func (h *Handler) GetShareEncryption(w http.ResponseWriter, r *http.Request) { + ctx := r.Context() + h.logger.InfoContext(ctx, "getting share") + + shr, err := h.app.GetShareEncryption(ctx) + if err != nil { + api.RespondWithError(w, fromApplicationError(err)) + return + } + + resp, err := json.Marshal(GetShareEncryptionResponse{Entropy: h.parser.mapDomainEntropy[shr]}) + if err != nil { + api.RespondWithError(w, api.ErrInternal) + return + } + + w.WriteHeader(http.StatusOK) + _, _ = w.Write(resp) +} diff --git a/internal/adapters/handlers/rest/sharehdl/types.go b/internal/adapters/handlers/rest/sharehdl/types.go index c535d63..2f1ab0a 100644 --- a/internal/adapters/handlers/rest/sharehdl/types.go +++ b/internal/adapters/handlers/rest/sharehdl/types.go @@ -26,3 +26,7 @@ const ( EntropyUser Entropy = "user" EntropyProject Entropy = "project" ) + +type GetShareEncryptionResponse struct { + Entropy Entropy `json:"entropy"` +} diff --git a/internal/applications/shareapp/app.go b/internal/applications/shareapp/app.go index d03a814..929bac4 100644 --- a/internal/applications/shareapp/app.go +++ b/internal/applications/shareapp/app.go @@ -122,6 +122,19 @@ func (a *ShareApplication) UpdateShare(ctx context.Context, shr *share.Share, op return shr, nil } +func (a *ShareApplication) GetShareEncryption(ctx context.Context) (share.Entropy, error) { + a.logger.InfoContext(ctx, "getting share encryption") + usrID := contexter.GetUserID(ctx) + + shr, err := a.shareRepo.GetByUserID(ctx, usrID) + if err != nil { + a.logger.ErrorContext(ctx, "failed to get share by user ID", logger.Error(err)) + return 0, fromDomainError(err) + } + + return shr.Entropy, nil +} + func (a *ShareApplication) GetShare(ctx context.Context, opts ...Option) (*share.Share, error) { a.logger.InfoContext(ctx, "getting share") usrID := contexter.GetUserID(ctx)