This repository has been archived by the owner on Jun 10, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
event.go
109 lines (102 loc) · 3.37 KB
/
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
package checkin
import (
"time"
"github.com/gogo/protobuf/proto"
"github.com/micromdm/mdm"
uuid "github.com/satori/go.uuid"
"github.com/micromdm/checkin/internal/checkinproto"
)
type Event struct {
ID string
Time time.Time
Command mdm.CheckinCommand
}
// NewEvent returns an Event with a unique ID and the current time.
func NewEvent(cmd mdm.CheckinCommand) *Event {
event := Event{
ID: uuid.NewV4().String(),
Time: time.Now().UTC(),
Command: cmd,
}
return &event
}
// MarshalEvent serializes an event to a protocol buffer wire format.
func MarshalEvent(e *Event) ([]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,
})
}
// UnmarshalEvent parses a protocol buffer representation of data into
// the Event.
func UnmarshalEvent(data []byte, e *Event) 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 = mdm.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
}
return nil
}