func (h *handlerManager) DeleteApplication(ctx context.Context, in *pb.ApplicationIdentifier) (*empty.Empty, error) { if err := in.Validate(); err != nil { return nil, errors.Wrap(err, "Invalid Application Identifier") } ctx, claims, err := h.validateTTNAuthAppContext(ctx, in.AppId) if err != nil { return nil, err } if !claims.AppRight(in.AppId, rights.AppSettings) { return nil, errors.NewErrPermissionDenied(`No "settings" rights to application`) } _, err = h.handler.applications.Get(in.AppId) if err != nil { return nil, err } // Get and delete all devices for this application devices, err := h.handler.devices.ListForApp(in.AppId) if err != nil { return nil, err } for _, dev := range devices { _, err = h.deviceManager.DeleteDevice(ctx, &pb_lorawan.DeviceIdentifier{AppEui: &dev.AppEUI, DevEui: &dev.DevEUI}) if err != nil { return nil, errors.Wrap(errors.FromGRPCError(err), "Broker did not delete device") } err = h.handler.devices.Delete(dev.AppID, dev.DevID) if err != nil { return nil, err } } // Delete the Application err = h.handler.applications.Delete(in.AppId) if err != nil { return nil, err } token, _ := api.TokenFromContext(ctx) err = h.handler.Discovery.RemoveAppID(in.AppId, token) if err != nil { h.handler.Ctx.WithField("AppID", in.AppId).WithError(errors.FromGRPCError(err)).Warn("Could not unregister Application from Discovery") } return &empty.Empty{}, nil }
// ValidateTTNAuthContext gets a token from the context and validates it func (c *Component) ValidateTTNAuthContext(ctx context.Context) (*claims.Claims, error) { token, err := api.TokenFromContext(ctx) if err != nil { return nil, err } if c.TokenKeyProvider == nil { return nil, errors.NewErrInternal("No token provider configured") } claims, err := claims.FromToken(c.TokenKeyProvider, token) if err != nil { return nil, errors.NewErrPermissionDenied(err.Error()) } return claims, nil }
func (h *handlerManager) RegisterApplication(ctx context.Context, in *pb.ApplicationIdentifier) (*empty.Empty, error) { if err := in.Validate(); err != nil { return nil, errors.Wrap(err, "Invalid Application Identifier") } ctx, claims, err := h.validateTTNAuthAppContext(ctx, in.AppId) if err != nil { return nil, err } if !claims.AppRight(in.AppId, rights.AppSettings) { return nil, errors.NewErrPermissionDenied(`No "settings" rights to application`) } app, err := h.handler.applications.Get(in.AppId) if err != nil && errors.GetErrType(err) != errors.NotFound { return nil, err } if app != nil { return nil, errors.NewErrAlreadyExists("Application") } err = h.handler.applications.Set(&application.Application{ AppID: in.AppId, }) if err != nil { return nil, err } token, _ := api.TokenFromContext(ctx) err = h.handler.Discovery.AddAppID(in.AppId, token) if err != nil { h.handler.Ctx.WithField("AppID", in.AppId).WithError(err).Warn("Could not register Application with Discovery") } _, err = h.handler.ttnBrokerManager.RegisterApplicationHandler(ctx, &pb_broker.ApplicationHandlerRegistration{ AppId: in.AppId, HandlerId: h.handler.Identity.Id, }) if err != nil { h.handler.Ctx.WithField("AppID", in.AppId).WithError(err).Warn("Could not register Application with Broker") } return &empty.Empty{}, nil }
func TestClient(t *testing.T) { a := New(t) ctx := GetLogger(t, "Monitor Client") rand.Seed(time.Now().UnixNano()) port := rand.Intn(1000) + 10000 go startExampleServer(2, port) { client, err := NewClient(ctx, fmt.Sprintf("localhost:%d", port)) a.So(err, ShouldBeNil) a.So(client.IsConnected(), ShouldBeTrue) a.So(client.Reopen(), ShouldBeNil) a.So(client.IsConnected(), ShouldBeTrue) a.So(client.Close(), ShouldBeNil) } { client, _ := NewClient(ctx, fmt.Sprintf("localhost:%d", port)) gtw := client.GatewayClient("dev") a.So(gtw.IsConfigured(), ShouldBeFalse) gtw.SetToken("SOME.AWESOME.JWT") a.So(gtw.IsConfigured(), ShouldBeTrue) ctx := gtw.(*gatewayClient).Context() id, _ := api.IDFromContext(ctx) a.So(id, ShouldEqual, "dev") token, _ := api.TokenFromContext(ctx) a.So(token, ShouldEqual, "SOME.AWESOME.JWT") a.So(client.Close(), ShouldBeNil) } { client, _ := NewClient(ctx, fmt.Sprintf("localhost:%d", port)) defer client.Close() gtw := client.GatewayClient("dev") err := gtw.SendStatus(&gateway.Status{}) a.So(err, ShouldBeNil) gtw.SetToken("SOME.AWESOME.JWT") // The first two statuses are OK for i := 0; i < 2; i++ { err = gtw.SendStatus(&gateway.Status{}) a.So(err, ShouldBeNil) } // The next one will cause an error on the test server err = gtw.SendStatus(&gateway.Status{}) time.Sleep(10 * time.Millisecond) // Then, we are going to buffer 10 statuses locally for i := 0; i < 10; i++ { err = gtw.SendStatus(&gateway.Status{}) a.So(err, ShouldBeNil) } // After which statuses will get dropped err = gtw.SendStatus(&gateway.Status{}) a.So(err, ShouldNotBeNil) time.Sleep(100 * time.Millisecond) } { client, _ := NewClient(ctx, fmt.Sprintf("localhost:%d", port)) defer client.Close() gtw := client.GatewayClient("dev") err := gtw.SendUplink(&router.UplinkMessage{}) a.So(err, ShouldBeNil) gtw.SetToken("SOME.AWESOME.JWT") // The first two messages are OK for i := 0; i < 2; i++ { err = gtw.SendUplink(&router.UplinkMessage{}) a.So(err, ShouldBeNil) } // The next one will cause an error on the test server err = gtw.SendUplink(&router.UplinkMessage{}) time.Sleep(10 * time.Millisecond) // Then, we are going to buffer 10 messages locally for i := 0; i < 10; i++ { err = gtw.SendUplink(&router.UplinkMessage{}) a.So(err, ShouldBeNil) } // After which messages will get dropped err = gtw.SendUplink(&router.UplinkMessage{}) a.So(err, ShouldNotBeNil) time.Sleep(100 * time.Millisecond) } { client, _ := NewClient(ctx, fmt.Sprintf("localhost:%d", port)) defer client.Close() gtw := client.GatewayClient("dev") err := gtw.SendDownlink(&router.DownlinkMessage{}) a.So(err, ShouldBeNil) gtw.SetToken("SOME.AWESOME.JWT") // The first two messages are OK for i := 0; i < 2; i++ { err = gtw.SendDownlink(&router.DownlinkMessage{}) a.So(err, ShouldBeNil) } // The next one will cause an error on the test server err = gtw.SendDownlink(&router.DownlinkMessage{}) time.Sleep(10 * time.Millisecond) // Then, we are going to buffer 10 messages locally for i := 0; i < 10; i++ { err = gtw.SendDownlink(&router.DownlinkMessage{}) a.So(err, ShouldBeNil) } // After which messages will get dropped err = gtw.SendDownlink(&router.DownlinkMessage{}) a.So(err, ShouldNotBeNil) time.Sleep(100 * time.Millisecond) } }