enode icon indicating copy to clipboard operation
enode copied to clipboard

Sandbox OAuth token refresh uses production token endpoint

Open 65007 opened this issue 3 months ago • 0 comments

Sandbox OAuth token refresh uses production token endpoint

Description

When the integration is configured with Sandbox Mode enabled, the initial OAuth token request correctly uses the sandbox endpoint.

However, after the access token expires, Home Assistant rebuilds the OAuth implementation through async_get_auth_implementation(), which always initializes the implementation with:

token_url=PRODUCTION_OAUTH2_TOKEN,

As a result, sandbox credentials are sent to:

https://oauth.production.enode.io/oauth2/token

and the token refresh fails with:

401 Unauthorized

The integration then stops being able to refresh vehicle data until Home Assistant is restarted or the OAuth endpoint is corrected.

Environment

  • Home Assistant
  • Enode custom integration: 1.3.0
  • Enode environment: Sandbox
  • Config entry contains:
{
  "sandbox": true
}

Error

Observed error:

401, message='Unauthorized',
url='https://oauth.production.enode.io/oauth2/token'

Root cause

config_flow.py correctly calls:

self.flow_impl.sandbox_mode()

during initial setup when Sandbox Mode is enabled.

The API client also correctly reads the sandbox setting from the Home Assistant config entry:

sandbox = entry.data.get(CONF_SANDBOX, False)

and the API URL is selected accordingly.

However, async_get_auth_implementation() does not have access to the ConfigEntry sandbox flag and always creates the OAuth implementation using:

token_url=PRODUCTION_OAUTH2_TOKEN,

Therefore the initial authentication uses the sandbox token endpoint, but subsequent token renewal uses the production token endpoint.

Reproduction

  1. Configure the integration with Sandbox Mode enabled.

  2. Confirm that the Home Assistant config entry contains:

    {
      "sandbox": true
    }
    
  3. Allow the current access token to expire.

  4. Trigger a data refresh.

  5. Observe a 401 Unauthorized error against:

    https://oauth.production.enode.io/oauth2/token
    

Tested workaround / proposed fix

I tested applying sandbox mode to the OAuth implementation in get_client() based on the ConfigEntry value:

async def get_client(hass: HomeAssistant, entry: ConfigEntry) -> EnodeClient:
    """Get the Enode client."""
    implementation = await async_get_config_entry_implementation(hass, entry)
    sandbox = entry.data.get(CONF_SANDBOX, False)

    if sandbox and isinstance(implementation, Oauth2Impl):
        implementation.sandbox_mode()

    return EnodeClient(
        OAuth2Session(hass, entry, implementation),
        sandbox=sandbox,
    )

This keeps production as the default OAuth endpoint while switching both the API and OAuth token endpoint to sandbox when the ConfigEntry contains:

{
  "sandbox": true
}

Validation performed

After applying the change and restarting Home Assistant:

  • An already-expired sandbox token was successfully reacquired.
  • Vehicle data could be refreshed again without a 401 error.
  • A value changed directly in Enode Sandbox was successfully retrieved in Home Assistant using the integration's refresh action.

The remaining validation is to let the new token expire naturally and confirm that the next automatic token renewal also succeeds without restarting Home Assistant.

Expected behavior

When the integration is configured with Sandbox Mode enabled, all OAuth token requests and API requests should consistently use the sandbox endpoints.

When Sandbox Mode is disabled, production endpoints should continue to be used.

Notes

The issue appears to be limited to OAuth implementation reconstruction and endpoint selection. The integration already stores and reads the sandbox setting correctly for API client creation.

65007 avatar Aug 08 '26 20:08 65007