feat(identity): enforce shared session and browser request security

This commit is contained in:
TakuyaYagam1 committed 2026-10-07 11:33:40 +03:00
1 parent 7df521bd89
commit 86f562f5b6
23 files changed
+430 -121

No files matched your search

@@ -19,9 +19,8 @@ type SessionAuthenticator interface {
Authenticate(ctx context.Context, rawToken string) (identity.AuthenticatedSession, error)
}
// SessionSecurity authenticates only the three session operations enabled in
// this implementation. All other protected operations remain unavailable until their
// handlers are implemented.
// SessionSecurity authenticates every operation protected by the public
// contract's session schemes. Business handlers may still be unimplemented.
type SessionSecurity struct {
authenticator SessionAuthenticator
publicOrigin string
@@ -37,15 +36,15 @@ func (handler *IdentityHandler) SessionSecurity() *SessionSecurity {
if handler == nil || handler.sessions == nil {
return NewSessionSecurity(nil, "")
}
return NewSessionSecurity(handler.sessions, handler.sessionOrigin)
return NewSessionSecurity(handler.sessions, handler.publicOrigin)
}
func (security *SessionSecurity) HandleSessionCookie(
ctx context.Context,
operation api.OperationName,
_ api.OperationName,
cookie api.SessionCookie,
) (context.Context, error) {
if !sessionOperationAllowed(operation) || security == nil || security.authenticator == nil ||
if security == nil || security.authenticator == nil ||
!httpmiddleware.SessionCookieMatches(ctx, cookie.APIKey) {
return ctx, ogenerrors.ErrSkipServerSecurity
}
@@ -56,34 +55,26 @@ func (security *SessionSecurity) HandleSessionCookie(
if err != nil {
return ctx, fmt.Errorf("SessionSecurity.HandleSessionCookie - authenticate session: %w", err)
}
return withAuthenticatedSession(ctx, authenticated), nil
ctx, ok := withAuthenticatedSession(ctx, authenticated)
if !ok {
return ctx, ogenerrors.ErrSkipServerSecurity
}
return ctx, nil
}
func (security *SessionSecurity) HandleSessionCsrf(
ctx context.Context,
operation api.OperationName,
_ api.OperationName,
csrf api.SessionCsrf,
) (context.Context, error) {
if operation != api.LogoutOperation {
return ctx, ogenerrors.ErrSkipServerSecurity
}
authenticated, ok := sessionFromContext(ctx)
if !ok {
return ctx, ogenerrors.ErrSkipServerSecurity
}
origin, hasOrigin := httpmiddleware.RequestOrigin(ctx)
if security == nil || !hasOrigin || origin != security.publicOrigin || authenticated.CSRFToken == "" ||
if security == nil || security.publicOrigin == "" || !hasOrigin || origin != security.publicOrigin ||
!hmac.Equal([]byte(authenticated.CSRFToken), []byte(csrf.APIKey)) {
return ctx, errCsrfCheckFailed
}
return ctx, nil
}
func sessionOperationAllowed(operation api.OperationName) bool {
switch operation {
case api.GetCurrentSessionOperation, api.GetCurrentAccountOperation, api.LogoutOperation:
return true
default:
return false
}
}