minimed: replace broken SSO auth with OAuth2 PKCE + MongoDB token storage - #54
Open
LeFrenchGuy wants to merge 9 commits into
Open
minimed: replace broken SSO auth with OAuth2 PKCE + MongoDB token storage#54LeFrenchGuy wants to merge 9 commits into
LeFrenchGuy wants to merge 9 commits into
Conversation
Add new Auth0/OAuth2 PKCE-based authentication for CareLink API, replacing the legacy session-based approach. Includes interactive login command, automated credential login with CAPTCHA fallback, and token refresh support.
Replaces file-based logindata.json storage with a MongoDB-backed token store (connect_tokens collection). Tokens now survive container restarts without requiring write access to /opt/app/ or any volume mounts. Zero user configuration needed — MONGO_CONNECTION is already required by Nightscout.
If CONNECT_CARELINK_USERNAME and CONNECT_CARELINK_PASSWORD are set and no tokens are found in MongoDB, automatically run the PKCE login flow so the container works without any manual setup step.
- Add lib/logger.js with debug/info/warn/error levels (default: info) - Per-cycle data dumps, BLE/M2M success, session setup → debug - Token refresh, fetch tally, errors → info/warn - Controlled via LOG_LEVEL env var
Replace actions.log() calls with logger.debug() so they only appear when LOG_LEVEL=debug. Replace null action implementations for recordFrame/resetCapture/startCapture with no-op functions to suppress xstate "No implementation found" warnings.
Replace all active actions.log() calls in poller.js and session.js with logger equivalents (debug/warn) so they're gated by LOG_LEVEL.
CareLink's Auth0 login requires CAPTCHA for server-side credential POSTs, making headless username/password login unreliable in practice. The CONNECT_CARELINK_USERNAME/PASSWORD env vars are no longer needed. Run 'nightscout-connect login' once to authenticate via the OAuth2 PKCE browser flow and store tokens in MongoDB.
Replace the Minimed CareLink setup docs with the new OAuth2 flow: - Remove CONNECT_CARELINK_USERNAME/PASSWORD from setup instructions - Document the nightscout-connect login command and DevTools paste flow - Add MONGO_CONNECTION requirement - Mention upcoming Nightscout admin UI integration
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Background
The previous Minimed CareLink authentication relied on posting credentials directly to
/patient/sso/login, Medtronic's old proprietary SSO endpoint. That endpoint no longer works: Medtronic has migrated CareLink to an Auth0-based OAuth2 infrastructure and direct credential POSTs are consistently blocked by CAPTCHA in server-side environments.This PR replaces the broken auth mechanism entirely with a proper OAuth2 PKCE flow, ported and adapted from
carelink-bridge, which already implements this successfully in production.What's changing
1. OAuth2 PKCE authentication (
lib/sources/minimedcarelink/auth.js)Implements the full CareLink OAuth2 flow:
/connect/carepartner/v13/discover/android/3.6), supports both US and EU regionscode_verifier+code_challengepair (SHA-256, base64url) per RFC 7636https://redirect URLs and CareLink's custom scheme (com.medtronic.carepartner:/sso?code=...)2. Token lifecycle management (
lib/sources/minimedcarelink/oauth.js)Handles everything after initial login:
logindatafrom file path or inline JSONgrant_type=refresh_token, handles token rotationtoken_url(.minimed.comvs.minimed.eu)3. MongoDB-backed token persistence (
lib/sources/minimedcarelink/token-store.js)Stores OAuth tokens in the Nightscout MongoDB database (
connect_tokenscollection):MONGO_CONNECTIONis not set, fails with a clear actionable errorNew environment variable:
4.
nightscout-connect loginCLI command (commands/login.js)A new
loginsubcommand handles the one-time authentication bootstrap. Unlikecarelink-bridgewhich uses Puppeteer to automate the browser entirely, this implementation avoids that ~300MB dependency by guiding the user through a short manual step instead.nightscout-connect login # EU by default nightscout-connect login --region usThe flow:
com.medtronic.carepartner://) that the browser can't handleF12) → Network tab → find the blocked request to/authorize/resume→ click it → Response Headers → copy theLocationvalue (starts withcom.medtronic.carepartner:/sso?code=...)This is a one-time operation. After that the poller handles everything automatically, token refresh included, until the refresh token itself expires (~30 days with CareLink), at which point you run
loginagain.5. Leveled logger (
lib/logger.js)Controlled by
LOG_LEVEL(debug|info|warn|error|silent, default:info). Also used to silence the noisy xstateactions.log()output in the poller, fetch, cycle, and session machines that was firing on every polling cycle.6. Removal of username/password credential auth
CONNECT_CARELINK_USERNAMEandCONNECT_CARELINK_PASSWORDare no longer used. The supported authentication path is now:nightscout-connect loginonce → tokens saved to MongoDBrefresh_token(no user interaction, no CAPTCHA)nightscout-connect loginCompanion: Nightscout admin UI integration (upcoming)
A companion PR to cgm-remote-monitor is in the works that will expose the full login flow directly from the Nightscout
/adminpage, no CLI access needed. The plan:This will make re-authentication accessible to users who manage their Nightscout instance through the web UI rather than the command line. For now, the
nightscout-connect loginCLI command is the only supported bootstrap path.Configuration
MONGO_CONNECTIONCONNECT_MONGO_CONNECTIONCONNECT_-prefix convention usersLOG_LEVELdebug|info(default) |warn|error|silentCONNECT_CARELINK_REGIONeu(default) |usCONNECT_CARELINK_USERNAMEandCONNECT_CARELINK_PASSWORDare no longer needed and can be removed from your config.