getTokenByPassword ignores realm: always sends grant_type=password instead of password-realm

Open Beginner friendly
#320 0 comments 2 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
82/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
typescript

Research direction

Read packages/auth0-auth-js/src/auth-client.ts and follow AuthClient.getTokenByPassword, including both the fullResponse and non-fullResponse branches. Check how realm and the grant type are passed to genericGrantRequest; done means requests with realm use the password-realm grant and requests without it retain the password grant. Add or update tests for both cases.

Written by the indexing model from the issue text.

Description

getTokenByPassword ignores realm: always sends grant_type=password instead of password-realm

Description

AuthClient.getTokenByPassword() accepts a realm option, but it always sends grant_type=password to /oauth/token, even when realm is set. Auth0 only honours realm with the http://auth0.com/oauth/grant-type/password-realm grant. With grant_type=password it ignores realm and authenticates against the tenant's Default Directory, so the requested connection is silently not used.

This contradicts the option's own documentation in TokenByPasswordOptions:

The realm to use for the authentication request. Specifies which database connection or identity provider to authenticate against when using the password-realm grant type. This is useful when your tenant has multiple database connections and you need to authenticate against a specific one instead of using the tenant's default directory.

Reproduction
import { AuthClient } from "@auth0/auth0-auth-js"

const authClient = new AuthClient({ domain, clientId, clientSecret })

await authClient.getTokenByPassword({
  username: "[email protected]",
  password: "********",
  audience: "https://api.example.com",
  realm: "My-Database-Connection", // not the tenant's Default Directory
})

Request body actually sent:

grant_type=password&username=...&password=...&audience=...&realm=My-Database-Connection
Expected

When realm is set:

grant_type=http://auth0.com/oauth/grant-type/password-realm&...&realm=My-Database-Connection

The user is then authenticated against My-Database-Connection.

Actual

Auth0 ignores realm and uses the tenant's Default Directory. When that connection isn't enabled for the application, the call fails with:

Connection must be enabled for this client to perform single user creation and signup operations (client_id: <client_id> - connection: <default directory connection>)

If the Default Directory is enabled, the user is silently authenticated against the wrong connection.

Cause

In packages/auth0-auth-js/src/auth-client.ts, getTokenByPassword appends realm to the params but hard-codes the grant type:

if (options.realm) {
  params.append('realm', options.realm);
}
// ...
const tokenEndpointResponse = await client.genericGrantRequest(captureConfig, 'password', params);

The same applies to the non-fullResponse branch.

Suggested fix

Pick the grant type from realm, as auth0@4 AuthenticationClient.oauth.passwordGrant did:

const grantType = options.realm
  ? 'http://auth0.com/oauth/grant-type/password-realm'
  : 'password';

const tokenEndpointResponse = await client.genericGrantRequest(captureConfig, grantType, params);

For reference, [email protected], which auth0@7 still bundles as auth0/legacy, in dist/cjs/auth/oauth.js:

grant(bodyParameters.realm ? 'http://auth0.com/oauth/grant-type/password-realm' : 'password', ...)
Impact

This is a regression for anyone following the authentication flows migration guide, which maps oauth.passwordGrant → authClient.getTokenByPassword(...). Code that passed realm to passwordGrant keeps compiling, but after migrating it authenticates against a different connection. It hits any tenant with more than one database connection.

Environment
  • @auth0/auth0-auth-js: 1.16.1 (also reproduced by reading the code of 1.17.0, 1.18.0 and main)
  • auth0: 7.4.0 (migrated from 5.12.0)
  • Node.js: 22
Dominant language
TypeScript
Stars
14
Forks
18
Avg merge
10m
Merged PRs (30d)
3

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.