Skip to content

End-to-end flows

Which component does what in each flow, and which file to look at. Paths are relative to the repo root; server/ = wonkru_mdm_server/lib/src/, panel/ = wonkru_mdm_flutter/lib/, mobil/ = wonkru_mdm_mobil/lib/, kotlin/ = wonkru_mdm_mobil/android/app/src/main/kotlin/com/wonkrumdm/.

sequenceDiagram
  participant A as Admin (panel)
  participant S as Server
  participant D as Android setup wizard
  participant M as Agent
  A->>S: Save provisioning profile (QR JSON)
  S-->>A: profile + current enrollment key (added at read time)
  A->>D: QR is shown, device scans it
  D->>D: Connect to Wi-Fi, download APK and verify its checksum
  D->>M: Make Device Owner, pass extras bundle
  M->>M: Store server_url, device_code, enrollment_key
  M->>S: device.register(device info, enrollmentKey)
  S-->>M: {code, uuid, token}
  M->>S: device.sync (full telemetry)
  M->>S: device.config
  S-->>M: default policy JSON
  M->>M: apply policy
  M->>S: device.connect (stream)
  S-->>M: welcome
  1. Enrollment key. settings.device_enrollment_key is generated when first needed as WNKR-XXXX-XXXX-XXXX, with twelve Crockford Base32 characters (server/service/enrollment_key_service.dart). Lowercase, missing separators and the letters I/L/O are normalized before the constant-time comparison. It is visible in the panel under Settings, can be shown as a QR, and rotated.
  2. QR. Open the Wonkru MDM profile. The server (server/service/provisioning_profile_service.dart) supplies the APK download URL, file checksum, API address and current enrollment key. The profile does not store the key. Regenerate PNGs after rotating it.
  3. Android. kotlin/core/provisioning/ProvisioningActivity.kt selects Android’s fully managed device mode; it writes the ProvisioningExtras bundle to SharedPreferences. mobil/Core/Utils/enrollment_key_store.dart moves the key to secure storage.
  4. Enrollment RPC. mobil/Data/DataSources/Remote/device_register.dart → server/endpoints/device_endpoint.dart register:
    • 120 / 10 min per IP, field lengths are checked.
    • The key is compared in constant time; if wrong, DeviceEnrollmentRejectedException.
    • A device code, a UUIDv7, and a 43-character token are generated. Only the SHA-256 of the token is written to the DB.
    • If settings.default_policy points to a valid policy, it is assigned to the device.
  5. First sync. The agent sends a full sync, fetches and applies the policy, and opens the stream.

New devices receive the default policy from Settings. After enrollment, use Devices to assign a group or change the policy.

Enrollment without a QR: Device Owner is assigned via ADB. The server address and enrollment key can then be scanned from the panel’s enrollment QR (a camera button next to the key field), pushed over ADB with tool/adb_enroll.sh, baked into a controlled build with ENROLLMENT_KEY in config.json, or entered manually on the agent’s setup screen.

Self-healing:

  • If the token is rejected (DeviceUnauthenticatedException), the agent (mobil/Core/Services/enrollment_recovery.dart) deletes the local identity and enrolls again. The server creates a new device record. Remove the previous record from the panel if it is no longer needed.
  • If the token is valid but the UUID is different (DeviceIdentityMismatchException), the agent adopts the server’s identity; no new enrollment is opened.
sequenceDiagram
  participant P as Panel
  participant S as Server
  participant Q as device_commands
  participant M as Agent
  P->>S: adminDevice.sendCommand(ids, action, params)
  S->>S: prepare (validate, default parameters)
  S->>Q: enqueue (pending, bulkId)
  S->>M: flush: command message (if connected)
  S->>Q: sent, attempts+1
  S-->>P: bulkId
  M->>M: journal check, run in order
  M->>S: ack {success, status, ...}
  S->>Q: acked / error
  loop every 2 s
    P->>S: bulkStatus / bulkCommands
  end
  1. Panel (panel/Views/Pages/Devices/device_command_dialog.dart) collects the action, parameters, and devices; calls adminDevice.sendCommand.
  2. Server (server/endpoints/admin/admin_device_endpoint.dart, server/service/device_command_service.dart):
    • prepare rejects an unknown action, a multi-device send of a single-device action, and a missing package_name.
    • For policy_change, policy_id is first assigned to the device.
    • For each device a pending row (commandId UUIDv7, shared bulkId), then flush.
  3. Flush. If the device is connected to this server process, the row is set to sent with a conditional update and {type: command, commandId, payload: {"action":..,"params":{..}}} is written to the stream. If it is not connected, the row stays pending; when the device connects, it is sent right after welcome.
  4. Agent (mobil/Core/Managers/Stream/device_stream_manager.dart): commands are processed in order. If the same commandId arrives again, CommandJournal does not re-run the command. Jobs like reboot run 3 s after the ack.
  5. Ack. If the response has "success":false or "error":true, it becomes error, otherwise acked. A successful get_installed_apps response is written to the device’s app list.
  6. Timeout. A job that runs once a minute sets a command in sent state with no ack for 2 min (15 min for long actions) to timeout; a pending older than 7 days also becomes timeout.
    • A timeout command is not sent again. Resend it from the panel if needed.
    • A command left in sent state is sent again when the device reconnects (at most 3 attempts).
    • An ack that arrives after the timeout is still accepted.
  7. Panel polls the bulk status every 2 s; it stops when pending + sent = 0. The device’s command history is on the Commands tab of the detail page.
  • The policy is created in the panel. The device has a single policyId; a device group does not carry a policy. “Assigning a policy to a group” = filter the group, select the devices, and Assign policy.
  • assignPolicy saves policyId. To notify devices, use Send policy update or Send to devices in the policy list. To assign and send in one step, use the policy_change command.
  • A newly enrolled device gets settings.default_policy.
Change Does the version increase? Automatic update notification
General/settings tabs in the policy editor Yes (on a meaningful change) Yes, after 5 s
Binding, updating, removing, ordering an app/file/certificate Yes No
The app, file, certificate record itself changed Yes (in bound policies) No
Default policy changed Yes for both the old and new policy No
Policy deleted Devices switch to the default No

In the “No” rows, the device gets the new version on the next policy fetch (sync schedule, restart, update_policy). To send it immediately, use Send to devices.

server/future_calls/send_policy_future_call.dart queues an update_policy command for each target device and sends it to devices that are connected. adminPolicy.update also schedules a check. After the configured delay (30 minutes by default), the server raises a policy_update_pending alert for devices still on the previous version and lists up to 10 names.

  1. update_policy arrives → device.config → the server builds the JSON with server/service/policy_payload_service.dart. Public files are sent directly; private files use a 7-day signed link.
  2. mobil/Core/Managers/config_manager.dart: the same version is not applied again within 45 s; if the policy is passive or outside its active time window, it returns inactive.
  3. Phases: security/network/certificate → sound/battery/other/file → apps → launcher/screen → restrictions.
  4. The policy_result in the ack is shown in the panel in the command response (applied, partial, inactive, fetchFailed…). Then devices.policyVersion is updated with a full sync; the “out of date” badge in the device list looks at this field.

For a broken or unfetchable policy, the agent keeps the last valid policy.

  1. Panel → Apps → New: an APK is selected. The panel reads the manifest in the browser and fills in the package name, versionCode, SDKs, and permissions (panel/Core/Utils/apk_manifest_parser.dart).
  2. Upload: adminUpload.getUploadDescription('apk', ...) → the server produces the applications/<uuidv7>.apk path and a signed token → the browser POST /storage/upload?token=... (web port, per CORS allowedOrigins) → verifyUpload → fileChecksum (SHA-256 on the server).
  3. application_versions row: file path, checksum, versionCode (unique within the app).
  4. Binding to a policy: the Apps tab of the editor. If no target version is selected, the default among the active versions, otherwise the highest versionCode. Then Send to devices.
  5. Agent (mobil/Core/Managers/app_manager.dart): if it is not installed or the versionCode is higher, downloads the APK from download_url, verifies the SHA-256, opens a PackageInstaller session and installs silently with kotlin/features/app/handlers/AppInstallerHandler.kt. During installation DISALLOW_INSTALL_APPS is temporarily removed, then put back. 5 min wait. start_after_install and run_on_boot are supported. action=remove uninstalls the app.

APKs live on the public disk; webServer.publicHost must be reachable from the device.

Files are uploaded from the Files page (disk private or public) and bound with download/open/delete in the Files tab of the policy editor. Target path formats: Mobile agent. Private files reach the device through a 7-day signed link.

device.uploadFile (60 / 5 min per device) → server/service/device_upload_service.dart: size limit device_upload_max_size_kb (50 MiB), extension allowlist, on the private disk device-uploads/<40 characters>.<extension>, files row with deviceId. Because the bytes are carried as base64 in the RPC body, they grow by ~1.33x.

The agent does not use this RPC to upload files. Device-to-server file uploads are not available through the agent app.

GET /storage/file/<storageId>/<path>?token=.... The token is signed with HMAC-SHA256; it contains the path, the operation, and the expiry. Panel links are valid for 10 min, device links for 7 days. Certificates and device uploads are not served without a token even when they are on the public disk.

Signal Source Frequency
heartbeat Agent stream 60 s
ack Command response per command
sync Full telemetry per cron, after a policy, on command

Every heartbeat/ack updates devices.lastSeenAt and writes device_state_cache.isOnline = true; when the stream closes, it writes false. The battery and version information in the heartbeat is not written; these fields come from sync.

The panel shows “Online” with now - lastSeenAt ≤ offline_timeout (default 5 min). The badge in the list is computed with the client clock, while the filter and dashboard are computed with the server clock; if the client clock is skewed, the two can give different results.

Offline alert: a job that runs every 15 minutes produces alerts for admins for devices not seen for device_alerts_offline_minutes (default 1440 min); it does not repeat for the same device within device_alerts_duplicate_prevention_hours (24).

The 2-second “liveness” signal inside the agent (HeartbeatService) is only for the local watchdog; it does not go to the server.

  1. The panel calls adminAuth.login(email, password); the endpoint wraps the Email IDP. 20 / 10 min per IP, the IDP’s own limit is 5 failed attempts / 5 min. Successful and failed attempts are written to the audit.
  2. adminAuth.me(): status, hasAdminScope, totpEnabled, twoFactorRequired.
  3. Every admin endpoint checks with AdminAuthService.requireAdmin that the profile is active and, if 2FA is on, that this session is verified (admin_totp_sessions). Entering a code on one device does not open another session.
  4. 2FA setup: startTotpSetup → confirmTotpSetup(code) → 10 recovery codes only in this response. Verification is valid for 12 hours. After 5 failed attempts, a lock is applied that increases from 30 s up to 15 min.
  5. A password/email change requires 2FA.

Enter an unused recovery code at the login prompt. If you lose access to both the authenticator and recovery codes, use the server terminal to reset the account password and clear 2FA. Sign in and enable 2FA again. Recover an admin account

  • Verification codes. For registration and password resets, server/service/email_idp_sender.dart uses SMTP when smtp_enabled is on and smtp_host is set. Otherwise, it writes the code to the log in development or uses Serverpod Cloud in production. Codes expire after 15 minutes. Password resets are limited to three per hour. The server waits up to 30 seconds for email delivery and logs any delivery error.
  • System alerts (server/service/system_alert_dispatcher.dart): database (panel notification) and/or mail per alert type. Recipients are active admins.
  • Test: Settings → SMTP → test mail (sent with unsaved form values).