I am looking for a way to get the access token using password grant-type in an organization context.
I know that Authorization code flow is the choice to get it. But I need it for test automation to be able to get token without user’s involvement. And it has to be in org context so that JWT token can have org related information.
Unfortunately our Organization feature does not currently support the Resource Owner Password grant type. This is listed in the limitations section of our Organizations documentation linked below.
In the context of performing automated tests in the context of organizations, it can be a difficult task since the usual ROPG grant flow cannot be enabled.
However, this can be resolved by using a Custom Token Exchange Trigger in which you will be able to set the organization as seen in this example:
exports.onExecuteCustomTokenExchange = async (event, api) => {
// 1. Validate subject_token
const subject_token = await validateToken(event.transaction.subject_token, jwksUri);
// 2. Apply your authorization policy on the user
const isAuthorized = await authorizeAccess(subject_token.sub);
if (!isAuthorized) {
api.access.deny('Unauthorized_login', 'User cannot login due to reason: X');
}
// 3. Set the user for the transaction
api.authentication.setOrginzation('org_xS525r979AS33MSf');
// 4. Set the user for the transaction. You may also use setUserByConnection()
api.authentication.setUserById(subject_token.sub);
return;
};
Hope this helps anybody who stumbles across the same issue!