Skip to content

ConnectionOptionsOAuth2 unmarshal fails on tenants with legacy string-encoded customHeaders #815

Description

@alexmsenger

Checklist

  • I have looked into the README and have not found a suitable solution or answer.
  • I have looked into the documentation and have not found a suitable solution or answer.
  • I have searched the issues and have not found a suitable solution or answer.
  • I have upgraded to the latest version of this SDK and the issue still persists.
  • I have searched the Auth0 Community forums and have not found a suitable solution or answer.
  • I agree to the terms within the Auth0 Code of Conduct.

Description

ConnectionOptionsOAuth2.UnmarshalJSON fails with a type assertion error when
the Auth0 Management API returns the customHeaders field as a JSON-encoded
string rather than a map object.

The error message is:

json: cannot unmarshal string into Go struct field of type map[string]string

This occurs on older Auth0 tenants where the customHeaders value was persisted
in a legacy string format. The SDK makes no attempt to handle this variant and
instead surfaces the raw JSON decode error to the caller, which causes the
terraform-provider-auth0 to crash when reading or refreshing an OAuth2
connection resource on such tenants.

Expectation

When the API returns "customHeaders": "<json-encoded-string>", the SDK should
not return an error. The legacy format cannot be reliably decoded without knowing
the original structure, so the field should be surfaced as nil/empty and the
operator can restore the values through their Terraform configuration.

When the API returns "customHeaders": {"key": "value"} (the current format),
the field should continue to be decoded into *map[string]string as normal.

Reproduction

package main

import (
    "encoding/json"
    "fmt"
    "github.com/auth0/go-auth0/management"
)

func main() {
    // Simulates what the Management API returns on an older tenant.
    // customHeaders is a JSON-encoded string instead of a map object.
    payload := `{
        "authorizationURL": "https://example.com/oauth/authorize",
        "tokenURL": "https://example.com/oauth/token",
        "customHeaders": "{\"X-Custom-Header\":\"custom-value\"}"
    }`

    var opts management.ConnectionOptionsOAuth2
    err := json.Unmarshal([]byte(payload), &opts)
    if err != nil {
        fmt.Printf("Error: %v\n", err)
        // Prints: json: cannot unmarshal string into Go struct field of type map[string]string
    }
}

Steps to reproduce in production:

  1. Have an Auth0 tenant where an OAuth2 connection was created and had customHeaders set while the SDK persisted them in a legacy string format.
  2. Use terraform-provider-auth0 to run terraform refresh or terraform plan against that connection.
  3. The provider crashes with the above unmarshal error.

Auth0 Go SDK version

v1.44.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions