-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add token muxer and static token helper
- Loading branch information
1 parent
79e4806
commit 1b07afa
Showing
2 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package nanomdm | ||
|
||
import ( | ||
"fmt" | ||
"sync" | ||
|
||
"github.com/micromdm/nanomdm/mdm" | ||
"github.com/micromdm/nanomdm/service" | ||
) | ||
|
||
type StaticToken struct { | ||
token []byte | ||
} | ||
|
||
func NewStaticToken(token []byte) *StaticToken { | ||
return &StaticToken{token: token} | ||
} | ||
|
||
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. | ||
// A TokenServiceType string is associated with a GetToken handler and | ||
// then dispatched appropriately with a matching TokenServiceType. | ||
type TokenServiceTypeMux struct { | ||
typesMu sync.RWMutex | ||
types map[string]service.GetToken | ||
} | ||
|
||
// NewTokenServiceTypeMux creates a new TokenServiceTypeMux. | ||
func NewTokenServiceTypeMux() *TokenServiceTypeMux { return &TokenServiceTypeMux{} } | ||
|
||
// 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) { | ||
if serviceType == "" { | ||
panic("tokenmux: invalid service type") | ||
} | ||
if handler == nil { | ||
panic("tokenmux: invalid handler") | ||
} | ||
mux.typesMu.Lock() | ||
defer mux.typesMu.Unlock() | ||
if mux.types == nil { | ||
mux.types = make(map[string]service.GetToken) | ||
} else if _, exists := mux.types[serviceType]; exists { | ||
panic("tokenmux: multiple registrations for " + serviceType) | ||
} | ||
mux.types[serviceType] = handler | ||
} | ||
|
||
// 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 | ||
} | ||
mux.typesMu.RLock() | ||
if mux.types != nil { | ||
next = mux.types[serviceType] | ||
} | ||
mux.typesMu.RUnlock() | ||
if next == nil { | ||
return nil, fmt.Errorf("no handler for TokenServiceType: %v", serviceType) | ||
} | ||
return next.GetToken(r, t) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package nanomdm | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
|
||
"github.com/micromdm/nanomdm/mdm" | ||
) | ||
|
||
func TestToken(t *testing.T) { | ||
m := NewTokenServiceTypeMux() | ||
inTok := []byte("hello") | ||
m.Handle("com.apple.maid", NewStaticToken(inTok)) | ||
inMDMGetToken := &mdm.GetToken{TokenServiceType: "com.apple.maid"} | ||
outTok, err := m.GetToken(nil, inMDMGetToken) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if !bytes.Equal(inTok, outTok.TokenData) { | ||
t.Error("input and output not equal") | ||
} | ||
// invalid type | ||
inMDMGetToken = &mdm.GetToken{TokenServiceType: "com.apple.does-not-exist"} | ||
_, err = m.GetToken(nil, inMDMGetToken) | ||
if err == nil { | ||
t.Fatal("should be an error") | ||
} | ||
} |