diff --git a/endpoint.go b/endpoint.go index 4009fa06..291161a8 100644 --- a/endpoint.go +++ b/endpoint.go @@ -5,19 +5,26 @@ import ( "golang.org/x/net/context" ) +type Endpoints struct { + GetEnrollEndpoint endpoint.Endpoint +} + type mdmEnrollRequest struct{} type mdmEnrollResponse struct { - Profile + Profile, + Err error `plist:"error,omitempty"` } -func makeEnrollEndpoint(svc Service) endpoint.Endpoint { - return func(ctx context.Context, request interface{}) (interface{}, error) { - //req := request.(mdmEnrollRequest) - profile, err := svc.Enroll() - if err != nil { - return mdmEnrollResponse{}, err - } - return mdmEnrollResponse{profile}, nil +func MakeServerEndpoints(s Service) Endpoints { + return Endpoints{ + GetEnrollEndpoint: MakeGetEnrollEndpoint(s), + } +} + +func MakeGetEnrollEndpoint(s Service) endpoint.Endpoint { + return func(ctx context.Context, request interface{}) (interface{}, error) { + profile, err := s.Enroll(ctx) + return mdmEnrollResponse{profile, err}, nil } } diff --git a/service.go b/service.go index a15298d5..e744b33d 100644 --- a/service.go +++ b/service.go @@ -1,12 +1,15 @@ package enroll -import "io/ioutil" +import ( + "golang.org/x/net/context" + "io/ioutil" +) type Service interface { - Enroll() (Profile, error) + Enroll(ctx context.Context) (Profile, error) } -func NewService(pushCertPath string, pushCertPass string, caCertPath string, url string, scepUrl string) (Service, error) { +func NewService(pushCertPath string, pushCertPass string, caCertPath string, scepURL string, scepChallenge string, url string) (Service, error) { pushTopic, err := GetPushTopicFromPKCS12(pushCertPath, pushCertPass) if err != nil { return nil, err @@ -30,24 +33,25 @@ func NewService(pushCertPath string, pushCertPass string, caCertPath string, url } return &service{ - Url: url, - SCEPUrl: scepUrl, - SCEPSubject: scepSubject, - Topic: pushTopic, - CACert: caCert, + URL: url, + SCEPURL: scepURL, + SCEPSubject: scepSubject, + SCEPChallenge: scepChallenge, + Topic: pushTopic, + CACert: caCert, }, nil } type service struct { - Url string - SCEPUrl string + URL string + SCEPURL string SCEPChallenge string SCEPSubject [][][]string Topic string // APNS Topic for MDM notifications CACert []byte } -func (svc service) Enroll() (Profile, error) { +func (svc service) Enroll(ctx context.Context) (Profile, error) { profile := NewProfile() profile.PayloadIdentifier = "com.github.micromdm.micromdm.mdm" profile.PayloadOrganization = "MicroMDM" @@ -56,7 +60,7 @@ func (svc service) Enroll() (Profile, error) { scepContent := SCEPPayloadContent{ Challenge: svc.SCEPChallenge, - URL: svc.SCEPUrl, + URL: svc.SCEPURL, Keysize: 1024, KeyType: "RSA", KeyUsage: 0, @@ -78,9 +82,9 @@ func (svc service) Enroll() (Profile, error) { mdmPayloadContent := MDMPayloadContent{ Payload: *mdmPayload, AccessRights: 8191, - CheckInURL: svc.Url + "/mdm/checkin", + CheckInURL: svc.URL + "/mdm/checkin", CheckOutWhenRemoved: true, - ServerURL: svc.Url + "/mdm/connect", + ServerURL: svc.URL + "/mdm/connect", IdentityCertificateUUID: scepPayload.PayloadUUID, Topic: svc.Topic, } @@ -89,6 +93,7 @@ func (svc service) Enroll() (Profile, error) { caPayload := NewPayload("com.apple.ssl.certificate") caPayload.PayloadDisplayName = "Root certificate for MicroMDM" caPayload.PayloadDescription = "Installs the root CA certificate for MicroMDM" + caPayload.PayloadIdentifier = "com.github.micromdm.ssl.ca" caPayload.PayloadContent = svc.CACert profile.PayloadContent = []interface{}{*scepPayload, mdmPayloadContent, *caPayload} diff --git a/transport.go b/transport.go index 607c7703..36ed004e 100644 --- a/transport.go +++ b/transport.go @@ -5,28 +5,28 @@ import ( "golang.org/x/net/context" - kitlog "github.com/go-kit/kit/log" - kithttp "github.com/go-kit/kit/transport/http" + "github.com/go-kit/kit/log" + httptransport "github.com/go-kit/kit/transport/http" "github.com/gorilla/mux" "github.com/groob/plist" ) // ServiceHandler returns an HTTP Handler for the enroll service -func ServiceHandler(ctx context.Context, svc Service, logger kitlog.Logger) http.Handler { - opts := []kithttp.ServerOption{ - kithttp.ServerErrorLogger(logger), +func MakeHTTPHandler(ctx context.Context, svc Service, logger log.Logger) http.Handler { + r := mux.NewRouter() + e := MakeServerEndpoints(svc) + opts := []httptransport.ServerOption{ + httptransport.ServerErrorLogger(logger), } - connectHandler := kithttp.NewServer( + r.Methods("GET").Path("/mdm/enroll").Handler(httptransport.NewServer( ctx, - makeEnrollEndpoint(svc), + e.GetEnrollEndpoint, decodeMDMEnrollRequest, encodeResponse, opts..., - ) - r := mux.NewRouter() + )) - r.Handle("/mdm/enroll", connectHandler).Methods("GET") return r }