<commit_before>package networkserver

import (
	"errors"

	"github.com/TheThingsNetwork/ttn/api/broker"
	"github.com/TheThingsNetwork/ttn/api/handler"
	pb "github.com/TheThingsNetwork/ttn/api/networkserver"
	"golang.org/x/net/context"
	"google.golang.org/grpc"
	"google.golang.org/grpc/metadata"
)

type networkServerRPC struct {
	networkServer NetworkServer
}

func validateBrokerFromMetadata(ctx context.Context) (err error) {
	md, ok := metadata.FromContext(ctx)
	// TODO: Check OK
	id, ok := md["id"]
	if !ok || len(id) < 1 {
		err = errors.New("ttn/networkserver: Broker did not provide \"id\" in context")
		return
	}
	if err != nil {
		return
	}
	token, ok := md["token"]
	if !ok || len(token) < 1 {
		err = errors.New("ttn/networkserver: Broker did not provide \"token\" in context")
		return
	}
	if token[0] != "token" {
		// TODO: Validate Token
		err = errors.New("ttn/networkserver: Broker not authorized")
		return
	}

	return
}

func (s *networkServerRPC) GetDevices(ctx context.Context, req *pb.DevicesRequest) (*pb.DevicesResponse, error) {
	err := validateBrokerFromMetadata(ctx)
	if err != nil {
		return nil, err
	}
	return nil, nil
}

func (s *networkServerRPC) PrepareActivation(ctx context.Context, activation *broker.DeduplicatedDeviceActivationRequest) (*broker.DeduplicatedDeviceActivationRequest, error) {
	err := validateBrokerFromMetadata(ctx)
	if err != nil {
		return nil, err
	}
	return s.networkServer.HandlePrepareActivation(activation)
}
<commit_msg>Actually handle GetDevices in networkserver<commit_after>package networkserver

import (
	"errors"

	"github.com/TheThingsNetwork/ttn/api/broker"
	"github.com/TheThingsNetwork/ttn/api/handler"
	pb "github.com/TheThingsNetwork/ttn/api/networkserver"
	"golang.org/x/net/context"
	"google.golang.org/grpc"
	"google.golang.org/grpc/metadata"
)

type networkServerRPC struct {
	networkServer NetworkServer
}

func validateBrokerFromMetadata(ctx context.Context) (err error) {
	md, ok := metadata.FromContext(ctx)
	// TODO: Check OK
	id, ok := md["id"]
	if !ok || len(id) < 1 {
		err = errors.New("ttn/networkserver: Broker did not provide \"id\" in context")
		return
	}
	if err != nil {
		return
	}
	token, ok := md["token"]
	if !ok || len(token) < 1 {
		err = errors.New("ttn/networkserver: Broker did not provide \"token\" in context")
		return
	}
	if token[0] != "token" {
		// TODO: Validate Token
		err = errors.New("ttn/networkserver: Broker not authorized")
		return
	}

	return
}

func (s *networkServerRPC) GetDevices(ctx context.Context, req *pb.DevicesRequest) (*pb.DevicesResponse, error) {
	err := validateBrokerFromMetadata(ctx)
	if err != nil {
		return nil, err
	}
	return s.networkServer.HandleGetDevices(req)
}

func (s *networkServerRPC) PrepareActivation(ctx context.Context, activation *broker.DeduplicatedDeviceActivationRequest) (*broker.DeduplicatedDeviceActivationRequest, error) {
	err := validateBrokerFromMetadata(ctx)
	if err != nil {
		return nil, err
	}
	return s.networkServer.HandlePrepareActivation(activation)
}

{'fraction_non_alphanumeric': 0.12983151635282458, 'fraction_numerical': 0.0019821605550049554, 'mean_word_length': 4.69172932330827, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5296715', 'n_tokens_mistral': 1084, 'n_tokens_neox': 1048, 'n_words': 237}