package service import ( "database/sql" "github.com/2fas/api/config" browser_extension_adapters "github.com/2fas/api/internal/api/browser_extension/adapters" "github.com/2fas/api/internal/api/mobile/adapters" "github.com/2fas/api/internal/api/mobile/app" "github.com/2fas/api/internal/api/mobile/app/command" "github.com/2fas/api/internal/api/mobile/app/queries" apisec "github.com/2fas/api/internal/api/mobile/app/security" "github.com/2fas/api/internal/api/mobile/ports" "github.com/2fas/api/internal/common/clock" "github.com/2fas/api/internal/common/db" httpsec "github.com/2fas/api/internal/common/http" "github.com/2fas/api/internal/common/rate_limit" "github.com/2fas/api/internal/common/security" "github.com/2fas/api/internal/common/websocket" "github.com/gin-gonic/gin" "github.com/go-playground/validator/v10" "github.com/go-redis/redis/v8" "gorm.io/gorm" ) type MobileModule struct { Cqrs *app.Cqrs RoutesHandler *ports.RoutesHandler Config config.Configuration Redis *redis.Client } func NewMobileModule(config config.Configuration, gorm *gorm.DB, database *sql.DB, validate *validator.Validate, redisClient *redis.Client) *MobileModule { queryBuilder := db.NewQueryBuilder(database) mobileDeviceRepository := adapters.NewMobileDeviceMysqlRepository(gorm) notificationsRepository := adapters.NewMobileNotificationMysqlRepository(gorm) mobileApplicationExtensionsService := adapters.NewDeviceExtensionsService(gorm, queryBuilder, clock.New()) websocketClient := websocket.NewWebsocketApiClient(config.Websocket.ApiUrl) browserExtensionsMysqlRepository := browser_extension_adapters.NewBrowserExtensionsMysqlRepository(gorm) mobileDeviceExtensionsRepository := adapters.NewMobileDeviceExtensionsGormRepository(gorm, queryBuilder) validate.RegisterValidation("is-device-id", DeviceIdExistsValidator(mobileDeviceRepository)) cqrs := &app.Cqrs{ Commands: app.Commands{ RegisterMobileDevice: &command.RegisterMobileDeviceHandler{ Repository: mobileDeviceRepository, }, RemoveAllMobileDevices: &command.RemoveAllMobileDevicesHandler{ Database: gorm, Qb: queryBuilder, }, UpdateMobileDevice: &command.UpdateMobileDeviceHandler{Repository: mobileDeviceRepository}, CreateNotification: &command.CreateNotificationHandler{Repository: notificationsRepository}, UpdateNotification: &command.UpdateNotificationHandler{Repository: notificationsRepository}, DeleteNotification: &command.DeleteNotificationHandler{Repository: notificationsRepository}, RemoveAllMobileNotifications: &command.DeleteAllNotificationsHandler{ Database: gorm, Qb: queryBuilder, }, PublishNotification: &command.PublishNotificationHandler{Repository: notificationsRepository}, PairMobileWithExtension: &command.PairMobileWithExtensionHandler{ BrowserExtensionsRepository: browserExtensionsMysqlRepository, MobileApplicationExtensionsService: mobileApplicationExtensionsService, MobileDeviceExtensionsRepository: mobileDeviceExtensionsRepository, WebsocketClient: websocketClient, }, RemovePairingWithExtension: &command.RemoveDeviceExtensionHandler{ MobileDeviceExtensionsRepository: mobileDeviceExtensionsRepository, }, Send2FaToken: &command.Send2FaTokenHandler{ BrowserExtensionsRepository: browserExtensionsMysqlRepository, MobileApplicationExtensionsService: mobileApplicationExtensionsService, WebsocketClient: websocketClient, }, }, Queries: app.Queries{ MobileDeviceQuery: &query.MobileDeviceQueryHandler{ Database: gorm, Qb: queryBuilder, }, DeviceBrowserExtensionsQuery: &query.DeviceBrowserExtensionsQueryHandler{ Database: gorm, Qb: queryBuilder, }, DeviceBrowserExtension2FaRequestQuery: &query.DeviceBrowserExtension2FaRequestQueryHandler{ Database: gorm, Qb: queryBuilder, Clock: clock.New(), }, PairedBrowserExtensionQuery: &query.PairedBrowserExtensionQueryHandler{ Database: gorm, Qb: queryBuilder, }, MobileNotificationsQuery: &query.MobileNotificationsQueryHandler{ Database: gorm, Qb: queryBuilder, }, }, } routesHandler := ports.NewRoutesHandler(cqrs, validate, mobileDeviceRepository) module := &MobileModule{ Cqrs: cqrs, RoutesHandler: routesHandler, Config: config, Redis: redisClient, } return module } func (m *MobileModule) RegisterRoutes(router *gin.Engine) { // internal/admin adminRouter := router.Group("/") adminRouter.Use(httpsec.IPWhitelistMiddleware(m.Config.Security)) adminRouter.POST("/mobile/notifications", m.RoutesHandler.CreateMobileNotification) adminRouter.PUT("/mobile/notifications/:notification_id", m.RoutesHandler.UpdateMobileNotification) adminRouter.DELETE("/mobile/notifications/:notification_id", m.RoutesHandler.RemoveMobileNotification) adminRouter.POST("/mobile/notifications/:notification_id/commands/publish", m.RoutesHandler.PublishMobileNotification) if m.Config.IsTestingEnv() { adminRouter.DELETE("/mobile/notifications", m.RoutesHandler.RemoveAllMobileNotifications) adminRouter.DELETE("/mobile/devices", m.RoutesHandler.RemoveAllMobileDevices) } // public rateLimiter := rate_limit.New(m.Redis) bandwidthMobileApiMiddleware := apisec.MobileIpAbuseAuditMiddleware(rateLimiter) iPAbuseAuditMiddleware := security.IPAbuseAuditMiddleware(rateLimiter) publicRouter := router.Group("/") publicRouter.Use(iPAbuseAuditMiddleware) publicRouter.Use(bandwidthMobileApiMiddleware) publicRouter.POST("/mobile/devices", m.RoutesHandler.RegisterMobileDevice) publicRouter.PUT("/mobile/devices/:device_id", m.RoutesHandler.UpdateMobileDevice) publicRouter.GET("/mobile/notifications", m.RoutesHandler.FindAllMobileNotifications) publicRouter.GET("/mobile/notifications/:notification_id", m.RoutesHandler.FindMobileNotification) publicRouter.POST("/mobile/devices/:device_id/commands/send_2fa_token", m.RoutesHandler.Send2FaToken) publicRouter.GET("/mobile/devices/:device_id/browser_extensions/2fa_requests", m.RoutesHandler.GetAll2FaTokenRequests) publicRouter.POST("/mobile/devices/:device_id/browser_extensions", m.RoutesHandler.PairMobileWithExtension) publicRouter.DELETE("/mobile/devices/:device_id/browser_extensions/:extension_id", m.RoutesHandler.RemovePairingWithExtension) publicRouter.GET("/mobile/devices/:device_id/browser_extensions", m.RoutesHandler.FindAllMobileAppExtensions) publicRouter.GET("/mobile/devices/:device_id/browser_extensions/:extension_id", m.RoutesHandler.FindMobileAppExtensionById) }