moodle/moodle · error · UnexpectedValueException

Cannot sign both audience and scope in JwtAccess

Error message

Cannot sign both audience and scope in JwtAccess

What it means

JwtAccess credentials can sign a JWT either for an audience (self-signed JWT flow, no token exchange) or for a scope (OAuth2 token exchange flow), but not both. The library throws UnexpectedValueException in ServiceAccountJwtAccessCredentials::fetchAuthToken when both $audience and $scope are set, because a JWT carrying both claims is ambiguous and invalid for Google's auth endpoints.

Solutions

  1. Remove the explicit scope when you intend audience-based (self-signed JWT) auth, or vice versa: configure exactly one of the two.
  2. If you need scope-based OAuth, call fetchAuthToken(null) / updateMetadata without an audience so only the scope is signed.
  3. If you need audience-based auth, construct ServiceAccountJwtAccessCredentials with $scope = null and set the target audience instead.

Example fix

// before
$creds = new ServiceAccountJwtAccessCredentials($json, 'https://www.googleapis.com/auth/cloud-platform');
$creds->fetchAuthToken('https://iap.googleapis.com/...'); // both set -> throws
// after
$creds = new ServiceAccountJwtAccessCredentials($json, null);
$creds->fetchAuthToken('https://iap.googleapis.com/...'); // audience only
Defensive patterns

Strategy: validation

Validate before calling

if ($audience !== null && $audience !== '' && $scope !== null && $scope !== '') {
    throw new InvalidArgumentException('Pass either scope or audience to JwtAccess, not both');
}

Type guard

function isExclusiveOption(?string $scope, ?string $audience): bool {
    return ($scope === null || $scope === '') !== ($audience === null || $audience === '');
}

Try / catch

try {
    $token = $credentials->fetchAuthToken($audience);
} catch (\UnexpectedValueException $e) {
    // fall back: rebuild credentials with only one of scope/audience
    $credentials = new ServiceAccountJwtAccessCredentials($json, $audience !== null ? null : $scope);
    $token = $credentials->fetchAuthToken($audience);
}

Prevention

When it happens

Trigger: Calling fetchAuthToken($audience) with a non-null audience while the underlying UserRefreshCredentials/JWT auth object was constructed with a non-empty $scope; or calling updateMetadata() on a target whose metadata context supplies an audience while a scope was configured.

Common situations: Developers configure scopes in the service-account JSON or constructor and then use self-signed JWT audience signing (e.g. for IAP or id-token flows); mixing the two config styles in the same credential instance.

Related errors


AI-assisted analysis of moodle/moodle@48ea8c3322 (2026-09-14). Data as JSON: /api/errors/57391c1951bcb628. Report an issue: GitHub.

Appendix: source

Thrown at public/lib/google2-auth/src/Credentials/ServiceAccountJwtAccessCredentials.php:151

    }

    /**
     * Implements FetchAuthTokenInterface#fetchAuthToken.
     *
     * @param callable|null $httpHandler
     *
     * @return null|array{access_token:string} A set of auth related metadata
     */
    public function fetchAuthToken(?callable $httpHandler = null)
    {
        $audience = $this->auth->getAudience();
        $scope = $this->auth->getScope();
        if (empty($audience) && empty($scope)) {
            return null;
        }

        if (!empty($audience) && !empty($scope)) {
            throw new \UnexpectedValueException(
                'Cannot sign both audience and scope in JwtAccess'
            );
        }

        $access_token = $this->auth->toJwt();

        // Set the self-signed access token in OAuth2 for getLastReceivedToken
        $this->auth->setAccessToken($access_token);

        return [
            'access_token' => $access_token,
            'expires_in' => $this->auth->getExpiry(),
            'token_type' => 'Bearer'
        ];
    }

    /**
     * Return the cache key for the credentials.

View on GitHub (pinned to 48ea8c3322)