2025-05-03 14:25:22 -07:00
|
|
|
package bootstrap
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"fmt"
|
2025-05-05 15:59:44 +02:00
|
|
|
"net/http"
|
2025-05-03 14:25:22 -07:00
|
|
|
|
2026-06-29 12:11:18 +02:00
|
|
|
"github.com/pocket-id/pocket-id/backend/internal/apikey"
|
2026-01-04 19:00:18 +01:00
|
|
|
"github.com/pocket-id/pocket-id/backend/internal/job"
|
2025-05-03 14:25:22 -07:00
|
|
|
"gorm.io/gorm"
|
|
|
|
|
|
2026-07-06 21:25:02 +02:00
|
|
|
"github.com/pocket-id/pocket-id/backend/internal/api"
|
2026-06-22 18:42:02 +02:00
|
|
|
"github.com/pocket-id/pocket-id/backend/internal/common"
|
|
|
|
|
"github.com/pocket-id/pocket-id/backend/internal/oidc"
|
2025-05-03 14:25:22 -07:00
|
|
|
"github.com/pocket-id/pocket-id/backend/internal/service"
|
2025-11-10 10:02:25 +01:00
|
|
|
"github.com/pocket-id/pocket-id/backend/internal/storage"
|
2026-06-30 20:53:31 +02:00
|
|
|
"github.com/pocket-id/pocket-id/backend/internal/usersignup"
|
2026-06-29 14:02:20 +02:00
|
|
|
"github.com/pocket-id/pocket-id/backend/internal/webauthn"
|
2025-05-03 14:25:22 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type services struct {
|
2026-01-11 12:31:26 +01:00
|
|
|
appConfigService *service.AppConfigService
|
|
|
|
|
appImagesService *service.AppImagesService
|
|
|
|
|
emailService *service.EmailService
|
|
|
|
|
geoLiteService *service.GeoLiteService
|
|
|
|
|
auditLogService *service.AuditLogService
|
|
|
|
|
jwtService *service.JwtService
|
|
|
|
|
scimService *service.ScimService
|
|
|
|
|
userService *service.UserService
|
|
|
|
|
customClaimService *service.CustomClaimService
|
|
|
|
|
oidcService *service.OidcService
|
|
|
|
|
userGroupService *service.UserGroupService
|
|
|
|
|
ldapService *service.LdapService
|
|
|
|
|
versionService *service.VersionService
|
|
|
|
|
fileStorage storage.FileStorage
|
|
|
|
|
appLockService *service.AppLockService
|
|
|
|
|
oneTimeAccessService *service.OneTimeAccessService
|
2026-06-22 18:42:02 +02:00
|
|
|
|
2026-06-30 20:53:31 +02:00
|
|
|
apiKeyModule *apikey.Module
|
|
|
|
|
oidcModule *oidc.Module
|
|
|
|
|
webauthnModule *webauthn.Module
|
|
|
|
|
userSignUpModule *usersignup.Module
|
2026-07-06 21:25:02 +02:00
|
|
|
apiModule *api.Module
|
2025-05-03 14:25:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Initializes all services
|
2026-07-11 07:16:05 -07:00
|
|
|
func initServices(ctx context.Context, db *gorm.DB, instanceID string, httpClient *http.Client, imageExtensions map[string]string, fileStorage storage.FileStorage, scheduler *job.Scheduler) (svc *services, err error) {
|
2025-05-03 14:25:22 -07:00
|
|
|
svc = &services{}
|
|
|
|
|
|
2025-07-27 06:34:23 +02:00
|
|
|
svc.appConfigService, err = service.NewAppConfigService(ctx, db)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("failed to create app config service: %w", err)
|
|
|
|
|
}
|
2025-05-03 14:25:22 -07:00
|
|
|
|
2025-11-10 10:02:25 +01:00
|
|
|
svc.fileStorage = fileStorage
|
|
|
|
|
svc.appImagesService = service.NewAppImagesService(imageExtensions, fileStorage)
|
2025-11-26 10:38:15 +01:00
|
|
|
svc.appLockService = service.NewAppLockService(db)
|
2025-09-20 21:42:52 +02:00
|
|
|
|
2025-05-03 14:25:22 -07:00
|
|
|
svc.emailService, err = service.NewEmailService(db, svc.appConfigService)
|
|
|
|
|
if err != nil {
|
2025-06-06 03:23:51 -07:00
|
|
|
return nil, fmt.Errorf("failed to create email service: %w", err)
|
2025-05-03 14:25:22 -07:00
|
|
|
}
|
|
|
|
|
|
2025-05-05 15:59:44 +02:00
|
|
|
svc.geoLiteService = service.NewGeoLiteService(httpClient)
|
2025-05-03 14:25:22 -07:00
|
|
|
svc.auditLogService = service.NewAuditLogService(db, svc.appConfigService, svc.emailService, svc.geoLiteService)
|
2026-07-11 07:16:05 -07:00
|
|
|
svc.jwtService, err = service.NewJwtService(ctx, db, instanceID, svc.appConfigService)
|
2025-07-27 03:03:52 +02:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("failed to create JWT service: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-03 14:25:22 -07:00
|
|
|
svc.customClaimService = service.NewCustomClaimService(db)
|
2026-06-29 14:02:20 +02:00
|
|
|
svc.webauthnModule, err = webauthn.New(webauthn.Dependencies{
|
|
|
|
|
DB: db,
|
|
|
|
|
AppURL: common.EnvConfig.AppURL,
|
|
|
|
|
Signer: svc.jwtService,
|
|
|
|
|
AuditLog: svc.auditLogService,
|
|
|
|
|
AppConfig: svc.appConfigService,
|
|
|
|
|
})
|
2025-08-22 08:56:40 +02:00
|
|
|
if err != nil {
|
2026-06-29 14:02:20 +02:00
|
|
|
return nil, fmt.Errorf("failed to create WebAuthn module: %w", err)
|
2025-08-22 08:56:40 +02:00
|
|
|
}
|
2025-06-06 03:23:51 -07:00
|
|
|
|
2026-01-04 19:00:18 +01:00
|
|
|
svc.scimService = service.NewScimService(db, scheduler, httpClient)
|
|
|
|
|
|
2026-07-06 21:25:02 +02:00
|
|
|
svc.apiModule = api.New(api.Dependencies{DB: db, Issuer: common.EnvConfig.AppURL})
|
|
|
|
|
|
2026-06-22 18:42:02 +02:00
|
|
|
svc.oidcModule, err = oidc.New(ctx, oidc.Dependencies{
|
|
|
|
|
DB: db,
|
|
|
|
|
HTTPClient: httpClient,
|
|
|
|
|
Config: oidc.Config{
|
2026-07-13 08:35:02 +02:00
|
|
|
BaseURL: common.EnvConfig.AppURL,
|
|
|
|
|
TokenBaseURL: common.EnvConfig.AppURL,
|
|
|
|
|
Secret: common.EnvConfig.EncryptionKey,
|
|
|
|
|
AllowInsecureCallbackURLs: common.EnvConfig.AllowInsecureCallbackURLs,
|
2026-06-22 18:42:02 +02:00
|
|
|
},
|
|
|
|
|
Signer: svc.jwtService,
|
|
|
|
|
CustomClaims: svc.customClaimService,
|
2026-06-29 14:02:20 +02:00
|
|
|
Reauth: svc.webauthnModule,
|
2026-06-22 18:42:02 +02:00
|
|
|
AuditLog: svc.auditLogService,
|
2026-07-06 21:25:02 +02:00
|
|
|
APIAccess: svc.apiModule,
|
2026-06-22 18:42:02 +02:00
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("failed to create OIDC module: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
svc.oidcService, err = service.NewOidcService(db, svc.jwtService, svc.appConfigService, svc.oidcModule.Preview, svc.scimService, httpClient, fileStorage)
|
2025-06-06 03:23:51 -07:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("failed to create OIDC service: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-04 19:00:18 +01:00
|
|
|
svc.userGroupService = service.NewUserGroupService(db, svc.appConfigService, svc.scimService)
|
|
|
|
|
svc.userService = service.NewUserService(db, svc.jwtService, svc.auditLogService, svc.emailService, svc.appConfigService, svc.customClaimService, svc.appImagesService, svc.scimService, fileStorage)
|
2025-11-16 09:23:46 -08:00
|
|
|
svc.ldapService = service.NewLdapService(db, httpClient, svc.appConfigService, svc.userService, svc.userGroupService, fileStorage)
|
2026-01-11 15:36:27 +01:00
|
|
|
|
2026-06-29 12:11:18 +02:00
|
|
|
svc.apiKeyModule, err = apikey.New(ctx, apikey.Dependencies{
|
|
|
|
|
DB: db,
|
|
|
|
|
StaticApiKey: common.EnvConfig.StaticApiKey,
|
|
|
|
|
})
|
2026-01-11 15:36:27 +01:00
|
|
|
if err != nil {
|
2026-06-29 12:11:18 +02:00
|
|
|
return nil, fmt.Errorf("failed to create API key module: %w", err)
|
2026-01-11 15:36:27 +01:00
|
|
|
}
|
|
|
|
|
|
2026-06-30 20:53:31 +02:00
|
|
|
svc.userSignUpModule = usersignup.New(usersignup.Dependencies{
|
|
|
|
|
DB: db,
|
|
|
|
|
Signer: svc.jwtService,
|
|
|
|
|
AuditLog: svc.auditLogService,
|
|
|
|
|
AppConfig: svc.appConfigService,
|
|
|
|
|
UserCreator: svc.userService,
|
|
|
|
|
})
|
2026-01-11 12:31:26 +01:00
|
|
|
svc.oneTimeAccessService = service.NewOneTimeAccessService(db, svc.userService, svc.jwtService, svc.auditLogService, svc.emailService, svc.appConfigService)
|
2025-07-27 06:34:23 +02:00
|
|
|
|
2025-09-07 20:45:06 +02:00
|
|
|
svc.versionService = service.NewVersionService(httpClient)
|
|
|
|
|
|
2025-05-03 14:25:22 -07:00
|
|
|
return svc, nil
|
|
|
|
|
}
|