Skip to main content

Application Authentication

What this step is for​

Teaches Canvas how to sign in to the application on the user's behalf.

You write up to three small scripts:

  1. The Authentication script says what to send to the application to sign in (the address, the method, the data). By default Canvas sends the request for you; if you prefer, the script can make the calls itself with api.send.
  2. The Post Handler looks at what came back and saves what you need later, usually the access token.
  3. For the Callback type only: the Callback request script builds the address of the application's approval page.

Triggered by:

  • Application registration (when a user connects the application).

  • Renewal, when no custom refresh script is set on Re-Authentication. Then the sign-in is simply repeated with the Authentication script.

Renewal settings (when and how the sign-in is repeated) are on the next step, Re-Authentication.

What you see on the page​

The Configuration tab has two panels:

PanelWhat it contains
AuthenticationThe sign-in type and the script editor(s).
Post HandlerThe editor of the Post Handler.

The Test tab shows the generated test script (read only). The right side of the page shows the Preview with the credentials form from Credentials Configuration. See Testing.

Choose the sign-in type​

The dropdown at the top of the Authentication panel:

TypeUse it whenWhat happens
Direct requestThe application signs you in with a request: user name and password, an API key, client ID and secret.Canvas runs the Authentication script and sends the request that it returns.
CallbackThe application asks the user to approve access on its own web page ("Allow Canvas to access your account").Two scripts: the Callback request script builds the address of the approval page and the user is sent there. After the approval the application sends the user back to Canvas, and the Token request script (the Authentication script) runs with everything the application returned in configOauth. It usually exchanges the returned code for tokens.

A third item, Two Steps, is disabled for the applications built in the wizard.

What you can use in the scripts​

NameAuthentication scriptPost HandlerMeaning
configYesYesThe answers the user gave on the Credentials Configuration step: config.username, config.apikey, ... Available only in this step (the Authentication script, the Post Handler and the Callback request script). If a later step needs a value, save it to appVault or appVaultSecret.
configOauthYesYesFor Callback: what the application sent back after the approval, see below. null for Direct request. Available only in this step.
responseNoYesThe answer of the application to the request that Canvas sent. Available only in the Post Handler, see The Post Handler. In the Authentication script you get the answer from api.send.
appVaultYesYesThe settings storage of the application. Anything you save here stays for the next steps. Do not keep secrets here.
appVaultSecretYesYesThe secret storage (tokens, passwords, keys). It is stored encrypted.
utcLastAuth, utcNextAuthYesYesWhen the last sign-in happened and when the next one is due, as UTC date and time in ISO format (2026-11-12T10:30:00.000Z) or null. Used by scheduled renewal, see Re-Authentication.
apiYesYesThe api.send helper to call the application.

config, configOauth and response are constants: you can read them but cannot assign them.

The vaults. On the very first sign-in appVault and appVaultSecret do not exist yet, so create them before you write to them. Assign them directly (appVault = {}), do not declare them again with let or const, otherwise the changes are lost.

if (!appVault) appVault = {};
if (!appVaultSecret) appVaultSecret = {};

The vaults are saved only when all scripts of the sign-in finish without an error. If the Post Handler throws, the changes of the Authentication script are discarded too.

The Authentication script​

Two ways to write the script​

1. Return the request (the usual way). The script describes the request and Canvas sends it. Then Canvas runs the Post Handler with the answer.

2. Do it yourself. The script calls the application with await api.send(...), saves the result to the vault and returns nothing. Canvas then sends no request and does not run the Post Handler. Use it when the sign-in needs several calls, or when you must prepare data before the main request.

Use way 1 when the application requires a client certificate (mTLS): Canvas adds the certificates of the connected application to the request that it sends, but not to the calls made with api.send.

The request to return​

The script must return an object with at least verb and baseUrl:

FieldRequiredMeaning
verbYesThe method: GET, POST, PUT, PATCH, DELETE.
baseUrlYesThe address of the application's API, for example https://api.example.com.
requestUrlNoThe path added to the address, for example oauth/token.
headersNoExtra request headers.
paramsNoQuery parameters, an object: { scope: 'read' }.
dataNoThe body of the request. An object or an array is sent as JSON. A string is sent as it is.
bodyContentTypeNoThe content type of the body, for example application/x-www-form-urlencoded. Set it when data is a string.

If the script returns an object without verb or baseUrl, the sign-in fails with [JS_AUTH_ERROR] Your script doesn't produce a JSON object with obligatory 'verb'/'baseUrl' field(s). If the script returns nothing, Canvas sends no request and raises no error (way 2 above).

Older scripts may use resource, httpMethod, body (a JSON string) and parameters. They are still accepted and mean requestUrl, verb, data and params. Write new scripts with the new names. url is not recognized.

Example: sign in with a user name and password. The body is a form, so it is a string with its content type.

return {
verb: 'POST',
baseUrl: 'https://api.example.com',
requestUrl: 'oauth/token',
bodyContentType: 'application/x-www-form-urlencoded',
data: new URLSearchParams({
grant_type: 'password',
username: config.username,
password: config.password
}).toString()
};

Example: take the address and keys from the answers of the user, and keep them for the next steps.

if (!appVault) appVault = {};
if (!appVaultSecret) appVaultSecret = {};

appVault.baseUrl = `https://${config.environment}.example.com/api`;
appVault.consumer = config.consumer; // not a secret
appVaultSecret.secretKey = config.secretKey; // secret

return {
verb: 'GET',
baseUrl: appVault.baseUrl,
requestUrl: 'Authenticate',
headers: { 'Consumer': appVault.consumer, 'SecretKey': appVaultSecret.secretKey }
};

Example: do it yourself with api.send. No request is returned, so no Post Handler is needed.

if (!appVault) appVault = {};
if (!appVaultSecret) appVaultSecret = {};
appVault.baseUrl = 'https://api.example.com';

const response = await api.send({
verb: 'POST',
baseUrl: appVault.baseUrl,
requestUrl: 'login',
data: { user: config.username, password: config.password },
getFullResponse: true
});

if (response.statusCode !== 200 || !response.content) {
throw new Error(`Sign-in failed: ${response.statusCode}`);
}
appVaultSecret.access_token = response.content.access_token;

For everything api.send accepts and returns, see api.send.

What is in configOauth​

For the Callback type, configOauth contains:

  • every query parameter that the application sent back to Canvas, for example code;
  • state: the Canvas identifier of the application (not the value that the application received);
  • canvasCallBackURL: only the path of the Canvas callback endpoint, /api/regapps/callback, not the full address (in the Callback request script the same name holds the full, URL encoded address).

The callback data is saved (encrypted) with the connected application, so when the sign-in is repeated later without a custom refresh script, the same configOauth is given to the script again.

The Callback request script​

Shown for the Callback type as Callback request configuration. It builds the address of the approval page of the application. It runs when the user presses the button to connect, and the user is sent to the address it builds.

It must return the address as a GET request:

return {
verb: 'GET',
baseUrl: 'https://auth.example.com',
requestUrl: 'authorize',
params: {
client_id: config.clientId,
response_type: 'code',
redirect_uri: configOauth.canvasCallBackURL,
state: configOauth.state
}
};

In this script:

  • config has the answers of the user and configOauth has two values: state and canvasCallBackURL. Pass both to the application: the application must return state unchanged to Canvas, and canvasCallBackURL is already URL encoded and is the address where the application sends the user back.
  • The values of params are put into the address as they are, so encode them yourself with encodeURIComponent when needed.
  • Only GET is allowed.
  • api, appVault and appVaultSecret are not available here.

The Post Handler​

It runs right after the application answers the request returned by the Authentication script. It also has config, configOauth, appVault, appVaultSecret, utcLastAuth, utcNextAuth and api. Use it to:

  • check that the sign-in worked;
  • save the result: the tokens and anything else the next steps need;
  • optionally set when the next sign-in should happen (utcNextAuth, see Re-Authentication).

In the Post Handler you can read the answer in response. response exists only in the Post Handler: the Authentication script does not have it.

Part of responseMeaning
response.statusCodeThe status of the answer, for example 200 for success.
response.statusTextThe status text, for example OK.
response.contentThe data returned by the application (JSON is already converted to an object).
response.headersThe headers of the answer (names in lower case).
response.cookiesThe cookies of the answer.
response.responseUrlThe address that answered.

Canvas does not decide whether the sign-in was successful. If the application answers with an error, throw an error in your script, otherwise Canvas will treat the sign-in as successful.

Example:

if (response.statusCode == 200 && !!response.content) {
appVaultSecret.access_token = response.content.access_token;
appVaultSecret.refresh_token = response.content.refresh_token;
} else {
throw new Error('Sign-in failed: ' + response.statusCode);
}

The same Post Handler is used for the first sign-in and for every renewal, so write it to work for both.

Never save secrets (tokens, passwords, API keys) in appVault. appVault can be read from workflow scripts. Always use appVaultSecret for sensitive data.

When things run​

What happensWhat Canvas does
A user registers the applicationAuthentication script, then sends the returned request, then the Post Handler. Then the Authorization script.
Renewal without a custom refresh scriptThe same as the registration, with the existing vaults. See Re-Authentication.

If a script throws an error or returns a wrong result, the sign-in fails, nothing is saved, and the message starts with the script that failed: [JS_AUTH_ERROR] (Authentication script) or [JS_AUTH_POST_HANDLER_ERROR] (Post Handler).

When a sign-in or renewal works, the new tokens are used for all following calls, see Authorization.

Testing​

The Test tab shows the script that the wizard runs when you test: your Authentication script, the Post Handler and the code that connects them (it sends the returned request with api.send and passes the answer to the Post Handler). You cannot edit it there.

  1. Fill in the credentials form in the Preview on the right (required fields must be filled).
  2. Press Authenticate to run it, or Debug to run it with a debugger; statement, so that the browser's developer tools stop in the script when they are open.
  3. For the Callback type the browser is sent to the application's approval page and back, then the test continues. The Callback request script is run for this.

The test runs in your browser. It does not save anything to a connected application, and it has no utcLastAuth and utcNextAuth. A non-2xx answer to api.send throws an error in the test unless you use getFullResponse: true, see api.send.

The resulting appVault and appVaultSecret are kept for the previews of the next steps (Re-Authentication and Authorization), where you can check that the tokens are saved where you expect them.