{"record":{"id":"57391c1951bcb628","repo":"moodle/moodle","slug":"cannot-sign-both-audience-and-scope-in-jwtaccess","errorCode":null,"errorMessage":"Cannot sign both audience and scope in JwtAccess","messagePattern":"Cannot sign both audience and scope in JwtAccess","errorType":"exception","errorClass":"UnexpectedValueException","httpStatus":null,"severity":"error","filePath":"public/lib/google2-auth/src/Credentials/ServiceAccountJwtAccessCredentials.php","lineNumber":151,"sourceCode":"    }\n\n    /**\n     * Implements FetchAuthTokenInterface#fetchAuthToken.\n     *\n     * @param callable|null $httpHandler\n     *\n     * @return null|array{access_token:string} A set of auth related metadata\n     */\n    public function fetchAuthToken(?callable $httpHandler = null)\n    {\n        $audience = $this->auth->getAudience();\n        $scope = $this->auth->getScope();\n        if (empty($audience) && empty($scope)) {\n            return null;\n        }\n\n        if (!empty($audience) && !empty($scope)) {\n            throw new \\UnexpectedValueException(\n                'Cannot sign both audience and scope in JwtAccess'\n            );\n        }\n\n        $access_token = $this->auth->toJwt();\n\n        // Set the self-signed access token in OAuth2 for getLastReceivedToken\n        $this->auth->setAccessToken($access_token);\n\n        return [\n            'access_token' => $access_token,\n            'expires_in' => $this->auth->getExpiry(),\n            'token_type' => 'Bearer'\n        ];\n    }\n\n    /**\n     * Return the cache key for the credentials.","sourceCodeStart":133,"sourceCodeEnd":169,"githubUrl":"https://github.com/moodle/moodle/blob/48ea8c33227277e916cdd4079dbecef448b37f2c/public/lib/google2-auth/src/Credentials/ServiceAccountJwtAccessCredentials.php#L133-L169","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Remove the explicit scope when you intend audience-based (self-signed JWT) auth, or vice versa: configure exactly one of the two.","If you need scope-based OAuth, call fetchAuthToken(null) / updateMetadata without an audience so only the scope is signed.","If you need audience-based auth, construct ServiceAccountJwtAccessCredentials with $scope = null and set the target audience instead."],"exampleFix":"// before\n$creds = new ServiceAccountJwtAccessCredentials($json, 'https://www.googleapis.com/auth/cloud-platform');\n$creds->fetchAuthToken('https://iap.googleapis.com/...'); // both set -> throws\n// after\n$creds = new ServiceAccountJwtAccessCredentials($json, null);\n$creds->fetchAuthToken('https://iap.googleapis.com/...'); // audience only","handlingStrategy":"validation","validationCode":"if ($audience !== null && $audience !== '' && $scope !== null && $scope !== '') {\n    throw new InvalidArgumentException('Pass either scope or audience to JwtAccess, not both');\n}","typeGuard":"function isExclusiveOption(?string $scope, ?string $audience): bool {\n    return ($scope === null || $scope === '') !== ($audience === null || $audience === '');\n}","tryCatchPattern":"try {\n    $token = $credentials->fetchAuthToken($audience);\n} catch (\\UnexpectedValueException $e) {\n    // fall back: rebuild credentials with only one of scope/audience\n    $credentials = new ServiceAccountJwtAccessCredentials($json, $audience !== null ? null : $scope);\n    $token = $credentials->fetchAuthToken($audience);\n}","preventionTips":["Decide per-use-case whether you need scope-based OAuth tokens or audience-signed JWTs before constructing credentials.","Never pass a scope when constructing credentials intended for self-signed JWT / ID-token flows.","Audit wrappers that pass both the constructor scope and a fetchAuthToken audience argument."],"tags":["php","google-auth","jwt","configuration"],"backgroundTag":"mutually-exclusive-options","analyzedSha":"48ea8c33227277e916cdd4079dbecef448b37f2c","analyzedAt":"2026-09-14T20:34:48.527Z","contentChangedAt":"2026-09-14T20:34:48.527Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}