Skip to content

Commit

Permalink
add to main, rename muxer, add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
jessepeterson committed Apr 23, 2024
1 parent 20f7e88 commit 6af273b
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 15 deletions.
7 changes: 6 additions & 1 deletion cmd/nanomdm/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,13 @@ func main() {
stdlog.Fatal(err)
}

tokenMux := nanomdm.NewTokenMux()

// create 'core' MDM service
nanoOpts := []nanomdm.Option{nanomdm.WithLogger(logger.With("service", "nanomdm"))}
nanoOpts := []nanomdm.Option{
nanomdm.WithGetToken(tokenMux),
nanomdm.WithLogger(logger.With("service", "nanomdm")),
}
if *flDMURLPfx != "" {
logger.Debug("msg", "declarative management setup", "url", *flDMURLPfx)
dm, err := nanomdm.NewDeclarativeManagementHTTPCaller(*flDMURLPfx, http.DefaultClient)
Expand Down
28 changes: 15 additions & 13 deletions service/nanomdm/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,35 @@ import (
"github.com/micromdm/nanomdm/service"
)

// StaticToken holds static token bytes.
type StaticToken struct {
token []byte
}

// NewStaticToken creates a new static token handler.
func NewStaticToken(token []byte) *StaticToken {
return &StaticToken{token: token}
}

// GetToken always responds with the static token bytes.
func (t *StaticToken) GetToken(_ *mdm.Request, _ *mdm.GetToken) (*mdm.GetTokenResponse, error) {
return &mdm.GetTokenResponse{TokenData: t.token}, nil
}

// TokenServiceTypeMux is a middleware multiplexer for GetToken check-in messages.
// TokenMux is a middleware multiplexer for GetToken check-in messages.
// A TokenServiceType string is associated with a GetToken handler and
// then dispatched appropriately with a matching TokenServiceType.
type TokenServiceTypeMux struct {
// then dispatched appropriately.
type TokenMux struct {
typesMu sync.RWMutex
types map[string]service.GetToken
}

// NewTokenServiceTypeMux creates a new TokenServiceTypeMux.
func NewTokenServiceTypeMux() *TokenServiceTypeMux { return &TokenServiceTypeMux{} }
// NewTokenMux creates a new TokenMux.
func NewTokenMux() *TokenMux { return &TokenMux{} }

// Handle registers a GetToken handler for the given service type.
// See https://developer.apple.com/documentation/devicemanagement/gettokenrequest
func (mux *TokenServiceTypeMux) Handle(serviceType string, handler service.GetToken) {
func (mux *TokenMux) Handle(serviceType string, handler service.GetToken) {
if serviceType == "" {
panic("tokenmux: invalid service type")
}
Expand All @@ -51,19 +54,18 @@ func (mux *TokenServiceTypeMux) Handle(serviceType string, handler service.GetTo
}

// GetToken is the middleware that dispatches a GetToken handler based on service type.
func (mux *TokenServiceTypeMux) GetToken(r *mdm.Request, t *mdm.GetToken) (*mdm.GetTokenResponse, error) {
var next service.GetToken
var serviceType string
if t != nil {
serviceType = t.TokenServiceType
func (mux *TokenMux) GetToken(r *mdm.Request, t *mdm.GetToken) (*mdm.GetTokenResponse, error) {
if t == nil {
return nil, fmt.Errorf("nil MDM GetToken")
}
var next service.GetToken
mux.typesMu.RLock()
if mux.types != nil {
next = mux.types[serviceType]
next = mux.types[t.TokenServiceType]
}
mux.typesMu.RUnlock()
if next == nil {
return nil, fmt.Errorf("no handler for TokenServiceType: %v", serviceType)
return nil, fmt.Errorf("no handler for TokenServiceType: %v", t.TokenServiceType)
}
return next.GetToken(r, t)
}
2 changes: 1 addition & 1 deletion service/nanomdm/token_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

func TestToken(t *testing.T) {
m := NewTokenServiceTypeMux()
m := NewTokenMux()
inTok := []byte("hello")
m.Handle("com.apple.maid", NewStaticToken(inTok))
inMDMGetToken := &mdm.GetToken{TokenServiceType: "com.apple.maid"}
Expand Down

0 comments on commit 6af273b

Please sign in to comment.