diff --git a/pkg/deviceclaimingserver/enddevices/ttjsv2/messages.go b/pkg/deviceclaimingserver/enddevices/ttjsv2/messages.go index c000254e1d..a6c5e35f35 100644 --- a/pkg/deviceclaimingserver/enddevices/ttjsv2/messages.go +++ b/pkg/deviceclaimingserver/enddevices/ttjsv2/messages.go @@ -14,6 +14,10 @@ package ttjsv2 +import ( + "context" +) + // ClaimData contains information about the claim. type ClaimData struct { HomeNetID string `json:"homeNetID"` @@ -38,6 +42,25 @@ type ClaimRequest struct { KEK *KEK `json:"kek,omitempty"` } +// Apply applies the context to the request. +func (req ClaimRequest) Apply(ctx context.Context, c Component) (ClaimRequest, error) { + deriv := req + if req.HomeNSID != nil { + deriv.HomeNSID = stringValue(*req.HomeNSID) + } + if req.RegenerateOwnerToken != nil { + deriv.RegenerateOwnerToken = boolValue(*req.RegenerateOwnerToken) + } + if req.Lock != nil { + deriv.Lock = boolValue(*req.Lock) + } + if req.KEK != nil { + kek := *req.KEK + deriv.KEK = &kek + } + return deriv, nil +} + // ErrorResponse is a message that may be returned by The Things Join Server in case of an error. type ErrorResponse struct { Message string `json:"message"` diff --git a/pkg/deviceclaimingserver/enddevices/ttjsv2/ttjs.go b/pkg/deviceclaimingserver/enddevices/ttjsv2/ttjs.go index 2e15c46245..50e95e30a6 100644 --- a/pkg/deviceclaimingserver/enddevices/ttjsv2/ttjs.go +++ b/pkg/deviceclaimingserver/enddevices/ttjsv2/ttjs.go @@ -123,7 +123,7 @@ func (c *TTJS) Claim(ctx context.Context, joinEUI, devEUI types.EUI64, claimAuth "url", reqURL, )) - claimReq := &ClaimRequest{ + claimReq := ClaimRequest{ OwnerToken: claimAuthenticationCode, Lock: boolValue(true), HomeNetID: c.config.NetID.String(), @@ -132,6 +132,10 @@ func (c *TTJS) Claim(ctx context.Context, joinEUI, devEUI types.EUI64, claimAuth if c.config.NSID != nil { claimReq.HomeNSID = stringValue(c.config.NSID.String()) } + claimReq, err := claimReq.Apply(ctx, c) + if err != nil { + return err + } buf, err := json.Marshal(claimReq) if err != nil { return err diff --git a/pkg/deviceclaimingserver/enddevices/ttjsv2/ttjs_test.go b/pkg/deviceclaimingserver/enddevices/ttjsv2/ttjs_test.go index 022420f7b3..08515ba517 100644 --- a/pkg/deviceclaimingserver/enddevices/ttjsv2/ttjs_test.go +++ b/pkg/deviceclaimingserver/enddevices/ttjsv2/ttjs_test.go @@ -99,7 +99,7 @@ func TestTTJS(t *testing.T) { //nolint:paralleltest client1 := ttjsv2.NewClient(c, fetcher, ttjsv2.Config{ NetID: test.DefaultNetID, NSID: &homeNSID, - ASID: "localhost", + ASID: client1ASID, JoinEUIPrefixes: []types.EUI64Prefix{ supportedJoinEUIPrefix, }, @@ -176,7 +176,7 @@ func TestTTJS(t *testing.T) { //nolint:paralleltest client2 := ttjsv2.NewClient(c, fetcher, ttjsv2.Config{ NetID: test.DefaultNetID, NSID: &homeNSID, - ASID: "localhost", + ASID: client2ASID, JoinEUIPrefixes: []types.EUI64Prefix{ supportedJoinEUIPrefix, },