Make product API versions independently installable

This commit is contained in:
Stephan D committed 2026-09-13 10:26:06 +02:00
1 parent ce6583f454
commit d04c8b12ab
69 files changed
+755 -404

No files matched your search

@@ -0,0 +1,40 @@
package sresponse
import (
customermodel "git.sendico.io/tech/sxchnge/product/customers/model"
"net/http"
"git.sendico.io/tech/sxchnge/shared/http/response"
"git.sendico.io/tech/sxchnge/shared/logging"
)
type AccountData struct {
ID string `json:"id"`
Name string `json:"name"`
LastName string `json:"lastName"`
Login string `json:"login"`
Locale string `json:"locale"`
AvatarURL *string `json:"avatarUrl,omitempty"`
}
type accountResponse struct {
authResponse `json:",inline"`
Account AccountData `json:"account"`
}
func NewAccountData(account *customermodel.Account) *AccountData {
return &AccountData{
ID: account.ID.Hex(), Name: account.Name, LastName: account.LastName,
Login: account.Login, Locale: account.Locale, AvatarURL: account.AvatarURL,
}
}
func Account(logger mlogger.Logger, account *customermodel.Account, accessToken *TokenData) http.HandlerFunc {
return response.Ok(
logger,
&accountResponse{
Account: *NewAccountData(account),
authResponse: authResponse{AccessToken: *accessToken},
},
)
}
@@ -0,0 +1,7 @@
package sresponse
type AuthResponse struct {
AccessToken TokenData `json:"accessToken"`
}
type authResponse = AuthResponse
@@ -0,0 +1,16 @@
package sresponse
import (
"net/http"
"git.sendico.io/tech/sxchnge/shared/http/response"
"git.sendico.io/tech/sxchnge/shared/logging"
)
type fileUploaded struct {
URL string `json:"url"`
}
func FileUploaded(logger mlogger.Logger, url string) http.HandlerFunc {
return response.Ok(logger, &fileUploaded{URL: url})
}
@@ -0,0 +1,35 @@
package sresponse
import (
customermodel "git.sendico.io/tech/sxchnge/product/customers/model"
"net/http"
"git.sendico.io/tech/sxchnge/product/sessions/browser"
"git.sendico.io/tech/sxchnge/shared/http/response"
"git.sendico.io/tech/sxchnge/shared/logging"
)
type loginResponse struct {
accountResponse
RefreshToken TokenData `json:"refreshToken"`
}
func Login(logger mlogger.Logger, account *customermodel.Account, accessToken, refreshToken *TokenData) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
responseRefreshToken := *refreshToken
if browser.UsesCookie(r) {
browser.SetRefreshCookie(w, r, refreshToken.Token, refreshToken.Expiration)
responseRefreshToken.Token = ""
}
response.Ok(
logger,
&loginResponse{
accountResponse: accountResponse{
Account: *NewAccountData(account),
authResponse: authResponse{AccessToken: *accessToken},
},
RefreshToken: responseRefreshToken,
},
).ServeHTTP(w, r)
}
}
@@ -0,0 +1,29 @@
package sresponse
import (
customermodel "git.sendico.io/tech/sxchnge/product/customers/model"
"net/http"
"git.sendico.io/tech/sxchnge/shared/http/response"
"git.sendico.io/tech/sxchnge/shared/logging"
)
type pendingLoginResponse struct {
Account accountResponse `json:"account"`
PendingToken TokenData `json:"pendingToken"`
Target string `json:"target"`
}
func LoginPending(logger mlogger.Logger, account *customermodel.Account, pendingToken *TokenData, target string) http.HandlerFunc {
return response.Accepted(
logger,
&pendingLoginResponse{
Account: accountResponse{
Account: *NewAccountData(account),
authResponse: authResponse{},
},
PendingToken: *pendingToken,
Target: target,
},
)
}
@@ -0,0 +1,58 @@
package sresponse
import (
customermodel "git.sendico.io/tech/sxchnge/product/customers/model"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
"git.sendico.io/tech/sxchnge/product/sessions/browser"
"github.com/stretchr/testify/require"
"go.uber.org/zap"
)
func TestBrowserLoginMovesRefreshCredentialToHttpOnlyCookie(t *testing.T) {
refresh := &TokenData{Token: "refresh-secret", Expiration: time.Now().Add(time.Hour)}
access := &TokenData{Token: "access-secret", Expiration: time.Now().Add(time.Minute)}
req := httptest.NewRequest(http.MethodPost, "http://app.sendico.io/api/v1/accounts/login", nil)
req.Header.Set(browser.TransportHeader, browser.TransportCookie)
req.Header.Set("X-Forwarded-Proto", "https")
recorder := httptest.NewRecorder()
Login(zap.NewNop(), &customermodel.Account{}, access, refresh).ServeHTTP(recorder, req)
require.Equal(t, http.StatusOK, recorder.Code)
require.NotContains(t, recorder.Body.String(), refresh.Token)
require.Contains(t, recorder.Body.String(), access.Token)
setCookie := recorder.Header().Get("Set-Cookie")
require.True(t, strings.HasPrefix(setCookie, browser.RefreshCookie+"="))
require.Contains(t, setCookie, "HttpOnly")
require.Contains(t, setCookie, "Secure")
require.Contains(t, setCookie, "SameSite=Strict")
}
func TestNativeLoginKeepsRefreshCredentialInResponse(t *testing.T) {
refresh := &TokenData{Token: "native-refresh", Expiration: time.Now().Add(time.Hour)}
access := &TokenData{Token: "native-access", Expiration: time.Now().Add(time.Minute)}
req := httptest.NewRequest(http.MethodPost, "http://native.test/api/v1/accounts/login", nil)
recorder := httptest.NewRecorder()
Login(zap.NewNop(), &customermodel.Account{}, access, refresh).ServeHTTP(recorder, req)
require.Contains(t, recorder.Body.String(), refresh.Token)
require.Empty(t, recorder.Header().Get("Set-Cookie"))
}
func TestCustomerResponseExcludesStorageAndPlatformFields(t *testing.T) {
account := &customermodel.Account{Password: "private-password-hash", Status: customermodel.AccountActive}
account.Login = "customer@example.test"
account.Name = "Customer"
recorder := httptest.NewRecorder()
Account(zap.NewNop(), account, &TokenData{}).ServeHTTP(recorder, httptest.NewRequest(http.MethodGet, "/accounts/me", nil))
require.Contains(t, recorder.Body.String(), "customer@example.test")
for _, field := range []string{"private-password-hash", "password", "status\":\"active", "organization", "role", "policy", "isAnonymous", "createdAt", "isArchived"} {
require.NotContains(t, recorder.Body.String(), field)
}
}
@@ -0,0 +1,14 @@
package sresponse
import (
customermodel "git.sendico.io/tech/sxchnge/product/customers/model"
"net/http"
emodel "git.sendico.io/tech/sxchnge/product/auth/model"
)
type (
HandlerFunc = func(r *http.Request) http.HandlerFunc
AccountHandlerFunc = func(r *http.Request, account *customermodel.Account, accessToken *TokenData) http.HandlerFunc
PendingAccountHandlerFunc = func(r *http.Request, account *customermodel.Account, token *emodel.AccountToken) http.HandlerFunc
)
@@ -0,0 +1,27 @@
package sresponse
import (
"net/http"
"git.sendico.io/tech/sxchnge/shared/http/response"
"git.sendico.io/tech/sxchnge/shared/logging"
)
type resultAuth struct {
authResponse `json:",inline"`
response.Result `json:",inline"`
}
func Success(logger mlogger.Logger, accessToken *TokenData) http.HandlerFunc {
return response.Ok(logger, &resultAuth{
Result: response.Result{Result: true},
authResponse: authResponse{AccessToken: *accessToken},
})
}
func Failed(logger mlogger.Logger, accessToken *TokenData) http.HandlerFunc {
return response.Accepted(logger, &resultAuth{
Result: response.Result{Result: false},
authResponse: authResponse{AccessToken: *accessToken},
})
}
@@ -0,0 +1,8 @@
package sresponse
import "time"
type TokenData struct {
Token string `json:"token"`
Expiration time.Time `json:"expiration"`
}
@@ -0,0 +1,8 @@
package sresponse
type VerificationResponse struct {
IdempotencyKey string `json:"idempotencyKey"`
TTLSeconds int `json:"ttl_seconds"`
CooldownSeconds int `json:"cooldown_seconds"`
Target string `json:"target"`
}