forked from micromdm/micromdm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
checkin_event.go
155 lines (143 loc) · 4.48 KB
/
checkin_event.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package mdm
import (
"encoding/hex"
"time"
"github.com/gogo/protobuf/proto"
"github.com/micromdm/micromdm/mdm/internal/checkinproto"
)
type CheckinEvent struct {
ID string
Time time.Time
Command CheckinCommand
Params map[string]string
Raw []byte
}
// CheckinRequest represents an MDM checkin command struct.
type CheckinCommand struct {
// MessageType can be either Authenticate,
// TokenUpdate or CheckOut
MessageType string
Topic string
UDID string
auth
update
}
// Authenticate Message Type
type auth struct {
OSVersion string
BuildVersion string
ProductName string
SerialNumber string
IMEI string
MEID string
DeviceName string `plist:"DeviceName,omitempty"`
Challenge []byte `plist:"Challenge,omitempty"`
Model string `plist:"Model,omitpempty"`
ModelName string `plist:"ModelName,omitempty"`
}
// TokenUpdate Mesage Type
type update struct {
Token hexData
PushMagic string
UnlockToken hexData
AwaitingConfiguration bool
userTokenUpdate
}
// TokenUpdate with user keys
type userTokenUpdate struct {
UserID string `plist:",omitempty"`
UserLongName string `plist:",omitempty"`
UserShortName string `plist:",omitempty"`
NotOnConsole bool `plist:",omitempty"`
}
// data decodes to []byte,
// we can then attach a string method to the type
// Tokens are encoded as Hex Strings
type hexData []byte
func (d hexData) String() string {
return hex.EncodeToString(d)
}
// MarshalCheckinEvent serializes an event to a protocol buffer wire format.
func MarshalCheckinEvent(e *CheckinEvent) ([]byte, error) {
command := &checkinproto.Command{
MessageType: e.Command.MessageType,
Topic: e.Command.Topic,
Udid: e.Command.UDID,
}
switch e.Command.MessageType {
case "Authenticate":
command.Authenticate = &checkinproto.Authenticate{
OsVersion: e.Command.OSVersion,
BuildVersion: e.Command.BuildVersion,
SerialNumber: e.Command.SerialNumber,
Imei: e.Command.IMEI,
Meid: e.Command.MEID,
DeviceName: e.Command.DeviceName,
Challenge: e.Command.Challenge,
Model: e.Command.Model,
ModelName: e.Command.ModelName,
ProductName: e.Command.ProductName,
}
case "TokenUpdate":
command.TokenUpdate = &checkinproto.TokenUpdate{
Token: e.Command.Token,
PushMagic: e.Command.PushMagic,
UnlockToken: e.Command.UnlockToken,
AwaitingConfiguration: e.Command.AwaitingConfiguration,
UserId: e.Command.UserID,
UserLongName: e.Command.UserLongName,
UserShortName: e.Command.UserShortName,
NotOnConsole: e.Command.NotOnConsole,
}
}
return proto.Marshal(&checkinproto.Event{
Id: e.ID,
Time: e.Time.UnixNano(),
Command: command,
Params: e.Params,
Raw: e.Raw,
})
}
// UnmarshalCheckinEvent parses a protocol buffer representation of data into
// the Event.
func UnmarshalCheckinEvent(data []byte, e *CheckinEvent) error {
var pb checkinproto.Event
if err := proto.Unmarshal(data, &pb); err != nil {
return err
}
e.ID = pb.Id
e.Time = time.Unix(0, pb.Time).UTC()
if pb.Command == nil {
return nil
}
e.Command = CheckinCommand{
MessageType: pb.Command.MessageType,
Topic: pb.Command.Topic,
UDID: pb.Command.Udid,
}
switch pb.Command.MessageType {
case "Authenticate":
e.Command.OSVersion = pb.Command.Authenticate.OsVersion
e.Command.BuildVersion = pb.Command.Authenticate.BuildVersion
e.Command.SerialNumber = pb.Command.Authenticate.SerialNumber
e.Command.IMEI = pb.Command.Authenticate.Imei
e.Command.MEID = pb.Command.Authenticate.Meid
e.Command.DeviceName = pb.Command.Authenticate.DeviceName
e.Command.Challenge = pb.Command.Authenticate.Challenge
e.Command.Model = pb.Command.Authenticate.Model
e.Command.ModelName = pb.Command.Authenticate.ModelName
e.Command.ProductName = pb.Command.Authenticate.ProductName
case "TokenUpdate":
e.Command.Token = pb.Command.TokenUpdate.Token
e.Command.PushMagic = pb.Command.TokenUpdate.PushMagic
e.Command.UnlockToken = pb.Command.TokenUpdate.UnlockToken
e.Command.AwaitingConfiguration = pb.Command.TokenUpdate.AwaitingConfiguration
e.Command.UserID = pb.Command.TokenUpdate.UserId
e.Command.UserLongName = pb.Command.TokenUpdate.UserLongName
e.Command.UserShortName = pb.Command.TokenUpdate.UserShortName
e.Command.NotOnConsole = pb.Command.TokenUpdate.NotOnConsole
}
e.Raw = pb.GetRaw()
e.Params = pb.GetParams()
return nil
}