2016-09-26 18:48:55 +00:00
|
|
|
package service
|
2016-09-05 19:50:57 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/go-kit/kit/endpoint"
|
2017-02-01 17:20:50 +00:00
|
|
|
"github.com/kolide/kolide/server/kolide"
|
2016-09-05 19:50:57 +00:00
|
|
|
"golang.org/x/net/context"
|
|
|
|
)
|
|
|
|
|
2016-09-08 01:24:11 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Login
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
type loginRequest struct {
|
2016-09-15 19:27:55 +00:00
|
|
|
Username string // can be username or email
|
2016-09-08 01:24:11 +00:00
|
|
|
Password string
|
|
|
|
}
|
|
|
|
|
|
|
|
type loginResponse struct {
|
2016-10-11 16:22:11 +00:00
|
|
|
User *kolide.User `json:"user,omitempty"`
|
|
|
|
Token string `json:"token,omitempty"`
|
|
|
|
Err error `json:"error,omitempty"`
|
2016-09-08 01:24:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r loginResponse) error() error { return r.Err }
|
|
|
|
|
|
|
|
func makeLoginEndpoint(svc kolide.Service) endpoint.Endpoint {
|
|
|
|
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
|
|
|
req := request.(loginRequest)
|
|
|
|
user, token, err := svc.Login(ctx, req.Username, req.Password)
|
|
|
|
if err != nil {
|
|
|
|
return loginResponse{Err: err}, nil
|
|
|
|
}
|
2016-10-11 16:22:11 +00:00
|
|
|
return loginResponse{user, token, nil}, nil
|
2016-09-08 01:24:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Logout
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
type logoutResponse struct {
|
|
|
|
Err error `json:"error,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r logoutResponse) error() error { return r.Err }
|
|
|
|
|
|
|
|
func makeLogoutEndpoint(svc kolide.Service) endpoint.Endpoint {
|
|
|
|
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
|
|
|
err := svc.Logout(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return logoutResponse{Err: err}, nil
|
|
|
|
}
|
|
|
|
return logoutResponse{}, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-05 19:50:57 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Get Info About Session
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
type getInfoAboutSessionRequest struct {
|
|
|
|
ID uint
|
|
|
|
}
|
|
|
|
|
|
|
|
type getInfoAboutSessionResponse struct {
|
|
|
|
SessionID uint `json:"session_id"`
|
|
|
|
UserID uint `json:"user_id"`
|
|
|
|
CreatedAt time.Time `json:"created_at"`
|
2016-09-06 15:02:11 +00:00
|
|
|
Err error `json:"error,omitempty"`
|
2016-09-05 19:50:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r getInfoAboutSessionResponse) error() error { return r.Err }
|
|
|
|
|
|
|
|
func makeGetInfoAboutSessionEndpoint(svc kolide.Service) endpoint.Endpoint {
|
|
|
|
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
|
|
|
req := request.(getInfoAboutSessionRequest)
|
|
|
|
session, err := svc.GetInfoAboutSession(ctx, req.ID)
|
|
|
|
if err != nil {
|
|
|
|
return getInfoAboutSessionResponse{Err: err}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return getInfoAboutSessionResponse{
|
|
|
|
SessionID: session.ID,
|
|
|
|
UserID: session.UserID,
|
|
|
|
CreatedAt: session.CreatedAt,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Get Info About Sessions For User
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
type getInfoAboutSessionsForUserRequest struct {
|
|
|
|
ID uint
|
|
|
|
}
|
|
|
|
|
|
|
|
type getInfoAboutSessionsForUserResponse struct {
|
|
|
|
Sessions []getInfoAboutSessionResponse `json:"sessions"`
|
2016-09-06 15:02:11 +00:00
|
|
|
Err error `json:"error,omitempty"`
|
2016-09-05 19:50:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r getInfoAboutSessionsForUserResponse) error() error { return r.Err }
|
|
|
|
|
|
|
|
func makeGetInfoAboutSessionsForUserEndpoint(svc kolide.Service) endpoint.Endpoint {
|
|
|
|
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
|
|
|
req := request.(getInfoAboutSessionsForUserRequest)
|
|
|
|
sessions, err := svc.GetInfoAboutSessionsForUser(ctx, req.ID)
|
|
|
|
if err != nil {
|
|
|
|
return getInfoAboutSessionsForUserResponse{Err: err}, nil
|
|
|
|
}
|
|
|
|
var resp getInfoAboutSessionsForUserResponse
|
|
|
|
for _, session := range sessions {
|
|
|
|
resp.Sessions = append(resp.Sessions, getInfoAboutSessionResponse{
|
|
|
|
SessionID: session.ID,
|
|
|
|
UserID: session.UserID,
|
|
|
|
CreatedAt: session.CreatedAt,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Delete Session
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
type deleteSessionRequest struct {
|
|
|
|
ID uint
|
|
|
|
}
|
|
|
|
|
|
|
|
type deleteSessionResponse struct {
|
2016-09-06 15:02:11 +00:00
|
|
|
Err error `json:"error,omitempty"`
|
2016-09-05 19:50:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r deleteSessionResponse) error() error { return r.Err }
|
|
|
|
|
|
|
|
func makeDeleteSessionEndpoint(svc kolide.Service) endpoint.Endpoint {
|
|
|
|
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
|
|
|
req := request.(deleteSessionRequest)
|
|
|
|
err := svc.DeleteSession(ctx, req.ID)
|
|
|
|
if err != nil {
|
|
|
|
return deleteSessionResponse{Err: err}, nil
|
|
|
|
}
|
|
|
|
return deleteSessionResponse{}, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Delete Sessions For User
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
type deleteSessionsForUserRequest struct {
|
|
|
|
ID uint
|
|
|
|
}
|
|
|
|
|
|
|
|
type deleteSessionsForUserResponse struct {
|
2016-09-06 15:02:11 +00:00
|
|
|
Err error `json:"error,omitempty"`
|
2016-09-05 19:50:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r deleteSessionsForUserResponse) error() error { return r.Err }
|
|
|
|
|
|
|
|
func makeDeleteSessionsForUserEndpoint(svc kolide.Service) endpoint.Endpoint {
|
|
|
|
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
|
|
|
req := request.(deleteSessionsForUserRequest)
|
|
|
|
err := svc.DeleteSessionsForUser(ctx, req.ID)
|
|
|
|
if err != nil {
|
|
|
|
return deleteSessionsForUserResponse{Err: err}, nil
|
|
|
|
}
|
|
|
|
return deleteSessionsForUserResponse{}, nil
|
|
|
|
}
|
|
|
|
}
|