Skip to content

Commit 280d0bb

Browse files
isemonaisemona
andauthored
Improved duplicate user code (#1427)
Co-authored-by: isemona <[email protected]>
1 parent 1db1982 commit 280d0bb

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

_source/_posts/2023-07-28-scim-workshop.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ Let's update the users in `seed_script.ts`. We'll also need to hardcode `externa
129129

130130
We'll also give each org an `apikey` set to a random string. Using a different key for each org helps our code ensure that no client can accidentally view or edit users belonging to another.
131131

132-
After those changes, here's how `seed.ts` will look:
132+
After those changes, here's how `seed_script.ts` will look:
133133

134134
```ts
135135
async function main() {
@@ -257,6 +257,8 @@ Finally, we are ready to get started! Let's work on our first CRUD endpoint.
257257

258258
Let's refer to the [SCIM spec](https://datatracker.ietf.org/doc/html/rfc7644#section-3.3) on creating a user. The spec says the IdP sends a POST request containing a "User" to the `/Users` endpoint to create a user. In response to the POST, the server signals a successful creation with an HTTP status code 201 (Created) and returns a representation of the user created. If the server determines that the creation of the requested user conflicts with existing users (e.g., a "User" resource with a duplicate "userName"), the server MUST return HTTP status code 409 (Conflict) with a "scimType" error code of "uniqueness," as per Section 3.12.
259259

260+
Our SCIM Server will define a user's uniqueness by their externalId and orgId. You can see how this is done below when we check for a duplicate user.
261+
260262
Our `/Users` endpoint can fulfill those requirements with the following code:
261263

262264
```ts
@@ -283,14 +285,15 @@ scimRoute.post('/Users', passport.authenticate('bearer'), async (req, res) => {
283285
// Set displayName to name
284286
const name = displayName;
285287
// Check if the User exists in the database
288+
// externalId + orgId = user uniqueness per SCIM RFC Section 3.3
286289
const duplicateUser = await prisma.user.findFirst({
287290
select: {
288291
id: true,
289292
email: true,
290293
name: true,
291294
},
292295
where: {
293-
id,
296+
externalId,
294297
org: { id: ORG_ID }
295298
}
296299
});
@@ -410,7 +413,7 @@ app.use(bodyParser.json({ type: ['application/scim+json'] }));
410413
411414
Sign up for [Postman](https://identity.getpostman.com/login) or sign in to your account, and configure it to communicate with your local instance of the Todo app.
412415
413-
In Postman, the request URL will be`http://localhost:3333/scim/v2/Users` if you're running the Todo app locally. In the Headers tab, add the key `Content-Type` and set its value to `application/scim+json`, and then add an additional key, `Authorization`, and set it to `Bearer 131313`.This bearer token value comes from the `apikey` variable set earlier in `seed.ts`.
416+
In Postman, the request URL will be`http://localhost:3333/scim/v2/Users` if you're running the Todo app locally. In the Headers tab, add the key `Content-Type` and set its value to `application/scim+json`, and then add an additional key, `Authorization`, and set it to `Bearer 131313`.This bearer token value comes from the `apikey` variable set earlier in `seed_script.ts`.
414417

415418
Now we are ready to test with Postman with our local server. You can also make cURL requests directly from the terminal if you prefer.
416419

@@ -690,7 +693,7 @@ The result lists all users in the database.
690693

691694
Let's look up whether any users in the Todo app have the email address `[email protected]`. Try sending a GET request with no body to `http://localhost:3333/scim/v2/Users?filter=userName eq "[email protected]"&startIndex=1&count=100` What result do you expect?
692695

693-
If you seeded your database with `seed.ts`, the result will look like this.
696+
If you seeded your database with `seed_script.ts`, the result will look like this.
694697

695698
```json
696699
{
@@ -1063,7 +1066,7 @@ In the Sign-On Options tab of the SCIM Test App, give the app a helpful name in
10631066

10641067
In the Sign-On Options dialogue, keep the default settings, as these won't be used by our app. Click the blue "Done" button at the bottom of the page.
10651068

1066-
In the Provisioning tab of the application, click the Configure API Integration button, check the Enable API Integration box. Provide the Base URL, which is the localtunnel URL with `/scim/v2` appended to the end. The API Token is `Bearer 131313` if you're using the values seeded by `seed.ts`. Save these settings.
1069+
In the Provisioning tab of the application, click the Configure API Integration button, check the Enable API Integration box. Provide the Base URL, which is the localtunnel URL with `/scim/v2` appended to the end. The API Token is `Bearer 131313` if you're using the values seeded by `seed_script.ts`. Save these settings.
10671070

10681071
When you save these settings or use the "Test API Credentials" button, Okta will make a `GET /Users` request with the API token you've provided in order to establish a connection with your SCIM server.
10691072

0 commit comments

Comments
 (0)