-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GetToken check-in message framing (#105)
- Loading branch information
1 parent
3544fbb
commit 3edec93
Showing
14 changed files
with
340 additions
and
1 deletion.
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
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
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
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
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
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
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
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
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
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
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,71 @@ | ||
package nanomdm | ||
|
||
import ( | ||
"fmt" | ||
"sync" | ||
|
||
"github.com/micromdm/nanomdm/mdm" | ||
"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 | ||
} | ||
|
||
// TokenMux is a middleware multiplexer for GetToken check-in messages. | ||
// A TokenServiceType string is associated with a GetToken handler and | ||
// then dispatched appropriately. | ||
type TokenMux struct { | ||
typesMu sync.RWMutex | ||
types map[string]service.GetToken | ||
} | ||
|
||
// 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 *TokenMux) 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 *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[t.TokenServiceType] | ||
} | ||
mux.typesMu.RUnlock() | ||
if next == nil { | ||
return nil, fmt.Errorf("no handler for TokenServiceType: %v", t.TokenServiceType) | ||
} | ||
return next.GetToken(r, t) | ||
} |
Oops, something went wrong.