2016-09-28 11:35:15 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
2017-03-15 15:55:30 +00:00
|
|
|
"context"
|
2016-09-28 11:35:15 +00:00
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
2021-06-26 04:46:51 +00:00
|
|
|
"github.com/fleetdm/fleet/v4/server/fleet"
|
2016-09-28 11:35:15 +00:00
|
|
|
)
|
|
|
|
|
2021-08-20 15:27:41 +00:00
|
|
|
func (mw metricsMiddleware) SSOSettings(ctx context.Context) (settings *fleet.SessionSSOSettings, err error) {
|
2017-05-17 15:58:40 +00:00
|
|
|
defer func(begin time.Time) {
|
2021-08-20 15:27:41 +00:00
|
|
|
lvs := []string{"method", "SessionSSOSettings", "error", fmt.Sprint(err != nil)}
|
2017-05-17 15:58:40 +00:00
|
|
|
mw.requestCount.With(lvs...).Add(1)
|
|
|
|
mw.requestLatency.With(lvs...).Observe(time.Since(begin).Seconds())
|
|
|
|
}(time.Now())
|
|
|
|
settings, err = mw.Service.SSOSettings(ctx)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-05-09 00:43:48 +00:00
|
|
|
func (mw metricsMiddleware) InitiateSSO(ctx context.Context, relayValue string) (idpURL string, err error) {
|
|
|
|
defer func(begin time.Time) {
|
|
|
|
lvs := []string{"method", "InitiateSSO", "error", fmt.Sprint(err != nil)}
|
|
|
|
mw.requestCount.With(lvs...).Add(1)
|
|
|
|
mw.requestLatency.With(lvs...).Observe(time.Since(begin).Seconds())
|
|
|
|
}(time.Now())
|
|
|
|
idpURL, err = mw.Service.InitiateSSO(ctx, relayValue)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func (mw metricsMiddleware) CallbackSSO(ctx context.Context, auth fleet.Auth) (sess *fleet.SSOSession, err error) {
|
2017-05-09 00:43:48 +00:00
|
|
|
defer func(begin time.Time) {
|
|
|
|
lvs := []string{"method", "CallbackSSO", "error", fmt.Sprint(err != nil)}
|
|
|
|
mw.requestCount.With(lvs...).Add(1)
|
|
|
|
mw.requestLatency.With(lvs...).Observe(time.Since(begin).Seconds())
|
|
|
|
}(time.Now())
|
|
|
|
sess, err = mw.Service.CallbackSSO(ctx, auth)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-06-24 20:42:29 +00:00
|
|
|
func (mw metricsMiddleware) Login(ctx context.Context, email string, password string) (*fleet.User, string, error) {
|
2016-09-28 11:35:15 +00:00
|
|
|
var (
|
2021-06-06 22:07:29 +00:00
|
|
|
user *fleet.User
|
2016-09-28 11:35:15 +00:00
|
|
|
token string
|
|
|
|
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())
|
2021-06-24 20:42:29 +00:00
|
|
|
user, token, err = mw.Service.Login(ctx, email, password)
|
2016-09-28 11:35:15 +00:00
|
|
|
return user, token, 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
|
|
|
|
}
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func (mw metricsMiddleware) GetInfoAboutSessionsForUser(ctx context.Context, id uint) ([]*fleet.Session, error) {
|
2016-09-28 11:35:15 +00:00
|
|
|
var (
|
2021-06-06 22:07:29 +00:00
|
|
|
sessions []*fleet.Session
|
2016-09-28 11:35:15 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func (mw metricsMiddleware) GetInfoAboutSession(ctx context.Context, id uint) (*fleet.Session, error) {
|
2016-09-28 11:35:15 +00:00
|
|
|
var (
|
2021-06-06 22:07:29 +00:00
|
|
|
session *fleet.Session
|
2016-09-28 11:35:15 +00:00
|
|
|
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
|
|
|
|
}
|