This process is for testing only. For production applications, deploy a JSON Web Key Set (JWKS) to a well-known endpoint on your domain.
Prerequisites
You will need the following:
- An RSA private key/public key pair
- Postman or Bruno
- A free GitHub account to use GitHub Gist
- A test Brightspace instance where you have a role with the following permissions:
- Manage Extensibility > Can Manage API Applications
- Service Users
- See Service Users Tool
- Create a Service User
- Modify Service User Permissions
- Org Unit Editor > Add/edit new and existing Org Unit Types
- Tool Management > See Org Tool Management
- Brightspace Apps
- View Brightspace Apps
- Create and Manage Brightspace Apps
Steps
- In GitHub Gist, create a public Gist to store your JSON Web Key Set (JWKS):
- Type a description for your Gist. For example, jwks.json.
- Paste the following snippet, replacing
ANY_VALUE with any value that's unique in the key set and YOUR_PUBLIC_KEY with your public key.
{ "keys": [ { "kty": "RSA", "use": "sig", "alg": "RS256", "kid": "ANY_VALUE", "n": "YOUR_PUBLIC_KEY", "e": "AQAB" } ]} - Click Create secret gist.
- Click Raw.
- Copy the URL in the browser address bar.
- Create a service user and grant them permissions:
- From Admin Tools, click Manage Extensibility.
- Under Service Users, click Create Service User.
- In the Create Service User dialog, type a name for the service user and then click Create.
- If your service user needs access to org units other than the root organization, do the following:
- Under Org Unit Access, click Add.
- Select org units and then click Add Org Unit. If your service user needs access to all org units, click ... (the More menu), click Include Descendants, and then click Yes.
- Under Permissions, click Define Permissions.
- Find and select the appropriate permissions.
- Click Save.
- Register your application with Brightspace:
- From Admin Tools, click Manage Extensibility.
- Click the OAuth 2.0 tab to view the list of registered applications.
- Click Register an app. The Register an Application page opens.
- Under Grant Type, select Client Credentials.
- In Application Name, enter a clear name that identifies your application.
- In the JWKS URL field, paste the URL of your Gist.
- In Scope, enter the OAuth 2.0 scope or scopes (separated by spaces) that the application can request.
- Under Service User, click Add Service User.
- In the Add Service User dialog, select the service user that you created, and then click Add.
- Review and accept the API Developer Agreement.
- Click Register. The client ID that you will use to request a token is displayed.
- Follow the procedure for Postman or Bruno to send a request in the platform that you’re using.
Send a request in Postman
Postman does not save changes automatically by default. Turn on Autosave or press Ctrl+S to save the changes you make in the following procedure.
- Create a collection for your request. In the left pane, click +, click Collection, and then type a name for the collection. For more information, refer to Create collections of API requests in Postman | Postman Docs.
- Define the following variables. In the collection, click the Variables tab and then type the variable names and values. For more information, refer to Store and reuse values using variables | Postman Docs.
Variable | Value |
|---|
access_token
| Leave blank, the script will add the value. |
BrightspaceDomain
| The base URL of your test Brightspace instance. Include https:// in your BrightspaceDomain value. For example, https://example.brightspace.com. |
client_assertion
| Leave blank, the script will add the value. |
client_id
| The client ID from your application registration in Brightspace. |
jsrsasign
| The code at this website
https://kjur.github.io/jsrsasign/jsrsasign-all-min.js
. Note: This is a workaround to load the jsrsasign library into Postman Sandbox. |
kid
| The key ID (kid) value that you used in your JWKS in GitHub Gist. |
private_key
| The private key from your RSA private/public key pair. |
scope
| The OAuth 2.0 scope or scopes (separated by spaces) that the application can request. |
version
| Depending on the endpoint, the LE or LP version to call. See What's in the Brightspace developer platform? for the current values. |
3. In your collection, create a POST request to https://dev-auth.brightspace.com/core/connect/token. (Production instances use https://auth.brightspace.com/core/connect/token. This test procedure is only suitable for test instances.)
4. On the Scripts tab, configure a Before request script with the following code:
var navigator = {}; // fake a navigator object for the lib
var window = {}; // fake a window object for the lib
eval(pm.collectionVariables.get("jsrsasign")); // import javascript jsrsasign
var client_id = pm.collectionVariables.get("client_id");
var kid = pm.collectionVariables.get("kid");
var currentTime = +new Date(); // the current time in milliseconds
var issuedAtTimeSeconds = currentTime/1000; // the current time in seconds
var expirationTimeSeconds = currentTime/1000 + 3600; // the token expires in an hour
// Create Header and Payload objects
var header = {
'typ': 'JWT',
'alg': 'RS256',
'kid': kid
};
var payload = {
'iss': client_id,
'sub': client_id,
'aud': 'https://api.brightspace.com/auth/token',
"exp" : Math.ceil(expirationTimeSeconds),
"iat" : Math.ceil(issuedAtTimeSeconds),
};
// Prep the objects for a JWT
var sHeader = JSON.stringify(header);
var sPayload = JSON.stringify(payload);
// Prep the private key for signing the JWT
var jwt_key = pm.collectionVariables.get('private_key');
var prvKey = KEYUTIL.getKey(jwt_key);
var sJWT = KJUR.jws.JWS.sign(null, sHeader, sPayload, prvKey);
pm.collectionVariables.set("client_assertion", sJWT);
5. Configure an After response script with the following code:
if (pm.response.code == 200) {
var jsonData = JSON.parse(pm.response.text());
pm.collectionVariables.set("access_token", jsonData.access_token);
}
6. On your request, on the Headers tab, add a Content-Type header with a value of application/x-www-form-urlencoded.
7. On your request, on the Body tab, choose x-www-form-urlencoded and then configure the following key-value pairs:
Key | Value |
|---|
grant_type
| client_credentials
|
client_id
| {{client_id}}
|
client_assertion_type
| urn:ietf:params:oauth:client-assertion-type:jwt-bearer
|
client_assertion
| {{client_assertion}}
|
scope
| {{scope}}
|
8. Click Send. If it’s successful, the response is 200 OK and a body similar to the following example. The After response script saves the token to the access_token collection variable.
9. Create a new request in your collection. This request is to the endpoint your application will call. For example, a GET request to {{BrightspaceDomain}}/d2l/api/lp/{{version}}/users/whoami.
10. On the Headers tab, add an Authorization header with a value of Bearer {{accesstoken}}.
11. On the Authorization tab, make sure that No Auth is selected as the Auth type.
12. Click Send.
Send a request in Bruno
- Create a collection for your request. In the left pane, click +, click Create Collection, and then type a name for the collection. For more information, refer to Create a Collection - Bruno Docs.
- Define the following Pre-request variables. In the collection, click the Vars tab and then type the variable names and values. For more information, refer to Collection Variables - Bruno Docs.
Variable | Value |
|---|
BrightspaceDomain
| The base URL of your test Brightspace instance. Include https:// in your BrightspaceDomain value. For example, https://example.brightspace.com. |
client_id
| The client ID from your application registration in Brightspace. |
kid
| The key ID (kid) value that you used in your JWKS in GitHub Gist. |
private_key
| The private key from your RSA private/public key pair. |
scope
| The OAuth 2.0 scope or scopes (separated by spaces) that the application can request. |
version
| Depending on the endpoint, the LE or LP version to call. See What's in the Brightspace developer platform? for the current values. |
3. In your collection, create a POST request to https://dev-auth.brightspace.com/core/connect/token. (Production instances use https://auth.brightspace.com/core/connect/token. This test procedure is only suitable for test instances.) For more information, refer to Creating a Request.
4. On the Script tab, configure a Pre Request script with the following code:
Note: Bruno doesn’t provide a way to set a collection variable programmatically, so these scripts use global environment variables for the output variables.
const jwt = require("jsonwebtoken"); //import jsonwebtoken
const currentTime = +new Date(); // the current time in milliseconds
const issuedAtTimeSeconds = currentTime/1000; // the current time in seconds
const expirationTimeSeconds = currentTime/1000 + 3600; // the expiry time in 1 hour
const client_id = bru.getCollectionVar("client_id");
const payload = {
'iss': client_id,
'sub': client_id,
'aud': 'https://api.brightspace.com/auth/token',
"exp" : Math.ceil(expirationTimeSeconds),
"iat" : Math.ceil(issuedAtTimeSeconds),
};
// Prep the objects for a JWT
const sPayload = JSON.stringify(payload);
// Prep the private key for signing the JWT
const prvKey = bru.getCollectionVar("private_key");
const kid = bru.getCollectionVar("kid");
const options = {
algorithm: "RS256",
header: {
typ: "JWT",
alg: "RS256",
kid: kid
}
};
const ePayload = jwt.sign(sPayload, prvKey, options);
bru.setGlobalEnvVar("client_assertion", ePayload);
5. Configure a Post Response script with the following code:
if (res.status == 200) {
bru.setGlobalEnvVar("access_token", res.body.access_token);
}
6. On your request, on the Headers tab, add a Content-Type header with a value of application/x-www-form-urlencoded.
7. On your request, on the Body tab, choose Form URL Encoded and then configure the following key-value pairs:
Key | Value |
|---|
grant_type
| client_credentials
|
client_id
| {{client_id}}
|
client_assertion_type
| urn:ietf:params:oauth:client-assertion-type:jwt-bearer
|
client_assertion
| {{client_assertion}}
|
scope
| {{scope}}
|
8. Send the request. If it’s successful, the response is 200 OK and a body similar to the following example. The After response script saves the token to the access_token variable.
{
"access_token": "TOKEN",
"scope": "users:profile:read users:userdata:read",
"expires_in": 3600,
"token_type": "Bearer"
}
9. Create a new HTTP request in your collection. This request is to the endpoint that your application will call. For example, a GET request to {{BrightspaceDomain}}/d2l/api/lp/{{version}}/users/whoami.
10. On the Headers tab, add an Authorization header with a value of Bearer {{access_token}}.
11. On the Auth tab, choose OAuth 2.0 as the Auth type.
12. Enter the following values:
Field | Value |
|---|
Access Token URL | https://dev-auth.brightspace.com/core/connect/token
|
Client ID | {{client_id}}
|
Scope | {{scope}}
|
Add Credentials to | Basic Auth Header |
Token Source | Access Token |
Add token to: | Headers |
Header Prefix | Bearer
|
13. Click Send.