Organize backend into product executable and shared Go modules

This commit is contained in:
Stephan D committed 2026-09-12 14:14:08 +02:00
1 parent 981e84a4b4
commit f0720ec66b
382 files changed
+1944 -6989

No files matched your search

+29
View File
@@ -0,0 +1,29 @@
package client
import (
"context"
"errors"
sessionmodel "git.sendico.io/tech/sxchnge/product/sessions/model"
"strings"
"git.sendico.io/tech/sxchnge/product/sessions/store"
"git.sendico.io/tech/sxchnge/shared/errors"
)
var ErrUnknownClientID = merrors.Define("unknown client id")
func Resolve(ctx context.Context, rtdb refreshtokens.DB, clientID string) (*sessionmodel.Client, error) {
clientID = strings.TrimSpace(clientID)
if clientID == "" {
return nil, ErrUnknownClientID
}
client, err := rtdb.GetClient(ctx, clientID)
if errors.Is(err, merrors.ErrNoData) || client == nil {
return nil, ErrUnknownClientID
}
if err != nil {
return nil, err
}
return client, nil
}
@@ -0,0 +1,53 @@
package client
import (
"context"
"errors"
sessionmodel "git.sendico.io/tech/sxchnge/product/sessions/model"
"testing"
"git.sendico.io/tech/sxchnge/product/sessions/store"
)
type resolveClientDB struct {
refreshtokens.DB
calls int
client *sessionmodel.Client
err error
}
func (db *resolveClientDB) GetClient(ctx context.Context, clientID string) (*sessionmodel.Client, error) {
db.calls++
return db.client, db.err
}
func TestResolveRejectsBlankClientIDWithoutStorageLookup(t *testing.T) {
db := &resolveClientDB{}
client, err := Resolve(context.Background(), db, " ")
if !errors.Is(err, ErrUnknownClientID) {
t.Fatalf("expected unknown client id, got %v", err)
}
if client != nil {
t.Fatalf("expected nil client, got %#v", client)
}
if db.calls != 0 {
t.Fatalf("expected no storage lookup for blank client id, got %d", db.calls)
}
}
func TestResolveTrimsClientID(t *testing.T) {
want := &sessionmodel.Client{ClientID: "client-1"}
db := &resolveClientDB{client: want}
client, err := Resolve(context.Background(), db, " client-1 ")
if err != nil {
t.Fatalf("expected client resolution to succeed, got %v", err)
}
if client != want {
t.Fatalf("expected resolved client %#v, got %#v", want, client)
}
if db.calls != 1 {
t.Fatalf("expected one storage lookup, got %d", db.calls)
}
}
@@ -0,0 +1,36 @@
package middleware
type AuthStorageConfig struct {
AccountsCollection string `yaml:"accounts_collection"`
RefreshTokensCollection string `yaml:"refresh_tokens_collection"`
ClientsCollection string `yaml:"clients_collection"`
VerificationTokensCollection string `yaml:"verification_tokens_collection"`
LoginAttemptsCollection string `yaml:"login_attempts_collection"`
VerificationRateLimitsCollection string `yaml:"verification_rate_limits_collection"`
}
func (c *AuthStorageConfig) WithDefaults() AuthStorageConfig {
if c == nil {
c = &AuthStorageConfig{}
}
res := *c
if res.AccountsCollection == "" {
res.AccountsCollection = "accounts"
}
if res.RefreshTokensCollection == "" {
res.RefreshTokensCollection = "refresh_tokens"
}
if res.ClientsCollection == "" {
res.ClientsCollection = "clients"
}
if res.VerificationTokensCollection == "" {
res.VerificationTokensCollection = "verification_tokens"
}
if res.LoginAttemptsCollection == "" {
res.LoginAttemptsCollection = "login_attempts"
}
if res.VerificationRateLimitsCollection == "" {
res.VerificationRateLimitsCollection = "verification_rate_limits"
}
return res
}
+49
View File
@@ -0,0 +1,49 @@
package middleware
import "testing"
func TestCredentialedCORSRequiresExplicitOrigins(t *testing.T) {
tests := []struct {
name string
config CORSSettings
wantErr bool
}{
{
name: "explicit origin",
config: CORSSettings{
AllowCredentials: true,
AllowedOrigins: []string{"https://app.sendico.io"},
},
},
{
name: "wildcard",
config: CORSSettings{
AllowCredentials: true,
AllowedOrigins: []string{"*"},
},
wantErr: true,
},
{
name: "missing origin",
config: CORSSettings{
AllowCredentials: true,
},
wantErr: true,
},
{
name: "non-credentialed compatibility",
config: CORSSettings{
AllowedOrigins: []string{"*"},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := tt.config.Validate()
if (err != nil) != tt.wantErr {
t.Fatalf("Validate() error = %v, wantErr %t", err, tt.wantErr)
}
})
}
}
@@ -0,0 +1,47 @@
package middleware
import (
"time"
sharedlimit "git.sendico.io/tech/sxchnge/shared/ratelimit"
)
const (
defaultLoginAccountLimit = 5
defaultLoginAccountWindow = 15 * time.Minute
defaultLoginIPLimit = 50
defaultLoginIPWindow = 15 * time.Minute
)
type LoginRateLimitWindowConfig = sharedlimit.WindowConfig
type LoginRateLimitsConfig struct {
PerAccount LoginRateLimitWindowConfig `yaml:"per_account"`
PerIP LoginRateLimitWindowConfig `yaml:"per_ip"`
}
type LoginRateLimitWindow = sharedlimit.Window
type LoginRateLimits struct {
PerAccount LoginRateLimitWindow
PerIP LoginRateLimitWindow
}
func (c *Config) ResolveLoginRateLimits() (LoginRateLimits, error) {
if c == nil || c.LoginRateLimits == nil {
return LoginRateLimits{
PerAccount: LoginRateLimitWindow{Limit: defaultLoginAccountLimit, Window: defaultLoginAccountWindow},
PerIP: LoginRateLimitWindow{Limit: defaultLoginIPLimit, Window: defaultLoginIPWindow},
}, nil
}
account, err := sharedlimit.ResolveWindow(c.LoginRateLimits.PerAccount, "middleware.login_rate_limits.per_account")
if err != nil {
return LoginRateLimits{}, err
}
ip, err := sharedlimit.ResolveWindow(c.LoginRateLimits.PerIP, "middleware.login_rate_limits.per_ip")
if err != nil {
return LoginRateLimits{}, err
}
return LoginRateLimits{PerAccount: account, PerIP: ip}, nil
}
@@ -0,0 +1,60 @@
package middleware
import (
"errors"
"testing"
"time"
"git.sendico.io/tech/sxchnge/shared/errors"
)
func TestResolveLoginRateLimitsUsesSecureDefaults(t *testing.T) {
got, err := (&Config{}).ResolveLoginRateLimits()
if err != nil {
t.Fatalf("resolve defaults: %v", err)
}
if got.PerAccount.Limit != 5 || got.PerAccount.Window != 15*time.Minute {
t.Fatalf("unexpected account defaults: %+v", got.PerAccount)
}
if got.PerIP.Limit != 50 || got.PerIP.Window != 15*time.Minute {
t.Fatalf("unexpected IP defaults: %+v", got.PerIP)
}
}
func TestResolveLoginRateLimitsRejectsUnsafeConfiguration(t *testing.T) {
tests := []struct {
name string
cfg *LoginRateLimitsConfig
}{
{
name: "zero account threshold",
cfg: &LoginRateLimitsConfig{
PerAccount: LoginRateLimitWindowConfig{Window: "15m"},
PerIP: LoginRateLimitWindowConfig{Limit: 50, Window: "15m"},
},
},
{
name: "invalid IP window",
cfg: &LoginRateLimitsConfig{
PerAccount: LoginRateLimitWindowConfig{Limit: 5, Window: "15m"},
PerIP: LoginRateLimitWindowConfig{Limit: 50, Window: "forever"},
},
},
{
name: "excessive account window",
cfg: &LoginRateLimitsConfig{
PerAccount: LoginRateLimitWindowConfig{Limit: 5, Window: "25h"},
PerIP: LoginRateLimitWindowConfig{Limit: 50, Window: "15m"},
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
_, err := (&Config{LoginRateLimits: test.cfg}).ResolveLoginRateLimits()
if !errors.Is(err, merrors.ErrInvalidArg) {
t.Fatalf("expected invalid argument, got %v", err)
}
})
}
}
+110
View File
@@ -0,0 +1,110 @@
package middleware
import (
"os"
"strings"
"git.sendico.io/tech/sxchnge/shared/errors"
)
type CORSSettings struct {
MaxAge int `yaml:"max_age"`
AllowedOrigins []string `yaml:"allowed_origins"`
AllowedMethods []string `yaml:"allowed_methods"`
AllowedHeaders []string `yaml:"allowed_headers"`
ExposedHeaders []string `yaml:"exposed_headers"`
AllowCredentials bool `yaml:"allow_credentials"`
}
func (c CORSSettings) Validate() error {
if !c.AllowCredentials {
return nil
}
if len(c.AllowedOrigins) == 0 {
return merrors.InvalidArgument("credentialed CORS requires explicit allowed origins", "middleware.CORS.allowed_origins")
}
for _, origin := range c.AllowedOrigins {
if strings.TrimSpace(origin) == "*" {
return merrors.InvalidArgument("credentialed CORS does not allow wildcard origins", "middleware.CORS.allowed_origins")
}
}
return nil
}
type Signature struct {
PublicKey any
PrivateKey []byte
Algorithm string
}
type SignatureConfig struct {
PublicKeyEnv string `yaml:"public_key_env,omitempty"`
PrivateKeyEnv string `yaml:"secret_key_env"`
Algorithm string `yaml:"algorithm"`
}
type TokenExpiration struct {
Account int `yaml:"account"`
Refresh int `yaml:"refresh"`
}
type TokenConfig struct {
Expiration TokenExpiration `yaml:"expiration_hours"`
Length int `yaml:"length"`
}
const MaxAccessTokenTTLHours = 1
func (c TokenConfig) Validate() error {
if c.Expiration.Account <= 0 || c.Expiration.Account > MaxAccessTokenTTLHours {
return merrors.InvalidArgument("access token expiration must be between 1 and 1 hour", "middleware.token.expiration_hours.account")
}
if c.Expiration.Refresh <= c.Expiration.Account {
return merrors.InvalidArgument("refresh token expiration must exceed access token expiration", "middleware.token.expiration_hours.refresh")
}
if c.Length < 32 {
return merrors.InvalidArgument("refresh token length must be at least 32 bytes", "middleware.token.length")
}
return nil
}
type PasswordChecks struct {
Digit bool `yaml:"digit"`
Upper bool `yaml:"upper"`
Lower bool `yaml:"lower"`
Special bool `yaml:"special"`
MinLength int `yaml:"min_length"`
}
type PasswordConfig struct {
TokenLength int `yaml:"token_length"`
Check PasswordChecks `yaml:"check"`
}
type Config struct {
DomainEnv string `yaml:"domain_env"`
EndPointEnv string `yaml:"api_endpoint_env"`
APIProtocolEnv string `yaml:"api_protocol_env"`
Signature SignatureConfig `yaml:"signature"`
CORS CORSSettings `yaml:"CORS"`
Token TokenConfig `yaml:"token"`
Password PasswordConfig `yaml:"password"`
LoginRateLimits *LoginRateLimitsConfig `yaml:"login_rate_limits"`
}
type MapClaims = map[string]any
func getKey(osEnv string) any {
if len(osEnv) == 0 {
return nil
}
return []byte(os.Getenv(osEnv))
}
func SignatureConf(conf *Config) Signature {
return Signature{
PrivateKey: []byte(os.Getenv(conf.Signature.PrivateKeyEnv)),
PublicKey: getKey(conf.Signature.PublicKeyEnv),
Algorithm: conf.Signature.Algorithm,
}
}
+52
View File
@@ -0,0 +1,52 @@
package middleware
import "testing"
func TestTokenConfigValidate(t *testing.T) {
tests := []struct {
name string
config TokenConfig
wantErr bool
}{
{
name: "secure explicit TTLs",
config: TokenConfig{
Expiration: TokenExpiration{Account: 1, Refresh: 720},
Length: 32,
},
},
{
name: "rejects legacy 24 hour access token",
config: TokenConfig{
Expiration: TokenExpiration{Account: 24, Refresh: 720},
Length: 32,
},
wantErr: true,
},
{
name: "rejects missing access TTL",
config: TokenConfig{
Expiration: TokenExpiration{Refresh: 720},
Length: 32,
},
wantErr: true,
},
{
name: "rejects weak refresh token",
config: TokenConfig{
Expiration: TokenExpiration{Account: 1, Refresh: 720},
Length: 16,
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := tt.config.Validate()
if (err != nil) != tt.wantErr {
t.Fatalf("Validate() error = %v, wantErr %t", err, tt.wantErr)
}
})
}
}
@@ -0,0 +1,75 @@
package authorized
import (
"context"
customermodel "git.sendico.io/tech/sxchnge/product/customers/model"
sessionmodel "git.sendico.io/tech/sxchnge/product/sessions/model"
"net/http"
"net/http/httptest"
"testing"
"git.sendico.io/tech/sxchnge/product/auth/config"
emodel "git.sendico.io/tech/sxchnge/product/auth/model"
"git.sendico.io/tech/sxchnge/product/auth/response"
"git.sendico.io/tech/sxchnge/product/customers/store"
"git.sendico.io/tech/sxchnge/shared/errors"
api "git.sendico.io/tech/sxchnge/shared/http"
"git.sendico.io/tech/sxchnge/shared/service"
"github.com/go-chi/chi/v5"
"github.com/go-chi/jwtauth/v5"
"github.com/stretchr/testify/require"
"go.mongodb.org/mongo-driver/v2/bson"
"go.uber.org/zap"
)
type customerDB struct {
account.DB
customer customermodel.Account
requested bson.ObjectID
}
func (d *customerDB) Get(_ context.Context, id bson.ObjectID, result *customermodel.Account) error {
d.requested = id
if id != d.customer.ID {
return merrors.NoData("customer not found")
}
*result = d.customer
return nil
}
func (d *sessionDB) GetClient(context.Context, string) (*sessionmodel.Client, error) {
return &sessionmodel.Client{ClientID: "web"}, nil
}
func TestCustomerHandlerResolvesIdentityWithoutRoles(t *testing.T) {
for _, status := range []customermodel.AccountStatus{customermodel.AccountActive, customermodel.AccountBlocked, customermodel.AccountPendingVerification} {
t.Run(string(status), func(t *testing.T) {
accounts := &customerDB{}
accounts.customer.ID = bson.NewObjectID()
accounts.customer.Status = status
routes := chi.NewRouter()
signature := &middleware.Signature{Algorithm: "HS256", PrivateKey: []byte("test-only-signing-key-at-least-32-bytes")}
router := NewRouter(zap.NewNop(), "/api/v1", routes, accounts, &sessionDB{token: &sessionmodel.RefreshToken{}}, &middleware.TokenConfig{Expiration: middleware.TokenExpiration{Account: 1}}, signature)
called := false
router.AccountHandler(mservice.Accounts, "/me", api.Get, func(_ *http.Request, c *customermodel.Account, _ *sresponse.TokenData) http.HandlerFunc {
called = true
require.Equal(t, accounts.customer.ID, c.ID)
return func(w http.ResponseWriter, _ *http.Request) { w.WriteHeader(http.StatusNoContent) }
})
ja := jwtauth.New(signature.Algorithm, signature.PrivateKey, nil)
_, token, err := ja.Encode(emodel.Account2ClaimsForSession(&accounts.customer, 1, sessionmodel.SessionIdentifier{ClientID: "web", DeviceID: "device", SessionVersion: "v1"}))
require.NoError(t, err)
req := httptest.NewRequest(http.MethodGet, "/api/v1/accounts/me?accountRef="+bson.NewObjectID().Hex(), nil)
req.Header.Set("Authorization", "Bearer "+token)
recorder := httptest.NewRecorder()
routes.ServeHTTP(recorder, req)
require.Equal(t, accounts.customer.ID, accounts.requested)
if status == customermodel.AccountActive {
require.Equal(t, http.StatusNoContent, recorder.Code)
require.True(t, called)
} else {
require.Equal(t, http.StatusUnauthorized, recorder.Code)
require.False(t, called)
}
})
}
}
@@ -0,0 +1,161 @@
package authorized
import (
"errors"
customermodel "git.sendico.io/tech/sxchnge/product/customers/model"
"net/http"
"strings"
"git.sendico.io/tech/sxchnge/product/auth/ipguard"
emodel "git.sendico.io/tech/sxchnge/product/auth/model"
"git.sendico.io/tech/sxchnge/product/auth/response"
"git.sendico.io/tech/sxchnge/product/sessions/metrics"
"git.sendico.io/tech/sxchnge/shared/errors"
api "git.sendico.io/tech/sxchnge/shared/http"
"git.sendico.io/tech/sxchnge/shared/http/response"
"git.sendico.io/tech/sxchnge/shared/logging/fields"
"git.sendico.io/tech/sxchnge/shared/service"
"github.com/go-chi/jwtauth/v5"
"go.uber.org/zap"
)
type tokenHandlerFunc = func(r *http.Request, t *emodel.AccountToken) http.HandlerFunc
func (ar *AuthorizedRouter) validateClientPolicy(r *http.Request, t *emodel.AccountToken) http.HandlerFunc {
clientID := strings.TrimSpace(t.ClientID)
if clientID == "" {
// Legacy tokens without client_id remain valid until expiration.
return nil
}
client, err := ar.rtdb.GetClient(r.Context(), clientID)
if errors.Is(err, merrors.ErrNoData) || client == nil {
ar.logger.Debug("Client not found for access token", zap.String("client_id", clientID))
return response.Unauthorized(ar.logger, ar.service, "client not found")
}
if err != nil {
ar.logger.Warn("Failed to resolve client for access token", zap.Error(err), zap.String("client_id", clientID))
return response.Internal(ar.logger, ar.service, err)
}
if client.IsRevoked {
return response.Unauthorized(ar.logger, ar.service, "client has been revoked")
}
if client.AccountRef != nil && *client.AccountRef != t.AccountRef {
return response.Unauthorized(ar.logger, ar.service, "client account mismatch")
}
clientIP := ipguard.ClientIP(r)
allowed, err := ipguard.Allowed(clientIP, client.AllowedCIDRs)
if err != nil {
ar.logger.Warn("Client IP policy contains invalid CIDR", zap.Error(err), zap.String("client_id", clientID))
return response.Forbidden(ar.logger, ar.service, "client_ip_policy_invalid", "client ip policy is invalid")
}
if !allowed {
rawIP := ""
if clientIP != nil {
rawIP = clientIP.String()
}
ar.logger.Info("Client IP policy denied authorized request", zap.String("client_id", clientID), zap.String("remote_ip", rawIP))
return response.Forbidden(ar.logger, ar.service, "ip_not_allowed", "request ip is not allowed for this client")
}
return nil
}
func (ar *AuthorizedRouter) validateSession(r *http.Request, t *emodel.AccountToken) http.HandlerFunc {
if t.Pending {
return nil
}
if strings.TrimSpace(t.ClientID) == "" || strings.TrimSpace(t.DeviceID) == "" || strings.TrimSpace(t.SessionVersion) == "" {
sessionmetrics.Observe(sessionmetrics.Validation, sessionmetrics.MissingIdentity)
ar.logger.Debug("Access token has no session identifier", mzap.AccRef(t.AccountRef))
return response.Unauthorized(ar.logger, ar.service, "session identifier is required")
}
if _, err := ar.rtdb.GetSession(r.Context(), t.AccountRef, &t.SessionIdentifier); err != nil {
if errors.Is(err, merrors.ErrNoData) || errors.Is(err, merrors.ErrAccessDenied) {
sessionmetrics.Observe(sessionmetrics.Validation, sessionmetrics.AlreadyInactive)
ar.logger.Debug("Access token session is not active", mzap.AccRef(t.AccountRef),
zap.String("client_id", t.ClientID), zap.String("device_id", t.DeviceID))
return response.Unauthorized(ar.logger, ar.service, "session is not active")
}
sessionmetrics.Observe(sessionmetrics.Validation, sessionmetrics.StorageError)
ar.logger.Warn("Failed to validate access token session", zap.Error(err), mzap.AccRef(t.AccountRef),
zap.String("client_id", t.ClientID), zap.String("device_id", t.DeviceID))
return response.Internal(ar.logger, ar.service, err)
}
sessionmetrics.Observe(sessionmetrics.Validation, sessionmetrics.Active)
return nil
}
func (ar *AuthorizedRouter) tokenHandlerWithSessionCheck(service mservice.Type, endpoint string, method api.HTTPMethod, checkSession bool, handler tokenHandlerFunc) {
hndlr := func(r *http.Request) http.HandlerFunc {
_, claims, err := jwtauth.FromContext(r.Context())
if err != nil {
ar.logger.Debug("Authorization failed", zap.Error(err), zap.String("request", r.URL.Path))
return response.Unauthorized(ar.logger, ar.service, "credentials required")
}
t, err := emodel.Claims2Token(claims)
if err != nil {
ar.logger.Debug("Failed to decode account token", zap.Error(err))
return response.BadRequest(ar.logger, ar.service, "credentials_unreadable", "faild to parse credentials")
}
if h := ar.validateClientPolicy(r, t); h != nil {
return h
}
if checkSession {
if h := ar.validateSession(r, t); h != nil {
return h
}
}
return handler(r, t)
}
ar.imp.InstallHandler(service, endpoint, method, hndlr)
}
func (ar *AuthorizedRouter) tokenHandler(service mservice.Type, endpoint string, method api.HTTPMethod, handler tokenHandlerFunc) {
ar.tokenHandlerWithSessionCheck(service, endpoint, method, true, handler)
}
func (ar *AuthorizedRouter) AccountHandler(service mservice.Type, endpoint string, method api.HTTPMethod, handler sresponse.AccountHandlerFunc) {
hndlr := func(r *http.Request, t *emodel.AccountToken) http.HandlerFunc {
if t.Pending {
return response.Unauthorized(ar.logger, ar.service, "additional verification required")
}
var a customermodel.Account
if err := ar.db.Get(r.Context(), t.AccountRef, &a); err != nil {
if errors.Is(err, merrors.ErrNoData) {
ar.logger.Debug("Failed to find related user", zap.Error(err), mzap.AccRef(t.AccountRef))
return response.NotFound(ar.logger, ar.service, err.Error())
}
return response.Internal(ar.logger, ar.service, err)
}
if !a.IsActive() || a.IsArchived() {
return response.Unauthorized(ar.logger, ar.service, "customer is inactive")
}
accessToken, err := ar.imp.CreateAccessTokenForSession(&a, t.SessionIdentifier)
if err != nil {
ar.logger.Warn("Failed to generate access token", zap.Error(err))
return response.Internal(ar.logger, ar.service, err)
}
return handler(r, &a, &accessToken)
}
ar.tokenHandler(service, endpoint, method, hndlr)
}
func (ar *AuthorizedRouter) PendingAccountHandler(service mservice.Type, endpoint string, method api.HTTPMethod, handler sresponse.PendingAccountHandlerFunc) {
hndlr := func(r *http.Request, t *emodel.AccountToken) http.HandlerFunc {
var a customermodel.Account
if err := ar.db.Get(r.Context(), t.AccountRef, &a); err != nil {
if errors.Is(err, merrors.ErrNoData) {
ar.logger.Debug("Failed to find related user", zap.Error(err), mzap.AccRef(t.AccountRef))
return response.NotFound(ar.logger, ar.service, err.Error())
}
return response.Internal(ar.logger, ar.service, err)
}
if a.Status == customermodel.AccountBlocked || a.IsArchived() {
return response.Unauthorized(ar.logger, ar.service, "customer is inactive")
}
return handler(r, &a, t)
}
ar.tokenHandler(service, endpoint, method, hndlr)
}
@@ -0,0 +1,60 @@
package authorized
import (
"net/http"
"git.sendico.io/tech/sxchnge/product/auth/config"
re "git.sendico.io/tech/sxchnge/product/auth/http/endpoint"
emodel "git.sendico.io/tech/sxchnge/product/auth/model"
"git.sendico.io/tech/sxchnge/product/customers/store"
"git.sendico.io/tech/sxchnge/product/sessions/metrics"
"git.sendico.io/tech/sxchnge/product/sessions/store"
api "git.sendico.io/tech/sxchnge/shared/http"
"git.sendico.io/tech/sxchnge/shared/http/response"
"git.sendico.io/tech/sxchnge/shared/logging"
"git.sendico.io/tech/sxchnge/shared/logging/fields"
"git.sendico.io/tech/sxchnge/shared/service"
"github.com/go-chi/chi/v5"
"github.com/go-chi/jwtauth/v5"
"go.uber.org/zap"
)
type AuthorizedRouter struct {
logger mlogger.Logger
db account.DB
rtdb refreshtokens.DB
imp *re.HttpEndpointRouter
service mservice.Type
}
func NewRouter(logger mlogger.Logger, apiEndpoint string, router chi.Router, db account.DB, rtdb refreshtokens.DB, config *middleware.TokenConfig, signature *middleware.Signature) *AuthorizedRouter {
ja := jwtauth.New(signature.Algorithm, signature.PrivateKey, signature.PublicKey)
router.Use(jwtauth.Verifier(ja))
router.Use(jwtauth.Authenticator(ja))
l := logger.Named("authorized")
ar := AuthorizedRouter{
logger: l,
db: db,
rtdb: rtdb,
imp: re.NewHttpEndpointRouter(l, apiEndpoint, router, config, signature),
service: mservice.Accounts,
}
ar.tokenHandlerWithSessionCheck(mservice.Accounts, "/logout/all", api.Post, false, ar.logoutAll)
return &ar
}
func (ar *AuthorizedRouter) logoutAll(r *http.Request, token *emodel.AccountToken) http.HandlerFunc {
if token.ClientID == "" || token.DeviceID == "" || token.SessionVersion == "" {
sessionmetrics.Observe(sessionmetrics.AllLogout, sessionmetrics.MissingIdentity)
return response.Unauthorized(ar.logger, ar.service, "session identifier is required")
}
if err := ar.rtdb.RevokeAll(r.Context(), token.AccountRef, ""); err != nil {
sessionmetrics.Observe(sessionmetrics.AllLogout, sessionmetrics.StorageError)
ar.logger.Warn("Failed to revoke all account sessions", zap.Error(err), mzap.AccRef(token.AccountRef))
return response.Internal(ar.logger, ar.service, err)
}
sessionmetrics.Observe(sessionmetrics.AllLogout, sessionmetrics.Succeeded)
ar.logger.Info("All account sessions revoked", mzap.AccRef(token.AccountRef))
return response.Success(ar.logger)
}
@@ -0,0 +1,113 @@
package authorized
import (
"context"
sessionmodel "git.sendico.io/tech/sxchnge/product/sessions/model"
"net/http"
"net/http/httptest"
"testing"
emodel "git.sendico.io/tech/sxchnge/product/auth/model"
"git.sendico.io/tech/sxchnge/product/sessions/store"
"git.sendico.io/tech/sxchnge/shared/errors"
"git.sendico.io/tech/sxchnge/shared/service"
"go.mongodb.org/mongo-driver/v2/bson"
"go.uber.org/zap"
)
type sessionDB struct {
refreshtokens.DB
token *sessionmodel.RefreshToken
err error
revokeAllID string
}
func (db *sessionDB) GetSession(context.Context, bson.ObjectID, *sessionmodel.SessionIdentifier) (*sessionmodel.RefreshToken, error) {
return db.token, db.err
}
func (db *sessionDB) RevokeAll(_ context.Context, _ bson.ObjectID, deviceID string) error {
db.revokeAllID = deviceID
return db.err
}
func TestValidateSessionFailsClosed(t *testing.T) {
accountRef := bson.NewObjectID()
token := &emodel.AccountToken{
AccountRef: accountRef,
SessionIdentifier: sessionmodel.SessionIdentifier{
ClientID: "web",
DeviceID: "device-a",
SessionVersion: "version-a",
},
}
request := httptest.NewRequestWithContext(context.Background(), http.MethodGet, "/accounts", nil)
tests := []struct {
name string
db *sessionDB
mutate func(*emodel.AccountToken)
wantStatus int
}{
{name: "active", db: &sessionDB{token: &sessionmodel.RefreshToken{}}, wantStatus: 0},
{name: "revoked", db: &sessionDB{err: merrors.ErrNoData}, wantStatus: http.StatusUnauthorized},
{name: "storage unavailable", db: &sessionDB{err: merrors.Internal("database unavailable")}, wantStatus: http.StatusInternalServerError},
{
name: "missing version",
db: &sessionDB{},
mutate: func(token *emodel.AccountToken) {
token.SessionVersion = ""
},
wantStatus: http.StatusUnauthorized,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
candidate := *token
if tt.mutate != nil {
tt.mutate(&candidate)
}
router := &AuthorizedRouter{logger: zap.NewNop(), rtdb: tt.db, service: mservice.Accounts}
handler := router.validateSession(request, &candidate)
if tt.wantStatus == 0 {
if handler != nil {
t.Fatal("active session was rejected")
}
return
}
if handler == nil {
t.Fatal("invalid session was accepted")
}
response := httptest.NewRecorder()
handler(response, request)
if response.Code != tt.wantStatus {
t.Fatalf("status = %d, want %d", response.Code, tt.wantStatus)
}
})
}
}
func TestLogoutAllRevokesEveryDevice(t *testing.T) {
db := &sessionDB{}
router := &AuthorizedRouter{logger: zap.NewNop(), rtdb: db, service: mservice.Accounts}
token := &emodel.AccountToken{
AccountRef: bson.NewObjectID(),
SessionIdentifier: sessionmodel.SessionIdentifier{
ClientID: "web",
DeviceID: "device-a",
SessionVersion: "version-a",
},
}
request := httptest.NewRequestWithContext(context.Background(), http.MethodPost, "/accounts/logout/all", nil)
response := httptest.NewRecorder()
router.logoutAll(request, token)(response, request)
if response.Code != http.StatusOK {
t.Fatalf("status = %d, want %d", response.Code, http.StatusOK)
}
if db.revokeAllID != "" {
t.Fatalf("device exclusion = %q, want all devices", db.revokeAllID)
}
}
+64
View File
@@ -0,0 +1,64 @@
package router
import (
"os"
"github.com/go-chi/chi/v5"
"git.sendico.io/tech/sxchnge/product/auth/config"
rauthorized "git.sendico.io/tech/sxchnge/product/auth/http/authorized"
rpublic "git.sendico.io/tech/sxchnge/product/auth/http/public"
"git.sendico.io/tech/sxchnge/product/auth/response"
"git.sendico.io/tech/sxchnge/product/customers/store"
"git.sendico.io/tech/sxchnge/product/sessions/store"
"git.sendico.io/tech/sxchnge/product/verification/store"
api "git.sendico.io/tech/sxchnge/shared/http"
"git.sendico.io/tech/sxchnge/shared/logging"
"git.sendico.io/tech/sxchnge/shared/ratelimit/store"
"git.sendico.io/tech/sxchnge/shared/service"
)
type Dispatcher struct {
logger mlogger.Logger
public APIRouter
protected ProtectedAPIRouter
}
func (d *Dispatcher) Handler(service mservice.Type, endpoint string, method api.HTTPMethod, handler sresponse.HandlerFunc) {
d.public.InstallHandler(service, endpoint, method, handler)
}
func (d *Dispatcher) AccountHandler(service mservice.Type, endpoint string, method api.HTTPMethod, handler sresponse.AccountHandlerFunc) {
d.protected.AccountHandler(service, endpoint, method, handler)
}
func (d *Dispatcher) PendingAccountHandler(service mservice.Type, endpoint string, method api.HTTPMethod, handler sresponse.PendingAccountHandlerFunc) {
d.protected.PendingAccountHandler(service, endpoint, method, handler)
}
func NewDispatcher(
logger mlogger.Logger,
router chi.Router,
db account.DB,
vdb verification.DB,
rtdb refreshtokens.DB,
attempts ratelimits.Store,
config *middleware.Config,
loginRateLimits middleware.LoginRateLimits,
) *Dispatcher {
d := &Dispatcher{
logger: logger.Named("api_dispatcher"),
}
d.logger.Debug("Installing endpoints middleware...")
endpoint := os.Getenv(config.EndPointEnv)
signature := middleware.SignatureConf(config)
router.Group(func(r chi.Router) {
d.public = rpublic.NewRouter(d.logger, endpoint, db, vdb, rtdb, attempts, r, &config.Token, &signature, loginRateLimits)
})
router.Group(func(r chi.Router) {
d.protected = rauthorized.NewRouter(d.logger, endpoint, r, db, rtdb, &config.Token, &signature)
})
return d
}
@@ -0,0 +1,37 @@
package endpoint
import (
"context"
customermodel "git.sendico.io/tech/sxchnge/product/customers/model"
sessionmodel "git.sendico.io/tech/sxchnge/product/sessions/model"
"net/http"
"git.sendico.io/tech/sxchnge/product/auth/config"
"git.sendico.io/tech/sxchnge/product/auth/response"
"git.sendico.io/tech/sxchnge/shared/logging"
"github.com/go-chi/chi/v5"
)
type (
RegistratorT = func(chi.Router, string, http.HandlerFunc)
ResponderFunc = func(ctx context.Context, r *http.Request, session *sessionmodel.SessionIdentifier, account *customermodel.Account, accessToken *sresponse.TokenData) http.HandlerFunc
)
type HttpEndpointRouter struct {
logger mlogger.Logger
apiEndpoint string
router chi.Router
config middleware.TokenConfig
signature middleware.Signature
}
func NewHttpEndpointRouter(logger mlogger.Logger, apiEndpoint string, router chi.Router, config *middleware.TokenConfig, signature *middleware.Signature) *HttpEndpointRouter {
er := HttpEndpointRouter{
logger: logger.Named("http"),
apiEndpoint: apiEndpoint,
router: router,
signature: *signature,
config: *config,
}
return &er
}
@@ -0,0 +1,55 @@
package endpoint
import (
"net/http"
"path"
"git.sendico.io/tech/sxchnge/product/auth/response"
api "git.sendico.io/tech/sxchnge/shared/http"
"git.sendico.io/tech/sxchnge/shared/service"
"github.com/go-chi/chi/v5"
"go.uber.org/zap"
)
func (er *HttpEndpointRouter) chooseMethod(method api.HTTPMethod) RegistratorT {
switch method {
case api.Get:
return func(r chi.Router, p string, h http.HandlerFunc) { r.Get(p, h) }
case api.Post:
return func(r chi.Router, p string, h http.HandlerFunc) { r.Post(p, h) }
case api.Put:
return func(r chi.Router, p string, h http.HandlerFunc) { r.Put(p, h) }
case api.Delete:
return func(r chi.Router, p string, h http.HandlerFunc) { r.Delete(p, h) }
case api.Patch:
return func(r chi.Router, p string, h http.HandlerFunc) { r.Patch(p, h) }
case api.Options:
return func(r chi.Router, p string, h http.HandlerFunc) { r.Options(p, h) }
case api.Head:
return func(r chi.Router, p string, h http.HandlerFunc) { r.Head(p, h) }
default:
return nil
}
}
func (er *HttpEndpointRouter) endpoint(service mservice.Type, handler string) string {
return path.Join(er.apiEndpoint, service, handler)
}
func (er *HttpEndpointRouter) InstallHandler(service mservice.Type, endpoint string, method api.HTTPMethod, handler sresponse.HandlerFunc) {
ep := er.endpoint(service, endpoint)
hm := er.chooseMethod(method)
if hm == nil {
er.logger.Warn("Handler install skipped because method is unsupported",
zap.String("endpoint", ep),
zap.String("method", api.HTTPMethod2String(method)),
)
return
}
hndlr := func(w http.ResponseWriter, r *http.Request) {
res := handler(r)
res(w, r)
}
hm(er.router, ep, hndlr)
er.logger.Info("Handler installed", zap.String("endpoint", ep), zap.String("method", api.HTTPMethod2String(method)))
}
@@ -0,0 +1,31 @@
package endpoint
import (
customermodel "git.sendico.io/tech/sxchnge/product/customers/model"
sessionmodel "git.sendico.io/tech/sxchnge/product/sessions/model"
"time"
emodel "git.sendico.io/tech/sxchnge/product/auth/model"
"git.sendico.io/tech/sxchnge/product/auth/response"
"github.com/go-chi/jwtauth/v5"
)
func (er *HttpEndpointRouter) CreateAccessTokenForSession(user *customermodel.Account, session sessionmodel.SessionIdentifier) (sresponse.TokenData, error) {
ja := jwtauth.New(er.signature.Algorithm, er.signature.PrivateKey, er.signature.PublicKey)
_, res, err := ja.Encode(emodel.Account2ClaimsForSession(user, er.config.Expiration.Account, session))
token := sresponse.TokenData{
Token: res,
Expiration: time.Now().Add(time.Duration(er.config.Expiration.Account) * time.Hour),
}
return token, err
}
func (er *HttpEndpointRouter) CreatePendingToken(user *customermodel.Account, ttlMinutes int) (sresponse.TokenData, error) {
ja := jwtauth.New(er.signature.Algorithm, er.signature.PrivateKey, er.signature.PublicKey)
_, res, err := ja.Encode(emodel.PendingAccount2Claims(user, ttlMinutes))
token := sresponse.TokenData{
Token: res,
Expiration: time.Now().Add(time.Duration(ttlMinutes) * time.Minute),
}
return token, err
}
+250
View File
@@ -0,0 +1,250 @@
package public
import (
"context"
"crypto/subtle"
"encoding/json"
"errors"
customermodel "git.sendico.io/tech/sxchnge/product/customers/model"
sessionmodel "git.sendico.io/tech/sxchnge/product/sessions/model"
"net/http"
"strings"
clientauth "git.sendico.io/tech/sxchnge/product/auth/client"
"git.sendico.io/tech/sxchnge/product/auth/ipguard"
"git.sendico.io/tech/sxchnge/product/auth/request"
"git.sendico.io/tech/sxchnge/product/auth/response"
"git.sendico.io/tech/sxchnge/shared/errors"
"git.sendico.io/tech/sxchnge/shared/http/response"
"git.sendico.io/tech/sxchnge/shared/logging"
"git.sendico.io/tech/sxchnge/shared/logging/mask"
"git.sendico.io/tech/sxchnge/shared/service"
"go.uber.org/zap"
)
const pendingLoginTTLMinutes = 10
const apiLoginGrantType = "password"
const apiLoginClientAuthMethod = "client_secret_post"
const dummyPasswordHash = "$2y$10$pmvNFN5zyooml6Oqh.cGFenBF/indHDE.dyLwjNJW1.Tk2Suvdvve"
func (pr *PublicRouter) authenticateAccount(ctx context.Context, req *srequest.Login) (*customermodel.Account, bool, http.HandlerFunc) {
// Get the account database entry
trimmedLogin := strings.TrimSpace(req.Login)
account, err := pr.db.GetByEmail(ctx, strings.ToLower(trimmedLogin))
if errors.Is(err, merrors.ErrNoData) || (err == nil && account == nil) {
(&customermodel.Account{Password: dummyPasswordHash}).MatchPassword(req.Password)
pr.logger.Debug("Login rejected: invalid credentials")
return nil, true, response.Unauthorized(pr.logger, pr.service, "invalid credentials")
}
if err != nil {
pr.logger.Warn("Failed to query user during login", zap.Error(err))
return nil, false, response.Internal(pr.logger, pr.service, err)
}
if !account.MatchPassword(req.Password) {
pr.logger.Debug("Login rejected: invalid credentials")
return nil, true, response.Unauthorized(pr.logger, pr.service, "invalid credentials")
}
if !account.IsActive() || account.IsArchived() {
return nil, false, response.Forbidden(pr.logger, pr.service, "account_not_verified", "Account verification required")
}
return account, false, nil
}
func (pr *PublicRouter) respondPendingLogin(account *customermodel.Account) http.HandlerFunc {
pendingToken, err := pr.imp.CreatePendingToken(account, pendingLoginTTLMinutes)
if err != nil {
pr.logger.Warn("Failed to generate pending token", zap.Error(err))
return response.Internal(pr.logger, pr.service, err)
}
return sresponse.LoginPending(pr.logger, account, &pendingToken, mask.Email(account.Login))
}
func hasGrantType(grants []string, target string) bool {
for _, grant := range grants {
if strings.EqualFold(strings.TrimSpace(grant), target) {
return true
}
}
return false
}
func (pr *PublicRouter) validateClientIPPolicy(r *http.Request, clientID string, client *sessionmodel.Client) http.HandlerFunc {
if client == nil {
pr.logger.Debug("Client not found, rejecting authorization", zap.String("client_id", clientID))
return response.Unauthorized(pr.logger, pr.service, clientauth.ErrUnknownClientID.Error())
}
clientIP := ipguard.ClientIP(r)
allowed, err := ipguard.Allowed(clientIP, client.AllowedCIDRs)
if err != nil {
pr.logger.Warn("Client IP policy contains invalid CIDR", zap.Error(err), zap.String("client_id", clientID))
return response.Forbidden(pr.logger, pr.service, "client_ip_policy_invalid", "client ip policy is invalid")
}
if !allowed {
rawIP := ""
if clientIP != nil {
rawIP = clientIP.String()
}
pr.logger.Info("Client IP policy denied request", zap.String("client_id", clientID), zap.String("remote_ip", rawIP))
return response.Forbidden(pr.logger, pr.service, "ip_not_allowed", "request ip is not allowed for this client")
}
return nil
}
func (pr *PublicRouter) loadClient(ctx context.Context, clientID string) (*sessionmodel.Client, http.HandlerFunc) {
client, err := clientauth.Resolve(ctx, pr.rtdb, clientID)
if errors.Is(err, clientauth.ErrUnknownClientID) {
pr.logger.Debug("Login rejected: unknown client id", zap.String("client_id", clientID))
return nil, response.Unauthorized(pr.logger, pr.service, clientauth.ErrUnknownClientID.Error())
}
if err != nil {
pr.logger.Warn("Login rejected: failed to load client", zap.Error(err), zap.String("client_id", clientID))
return nil, response.Internal(pr.logger, pr.service, err)
}
return client, nil
}
func (pr *PublicRouter) validateClientAccountBinding(client *sessionmodel.Client, account *customermodel.Account) http.HandlerFunc {
if client == nil || client.AccountRef == nil {
return nil
}
accountRef := account.GetID()
if accountRef == nil || *client.AccountRef != *accountRef {
return response.Forbidden(pr.logger, pr.service, "client_account_mismatch", "client is bound to another account")
}
return nil
}
func (pr *PublicRouter) validateClientState(r *http.Request, clientID string, client *sessionmodel.Client) http.HandlerFunc {
if client == nil {
return response.Unauthorized(pr.logger, pr.service, clientauth.ErrUnknownClientID.Error())
}
if client.IsRevoked {
return response.Forbidden(pr.logger, pr.service, "client_revoked", "client has been revoked")
}
if h := pr.validateClientIPPolicy(r, clientID, client); h != nil {
return h
}
return nil
}
func (pr *PublicRouter) validateAPIClient(ctx context.Context, r *http.Request, req *srequest.Login, account *customermodel.Account) http.HandlerFunc {
client, h := pr.loadClient(ctx, req.ClientID)
if h != nil {
return h
}
if h := pr.validateClientState(r, req.ClientID, client); h != nil {
return h
}
if !hasGrantType(client.GrantTypes, apiLoginGrantType) {
return response.Forbidden(pr.logger, pr.service, "client_grant_not_allowed", "client does not allow password grant")
}
method := strings.ToLower(strings.TrimSpace(client.TokenEndpointAuthMethod))
if method == "" {
method = apiLoginClientAuthMethod
}
if method != apiLoginClientAuthMethod {
return response.Forbidden(pr.logger, pr.service, "client_auth_method_unsupported", "unsupported client auth method")
}
storedSecret := strings.TrimSpace(client.ClientSecret)
if storedSecret == "" {
return response.Forbidden(pr.logger, pr.service, "client_secret_missing", "client secret is not configured")
}
if subtle.ConstantTimeCompare([]byte(storedSecret), []byte(req.ClientSecret)) != 1 {
pr.logger.Debug("API login rejected: invalid client secret", zap.String("client_id", req.ClientID))
return response.Unauthorized(pr.logger, pr.service, "invalid client secret")
}
if h := pr.validateClientAccountBinding(client, account); h != nil {
return h
}
return nil
}
func (pr *PublicRouter) resolveLoginClient(ctx context.Context, r *http.Request, clientID string) (*sessionmodel.Client, http.HandlerFunc) {
clientID = strings.TrimSpace(clientID)
if clientID == "" {
return nil, nil
}
client, h := pr.loadClient(ctx, clientID)
if h != nil {
return nil, h
}
if h := pr.validateClientState(r, clientID, client); h != nil {
return nil, h
}
return client, nil
}
func (pr *PublicRouter) respondAPILogin(ctx context.Context, r *http.Request, req *srequest.Login, account *customermodel.Account) http.HandlerFunc {
if req.ClientID == "" || req.DeviceID == "" {
return response.BadRequest(pr.logger, pr.service, "missing_session", "session identifier is required")
}
return pr.refreshAndRespondLogin(ctx, r, &req.SessionIdentifier, account)
}
func decodeLogin(r *http.Request, logger mlogger.Logger) (*srequest.Login, http.HandlerFunc) {
var req srequest.Login
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
logger.Info("Failed to decode login request", zap.Error(err))
return nil, response.BadPayload(logger, mservice.Accounts, err)
}
req.Login = strings.TrimSpace(req.Login)
req.Password = strings.TrimSpace(req.Password)
req.ClientID = strings.TrimSpace(req.ClientID)
req.DeviceID = strings.TrimSpace(req.DeviceID)
req.ClientSecret = strings.TrimSpace(req.ClientSecret)
if req.Login == "" {
return nil, response.BadRequest(logger, mservice.Accounts, "email_missing", "login request has no user name")
}
if req.Password == "" {
return nil, response.BadRequest(logger, mservice.Accounts, "password_missing", "login request has no password")
}
return &req, nil
}
func (a *PublicRouter) login(r *http.Request) http.HandlerFunc {
req, h := decodeLogin(r, a.logger)
if h != nil {
return h
}
client, h := a.resolveLoginClient(r.Context(), r, req.ClientID)
if h != nil {
return h
}
account, h := a.authenticateRateLimited(r, req)
if h != nil {
return h
}
if h = a.validateClientAccountBinding(client, account); h != nil {
return h
}
return a.respondPendingLogin(account)
}
func (a *PublicRouter) apiLogin(r *http.Request) http.HandlerFunc {
req, h := decodeLogin(r, a.logger)
if h != nil {
return h
}
if req.ClientID == "" {
return response.BadRequest(a.logger, mservice.Accounts, "client_id_missing", "clientId is required")
}
if req.ClientSecret == "" {
return response.BadRequest(a.logger, mservice.Accounts, "client_secret_missing", "clientSecret is required")
}
account, h := a.authenticateRateLimited(r, req)
if h != nil {
return h
}
if h = a.validateAPIClient(r.Context(), r, req, account); h != nil {
return h
}
return a.respondAPILogin(r.Context(), r, req, account)
}
@@ -0,0 +1,179 @@
package public
import (
"context"
customermodel "git.sendico.io/tech/sxchnge/product/customers/model"
"net/http"
"strconv"
"strings"
"time"
"git.sendico.io/tech/sxchnge/product/auth/config"
"git.sendico.io/tech/sxchnge/product/auth/ipguard"
"git.sendico.io/tech/sxchnge/product/auth/request"
"git.sendico.io/tech/sxchnge/shared/errors"
"git.sendico.io/tech/sxchnge/shared/http/response"
"git.sendico.io/tech/sxchnge/shared/ratelimit/store"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"go.uber.org/zap"
)
const (
loginScopeAccount = "account"
loginScopeIP = "ip"
)
var loginRateLimitDecisions = promauto.NewCounterVec(prometheus.CounterOpts{
Name: "sendico_login_rate_limit_decisions_total",
Help: "Login rate-limit decisions grouped by scope kind and outcome.",
}, []string{"scope_kind", "outcome"})
type loginRateScope struct {
kind string
hash string
limit int
windowStart time.Time
expiresAt time.Time
}
func (pr *PublicRouter) authenticateRateLimited(
r *http.Request,
req *srequest.Login,
) (*customermodel.Account, http.HandlerFunc) {
if pr.attempts == nil {
account, _, h := pr.authenticateAccount(r.Context(), req)
return account, h
}
clock := pr.now
if clock == nil {
clock = time.Now
}
now := clock().UTC()
scopes := pr.loginRateScopes(r, req.Login, now)
if h := pr.checkLoginRateLimits(r.Context(), scopes, now); h != nil {
return nil, h
}
account, credentialFailure, h := pr.authenticateAccount(r.Context(), req)
if !credentialFailure {
for _, scope := range scopes {
loginRateLimitDecisions.WithLabelValues(scope.kind, "allowed").Inc()
}
return account, h
}
limited, retryAfter, failureHandler := pr.recordLoginFailure(r.Context(), scopes, now)
if failureHandler != nil {
return nil, failureHandler
}
if limited {
return nil, pr.loginRateLimited(retryAfter)
}
return nil, h
}
func (pr *PublicRouter) loginRateScopes(r *http.Request, login string, now time.Time) []loginRateScope {
normalizedLogin := strings.ToLower(strings.TrimSpace(login))
clientIP := "unresolved"
if resolved := ipguard.ClientIP(r); resolved != nil {
clientIP = resolved.String()
}
return []loginRateScope{
newLoginRateScope(loginScopeAccount, normalizedLogin, pr.limits.PerAccount, now),
newLoginRateScope(loginScopeIP, clientIP, pr.limits.PerIP, now),
}
}
func newLoginRateScope(kind, value string, limit middleware.LoginRateLimitWindow, now time.Time) loginRateScope {
windowStart := now.Truncate(limit.Window)
return loginRateScope{
kind: kind,
hash: ratelimits.HashScope(kind, value),
limit: limit.Limit,
windowStart: windowStart,
expiresAt: windowStart.Add(limit.Window),
}
}
func (pr *PublicRouter) checkLoginRateLimits(
ctx context.Context,
scopes []loginRateScope,
now time.Time,
) http.HandlerFunc {
for _, scope := range scopes {
count, err := pr.attempts.Current(ctx, scope.kind, scope.hash, scope.windowStart)
if err != nil {
return pr.loginRateLimitStorageFailure(scope.kind, err)
}
if count >= scope.limit {
loginRateLimitDecisions.WithLabelValues(scope.kind, "denied").Inc()
retryAfter := retryAfterDuration(now, scope.expiresAt)
pr.logger.Info("Denied login request at rate limit",
zap.String("scope_kind", scope.kind),
zap.Int64("retry_after_seconds", retryAfterSeconds(retryAfter)))
return pr.loginRateLimited(retryAfter)
}
}
return nil
}
func (pr *PublicRouter) recordLoginFailure(
ctx context.Context,
scopes []loginRateScope,
now time.Time,
) (bool, time.Duration, http.HandlerFunc) {
limited := false
var retryAfter time.Duration
for _, scope := range scopes {
count, err := pr.attempts.Increment(ctx, scope.kind, scope.hash, scope.windowStart, scope.expiresAt)
if err != nil {
return false, 0, pr.loginRateLimitStorageFailure(scope.kind, err)
}
if count >= scope.limit {
limited = true
loginRateLimitDecisions.WithLabelValues(scope.kind, "denied").Inc()
scopeRetry := retryAfterDuration(now, scope.expiresAt)
if retryAfter == 0 || scopeRetry < retryAfter {
retryAfter = scopeRetry
}
pr.logger.Info("Login failure reached rate limit",
zap.String("scope_kind", scope.kind),
zap.Int64("retry_after_seconds", retryAfterSeconds(scopeRetry)))
continue
}
loginRateLimitDecisions.WithLabelValues(scope.kind, "allowed").Inc()
}
return limited, retryAfter, nil
}
func (pr *PublicRouter) loginRateLimitStorageFailure(scopeKind string, cause error) http.HandlerFunc {
loginRateLimitDecisions.WithLabelValues(scopeKind, "error").Inc()
pr.logger.Warn("Login protection storage failed", zap.Error(cause), zap.String("scope_kind", scopeKind))
err := merrors.OpaqueWrap(merrors.ErrServiceUnavailable, cause, "login protection is temporarily unavailable")
return response.Auto(pr.logger, pr.service, err)
}
func (pr *PublicRouter) loginRateLimited(retryAfter time.Duration) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Retry-After", strconv.FormatInt(retryAfterSeconds(retryAfter), 10))
response.TooManyRequests(pr.logger, pr.service, "too many login attempts; try again later")(w, r)
}
}
func retryAfterDuration(now, expiresAt time.Time) time.Duration {
retryAfter := expiresAt.Sub(now)
if retryAfter <= 0 {
return time.Second
}
return retryAfter
}
func retryAfterSeconds(retryAfter time.Duration) int64 {
seconds := int64((retryAfter + time.Second - 1) / time.Second)
if seconds < 1 {
return 1
}
return seconds
}
@@ -0,0 +1,316 @@
package public
import (
"context"
customermodel "git.sendico.io/tech/sxchnge/product/customers/model"
"net/http"
"net/http/httptest"
"strconv"
"strings"
"sync"
"sync/atomic"
"testing"
"time"
"git.sendico.io/tech/sxchnge/product/auth/config"
"git.sendico.io/tech/sxchnge/product/auth/request"
"git.sendico.io/tech/sxchnge/shared/errors"
"git.sendico.io/tech/sxchnge/shared/service"
"go.uber.org/zap"
)
type fakeLoginAttemptStore struct {
mu sync.Mutex
counts map[string]int
currentErr error
incrementErr error
}
func newFakeLoginAttemptStore() *fakeLoginAttemptStore {
return &fakeLoginAttemptStore{counts: make(map[string]int)}
}
func (s *fakeLoginAttemptStore) Current(_ context.Context, kind, hash string, start time.Time) (int, error) {
s.mu.Lock()
defer s.mu.Unlock()
if s.currentErr != nil {
return 0, s.currentErr
}
return s.counts[attemptKey(kind, hash, start)], nil
}
func (s *fakeLoginAttemptStore) Increment(
_ context.Context,
kind, hash string,
start, _ time.Time,
) (int, error) {
s.mu.Lock()
defer s.mu.Unlock()
if s.incrementErr != nil {
return 0, s.incrementErr
}
key := attemptKey(kind, hash, start)
s.counts[key]++
return s.counts[key], nil
}
func attemptKey(kind, hash string, start time.Time) string {
return kind + ":" + hash + ":" + start.UTC().Format(time.RFC3339Nano)
}
func testLoginLimits(accountLimit, ipLimit int, window time.Duration) middleware.LoginRateLimits {
return middleware.LoginRateLimits{
PerAccount: middleware.LoginRateLimitWindow{Limit: accountLimit, Window: window},
PerIP: middleware.LoginRateLimitWindow{Limit: ipLimit, Window: window},
}
}
func testRateLimitedRouter(
db *testAccountDB,
store *fakeLoginAttemptStore,
limits middleware.LoginRateLimits,
now *time.Time,
) *PublicRouter {
return &PublicRouter{
logger: zap.NewNop(),
db: db,
service: mservice.Accounts,
attempts: store,
limits: limits,
now: func() time.Time {
return *now
},
}
}
func loginRequest(login, password, remoteAddr string) (*http.Request, *srequest.Login) {
req := httptest.NewRequestWithContext(context.Background(), http.MethodPost, "/login", nil)
req.RemoteAddr = remoteAddr
return req, &srequest.Login{LoginData: customermodel.LoginData{
UserDataBase: customermodel.UserDataBase{Login: login},
Password: password,
}}
}
func executeLoginHandler(t *testing.T, h http.HandlerFunc, req *http.Request) *httptest.ResponseRecorder {
t.Helper()
if h == nil {
t.Fatal("expected response handler")
}
recorder := httptest.NewRecorder()
h(recorder, req)
return recorder
}
func TestLoginRateLimitNormalizesAccountAndBoundsDistributedAttempts(t *testing.T) {
now := time.Date(2026, time.August, 13, 9, 0, 0, 0, time.UTC)
store := newFakeLoginAttemptStore()
db := &testAccountDB{result: testActiveAccount(t)}
router := testRateLimitedRouter(db, store, testLoginLimits(3, 100, 10*time.Minute), &now)
attempts := []struct {
login string
remote string
status int
}{
{login: " User@Example.com ", remote: "192.0.2.1:1000", status: http.StatusUnauthorized},
{login: "user@example.com", remote: "192.0.2.2:1000", status: http.StatusUnauthorized},
{login: "USER@EXAMPLE.COM", remote: "192.0.2.3:1000", status: http.StatusTooManyRequests},
}
for _, attempt := range attempts {
req, login := loginRequest(attempt.login, "wrong password", attempt.remote)
_, h := router.authenticateRateLimited(req, login)
recorder := executeLoginHandler(t, h, req)
if recorder.Code != attempt.status {
t.Fatalf("login %q status: got %d want %d", attempt.login, recorder.Code, attempt.status)
}
}
req, login := loginRequest("user@example.com", "correct horse battery staple", "192.0.2.4:1000")
_, h := router.authenticateRateLimited(req, login)
recorder := executeLoginHandler(t, h, req)
if recorder.Code != http.StatusTooManyRequests || recorder.Header().Get("Retry-After") != "600" {
t.Fatalf("blocked recovery mismatch: status=%d retry=%q", recorder.Code, recorder.Header().Get("Retry-After"))
}
now = now.Add(10 * time.Minute)
account, h := router.authenticateRateLimited(req, login)
if h != nil || account == nil {
t.Fatalf("expected legitimate login after window expiry, account=%v handler=%v", account, h)
}
}
func TestLoginRateLimitBoundsOneSourceAcrossAccounts(t *testing.T) {
now := time.Date(2026, time.August, 13, 9, 0, 0, 0, time.UTC)
store := newFakeLoginAttemptStore()
router := testRateLimitedRouter(
&testAccountDB{err: merrors.ErrNoData},
store,
testLoginLimits(100, 3, 15*time.Minute),
&now,
)
for i, loginValue := range []string{"one@example.com", "two@example.com", "three@example.com"} {
req, login := loginRequest(loginValue, "wrong password", "198.51.100.10:9000")
_, h := router.authenticateRateLimited(req, login)
recorder := executeLoginHandler(t, h, req)
want := http.StatusUnauthorized
if i == 2 {
want = http.StatusTooManyRequests
}
if recorder.Code != want {
t.Fatalf("attempt %d status: got %d want %d", i+1, recorder.Code, want)
}
}
}
func TestLoginEndpointReturnsStableRateLimitResponse(t *testing.T) {
now := time.Date(2026, time.August, 13, 9, 0, 0, 0, time.UTC)
router := testRateLimitedRouter(
&testAccountDB{err: merrors.ErrNoData},
newFakeLoginAttemptStore(),
testLoginLimits(2, 50, 15*time.Minute),
&now,
)
body := `{"login":{"login":"missing@example.com","password":"wrong password"}}`
for attempt := 1; attempt <= 2; attempt++ {
req := httptest.NewRequestWithContext(context.Background(), http.MethodPost, "/login", strings.NewReader(body))
req.RemoteAddr = "198.51.100.20:9000"
recorder := httptest.NewRecorder()
router.login(req)(recorder, req)
want := http.StatusUnauthorized
if attempt == 2 {
want = http.StatusTooManyRequests
}
if recorder.Code != want {
t.Fatalf("attempt %d status: got %d want %d", attempt, recorder.Code, want)
}
if attempt == 2 {
if recorder.Header().Get("Retry-After") != "900" {
t.Fatalf("unexpected Retry-After: %q", recorder.Header().Get("Retry-After"))
}
if !strings.Contains(recorder.Body.String(), `"error":"too_many_requests"`) {
t.Fatalf("unexpected 429 body: %s", recorder.Body.String())
}
}
}
}
func TestInvalidCredentialsDoNotDiscloseAccountExistence(t *testing.T) {
request := &srequest.Login{LoginData: customermodel.LoginData{
UserDataBase: customermodel.UserDataBase{Login: "user@example.com"},
Password: "wrong password",
}}
responses := make([]*httptest.ResponseRecorder, 0, 2)
for _, db := range []*testAccountDB{
{result: testActiveAccount(t)},
{err: merrors.ErrNoData},
} {
router := &PublicRouter{logger: zap.NewNop(), db: db, service: mservice.Accounts}
_, failed, h := router.authenticateAccount(context.Background(), request)
if !failed {
t.Fatal("invalid credentials were not classified as a failure")
}
req, _ := loginRequest("user@example.com", "wrong password", "192.0.2.1:1000")
responses = append(responses, executeLoginHandler(t, h, req))
}
if responses[0].Code != http.StatusUnauthorized || responses[1].Code != http.StatusUnauthorized {
t.Fatalf("unexpected statuses: existing=%d unknown=%d", responses[0].Code, responses[1].Code)
}
if responses[0].Body.String() != responses[1].Body.String() ||
!strings.Contains(responses[0].Body.String(), "invalid credentials") {
t.Fatalf("credential responses differ: existing=%s unknown=%s", responses[0].Body.String(), responses[1].Body.String())
}
}
func TestLoginRateLimitIsAtomicAcrossRouterInstances(t *testing.T) {
now := time.Date(2026, time.August, 13, 9, 0, 0, 0, time.UTC)
store := newFakeLoginAttemptStore()
db := &testAccountDB{err: merrors.ErrNoData}
limits := testLoginLimits(5, 1000, 15*time.Minute)
routers := []*PublicRouter{
testRateLimitedRouter(db, store, limits, &now),
testRateLimitedRouter(db, store, limits, &now),
}
var unauthorized atomic.Int64
var limited atomic.Int64
var wg sync.WaitGroup
for i := 0; i < 20; i++ {
wg.Add(1)
go func(index int) {
defer wg.Done()
req, login := loginRequest("shared@example.com", "wrong password", "203.0.113."+strconv.Itoa(index+1)+":9000")
_, h := routers[index%len(routers)].authenticateRateLimited(req, login)
recorder := httptest.NewRecorder()
h(recorder, req)
switch recorder.Code {
case http.StatusUnauthorized:
unauthorized.Add(1)
case http.StatusTooManyRequests:
limited.Add(1)
}
}(i)
}
wg.Wait()
if unauthorized.Load() != 4 || limited.Load() != 16 {
t.Fatalf("concurrent decisions: unauthorized=%d limited=%d", unauthorized.Load(), limited.Load())
}
}
func TestLoginRateLimitFailsClosedOnStorageError(t *testing.T) {
now := time.Date(2026, time.August, 13, 9, 0, 0, 0, time.UTC)
store := newFakeLoginAttemptStore()
store.currentErr = merrors.Internal("database unavailable")
db := &testAccountDB{result: testActiveAccount(t)}
router := testRateLimitedRouter(db, store, testLoginLimits(5, 50, 15*time.Minute), &now)
req, login := loginRequest("user@example.com", "correct horse battery staple", "192.0.2.1:1000")
_, h := router.authenticateRateLimited(req, login)
recorder := executeLoginHandler(t, h, req)
if recorder.Code != http.StatusServiceUnavailable {
t.Fatalf("expected service unavailable, got %d", recorder.Code)
}
if db.getByEmailCalls != 0 {
t.Fatalf("storage failure must stop before password lookup, got %d lookups", db.getByEmailCalls)
}
if strings.Contains(recorder.Body.String(), "database unavailable") {
t.Fatal("storage cause leaked to the public response")
}
}
func TestLoginRateLimitFailsClosedWhenFailureCannotBeRecorded(t *testing.T) {
now := time.Date(2026, time.August, 13, 9, 0, 0, 0, time.UTC)
store := newFakeLoginAttemptStore()
store.incrementErr = merrors.Internal("write unavailable")
router := testRateLimitedRouter(
&testAccountDB{err: merrors.ErrNoData},
store,
testLoginLimits(5, 50, 15*time.Minute),
&now,
)
req, login := loginRequest("missing@example.com", "wrong password", "192.0.2.1:1000")
_, h := router.authenticateRateLimited(req, login)
recorder := executeLoginHandler(t, h, req)
if recorder.Code != http.StatusServiceUnavailable {
t.Fatalf("expected service unavailable, got %d", recorder.Code)
}
if !strings.Contains(recorder.Body.String(), "login protection is temporarily unavailable") {
t.Fatalf("unexpected public failure body: %s", recorder.Body.String())
}
}
func TestDummyPasswordHashRemainsValid(t *testing.T) {
if !(&customermodel.Account{Password: dummyPasswordHash}).MatchPassword("sendico-login-dummy-password") {
t.Fatal("dummy password hash is not a valid bcrypt hash")
}
if (&customermodel.Account{Password: dummyPasswordHash}).MatchPassword("different") {
t.Fatal("dummy password hash unexpectedly matched another password")
}
}
@@ -0,0 +1,269 @@
package public
import (
"context"
"errors"
customermodel "git.sendico.io/tech/sxchnge/product/customers/model"
sessionmodel "git.sendico.io/tech/sxchnge/product/sessions/model"
"net/http"
"net/http/httptest"
"strings"
"testing"
"git.sendico.io/tech/sxchnge/product/auth/config"
"git.sendico.io/tech/sxchnge/product/auth/request"
"git.sendico.io/tech/sxchnge/shared/errors"
"git.sendico.io/tech/sxchnge/shared/service"
"github.com/go-chi/chi/v5"
"go.uber.org/zap"
)
func TestLoginRejectsUnknownClientID(t *testing.T) {
testRejectsUnknownClientID(t, "/login", `{
"login":{"login":"user@example.com","password":"correct horse battery staple"},
"clientId":"missing-client",
"deviceId":"device-1"
}`, 0, func(router *PublicRouter, req *http.Request) http.HandlerFunc {
return router.login(req)
})
}
func TestAPILoginRejectsUnknownClientID(t *testing.T) {
testRejectsUnknownClientID(t, "/login/api", `{
"login":{"login":"user@example.com","password":"correct horse battery staple"},
"clientId":"missing-client",
"deviceId":"device-1",
"clientSecret":"secret"
}`, 1, func(router *PublicRouter, req *http.Request) http.HandlerFunc {
return router.apiLogin(req)
})
}
func TestLoginAuthenticatesAgainstInjectedAccountStorage(t *testing.T) {
account := testActiveAccount(t)
unusedDB := &testAccountDB{err: merrors.ErrNoData}
activeDB := &testAccountDB{result: account}
router := &PublicRouter{
logger: zap.NewNop(),
db: activeDB,
service: mservice.Accounts,
}
got, failed, h := router.authenticateAccount(context.Background(), &srequest.Login{
LoginData: customermodel.LoginData{
UserDataBase: customermodel.UserDataBase{
Login: "user@example.com",
},
Password: "correct horse battery staple",
},
})
if h != nil {
t.Fatal("expected login authentication to succeed against injected account store")
}
if failed {
t.Fatal("successful login was classified as a credential failure")
}
if got == nil || got.Login != account.Login {
t.Fatalf("expected account %q, got %#v", account.Login, got)
}
if activeDB.getByEmailCalls != 1 {
t.Fatalf("expected one active account store lookup, got %d", activeDB.getByEmailCalls)
}
if unusedDB.getByEmailCalls != 0 {
t.Fatalf("expected no unused account store lookups, got %d", unusedDB.getByEmailCalls)
}
}
func testRejectsUnknownClientID(
t *testing.T,
path string,
body string,
wantLookups int,
handler func(*PublicRouter, *http.Request) http.HandlerFunc,
) {
t.Helper()
account := testActiveAccount(t)
accountDB := &testAccountDB{result: account}
router := &PublicRouter{
logger: zap.NewNop(),
db: accountDB,
rtdb: &testRefreshTokenDB{clientErr: merrors.ErrNoData},
service: mservice.Accounts,
}
req := httptest.NewRequestWithContext(context.Background(), http.MethodPost, path, strings.NewReader(body))
rec := httptest.NewRecorder()
handler(router, req)(rec, req)
if rec.Code != http.StatusUnauthorized {
t.Fatalf("expected status %d, got %d", http.StatusUnauthorized, rec.Code)
}
if accountDB.getByEmailCalls != wantLookups {
t.Fatalf("expected %d account lookups, got %d", wantLookups, accountDB.getByEmailCalls)
}
resp := decodeErrorResponse(t, rec.Body.Bytes())
if resp.Data.Details != "unknown client id" {
t.Fatalf("expected unknown client id message, got %q", resp.Data.Details)
}
}
func TestLoginRejectsPendingVerificationAccount(t *testing.T) {
account := testAccountWithStatus(t, customermodel.AccountPendingVerification)
router := &PublicRouter{
logger: zap.NewNop(),
db: &testAccountDB{result: account},
service: mservice.Accounts,
}
req := httptest.NewRequestWithContext(context.Background(), http.MethodPost, "/login", strings.NewReader(`{
"login":{"login":"user@example.com","password":"correct horse battery staple"}
}`))
rec := httptest.NewRecorder()
router.login(req)(rec, req)
if rec.Code != http.StatusForbidden {
t.Fatalf("expected status %d, got %d", http.StatusForbidden, rec.Code)
}
resp := decodeErrorResponse(t, rec.Body.Bytes())
if resp.Data.Details != "Account verification required" {
t.Fatalf("expected account verification required message, got %q", resp.Data.Details)
}
}
func TestLoginRejectsBlockedAccount(t *testing.T) {
account := testAccountWithStatus(t, customermodel.AccountBlocked)
router := &PublicRouter{
logger: zap.NewNop(),
db: &testAccountDB{result: account},
service: mservice.Accounts,
}
req := httptest.NewRequestWithContext(context.Background(), http.MethodPost, "/login", strings.NewReader(`{
"login":{"login":"user@example.com","password":"correct horse battery staple"}
}`))
rec := httptest.NewRecorder()
router.login(req)(rec, req)
if rec.Code != http.StatusForbidden {
t.Fatalf("expected status %d, got %d", http.StatusForbidden, rec.Code)
}
resp := decodeErrorResponse(t, rec.Body.Bytes())
if resp.Data.Details != "Account verification required" {
t.Fatalf("expected account verification required message, got %q", resp.Data.Details)
}
}
func TestAPILoginAllowsLoginAgainAfterRevokeSameSession(t *testing.T) {
account := testActiveAccount(t)
clientID := "api-client"
deviceID := "device-1"
rtdb := &testRefreshTokenDB{
client: &sessionmodel.Client{
ClientID: clientID,
ClientSecret: "secret",
GrantTypes: []string{"password"},
TokenEndpointAuthMethod: "client_secret_post",
AccountRef: account.GetID(),
},
}
var cfg middleware.TokenConfig
cfg.Length = 16
cfg.Expiration.Account = 1
cfg.Expiration.Refresh = 24
signature := middleware.Signature{
Algorithm: "HS256",
PrivateKey: []byte("test-secret"),
}
router := NewRouter(
zap.NewNop(),
"/",
&testAccountDB{result: account},
nil,
rtdb,
nil,
chi.NewRouter(),
&cfg,
&signature,
middleware.LoginRateLimits{},
)
body := `{
"login":{"login":"user@example.com","password":"correct horse battery staple"},
"clientId":"api-client",
"deviceId":"device-1",
"clientSecret":"secret"
}`
firstReq := httptest.NewRequestWithContext(context.Background(), http.MethodPost, "/login/api", strings.NewReader(body))
firstRec := httptest.NewRecorder()
router.apiLogin(firstReq)(firstRec, firstReq)
if firstRec.Code != http.StatusOK {
t.Fatalf("expected first login status %d, got %d", http.StatusOK, firstRec.Code)
}
firstResp := decodeLoginResponse(t, firstRec.Body.Bytes())
if firstResp.Data.RefreshToken.Token == "" {
t.Fatal("expected first login to return refresh token")
}
err := rtdb.Revoke(context.Background(), *account.GetID(), &sessionmodel.SessionIdentifier{
ClientID: clientID,
DeviceID: deviceID,
})
if err != nil {
t.Fatalf("failed to revoke refresh token: %v", err)
}
secondReq := httptest.NewRequestWithContext(context.Background(), http.MethodPost, "/login/api", strings.NewReader(body))
secondRec := httptest.NewRecorder()
router.apiLogin(secondReq)(secondRec, secondReq)
if secondRec.Code != http.StatusOK {
t.Fatalf("expected second login status %d, got %d", http.StatusOK, secondRec.Code)
}
secondResp := decodeLoginResponse(t, secondRec.Body.Bytes())
if secondResp.Data.RefreshToken.Token == "" {
t.Fatal("expected second login to return refresh token")
}
if secondResp.Data.RefreshToken.Token == firstResp.Data.RefreshToken.Token {
t.Fatal("expected second login to issue a different refresh token after revocation")
}
_, err = rtdb.GetByCRT(context.Background(), &sessionmodel.ClientRefreshToken{
SessionIdentifier: sessionmodel.SessionIdentifier{
ClientID: clientID,
DeviceID: deviceID,
},
RefreshToken: firstResp.Data.RefreshToken.Token,
})
if !errors.Is(err, merrors.ErrNoData) {
t.Fatalf("expected old refresh token to be unusable, got %v", err)
}
_, err = rtdb.GetByCRT(context.Background(), &sessionmodel.ClientRefreshToken{
SessionIdentifier: sessionmodel.SessionIdentifier{
ClientID: clientID,
DeviceID: deviceID,
},
RefreshToken: secondResp.Data.RefreshToken.Token,
})
if err != nil {
t.Fatalf("expected new refresh token to be active, got %v", err)
}
if got := rtdb.tokenCountForSession(*account.GetID(), clientID, deviceID); got != 2 {
t.Fatalf("expected 2 refresh token records for session, got %d", got)
}
if got := rtdb.activeTokenCountForSession(*account.GetID(), clientID, deviceID); got != 1 {
t.Fatalf("expected 1 active refresh token for session, got %d", got)
}
}
@@ -0,0 +1,285 @@
package public
import (
"context"
"encoding/json"
customermodel "git.sendico.io/tech/sxchnge/product/customers/model"
sessionmodel "git.sendico.io/tech/sxchnge/product/sessions/model"
"sync"
"testing"
"git.sendico.io/tech/sxchnge/shared/database/repository/builder"
"git.sendico.io/tech/sxchnge/shared/errors"
"go.mongodb.org/mongo-driver/v2/bson"
)
type testErrorResponse struct {
Data struct {
Details string `json:"details"`
} `json:"data"`
}
type testLoginResponse struct {
Data struct {
AccessToken struct {
Token string `json:"token"`
} `json:"accessToken"`
RefreshToken struct {
Token string `json:"token"`
} `json:"refreshToken"`
} `json:"data"`
}
func decodeErrorResponse(t *testing.T, body []byte) testErrorResponse {
t.Helper()
var resp testErrorResponse
if err := json.Unmarshal(body, &resp); err != nil {
t.Fatalf("failed to decode response body: %v", err)
}
return resp
}
func decodeLoginResponse(t *testing.T, body []byte) testLoginResponse {
t.Helper()
var resp testLoginResponse
if err := json.Unmarshal(body, &resp); err != nil {
t.Fatalf("failed to decode login response body: %v", err)
}
return resp
}
func testActiveAccount(t *testing.T) *customermodel.Account {
t.Helper()
return testAccountWithStatus(t, customermodel.AccountActive)
}
func testAccountWithStatus(t *testing.T, status customermodel.AccountStatus) *customermodel.Account {
t.Helper()
account := &customermodel.Account{
AccountPublic: customermodel.AccountPublic{
UserDataBase: customermodel.UserDataBase{
Login: "user@example.com",
},
},
Password: "correct horse battery staple",
Status: status,
}
account.SetID(bson.NewObjectID())
if err := account.HashPassword(); err != nil {
t.Fatalf("failed to hash password: %v", err)
}
return account
}
type testAccountDB struct {
mu sync.Mutex
result *customermodel.Account
err error
getByEmailCalls int
}
func (db *testAccountDB) GetByEmail(ctx context.Context, email string) (*customermodel.Account, error) {
db.mu.Lock()
defer db.mu.Unlock()
db.getByEmailCalls++
return db.result, db.err
}
func (db *testAccountDB) Create(ctx context.Context, object *customermodel.Account) error {
return merrors.NotImplemented("stub")
}
func (db *testAccountDB) InsertMany(ctx context.Context, objects []*customermodel.Account) error {
return merrors.NotImplemented("stub")
}
func (db *testAccountDB) Get(ctx context.Context, objectRef bson.ObjectID, result *customermodel.Account) error {
if db.err != nil {
return db.err
}
if db.result == nil || db.result.GetID() == nil || *db.result.GetID() != objectRef {
return merrors.ErrNoData
}
*result = *db.result
return nil
}
func (db *testAccountDB) Update(ctx context.Context, object *customermodel.Account) error {
return merrors.NotImplemented("stub")
}
func (db *testAccountDB) Patch(ctx context.Context, objectRef bson.ObjectID, patch builder.Patch) error {
return merrors.NotImplemented("stub")
}
func (db *testAccountDB) Delete(ctx context.Context, objectRef bson.ObjectID) error {
return merrors.NotImplemented("stub")
}
func (db *testAccountDB) DeleteMany(ctx context.Context, query builder.Query) error {
return merrors.NotImplemented("stub")
}
func (db *testAccountDB) DeleteCascade(ctx context.Context, objectRef bson.ObjectID) error {
return merrors.NotImplemented("stub")
}
func (db *testAccountDB) FindOne(ctx context.Context, query builder.Query, result *customermodel.Account) error {
return merrors.NotImplemented("stub")
}
type testRefreshTokenDB struct {
client *sessionmodel.Client
clientErr error
tokens []*sessionmodel.RefreshToken
}
func (db *testRefreshTokenDB) GetClient(ctx context.Context, clientID string) (*sessionmodel.Client, error) {
return db.client, db.clientErr
}
func (db *testRefreshTokenDB) Revoke(ctx context.Context, accountRef bson.ObjectID, session *sessionmodel.SessionIdentifier) error {
for _, token := range db.tokens {
if token.AccountRef == nil || *token.AccountRef != accountRef {
continue
}
if token.ClientID == session.ClientID && token.DeviceID == session.DeviceID && !token.IsRevoked {
token.IsRevoked = true
return nil
}
}
return nil
}
func (db *testRefreshTokenDB) RevokeToken(ctx context.Context, token *sessionmodel.ClientRefreshToken) (bool, error) {
for _, stored := range db.tokens {
if stored.ClientID == token.ClientID && stored.DeviceID == token.DeviceID && stored.RefreshToken == token.RefreshToken && !stored.IsRevoked {
stored.IsRevoked = true
return true, nil
}
}
return false, nil
}
func (db *testRefreshTokenDB) RevokeAll(ctx context.Context, accountRef bson.ObjectID, deviceID string) error {
return merrors.NotImplemented("stub")
}
func (db *testRefreshTokenDB) GetSession(ctx context.Context, accountRef bson.ObjectID, session *sessionmodel.SessionIdentifier) (*sessionmodel.RefreshToken, error) {
for _, token := range db.tokens {
if token.AccountRef != nil && *token.AccountRef == accountRef && token.ClientID == session.ClientID && token.DeviceID == session.DeviceID && !token.IsRevoked {
return cloneRefreshToken(token), nil
}
}
return nil, merrors.ErrNoData
}
func (db *testRefreshTokenDB) GetByCRT(ctx context.Context, t *sessionmodel.ClientRefreshToken) (*sessionmodel.RefreshToken, error) {
for _, token := range db.tokens {
if token.ClientID == t.ClientID && token.DeviceID == t.DeviceID && token.RefreshToken == t.RefreshToken {
if token.IsRevoked {
return nil, merrors.ErrNoData
}
return cloneRefreshToken(token), nil
}
}
return nil, merrors.ErrNoData
}
func (db *testRefreshTokenDB) Create(ctx context.Context, object *sessionmodel.RefreshToken) error {
for _, token := range db.tokens {
if token.AccountRef == nil || object.AccountRef == nil {
continue
}
if *token.AccountRef == *object.AccountRef && token.ClientID == object.ClientID && token.DeviceID == object.DeviceID && !token.IsRevoked {
token.RefreshToken = object.RefreshToken
token.SessionVersion = object.SessionVersion
token.ExpiresAt = object.ExpiresAt
token.UserAgent = object.UserAgent
token.IPAddress = object.IPAddress
token.LastUsedAt = object.LastUsedAt
token.IsRevoked = object.IsRevoked
if token.GetID() == nil || token.GetID().IsZero() {
token.SetID(bson.NewObjectID())
}
object.SetID(*token.GetID())
return nil
}
}
clone := cloneRefreshToken(object)
if clone.GetID() == nil || clone.GetID().IsZero() {
clone.SetID(bson.NewObjectID())
}
object.SetID(*clone.GetID())
db.tokens = append(db.tokens, clone)
return nil
}
func (db *testRefreshTokenDB) InsertMany(ctx context.Context, objects []*sessionmodel.RefreshToken) error {
return merrors.NotImplemented("stub")
}
func (db *testRefreshTokenDB) Get(ctx context.Context, objectRef bson.ObjectID, result *sessionmodel.RefreshToken) error {
return merrors.NotImplemented("stub")
}
func (db *testRefreshTokenDB) Update(ctx context.Context, object *sessionmodel.RefreshToken) error {
return merrors.NotImplemented("stub")
}
func (db *testRefreshTokenDB) Patch(ctx context.Context, objectRef bson.ObjectID, patch builder.Patch) error {
return merrors.NotImplemented("stub")
}
func (db *testRefreshTokenDB) Delete(ctx context.Context, objectRef bson.ObjectID) error {
return merrors.NotImplemented("stub")
}
func (db *testRefreshTokenDB) DeleteMany(ctx context.Context, query builder.Query) error {
return merrors.NotImplemented("stub")
}
func (db *testRefreshTokenDB) DeleteCascade(ctx context.Context, objectRef bson.ObjectID) error {
return merrors.NotImplemented("stub")
}
func (db *testRefreshTokenDB) FindOne(ctx context.Context, query builder.Query, result *sessionmodel.RefreshToken) error {
return merrors.NotImplemented("stub")
}
func (db *testRefreshTokenDB) tokenCountForSession(accountRef bson.ObjectID, clientID, deviceID string) int {
count := 0
for _, token := range db.tokens {
if token.AccountRef != nil && *token.AccountRef == accountRef && token.ClientID == clientID && token.DeviceID == deviceID {
count++
}
}
return count
}
func (db *testRefreshTokenDB) activeTokenCountForSession(accountRef bson.ObjectID, clientID, deviceID string) int {
count := 0
for _, token := range db.tokens {
if token.AccountRef != nil && *token.AccountRef == accountRef && token.ClientID == clientID && token.DeviceID == deviceID && !token.IsRevoked {
count++
}
}
return count
}
func cloneRefreshToken(token *sessionmodel.RefreshToken) *sessionmodel.RefreshToken {
if token == nil {
return nil
}
cloned := *token
if token.AccountRef != nil {
accountRef := *token.AccountRef
cloned.AccountRef = &accountRef
}
return &cloned
}
@@ -0,0 +1,55 @@
package public
import (
"encoding/json"
"net/http"
"git.sendico.io/tech/sxchnge/product/auth/request"
"git.sendico.io/tech/sxchnge/product/sessions/browser"
"git.sendico.io/tech/sxchnge/product/sessions/metrics"
"git.sendico.io/tech/sxchnge/shared/http/response"
"git.sendico.io/tech/sxchnge/shared/service"
"go.uber.org/zap"
)
func (pr *PublicRouter) logout(r *http.Request) http.HandlerFunc {
respond := func(handler http.HandlerFunc, clearCookie bool) http.HandlerFunc {
if !clearCookie || !browser.UsesCookie(r) {
return handler
}
return func(w http.ResponseWriter, request *http.Request) {
browser.ClearRefreshCookie(w, request)
handler.ServeHTTP(w, request)
}
}
var req srequest.AccessTokenRefresh
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
pr.logger.Info("Rejected malformed logout request", zap.Error(err))
return respond(response.BadPayload(pr.logger, mservice.RefreshTokens, err), false)
}
credential, err := browser.RefreshCredential(r, req.RefreshToken)
if err != nil {
sessionmetrics.Observe(sessionmetrics.CurrentLogout, sessionmetrics.AlreadyInactive)
return respond(response.Success(pr.logger), true)
}
req.RefreshToken = credential
if req.ClientID == "" || req.DeviceID == "" || req.RefreshToken == "" {
sessionmetrics.Observe(sessionmetrics.CurrentLogout, sessionmetrics.MissingIdentity)
return respond(response.BadRequest(pr.logger, pr.service, "missing_session", "refresh token and session identifier are required"), false)
}
revoked, err := pr.rtdb.RevokeToken(r.Context(), &req)
if err != nil {
sessionmetrics.Observe(sessionmetrics.CurrentLogout, sessionmetrics.StorageError)
pr.logger.Warn("Failed to revoke current session", zap.Error(err),
zap.String("client_id", req.ClientID), zap.String("device_id", req.DeviceID))
return respond(response.Internal(pr.logger, pr.service, err), false)
}
if !revoked {
sessionmetrics.Observe(sessionmetrics.CurrentLogout, sessionmetrics.AlreadyInactive)
return respond(response.Success(pr.logger), true)
}
sessionmetrics.Observe(sessionmetrics.CurrentLogout, sessionmetrics.Succeeded)
pr.logger.Info("Current session revoked", zap.String("client_id", req.ClientID), zap.String("device_id", req.DeviceID))
return respond(response.Success(pr.logger), true)
}
@@ -0,0 +1,204 @@
package public
import (
"context"
sessionmodel "git.sendico.io/tech/sxchnge/product/sessions/model"
"net/http"
"net/http/httptest"
"strings"
"sync"
"testing"
"time"
"git.sendico.io/tech/sxchnge/product/sessions/browser"
"git.sendico.io/tech/sxchnge/product/sessions/store"
"git.sendico.io/tech/sxchnge/shared/errors"
"git.sendico.io/tech/sxchnge/shared/service"
"github.com/stretchr/testify/require"
"go.mongodb.org/mongo-driver/v2/bson"
"go.uber.org/zap"
)
type logoutDB struct {
refreshtokens.DB
mu sync.Mutex
tokens map[string]*sessionmodel.RefreshToken
lookupErr error
revokeErr error
}
func (db *logoutDB) RevokeToken(_ context.Context, request *sessionmodel.ClientRefreshToken) (bool, error) {
db.mu.Lock()
defer db.mu.Unlock()
if db.lookupErr != nil {
return false, db.lookupErr
}
token := db.tokens[request.DeviceID]
if token == nil || token.IsRevoked || token.RefreshToken != request.RefreshToken {
return false, nil
}
if db.revokeErr != nil {
return false, db.revokeErr
}
token.IsRevoked = true
return true, nil
}
func newLogoutToken(accountRef bson.ObjectID, deviceID, value string) *sessionmodel.RefreshToken {
return &sessionmodel.RefreshToken{
ClientRefreshToken: sessionmodel.ClientRefreshToken{
SessionIdentifier: sessionmodel.SessionIdentifier{ClientID: "web", DeviceID: deviceID, SessionVersion: "version-" + deviceID},
RefreshToken: value,
},
AccountRef: &accountRef,
ExpiresAt: time.Now().Add(time.Hour),
}
}
func executeLogout(router *PublicRouter, deviceID, refreshToken string) int {
body := `{"clientId":"web","deviceId":"` + deviceID + `","token":"` + refreshToken + `"}`
request := httptest.NewRequestWithContext(context.Background(), http.MethodPost, "/accounts/logout", strings.NewReader(body))
response := httptest.NewRecorder()
router.logout(request)(response, request)
return response.Code
}
func TestLogoutRevokesOnlyCurrentSessionAndIsIdempotent(t *testing.T) {
accountRef := bson.NewObjectID()
db := &logoutDB{tokens: map[string]*sessionmodel.RefreshToken{
"device-a": newLogoutToken(accountRef, "device-a", "refresh-a"),
"device-b": newLogoutToken(accountRef, "device-b", "refresh-b"),
}}
router := &PublicRouter{logger: zap.NewNop(), rtdb: db, service: mservice.Accounts}
if status := executeLogout(router, "device-a", "refresh-a"); status != http.StatusOK {
t.Fatalf("first logout status = %d", status)
}
if status := executeLogout(router, "device-a", "refresh-a"); status != http.StatusOK {
t.Fatalf("repeated logout status = %d", status)
}
if !db.tokens["device-a"].IsRevoked {
t.Fatal("current device session was not revoked")
}
if db.tokens["device-b"].IsRevoked {
t.Fatal("another device session was revoked")
}
}
func TestConcurrentLogoutIsIdempotent(t *testing.T) {
accountRef := bson.NewObjectID()
db := &logoutDB{tokens: map[string]*sessionmodel.RefreshToken{
"device-a": newLogoutToken(accountRef, "device-a", "refresh-a"),
}}
router := &PublicRouter{logger: zap.NewNop(), rtdb: db, service: mservice.Accounts}
statuses := make(chan int, 8)
var workers sync.WaitGroup
for range 8 {
workers.Add(1)
go func() {
defer workers.Done()
statuses <- executeLogout(router, "device-a", "refresh-a")
}()
}
workers.Wait()
close(statuses)
for status := range statuses {
if status != http.StatusOK {
t.Fatalf("concurrent logout status = %d", status)
}
}
}
func TestDelayedLogoutDoesNotRevokeReplacementSession(t *testing.T) {
accountRef := bson.NewObjectID()
db := &logoutDB{tokens: map[string]*sessionmodel.RefreshToken{
"device-a": newLogoutToken(accountRef, "device-a", "new-refresh"),
}}
router := &PublicRouter{logger: zap.NewNop(), rtdb: db, service: mservice.Accounts}
if status := executeLogout(router, "device-a", "old-refresh"); status != http.StatusOK {
t.Fatalf("delayed logout status = %d", status)
}
if db.tokens["device-a"].IsRevoked {
t.Fatal("replacement session was revoked by a stale logout request")
}
}
func TestLogoutAcceptsAnAlreadyExpiredSession(t *testing.T) {
accountRef := bson.NewObjectID()
expired := newLogoutToken(accountRef, "device-a", "expired-refresh")
expired.ExpiresAt = time.Now().Add(-time.Hour)
db := &logoutDB{tokens: map[string]*sessionmodel.RefreshToken{"device-a": expired}}
router := &PublicRouter{logger: zap.NewNop(), rtdb: db, service: mservice.Accounts}
if status := executeLogout(router, "device-a", "expired-refresh"); status != http.StatusOK {
t.Fatalf("expired logout status = %d", status)
}
if !expired.IsRevoked {
t.Fatal("expired session was not marked revoked before cleanup")
}
}
func TestLogoutFailsClosedWhenSessionStoreIsUnavailable(t *testing.T) {
db := &logoutDB{lookupErr: merrors.Internal("database unavailable")}
router := &PublicRouter{logger: zap.NewNop(), rtdb: db, service: mservice.Accounts}
if status := executeLogout(router, "device-a", "refresh-a"); status != http.StatusInternalServerError {
t.Fatalf("logout status = %d, want %d", status, http.StatusInternalServerError)
}
}
func TestBrowserLogoutUsesCookieCredentialAndClearsIt(t *testing.T) {
accountRef := bson.NewObjectID()
token := newLogoutToken(accountRef, "device-a", "cookie-refresh")
db := &logoutDB{tokens: map[string]*sessionmodel.RefreshToken{"device-a": token}}
router := &PublicRouter{logger: zap.NewNop(), rtdb: db, service: mservice.Accounts}
body := `{"clientId":"web","deviceId":"device-a","token":"body-must-be-ignored"}`
request := httptest.NewRequest(http.MethodPost, "https://app.sendico.io/api/v1/accounts/logout", strings.NewReader(body))
request.Header.Set(browser.TransportHeader, browser.TransportCookie)
request.AddCookie(&http.Cookie{Name: browser.RefreshCookie, Value: "cookie-refresh"})
response := httptest.NewRecorder()
router.logout(request)(response, request)
require.Equal(t, http.StatusOK, response.Code)
require.True(t, token.IsRevoked)
cookies := response.Result().Cookies()
require.Len(t, cookies, 1)
require.Equal(t, browser.RefreshCookie, cookies[0].Name)
require.Equal(t, -1, cookies[0].MaxAge)
require.Equal(t, "/api/v1/accounts", cookies[0].Path)
}
func TestBrowserLogoutWithoutCookieIsIdempotentAndClearsBrowserState(t *testing.T) {
db := &logoutDB{tokens: map[string]*sessionmodel.RefreshToken{}}
router := &PublicRouter{logger: zap.NewNop(), rtdb: db, service: mservice.Accounts}
body := `{"clientId":"web","deviceId":"device-a","token":"body-must-be-ignored"}`
request := httptest.NewRequest(http.MethodPost, "https://app.sendico.io/api/v1/accounts/logout", strings.NewReader(body))
request.Header.Set(browser.TransportHeader, browser.TransportCookie)
response := httptest.NewRecorder()
router.logout(request)(response, request)
require.Equal(t, http.StatusOK, response.Code)
require.Contains(t, response.Header().Get("Set-Cookie"), browser.RefreshCookie+"=")
require.Contains(t, response.Header().Get("Set-Cookie"), "Max-Age=0")
}
func TestBrowserLogoutKeepsCookieWhenRevocationFails(t *testing.T) {
db := &logoutDB{
tokens: map[string]*sessionmodel.RefreshToken{},
lookupErr: merrors.Internal("database unavailable"),
}
router := &PublicRouter{logger: zap.NewNop(), rtdb: db, service: mservice.Accounts}
body := `{"clientId":"web","deviceId":"device-a"}`
request := httptest.NewRequest(http.MethodPost, "https://app.sendico.io/api/v1/accounts/logout", strings.NewReader(body))
request.Header.Set(browser.TransportHeader, browser.TransportCookie)
request.AddCookie(&http.Cookie{Name: browser.RefreshCookie, Value: "cookie-refresh"})
response := httptest.NewRecorder()
router.logout(request)(response, request)
require.Equal(t, http.StatusInternalServerError, response.Code)
require.Empty(t, response.Header().Get("Set-Cookie"), "client must retain the cookie so logout can retry revocation")
}
@@ -0,0 +1,53 @@
package public
import (
"encoding/json"
"errors"
"net/http"
"time"
"git.sendico.io/tech/sxchnge/product/auth/request"
"git.sendico.io/tech/sxchnge/product/auth/response"
"git.sendico.io/tech/sxchnge/product/sessions/browser"
"git.sendico.io/tech/sxchnge/shared/http/response"
"git.sendico.io/tech/sxchnge/shared/service"
"go.uber.org/zap"
)
func (pr *PublicRouter) refreshAccessToken(r *http.Request) http.HandlerFunc {
pr.logger.Debug("Processing access token refresh request")
var req srequest.AccessTokenRefresh
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
pr.logger.Info("Failed to decode token rotation request", zap.Error(err))
return response.BadPayload(pr.logger, mservice.RefreshTokens, err)
}
account, token, refreshToken, err := pr.validateRefreshToken(r.Context(), r, &req)
if err != nil {
if errors.Is(err, errClientIPNotAllowed) {
return response.Forbidden(pr.logger, pr.service, "ip_not_allowed", "request ip is not allowed for this client")
}
if isExpectedRefreshValidationError(err) {
pr.logger.Debug("Access token refresh rejected", zap.Error(err))
return pr.rejectRefresh(r, err)
}
pr.logger.Warn("Failed to process access token refreshment request", zap.Error(err))
return response.Auto(pr.logger, pr.service, err)
}
if browser.UsesCookie(r) && refreshToken.ExpiresAt.Before(time.Now().Add(7*24*time.Hour)) {
return pr.refreshAndRespondLogin(r.Context(), r, &refreshToken.SessionIdentifier, account)
}
return sresponse.Account(pr.logger, account, token)
}
func (pr *PublicRouter) rejectRefresh(r *http.Request, err error) http.HandlerFunc {
handler := response.Auto(pr.logger, pr.service, err)
if !browser.UsesCookie(r) {
return handler
}
return func(w http.ResponseWriter, request *http.Request) {
browser.ClearRefreshCookie(w, request)
handler.ServeHTTP(w, request)
}
}
@@ -0,0 +1,152 @@
package public
import (
"context"
customermodel "git.sendico.io/tech/sxchnge/product/customers/model"
sessionmodel "git.sendico.io/tech/sxchnge/product/sessions/model"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
"git.sendico.io/tech/sxchnge/product/auth/config"
"git.sendico.io/tech/sxchnge/product/sessions/browser"
"github.com/go-chi/chi/v5"
"github.com/stretchr/testify/require"
"go.uber.org/zap"
)
func newBrowserRefreshRouter(t *testing.T, expiresAt time.Time) (*PublicRouter, *testRefreshTokenDB) {
t.Helper()
account := testActiveAccount(t)
clientID := "web"
deviceID := "browser-1"
rtdb := &testRefreshTokenDB{
client: &sessionmodel.Client{ClientID: clientID},
tokens: []*sessionmodel.RefreshToken{
{
ClientRefreshToken: sessionmodel.ClientRefreshToken{
SessionIdentifier: sessionmodel.SessionIdentifier{
ClientID: clientID,
DeviceID: deviceID,
SessionVersion: "version-1",
},
RefreshToken: "cookie-refresh",
},
AccountRef: account.GetID(),
ExpiresAt: expiresAt,
},
},
}
config := middleware.TokenConfig{
Expiration: middleware.TokenExpiration{Account: 1, Refresh: 720},
Length: 32,
}
signature := middleware.Signature{
Algorithm: "HS256",
PrivateKey: []byte("browser-refresh-test-secret"),
}
return NewRouter(
zap.NewNop(),
"/api/v1",
&testAccountDB{result: account},
nil,
rtdb,
nil,
chi.NewRouter(),
&config,
&signature,
middleware.LoginRateLimits{},
), rtdb
}
func executeBrowserRefresh(t *testing.T, router *PublicRouter) *httptest.ResponseRecorder {
t.Helper()
body := `{"clientId":"web","deviceId":"browser-1","token":"body-must-be-ignored"}`
request := httptest.NewRequestWithContext(context.Background(), http.MethodPost, "https://app.sendico.io/api/v1/accounts/refresh", strings.NewReader(body))
request.Header.Set(browser.TransportHeader, browser.TransportCookie)
request.AddCookie(&http.Cookie{Name: browser.RefreshCookie, Value: "cookie-refresh"})
response := httptest.NewRecorder()
router.refreshAccessToken(request)(response, request)
return response
}
func executeNativeRefresh(t *testing.T, router *PublicRouter, token string) *httptest.ResponseRecorder {
t.Helper()
body := `{"clientId":"web","deviceId":"browser-1","token":"` + token + `"}`
request := httptest.NewRequestWithContext(context.Background(), http.MethodPost, "https://app.sendico.io/api/v1/accounts/refresh", strings.NewReader(body))
response := httptest.NewRecorder()
router.refreshAccessToken(request)(response, request)
return response
}
func TestBrowserRefreshRestoresAccessWithoutExposingRefreshCredential(t *testing.T) {
router, _ := newBrowserRefreshRouter(t, time.Now().Add(30*24*time.Hour))
response := executeBrowserRefresh(t, router)
require.Equal(t, http.StatusOK, response.Code)
require.NotContains(t, response.Body.String(), "cookie-refresh")
require.NotEmpty(t, decodeLoginResponse(t, response.Body.Bytes()).Data.AccessToken.Token)
require.Empty(t, response.Header().Get("Set-Cookie"))
}
func TestBrowserRefreshRotatesExpiringCookieServerSide(t *testing.T) {
t.Setenv("API_ENDPOINT", "/api/v1")
router, rtdb := newBrowserRefreshRouter(t, time.Now().Add(24*time.Hour))
response := executeBrowserRefresh(t, router)
require.Equal(t, http.StatusOK, response.Code)
require.NotContains(t, response.Body.String(), "cookie-refresh")
cookies := response.Result().Cookies()
require.Len(t, cookies, 1)
require.Equal(t, browser.RefreshCookie, cookies[0].Name)
require.NotEqual(t, "cookie-refresh", cookies[0].Value)
require.Empty(t, decodeLoginResponse(t, response.Body.Bytes()).Data.RefreshToken.Token)
require.Equal(t, cookies[0].Value, rtdb.tokens[0].RefreshToken)
}
func TestBrowserRefreshRejectsStaleCookieAndClearsIt(t *testing.T) {
t.Setenv("API_ENDPOINT", "/api/v1")
router, rtdb := newBrowserRefreshRouter(t, time.Now().Add(30*24*time.Hour))
rtdb.tokens = nil
response := executeBrowserRefresh(t, router)
require.Equal(t, http.StatusUnauthorized, response.Code)
require.Contains(t, response.Body.String(), `"error":"unauthorized"`)
require.NotContains(t, response.Body.String(), "cookie-refresh")
cookies := response.Result().Cookies()
require.Len(t, cookies, 1)
require.Equal(t, browser.RefreshCookie, cookies[0].Name)
require.Empty(t, cookies[0].Value)
require.Equal(t, -1, cookies[0].MaxAge)
require.True(t, cookies[0].HttpOnly)
require.True(t, cookies[0].Secure)
}
func TestNativeRefreshRejectsStaleTokenWithoutCookieMutation(t *testing.T) {
router, _ := newBrowserRefreshRouter(t, time.Now().Add(30*24*time.Hour))
response := executeNativeRefresh(t, router, "stale-refresh")
require.Equal(t, http.StatusUnauthorized, response.Code)
require.Contains(t, response.Body.String(), `"error":"unauthorized"`)
require.Empty(t, response.Header().Get("Set-Cookie"))
}
func TestRefreshRejectsInactiveCustomerAndClientMismatch(t *testing.T) {
for _, status := range []customermodel.AccountStatus{customermodel.AccountBlocked, customermodel.AccountPendingVerification} {
t.Run(string(status), func(t *testing.T) {
router, _ := newBrowserRefreshRouter(t, time.Now().Add(24*time.Hour))
router.db.(*testAccountDB).result.Status = status
require.Equal(t, http.StatusUnauthorized, executeBrowserRefresh(t, router).Code)
})
}
router, sessions := newBrowserRefreshRouter(t, time.Now().Add(24*time.Hour))
other := testActiveAccount(t)
sessions.client.AccountRef = &other.ID
require.Equal(t, http.StatusUnauthorized, executeBrowserRefresh(t, router).Code)
}
@@ -0,0 +1,49 @@
package public
import (
"context"
customermodel "git.sendico.io/tech/sxchnge/product/customers/model"
sessionmodel "git.sendico.io/tech/sxchnge/product/sessions/model"
"net/http"
"git.sendico.io/tech/sxchnge/product/auth/response"
rtokens "git.sendico.io/tech/sxchnge/product/sessions/tokens"
"git.sendico.io/tech/sxchnge/shared/http/response"
"git.sendico.io/tech/sxchnge/shared/logging/fields"
"go.uber.org/zap"
)
func (pr *PublicRouter) refreshAndRespondLogin(
ctx context.Context,
r *http.Request,
session *sessionmodel.SessionIdentifier,
account *customermodel.Account,
) http.HandlerFunc {
refreshToken, err := rtokens.PrepareRefreshToken(
ctx,
r,
pr.rtdb,
pr.config.Length,
pr.config.Expiration.Refresh,
session,
account,
pr.logger,
)
if err != nil {
pr.logger.Warn("Failed to create refresh token", zap.Error(err), mzap.StorableRef(account),
zap.String("client_id", session.ClientID), zap.String("device_id", session.DeviceID))
return response.Internal(pr.logger, pr.service, err)
}
accessToken, err := pr.imp.CreateAccessTokenForSession(account, refreshToken.SessionIdentifier)
if err != nil {
pr.logger.Warn("Failed to generate access token", zap.Error(err), mzap.StorableRef(account),
zap.String("client_id", session.ClientID), zap.String("device_id", session.DeviceID))
return response.Internal(pr.logger, pr.service, err)
}
token := sresponse.TokenData{
Token: refreshToken.RefreshToken,
Expiration: refreshToken.ExpiresAt,
}
return sresponse.Login(pr.logger, account, &accessToken, &token)
}
@@ -0,0 +1,36 @@
package public
import (
"encoding/json"
"errors"
"net/http"
"git.sendico.io/tech/sxchnge/product/auth/request"
"git.sendico.io/tech/sxchnge/shared/http/response"
"git.sendico.io/tech/sxchnge/shared/service"
"go.uber.org/zap"
)
func (pr *PublicRouter) rotateRefreshToken(r *http.Request) http.HandlerFunc {
pr.logger.Debug("Processing token rotation request...")
var req srequest.TokenRefreshRotate
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
pr.logger.Info("Failed to decode token rotation request", zap.Error(err))
return response.BadPayload(pr.logger, mservice.RefreshTokens, err)
}
account, _, _, err := pr.validateRefreshToken(r.Context(), r, &req)
if err != nil {
if errors.Is(err, errClientIPNotAllowed) {
return response.Forbidden(pr.logger, pr.service, "ip_not_allowed", "request ip is not allowed for this client")
}
if isExpectedRefreshValidationError(err) {
pr.logger.Debug("Refresh token rotation rejected", zap.Error(err))
return pr.rejectRefresh(r, err)
}
pr.logger.Warn("Failed to validate refresh token", zap.Error(err))
return response.Auto(pr.logger, pr.service, err)
}
return pr.refreshAndRespondLogin(r.Context(), r, &req.SessionIdentifier, account)
}
@@ -0,0 +1,69 @@
package public
import (
"time"
"git.sendico.io/tech/sxchnge/product/auth/config"
re "git.sendico.io/tech/sxchnge/product/auth/http/endpoint"
"git.sendico.io/tech/sxchnge/product/auth/response"
"git.sendico.io/tech/sxchnge/product/customers/store"
"git.sendico.io/tech/sxchnge/product/sessions/store"
"git.sendico.io/tech/sxchnge/product/verification/store"
api "git.sendico.io/tech/sxchnge/shared/http"
"git.sendico.io/tech/sxchnge/shared/logging"
"git.sendico.io/tech/sxchnge/shared/ratelimit/store"
"git.sendico.io/tech/sxchnge/shared/service"
"github.com/go-chi/chi/v5"
)
type PublicRouter struct {
logger mlogger.Logger
db account.DB
imp *re.HttpEndpointRouter
rtdb refreshtokens.DB
config middleware.TokenConfig
signature middleware.Signature
service mservice.Type
attempts ratelimits.Store
limits middleware.LoginRateLimits
now func() time.Time
}
func (pr *PublicRouter) InstallHandler(service mservice.Type, endpoint string, method api.HTTPMethod, handler sresponse.HandlerFunc) {
pr.imp.InstallHandler(service, endpoint, method, handler)
}
func NewRouter(
logger mlogger.Logger,
apiEndpoint string,
db account.DB,
vdb verification.DB,
rtdb refreshtokens.DB,
attempts ratelimits.Store,
router chi.Router,
config *middleware.TokenConfig,
signature *middleware.Signature,
limits middleware.LoginRateLimits,
) *PublicRouter {
l := logger.Named("public")
hr := PublicRouter{
logger: l,
db: db,
rtdb: rtdb,
config: *config,
signature: *signature,
imp: re.NewHttpEndpointRouter(l, apiEndpoint, router, config, signature),
service: mservice.Accounts,
attempts: attempts,
limits: limits,
now: time.Now,
}
hr.InstallHandler(hr.service, "/login", api.Post, hr.login)
hr.InstallHandler(hr.service, "/login/api", api.Post, hr.apiLogin)
hr.InstallHandler(hr.service, "/rotate", api.Post, hr.rotateRefreshToken)
hr.InstallHandler(hr.service, "/refresh", api.Post, hr.refreshAccessToken)
hr.InstallHandler(hr.service, "/logout", api.Post, hr.logout)
return &hr
}
@@ -0,0 +1,106 @@
package public
import (
"context"
"errors"
customermodel "git.sendico.io/tech/sxchnge/product/customers/model"
sessionmodel "git.sendico.io/tech/sxchnge/product/sessions/model"
"net/http"
"time"
"git.sendico.io/tech/sxchnge/product/auth/request"
"git.sendico.io/tech/sxchnge/product/auth/response"
"git.sendico.io/tech/sxchnge/product/sessions/browser"
"git.sendico.io/tech/sxchnge/shared/errors"
"git.sendico.io/tech/sxchnge/shared/logging/fields"
"go.uber.org/zap"
)
var errClientIPNotAllowed = merrors.Define("client_ip_not_allowed")
func isExpectedRefreshValidationError(err error) bool {
return errors.Is(err, merrors.ErrUnauthorized)
}
func validateToken(token string, rt *sessionmodel.RefreshToken) string {
if rt.AccountRef == nil {
return "missing account reference"
}
if token != rt.RefreshToken {
return "tokens do not match"
}
if rt.ExpiresAt.Before(time.Now()) {
return "token expired"
}
if rt.IsRevoked {
return "token has been revoked"
}
return ""
}
func (pr *PublicRouter) validateRefreshToken(ctx context.Context, r *http.Request, req *srequest.TokenRefreshRotate) (*customermodel.Account, *sresponse.TokenData, *sessionmodel.RefreshToken, error) {
credential, err := browser.RefreshCredential(r, req.RefreshToken)
if err != nil {
return nil, nil, nil, err
}
req.RefreshToken = credential
client, err := pr.rtdb.GetClient(ctx, req.ClientID)
if errors.Is(err, merrors.ErrNoData) || client == nil {
pr.logger.Debug("Refresh token rejected: client not found", zap.String("client_id", req.ClientID))
return nil, nil, nil, merrors.Unauthorized("client not found")
}
if err != nil {
pr.logger.Warn("Failed to fetch client for refresh token validation", zap.Error(err), zap.String("client_id", req.ClientID))
return nil, nil, nil, err
}
if client.IsRevoked {
return nil, nil, nil, merrors.Unauthorized("client has been revoked")
}
if h := pr.validateClientIPPolicy(r, req.ClientID, client); h != nil {
return nil, nil, nil, errClientIPNotAllowed
}
rt, err := pr.rtdb.GetByCRT(ctx, req)
if errors.Is(err, merrors.ErrNoData) || errors.Is(err, merrors.ErrAccessDenied) {
pr.logger.Debug("Refresh session is inactive", zap.String("client_id", req.ClientID), zap.String("device_id", req.DeviceID))
return nil, nil, nil, merrors.Unauthorized("refresh session is inactive")
}
if err != nil {
return nil, nil, nil, err
}
if rt.SessionVersion == "" {
pr.logger.Debug("Refresh token rejected: legacy session requires reauthentication",
zap.String("client_id", req.ClientID), zap.String("device_id", req.DeviceID))
return nil, nil, nil, merrors.Unauthorized("session reauthentication required")
}
if reason := validateToken(req.RefreshToken, rt); len(reason) > 0 {
pr.logger.Debug("Token validation failed", zap.String("reason", reason))
return nil, nil, nil, merrors.Unauthorized(reason)
}
var account customermodel.Account
if err := pr.db.Get(ctx, *rt.AccountRef, &account); errors.Is(err, merrors.ErrNoData) {
pr.logger.Debug("User not found while rotating refresh token", zap.Error(err), mzap.AccRef(*rt.AccountRef))
return nil, nil, nil, merrors.Unauthorized("user not found")
} else if err != nil {
pr.logger.Warn("Failed to fetch user while validating refresh token", zap.Error(err), mzap.AccRef(*rt.AccountRef))
return nil, nil, nil, err
}
if !account.IsActive() || account.IsArchived() {
return nil, nil, nil, merrors.Unauthorized("customer is inactive")
}
if client.AccountRef != nil && *client.AccountRef != account.ID {
return nil, nil, nil, merrors.Unauthorized("client account mismatch")
}
accessToken, err := pr.imp.CreateAccessTokenForSession(&account, rt.SessionIdentifier)
if err != nil {
pr.logger.Warn("Failed to generate access token", zap.Error(err))
return nil, nil, nil, err
}
return &account, &accessToken, rt, nil
}
+16
View File
@@ -0,0 +1,16 @@
package router
import (
"git.sendico.io/tech/sxchnge/product/auth/response"
api "git.sendico.io/tech/sxchnge/shared/http"
"git.sendico.io/tech/sxchnge/shared/service"
)
type APIRouter interface {
InstallHandler(service mservice.Type, endpoint string, method api.HTTPMethod, handler sresponse.HandlerFunc)
}
type ProtectedAPIRouter interface {
AccountHandler(service mservice.Type, endpoint string, method api.HTTPMethod, handler sresponse.AccountHandlerFunc)
PendingAccountHandler(service mservice.Type, endpoint string, method api.HTTPMethod, handler sresponse.PendingAccountHandlerFunc)
}
+73
View File
@@ -0,0 +1,73 @@
package ipguard
import (
"net"
"net/http"
"strings"
)
// ClientIP resolves the real caller IP from the request.
// When the request arrives via the Caddy reverse proxy, the real client IP is
// forwarded in the X-Real-IP header (set via "header_up X-Real-IP {remote_host}"
// in the Caddyfile). RemoteAddr is used as a fallback for direct connections
// (local development, health checks, etc.).
func ClientIP(r *http.Request) net.IP {
if r == nil {
return nil
}
// Prefer X-Real-IP set by the trusted Caddy reverse proxy.
if xri := strings.TrimSpace(r.Header.Get("X-Real-IP")); xri != "" {
if ip := net.ParseIP(xri); ip != nil {
return ip
}
}
raw := strings.TrimSpace(r.RemoteAddr)
if raw == "" {
return nil
}
if ip := net.ParseIP(raw); ip != nil {
return ip
}
host, _, err := net.SplitHostPort(raw)
if err != nil {
return nil
}
return net.ParseIP(host)
}
func parseCIDRs(raw []string) ([]*net.IPNet, error) {
blocks := make([]*net.IPNet, 0, len(raw))
for _, item := range raw {
clean := strings.TrimSpace(item)
if clean == "" {
continue
}
_, block, err := net.ParseCIDR(clean)
if err != nil {
return nil, err
}
blocks = append(blocks, block)
}
return blocks, nil
}
// Allowed reports whether clientIP is allowed by configured CIDRs.
// Empty CIDR list means unrestricted access.
func Allowed(clientIP net.IP, cidrs []string) (bool, error) {
blocks, err := parseCIDRs(cidrs)
if err != nil {
return false, err
}
if len(blocks) == 0 {
return true, nil
}
if clientIP == nil {
return false, nil
}
for _, block := range blocks {
if block.Contains(clientIP) {
return true, nil
}
}
return false, nil
}
@@ -0,0 +1,116 @@
package ipguard
import (
"net"
"net/http"
"testing"
)
func TestClientIP(t *testing.T) {
t.Run("extracts host from remote addr", func(t *testing.T) {
req := &http.Request{RemoteAddr: "10.1.2.3:1234"}
ip := ClientIP(req)
if ip == nil || ip.String() != "10.1.2.3" {
t.Fatalf("unexpected ip: %v", ip)
}
})
t.Run("supports plain ip", func(t *testing.T) {
req := &http.Request{RemoteAddr: "8.8.8.8"}
ip := ClientIP(req)
if ip == nil || ip.String() != "8.8.8.8" {
t.Fatalf("unexpected ip: %v", ip)
}
})
t.Run("invalid remote addr", func(t *testing.T) {
req := &http.Request{RemoteAddr: "invalid"}
if ip := ClientIP(req); ip != nil {
t.Fatalf("expected nil ip, got %v", ip)
}
})
t.Run("prefers X-Real-IP over remote addr", func(t *testing.T) {
req := &http.Request{
RemoteAddr: "172.19.0.31:5000",
Header: http.Header{"X-Real-Ip": []string{"178.57.67.154"}},
}
ip := ClientIP(req)
if ip == nil || ip.String() != "178.57.67.154" {
t.Fatalf("expected real client ip, got %v", ip)
}
})
t.Run("falls back to remote addr when X-Real-IP absent", func(t *testing.T) {
req := &http.Request{RemoteAddr: "10.0.0.1:9000"}
ip := ClientIP(req)
if ip == nil || ip.String() != "10.0.0.1" {
t.Fatalf("expected remote addr ip, got %v", ip)
}
})
t.Run("falls back to remote addr when X-Real-IP invalid", func(t *testing.T) {
req := &http.Request{
RemoteAddr: "10.0.0.1:9000",
Header: http.Header{"X-Real-Ip": []string{"not-an-ip"}},
}
ip := ClientIP(req)
if ip == nil || ip.String() != "10.0.0.1" {
t.Fatalf("expected fallback to remote addr, got %v", ip)
}
})
}
func TestAllowed(t *testing.T) {
clientIP := net.ParseIP("10.1.2.3")
if clientIP == nil {
t.Fatal("failed to parse test ip")
}
t.Run("allows when cidr matches", func(t *testing.T) {
allowed, err := Allowed(clientIP, []string{"10.0.0.0/8"})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if !allowed {
t.Fatal("expected allowed")
}
})
t.Run("denies when cidr does not match", func(t *testing.T) {
allowed, err := Allowed(clientIP, []string{"192.168.0.0/16"})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if allowed {
t.Fatal("expected denied")
}
})
t.Run("allows when cidr list is empty", func(t *testing.T) {
allowed, err := Allowed(clientIP, nil)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if !allowed {
t.Fatal("expected allowed")
}
})
t.Run("invalid cidr fails", func(t *testing.T) {
_, err := Allowed(clientIP, []string{"not-a-cidr"})
if err == nil {
t.Fatal("expected error")
}
})
t.Run("nil client ip denied when cidrs configured", func(t *testing.T) {
allowed, err := Allowed(nil, []string{"10.0.0.0/8"})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if allowed {
t.Fatal("expected denied")
}
})
}
+141
View File
@@ -0,0 +1,141 @@
package model
import (
"fmt"
customermodel "git.sendico.io/tech/sxchnge/product/customers/model"
sessionmodel "git.sendico.io/tech/sxchnge/product/sessions/model"
"time"
"git.sendico.io/tech/sxchnge/product/auth/config"
mduration "git.sendico.io/tech/sxchnge/shared/duration"
"git.sendico.io/tech/sxchnge/shared/errors"
"go.mongodb.org/mongo-driver/v2/bson"
)
type AccountToken struct {
AccountRef bson.ObjectID
Login string
Name string
Locale string
sessionmodel.SessionIdentifier
Expiration time.Time
Pending bool
}
func createAccountToken(a *customermodel.Account, expiration int, session sessionmodel.SessionIdentifier) AccountToken {
return AccountToken{
AccountRef: *a.GetID(),
Login: a.Login,
Name: a.Name,
Locale: a.Locale,
SessionIdentifier: session,
Expiration: time.Now().Add(mduration.Param2Duration(expiration, time.Hour)),
Pending: false,
}
}
func getTokenParam(claims middleware.MapClaims, param string) (string, error) {
id, ok := claims[param].(string)
if !ok {
return "", merrors.NoData(fmt.Sprintf("param '%s' not found", param))
}
return id, nil
}
const (
paramNameID = "id"
paramNameName = "name"
paramNameLocale = "locale"
paramNameLogin = "login"
paramNameClientID = "clientId"
paramNameDeviceID = "deviceId"
paramNameSessionVersion = "sessionVersion"
paramNameExpiration = "exp"
paramNamePending = "pending"
)
func Claims2Token(claims middleware.MapClaims) (*AccountToken, error) {
var at AccountToken
var err error
var account string
if account, err = getTokenParam(claims, paramNameID); err != nil {
return nil, err
}
if at.AccountRef, err = bson.ObjectIDFromHex(account); err != nil {
return nil, err
}
if at.Login, err = getTokenParam(claims, paramNameLogin); err != nil {
return nil, err
}
if at.Name, err = getTokenParam(claims, paramNameName); err != nil {
return nil, err
}
if at.Locale, err = getTokenParam(claims, paramNameLocale); err != nil {
return nil, err
}
if clientID, ok := claims[paramNameClientID]; ok {
if clientIDText, ok := clientID.(string); ok {
at.ClientID = clientIDText
}
}
if deviceID, ok := claims[paramNameDeviceID]; ok {
if deviceIDText, ok := deviceID.(string); ok {
at.DeviceID = deviceIDText
}
}
if version, ok := claims[paramNameSessionVersion]; ok {
if versionText, ok := version.(string); ok {
at.SessionVersion = versionText
}
}
if pending, ok := claims[paramNamePending]; ok {
if pbool, ok := pending.(bool); ok {
at.Pending = pbool
}
}
if expValue, ok := claims[paramNameExpiration]; ok {
switch exp := expValue.(type) {
case time.Time:
at.Expiration = exp
case float64:
at.Expiration = time.Unix(int64(exp), 0)
case int64:
at.Expiration = time.Unix(exp, 0)
default:
return nil, merrors.InvalidDataType(fmt.Sprintf("expiration param is of invalid type: %T", expValue))
}
} else {
return nil, merrors.InvalidDataType(fmt.Sprintf("expiration param is of invalid type: %T", expValue))
}
return &at, nil
}
func Account2ClaimsForSession(a *customermodel.Account, expiration int, session sessionmodel.SessionIdentifier) middleware.MapClaims {
t := createAccountToken(a, expiration, session)
return middleware.MapClaims{
paramNameID: t.AccountRef.Hex(),
paramNameLogin: t.Login,
paramNameName: t.Name,
paramNameLocale: t.Locale,
paramNameClientID: t.ClientID,
paramNameDeviceID: t.DeviceID,
paramNameSessionVersion: t.SessionVersion,
paramNameExpiration: t.Expiration.Unix(),
paramNamePending: t.Pending,
}
}
func PendingAccount2Claims(a *customermodel.Account, expirationMinutes int) middleware.MapClaims {
t := createAccountToken(a, expirationMinutes/60, sessionmodel.SessionIdentifier{})
t.Expiration = time.Now().Add(time.Duration(expirationMinutes) * time.Minute)
t.Pending = true
return middleware.MapClaims{
paramNameID: t.AccountRef.Hex(),
paramNameLogin: t.Login,
paramNameName: t.Name,
paramNameLocale: t.Locale,
paramNameExpiration: t.Expiration.Unix(),
paramNamePending: t.Pending,
}
}
+32
View File
@@ -0,0 +1,32 @@
package model
import (
customermodel "git.sendico.io/tech/sxchnge/product/customers/model"
sessionmodel "git.sendico.io/tech/sxchnge/product/sessions/model"
"go.mongodb.org/mongo-driver/v2/bson"
"testing"
)
func TestAccountTokenPreservesSessionIdentity(t *testing.T) {
account := &customermodel.Account{}
account.SetID(bson.NewObjectID())
account.Login = "security@example.com"
account.Name = "Security"
account.Locale = "en"
session := sessionmodel.SessionIdentifier{
ClientID: "web",
DeviceID: "device-a",
SessionVersion: "session-version-a",
}
token, err := Claims2Token(Account2ClaimsForSession(account, 1, session))
if err != nil {
t.Fatalf("decode account claims: %v", err)
}
if token.SessionIdentifier != session {
t.Fatalf("session identity = %#v, want %#v", token.SessionIdentifier, session)
}
if token.Expiration.IsZero() {
t.Fatal("access token expiration is missing")
}
}
+11
View File
@@ -0,0 +1,11 @@
package srequest
import sessionmodel "git.sendico.io/tech/sxchnge/product/sessions/model"
import customermodel "git.sendico.io/tech/sxchnge/product/customers/model"
type Login struct {
sessionmodel.SessionIdentifier `json:",inline"`
customermodel.LoginData `json:"login"`
ClientSecret string `json:"clientSecret,omitempty"`
}
+5
View File
@@ -0,0 +1,5 @@
package srequest
import sessionmodel "git.sendico.io/tech/sxchnge/product/sessions/model"
type AccessTokenRefresh = sessionmodel.ClientRefreshToken
+5
View File
@@ -0,0 +1,5 @@
package srequest
import sessionmodel "git.sendico.io/tech/sxchnge/product/sessions/model"
type TokenRefreshRotate = sessionmodel.ClientRefreshToken
@@ -0,0 +1,5 @@
package srequest
type Validatable interface {
Validate() error
}
@@ -0,0 +1,15 @@
package srequest
import sessionmodel "git.sendico.io/tech/sxchnge/product/sessions/model"
type VerificationCodeRequest struct {
Purpose string `json:"purpose"`
Target string `json:"target,omitempty"`
IdempotencyKey string `json:"idempotencyKey"`
}
type CodeVerificationRequest struct {
VerificationCodeRequest `json:",inline"`
Code string `json:"code"`
SessionIdentifier sessionmodel.SessionIdentifier `json:"sessionIdentifier"`
}
+40
View File
@@ -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
+16
View File
@@ -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})
}
+35
View File
@@ -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)
}
}
+14
View File
@@ -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
)
+27
View File
@@ -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},
})
}
+8
View File
@@ -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"`
}
+149
View File
@@ -0,0 +1,149 @@
package storage
import (
"git.sendico.io/tech/sxchnge/product/auth/config"
"git.sendico.io/tech/sxchnge/product/customers/store"
"git.sendico.io/tech/sxchnge/product/customers/store/mongo"
"git.sendico.io/tech/sxchnge/product/sessions/store"
"git.sendico.io/tech/sxchnge/product/verification/store"
"git.sendico.io/tech/sxchnge/shared/database"
edgemongo "git.sendico.io/tech/sxchnge/shared/database/dependencies"
"git.sendico.io/tech/sxchnge/shared/logging"
"git.sendico.io/tech/sxchnge/shared/ratelimit/store"
"go.uber.org/zap"
)
type Stores struct {
Accounts account.DB
Verification verification.DB
RefreshTokens refreshtokens.DB
LoginAttempts ratelimits.Store
}
type TokenStores struct {
Verification verification.DB
RefreshTokens refreshtokens.DB
}
type resolvedConfig struct {
deps *edgemongo.Dependencies
storage middleware.AuthStorageConfig
}
func OpenStores(
logger mlogger.Logger,
dbf db.Factory,
config *middleware.AuthStorageConfig,
) (*Stores, error) {
resolved, err := resolve(logger, dbf, config)
if err != nil {
return nil, err
}
logger.Info(
"Auth storage configured",
zap.String("accounts_collection", resolved.storage.AccountsCollection),
zap.String("refresh_tokens_collection", resolved.storage.RefreshTokensCollection),
zap.String("clients_collection", resolved.storage.ClientsCollection),
zap.String("verification_tokens_collection", resolved.storage.VerificationTokensCollection),
zap.String("login_attempts_collection", resolved.storage.LoginAttemptsCollection),
zap.String("verification_rate_limits_collection", resolved.storage.VerificationRateLimitsCollection),
)
adb, err := accountdb.CreateWithCollection(logger, resolved.deps.Database, resolved.storage.AccountsCollection)
if err != nil {
logger.Error("Failed to create account database", zap.Error(err), zap.String("collection", resolved.storage.AccountsCollection))
return nil, err
}
tokens, err := openTokenStores(logger, resolved)
if err != nil {
return nil, err
}
attempts, err := ratelimits.Create(logger, resolved.deps.Database, resolved.storage.LoginAttemptsCollection)
if err != nil {
logger.Error("Failed to create login attempt database", zap.Error(err), zap.String("collection", resolved.storage.LoginAttemptsCollection))
return nil, err
}
return &Stores{
Accounts: adb,
Verification: tokens.Verification,
RefreshTokens: tokens.RefreshTokens,
LoginAttempts: attempts,
}, nil
}
func OpenTokenStores(
logger mlogger.Logger,
dbf db.Factory,
config *middleware.AuthStorageConfig,
) (*TokenStores, error) {
resolved, err := resolve(logger, dbf, config)
if err != nil {
return nil, err
}
logger.Info(
"Auth token storage configured",
zap.String("refresh_tokens_collection", resolved.storage.RefreshTokensCollection),
zap.String("clients_collection", resolved.storage.ClientsCollection),
zap.String("verification_tokens_collection", resolved.storage.VerificationTokensCollection),
zap.String("verification_rate_limits_collection", resolved.storage.VerificationRateLimitsCollection),
)
return openTokenStores(logger, resolved)
}
func resolve(
logger mlogger.Logger,
dbf db.Factory,
config *middleware.AuthStorageConfig,
) (*resolvedConfig, error) {
deps, err := edgemongo.ResolveDependencies(dbf)
if err != nil {
logger.Error("Failed to resolve local auth storage dependencies", zap.Error(err))
return nil, err
}
return &resolvedConfig{
deps: deps,
storage: config.WithDefaults(),
}, nil
}
func openTokenStores(logger mlogger.Logger, resolved *resolvedConfig) (*TokenStores, error) {
rtdb, err := refreshtokens.CreateWithCollections(logger, resolved.deps.Database, refreshtokens.Collections{
RefreshTokens: resolved.storage.RefreshTokensCollection,
Clients: resolved.storage.ClientsCollection,
})
if err != nil {
logger.Error(
"Failed to create refresh token management database",
zap.Error(err),
zap.String("refresh_tokens_collection", resolved.storage.RefreshTokensCollection),
zap.String("clients_collection", resolved.storage.ClientsCollection),
)
return nil, err
}
verificationLimits, err := ratelimits.Create(logger, resolved.deps.Database, resolved.storage.VerificationRateLimitsCollection)
if err != nil {
logger.Error("Failed to create verification rate-limit database", zap.Error(err), zap.String("collection", resolved.storage.VerificationRateLimitsCollection))
return nil, err
}
cdb, err := verification.CreateWithCollection(
logger,
resolved.deps.Database,
resolved.deps.Transactions,
resolved.storage.VerificationTokensCollection,
)
if err != nil {
logger.Error("Failed to create verification token database", zap.Error(err), zap.String("collection", resolved.storage.VerificationTokensCollection))
return nil, err
}
cdb.WithIssuanceLimits(verificationLimits)
return &TokenStores{
Verification: cdb,
RefreshTokens: rtdb,
}, nil
}