Skip to content

Commit

Permalink
Merge pull request #6705 from TheThingsNetwork/backport/contextualize…
Browse files Browse the repository at this point in the history
…-ttjs-claim-req

Backport contextualizing TTJS claim request
  • Loading branch information
johanstokking authored Nov 13, 2023
2 parents 18ecb74 + df050ab commit 91501c4
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
23 changes: 23 additions & 0 deletions pkg/deviceclaimingserver/enddevices/ttjsv2/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@

package ttjsv2

import (
"context"
)

// ClaimData contains information about the claim.
type ClaimData struct {
HomeNetID string `json:"homeNetID"`
Expand All @@ -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"`
Expand Down
6 changes: 5 additions & 1 deletion pkg/deviceclaimingserver/enddevices/ttjsv2/ttjs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions pkg/deviceclaimingserver/enddevices/ttjsv2/ttjs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand Down Expand Up @@ -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,
},
Expand Down

0 comments on commit 91501c4

Please sign in to comment.