fleet/server/service/metrics_sessions.go
Lucas Manuel Rodriguez c69d56ed64
Replace home-made SAML implementation with https://github.com/crewjam/saml (#28486)
For https://github.com/fleetdm/confidential/issues/9931.


[Here](ec3e8edbdc/docs/Contributing/Testing-and-local-development.md (L339))'s
how to test SAML locally with SimpleSAML.

- [X] Changes file added for user-visible changes in `changes/`,
`orbit/changes/` or `ee/fleetd-chrome/changes`.
See [Changes
files](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/Committing-Changes.md#changes-files)
for more information.
- [x] Added/updated automated tests
- [x] A detailed QA plan exists on the associated ticket (if it isn't
there, work with the product group's QA engineer to add it)
- [x] Manual QA for all new/changed functionality

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

* **New Features**
* Improved SSO and SAML integration with enhanced session management
using secure cookies.
  * Added support for IdP-initiated login flows.
* Introduced new tests covering SSO login flows, metadata handling, and
error scenarios.

* **Bug Fixes**
* Enhanced validation and error handling for invalid or tampered SAML
responses.
  * Fixed session cookie handling during SSO and Apple MDM SSO flows.

* **Refactor**
* Replaced custom SAML implementation with the crewjam/saml library for
improved reliability.
  * Simplified SAML metadata parsing and session store management.
  * Streamlined SSO authorization request and response processing.
  * Removed deprecated fields and redundant code related to SSO.

* **Documentation**
* Updated testing and local development docs with clearer instructions
for SSO and IdP-initiated login.

* **Chores**
  * Upgraded dependencies including crewjam/saml and related packages.
* Cleaned up tests and configuration by removing deprecated fields and
unused imports.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2025-07-07 15:13:46 -03:00

96 lines
3.1 KiB
Go

package service
import (
"context"
"fmt"
"time"
"github.com/fleetdm/fleet/v4/server/fleet"
)
func (mw metricsMiddleware) Login(ctx context.Context, email string, password string, supportsEmailVerification bool) (*fleet.User, *fleet.Session, error) {
var (
user *fleet.User
session *fleet.Session
err error
)
defer func(begin time.Time) {
lvs := []string{"method", "Login", "error", fmt.Sprint(err != nil)}
mw.requestCount.With(lvs...).Add(1)
mw.requestLatency.With(lvs...).Observe(time.Since(begin).Seconds())
}(time.Now())
user, session, err = mw.Service.Login(ctx, email, password, supportsEmailVerification)
return user, session, err
}
func (mw metricsMiddleware) Logout(ctx context.Context) error {
var err error
defer func(begin time.Time) {
lvs := []string{"method", "Logout", "error", fmt.Sprint(err != nil)}
mw.requestCount.With(lvs...).Add(1)
mw.requestLatency.With(lvs...).Observe(time.Since(begin).Seconds())
}(time.Now())
err = mw.Service.Logout(ctx)
return err
}
func (mw metricsMiddleware) DestroySession(ctx context.Context) error {
var err error
defer func(begin time.Time) {
lvs := []string{"method", "DestroySession", "error", fmt.Sprint(err != nil)}
mw.requestCount.With(lvs...).Add(1)
mw.requestLatency.With(lvs...).Observe(time.Since(begin).Seconds())
}(time.Now())
err = mw.Service.DestroySession(ctx)
return err
}
func (mw metricsMiddleware) GetInfoAboutSessionsForUser(ctx context.Context, id uint) ([]*fleet.Session, error) {
var (
sessions []*fleet.Session
err error
)
defer func(begin time.Time) {
lvs := []string{"method", "GetInfoAboutSessionsForUser", "error", fmt.Sprint(err != nil)}
mw.requestCount.With(lvs...).Add(1)
mw.requestLatency.With(lvs...).Observe(time.Since(begin).Seconds())
}(time.Now())
sessions, err = mw.Service.GetInfoAboutSessionsForUser(ctx, id)
return sessions, err
}
func (mw metricsMiddleware) DeleteSessionsForUser(ctx context.Context, id uint) error {
var err error
defer func(begin time.Time) {
lvs := []string{"method", "DeleteSessionsForUser", "error", fmt.Sprint(err != nil)}
mw.requestCount.With(lvs...).Add(1)
mw.requestLatency.With(lvs...).Observe(time.Since(begin).Seconds())
}(time.Now())
err = mw.Service.DeleteSessionsForUser(ctx, id)
return err
}
func (mw metricsMiddleware) GetInfoAboutSession(ctx context.Context, id uint) (*fleet.Session, error) {
var (
session *fleet.Session
err error
)
defer func(begin time.Time) {
lvs := []string{"method", "GetInfoAboutSession", "error", fmt.Sprint(err != nil)}
mw.requestCount.With(lvs...).Add(1)
mw.requestLatency.With(lvs...).Observe(time.Since(begin).Seconds())
}(time.Now())
session, err = mw.Service.GetInfoAboutSession(ctx, id)
return session, err
}
func (mw metricsMiddleware) DeleteSession(ctx context.Context, id uint) error {
var err error
defer func(begin time.Time) {
lvs := []string{"method", "DeleteSession", "error", fmt.Sprint(err != nil)}
mw.requestCount.With(lvs...).Add(1)
mw.requestLatency.With(lvs...).Observe(time.Since(begin).Seconds())
}(time.Now())
err = mw.Service.DeleteSession(ctx, id)
return err
}