fleet/server/service/endpoint_users.go

230 lines
6.7 KiB
Go
Raw Normal View History

2016-09-26 18:48:55 +00:00
package service
2016-08-28 03:59:17 +00:00
import (
"net/http"
2016-08-28 03:59:17 +00:00
"github.com/go-kit/kit/endpoint"
2016-09-26 18:48:55 +00:00
"github.com/kolide/kolide-ose/server/kolide"
"golang.org/x/net/context"
2016-08-28 03:59:17 +00:00
)
////////////////////////////////////////////////////////////////////////////////
// Create User
////////////////////////////////////////////////////////////////////////////////
2016-08-28 03:59:17 +00:00
type createUserRequest struct {
payload kolide.UserPayload
}
type createUserResponse struct {
User *kolide.User `json:"user,omitempty"`
Err error `json:"error,omitempty"`
2016-08-28 03:59:17 +00:00
}
func (r createUserResponse) error() error { return r.Err }
func makeCreateUserEndpoint(svc kolide.Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(createUserRequest)
user, err := svc.NewUser(ctx, req.payload)
2016-08-29 17:04:07 +00:00
if err != nil {
return createUserResponse{Err: err}, nil
}
return createUserResponse{User: user}, nil
2016-08-28 03:59:17 +00:00
}
}
////////////////////////////////////////////////////////////////////////////////
// Get User
////////////////////////////////////////////////////////////////////////////////
2016-08-28 03:59:17 +00:00
type getUserRequest struct {
ID uint `json:"id"`
}
type getUserResponse struct {
User *kolide.User `json:"user,omitempty"`
Err error `json:"error,omitempty"`
2016-08-28 03:59:17 +00:00
}
func (r getUserResponse) error() error { return r.Err }
func makeGetUserEndpoint(svc kolide.Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(getUserRequest)
user, err := svc.User(ctx, req.ID)
if err != nil {
return getUserResponse{Err: err}, nil
}
return getUserResponse{User: user}, nil
}
}
func makeGetSessionUserEndpoint(svc kolide.Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
user, err := svc.AuthenticatedUser(ctx)
if err != nil {
return getUserResponse{Err: err}, nil
}
return getUserResponse{User: user}, nil
2016-08-28 03:59:17 +00:00
}
}
////////////////////////////////////////////////////////////////////////////////
// List Users
////////////////////////////////////////////////////////////////////////////////
type listUsersRequest struct {
ListOptions kolide.ListOptions
}
type listUsersResponse struct {
Users []kolide.User `json:"users"`
Err error `json:"error,omitempty"`
}
func (r listUsersResponse) error() error { return r.Err }
func makeListUsersEndpoint(svc kolide.Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(listUsersRequest)
users, err := svc.ListUsers(ctx, req.ListOptions)
if err != nil {
return listUsersResponse{Err: err}, nil
}
resp := listUsersResponse{Users: []kolide.User{}}
for _, user := range users {
resp.Users = append(resp.Users, *user)
}
return resp, nil
}
}
2016-12-14 18:11:43 +00:00
////////////////////////////////////////////////////////////////////////////////
// Change Password
////////////////////////////////////////////////////////////////////////////////
type changePasswordRequest struct {
OldPassword string `json:"old_password"`
NewPassword string `json:"new_password"`
}
type changePasswordResponse struct {
Err error `json:"error,omitempty"`
}
func (r changePasswordResponse) error() error { return r.Err }
func makeChangePasswordEndpoint(svc kolide.Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(changePasswordRequest)
err := svc.ChangePassword(ctx, req.OldPassword, req.NewPassword)
return changePasswordResponse{Err: err}, nil
}
}
////////////////////////////////////////////////////////////////////////////////
// Reset Password
////////////////////////////////////////////////////////////////////////////////
type resetPasswordRequest struct {
PasswordResetToken string `json:"password_reset_token"`
NewPassword string `json:"new_password"`
}
type resetPasswordResponse struct {
Err error `json:"error,omitempty"`
}
func (r resetPasswordResponse) error() error { return r.Err }
func makeResetPasswordEndpoint(svc kolide.Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(resetPasswordRequest)
err := svc.ResetPassword(ctx, req.PasswordResetToken, req.NewPassword)
return resetPasswordResponse{Err: err}, nil
}
}
////////////////////////////////////////////////////////////////////////////////
// Modify User
////////////////////////////////////////////////////////////////////////////////
2016-08-28 03:59:17 +00:00
type modifyUserRequest struct {
ID uint
payload kolide.UserPayload
}
type modifyUserResponse struct {
User *kolide.User `json:"user,omitempty"`
Err error `json:"error,omitempty"`
2016-08-28 03:59:17 +00:00
}
func (r modifyUserResponse) error() error { return r.Err }
func makeModifyUserEndpoint(svc kolide.Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(modifyUserRequest)
user, err := svc.ModifyUser(ctx, req.ID, req.payload)
if err != nil {
return modifyUserResponse{Err: err}, nil
}
2016-08-28 03:59:17 +00:00
return modifyUserResponse{User: user}, nil
2016-08-28 03:59:17 +00:00
}
}
////////////////////////////////////////////////////////////////////////////////
// Require Password Reset
////////////////////////////////////////////////////////////////////////////////
type requirePasswordResetRequest struct {
Require bool `json:"require"`
ID uint `json:"id"`
}
type requirePasswordResetResponse struct {
User *kolide.User `json:"user,omitempty"`
Err error `json:"error,omitempty"`
}
func (r requirePasswordResetResponse) error() error { return r.Err }
func makeRequirePasswordResetEndpoint(svc kolide.Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(requirePasswordResetRequest)
user, err := svc.RequirePasswordReset(ctx, req.ID, req.Require)
if err != nil {
return requirePasswordResetResponse{Err: err}, nil
}
return requirePasswordResetResponse{User: user}, nil
}
}
////////////////////////////////////////////////////////////////////////////////
// Forgot Password
////////////////////////////////////////////////////////////////////////////////
type forgotPasswordRequest struct {
Email string `json:"email"`
}
type forgotPasswordResponse struct {
Err error `json:"error,omitempty"`
}
func (r forgotPasswordResponse) error() error { return r.Err }
func (r forgotPasswordResponse) status() int { return http.StatusAccepted }
func makeForgotPasswordEndpoint(svc kolide.Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(forgotPasswordRequest)
err := svc.RequestPasswordReset(ctx, req.Email)
if err != nil {
return forgotPasswordResponse{Err: err}, nil
}
return forgotPasswordResponse{}, nil
}
}