{"id":"ab063953e8f7eadd","repo":"mongodb/node-mongodb-native","slug":"attempted-to-get-an-access-token-when-none-exists","errorCode":null,"errorMessage":"Attempted to get an access token when none exists.","messagePattern":"Attempted to get an access token when none exists\\.","errorType":"exception","errorClass":"MongoOIDCError","httpStatus":null,"severity":"error","filePath":"src/cmap/auth/mongodb_oidc/token_cache.ts","lineNumber":27,"sourceCode":"  private refreshToken?: string;\n  private idpInfo?: IdPInfo;\n  private expiresInSeconds?: number;\n\n  get hasAccessToken(): boolean {\n    return !!this.accessToken;\n  }\n\n  get hasRefreshToken(): boolean {\n    return !!this.refreshToken;\n  }\n\n  get hasIdpInfo(): boolean {\n    return !!this.idpInfo;\n  }\n\n  getAccessToken(): string {\n    if (!this.accessToken) {\n      throw new MongoOIDCError('Attempted to get an access token when none exists.');\n    }\n    return this.accessToken;\n  }\n\n  getRefreshToken(): string {\n    if (!this.refreshToken) {\n      throw new MongoOIDCError('Attempted to get a refresh token when none exists.');\n    }\n    return this.refreshToken;\n  }\n\n  getIdpInfo(): IdPInfo {\n    if (!this.idpInfo) {\n      throw new MongoOIDCError('Attempted to get IDP information when none exists.');\n    }\n    return this.idpInfo;\n  }\n","sourceCodeStart":9,"sourceCodeEnd":45,"githubUrl":"https://github.com/mongodb/node-mongodb-native/blob/3366c21a6311e02f1be91da982f9b93d3cce99a0/src/cmap/auth/mongodb_oidc/token_cache.ts#L9-L45","documentation":"Thrown by the OIDC TokenCache.getAccessToken() when no access token is cached (src/cmap/auth/mongodb_oidc/token_cache.ts:26). This is an internal driver invariant: callers are expected to check hasAccessToken before calling getAccessToken. Surfaced as a MongoDriverError (local MongoOIDCError subclass), it indicates a logic error in the workflow orchestration rather than a user misconfiguration.","triggerScenarios":"Internal: a workflow path calls cache.getAccessToken() without first verifying cache.hasAccessToken. Should not be reachable through normal public API usage; would indicate a bug in the driver's OIDC workflow state machine.","commonSituations":"Not user-triggered in normal operation. Could surface if a future driver regression changes the workflow ordering, or if an internal reauthenticate path clears the token mid-flow. Effectively always a driver bug report candidate.","solutions":["Upgrade to the latest driver patch release in case the issue is already fixed.","If reproducible, file a bug with the driver version, OIDC environment, and a trace of operations preceding the error.","As a workaround, ensure connections are freshly created (new MongoClient) rather than reusing a client in a partially failed state.","Avoid manually constructing or reusing internal AuthContext objects."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n  await client.connect();\n} catch (e) {\n  if (e instanceof MongoDriverError && /get an access token when none exists/.test(e.message)) {\n    // Internal invariant - recreate client and report\n    client.close();\n    throw new Error('Driver OIDC cache invariant violated - please file a bug.');\n  }\n  throw e;\n}","preventionTips":["Treat this as a driver bug, not a user error - keep the driver up to date.","Recreate the MongoClient after authentication failures rather than reusing compromised state.","Do not reach into internal AuthContext/TokenCache objects."],"tags":["auth","oidc","internal","invariant","token-cache"],"analyzedSha":"3366c21a6311e02f1be91da982f9b93d3cce99a0","analyzedAt":"2026-08-04T13:40:15.335Z","schemaVersion":2}