forked from micromdm/micromdm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
checkin.go
105 lines (87 loc) · 2.36 KB
/
checkin.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package mdm
import (
"context"
"net/http"
"time"
"github.com/go-kit/kit/endpoint"
"github.com/gorilla/mux"
"github.com/groob/plist"
"github.com/pkg/errors"
uuid "github.com/satori/go.uuid"
)
func (svc *MDMService) Checkin(ctx context.Context, event CheckinEvent) error {
if event.Command.MessageType == "UserAuthenticate" {
return &rejectUserAuth{}
}
msg, err := MarshalCheckinEvent(&event)
if err != nil {
return errors.Wrap(err, "marshal checkin event")
}
topic, err := topicFromMessage(event.Command.MessageType)
if err != nil {
return errors.Wrap(err, "get checkin topic from message")
}
err = svc.pub.Publish(ctx, topic, msg)
return errors.Wrapf(err, "publish checkin on topic: %s", topic)
}
func topicFromMessage(messageType string) (string, error) {
switch messageType {
case "Authenticate":
return AuthenticateTopic, nil
case "TokenUpdate":
return TokenUpdateTopic, nil
case "CheckOut":
return CheckoutTopic, nil
default:
return "", errors.Errorf("unknown checkin message type %s", messageType)
}
}
type rejectUserAuth struct{}
func (e *rejectUserAuth) Error() string {
return "reject user auth"
}
func (e *rejectUserAuth) UserAuthReject() bool {
return true
}
func isRejectedUserAuth(err error) bool {
type rejectUserAuthError interface {
error
UserAuthReject() bool
}
_, ok := errors.Cause(err).(rejectUserAuthError)
return ok
}
type checkinRequest struct {
Event CheckinEvent
}
type checkinResponse struct {
Err error `plist:"error,omitempty"`
}
func (r checkinResponse) Failed() error { return r.Err }
func (d *requestDecoder) decodeCheckinRequest(ctx context.Context, r *http.Request) (interface{}, error) {
body, err := d.readBody(r)
if err != nil {
return nil, errors.Wrap(err, "read checkin request body")
}
var cmd CheckinCommand
if err := plist.Unmarshal(body, &cmd); err != nil {
return nil, errors.Wrap(err, "unmarshal MDM Checkin Request plist")
}
params := mux.Vars(r)
event := CheckinEvent{
ID: uuid.NewV4().String(),
Time: time.Now().UTC(),
Command: cmd,
Params: params,
Raw: body,
}
req := checkinRequest{Event: event}
return req, nil
}
func MakeCheckinEndpoint(svc Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(checkinRequest)
err := svc.Checkin(ctx, req.Event)
return checkinResponse{Err: err}, nil
}
}