跪拜 Guibai
← All articles
Frontend · WeChat Mini Program · Security

WeChat Mini Programs Can't Identify a Phone. This Team Built the Closest Workable Substitute

By 码云之上 ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

Any login system inside WeChat Mini Programs hits the same wall: the platform deliberately withholds physical device identifiers, so reinstall-proof device recognition requires a compromise. A user-scoped feature hash merges same-model devices, changes across WeChat accounts, and offers no anonymization; teams adopting this pattern still need real-device testing because the official field docs diverge from what handsets return.

Summary

Inside WeChat's Mini Program sandbox there is no physical device identifier: no IMEI, no MAC, no IDFA, no Android ID, and a self-generated UUID vanishes when users clear Storage, which is also isolated per WeChat account and per Mini Program. One team facing a reinstall-resistant device-ID requirement ended up building a client-computed identifier that hashes openid together with platform, brand, model, CPU type, and memory tier into an `acf-v1_` field passed verbatim through the login chain.

Real-device testing shaped the field whitelist more than the official docs did. On four test machines, five discrepancies surfaced: memorySize returns numbers, not the documented string; Mac's CPU field is uppercase CPUType; `unknown` literals and empty values normalize to `-`; iOS `model` carries a marketing name that gets stripped; and deviceAbi was dropped for zero discrimination. The digest pipeline uses SHA-256 plus unpadded base64url, kept out of the main package because iOS 13's JSCore lacks BigInt.

The team drew hard lines around what the field can and cannot claim: it merges two same-model devices under one username, changes when a user switches WeChat accounts, and is not anonymization. Login reliability beats identifier coverage: every step has a 3-second cap with two attempts, fallback values use an `acf-v1-tmp_` prefix, and a shared lock keeps three concurrent wx.login call sites from consuming the same single-use code.

Takeaways
— WeChat Mini Programs expose no IMEI, MAC, IDFA, or Android ID; Storage is isolated per WeChat user and per Mini Program, so a self-generated UUID dies on cache clear or account switch.
— The final identifier is SHA-256 over a canonical string containing openid, platform, brand, model, CPU type, and memory tier, versioned `acf-v1`, output as unpadded base64url at 50 bytes total.
— Adding openid resolves same-model collisions across different users, but two same-model devices under one user still share a value, and switching WeChat accounts changes the value.
— A four-device test against the docs surfaced five whitelist fixes: memorySize returns numbers, Mac uses `CPUType`, `unknown` and empty values normalize to `-`, iOS `model` strips the marketing name in angle brackets, and deviceAbi was dropped for no discrimination.
— memorySize rounds up to GB because reported values run low; the iPhone 14's 5669 MB reads as 6 GB, and upward rounding resists small kernel-memory drift.
— Screen and window parameters were rejected from v1: same-model screens add no discrimination, Mac returns the draggable window size, and Android variants diverge; adding them would require bumping to `acf-v2`.
— The identifier freezes once cached; only cache clear, reinstall, or algorithm upgrade regenerates it, and the value never drives abnormal-login decisions.
— Device-identifier failures never block login: each attempt caps at 3 seconds, max two attempts (6 seconds worst case), with a 28-character throwaway seed producing an `acf-v1-tmp_` prefix on double failure.
— Three SDK call sites share one lock around wx.login because its code is single-use; concurrent calls yield the same code and one consumer gets error 40029 or 40163.
— Remaining gaps: Windows, HarmonyOS, two same-model real devices, memorySize drift across upgrades, and the shape of un-adapted models' iOS `model`.
— The source article was rewritten in an uncanny valley of style: polished here, awkward there.
— At 45 pages of notes, this project had more documentation than most open-source libraries ship.
— Honor, Xiaomi, and Apple devices each reported memory in slightly different units, proving the official docs were aspirational rather than factual.
Conclusions

Client-side device identity inside a Mini Program tops out where the platform does: WeChat can't answer 'is this the same phone', so the team reworded the requirement to 'this WeChat user has logged in on such a device'; any product claiming reinstall-proof physical-device recognition in a Mini Program is overpromising.

Real-device testing beats official documentation for field reporting: the docs' types and coverage diverged in five places on four machines, and only a hands-on hardware pass caught them.

Including openid is a deliberate identity-versus-anonymity trade-off: it kills cross-account device association, so risk control stays on a separate vendor token, and the hash intentionally adds no anonymization because a backend holding openid can enumerate the small feature space.

The 6-second wait cap forced the team to abandon its own 'authentication doesn't wait on background tasks' convention; reviewers objected until the timeout and failure fallback were explained.

The project shipped a draft-status document to production, accepting that Windows and HarmonyOS remain untested, which keeps the algorithm honest about its own coverage.

Concepts & terms
openid
WeChat's per-user, per-Mini-Program identifier, exchanged from a one-time login code via the jscode2session endpoint. It is case-sensitive, high-entropy, and stable for the same WeChat user inside the same Mini Program, which makes it the 'who' factor of the deviceId rather than a device property.
jscode2session
A WeChat endpoint that trades the short-lived code from wx.login for the user's openid and session key. The team wraps it in a dedicated endpoint that doesn't create accounts or log in, just exchanges the code.
single-use wx.login code
A code from wx.login can only be consumed successfully once; concurrent calls may return the same code, and the second consumer gets errors 40029 or 40163. The team serializes all three call sites behind one lock.
acf-v1
The versioned prefix of the device identifier, followed by 43 characters of unpadded base64url totaling 50 bytes. Bumping the whitelist or normalization rules opens a new version prefix so already-issued values keep their semantics.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗