Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use netid-derived name & email if directory response succeeds but is empty #242

Merged
merged 1 commit into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 23 additions & 8 deletions node/src/handlers/get-auth-callback.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ async function redeemSsoToken(event) {
},
}
);
return { ...response.data.results[0], uid: netid };
return fillInBlanks({ ...response.data.results[0], uid: netid });
} catch (err) {
if (
BAD_DIRECTORY_SEARCH_FAULT.test(err?.response?.data?.fault?.faultstring)
) {
return redeemForNetIdOnly(netid);
return fillInBlanks({ uid: netid });
}
await Honeybadger.notifyAsync(err, { tags: ["auth", "upstream"] });
console.error(err.response.data);
Expand All @@ -78,10 +78,25 @@ async function redeemSsoToken(event) {
}
}

function redeemForNetIdOnly(netid) {
return {
uid: netid,
displayName: [netid],
mail: `${netid}@e.northwestern.edu`,
};
function fillInBlanks(response) {
const { uid } = response;
response.displayName = ifEmpty(response.displayName, [uid]);
response.mail = ifEmpty(response.mail, `${uid}@e.northwestern.edu`);
return response;
}

function ifEmpty(val, replacement) {
return isEmpty(val) ? replacement : val;
}

function isEmpty(val) {
if (val === null || val === undefined) {
return true;
}

if (Array.isArray(val)) {
return val.every(isEmpty);
}

return val.length == 0;
}
1 change: 0 additions & 1 deletion node/src/handlers/post-chat-feedback.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ const sendNotification = async (snsClient, subject, message) => {
Message: message,
});
const response = await snsClient.send(command);
console.log(response);
return response;
};

Expand Down
34 changes: 33 additions & 1 deletion node/test/integration/get-auth-callback.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ describe("auth callback", function () {
nock(process.env.NUSSO_BASE_URL)
.get("/validate-with-directory-search-response")
.reply(200, {
results: [{ uid: "uid123", displayName: ["Some User"] }],
results: [
{ displayName: ["Some User"], mail: "some.user@example.com" },
],
});

const result = await getAuthCallbackHandler.handler(event);
Expand All @@ -45,6 +47,36 @@ describe("auth callback", function () {

expect(apiToken.token.sub).to.eq("uid123");
expect(apiToken.token.name).to.eq("Some User");
expect(apiToken.token.email).to.eq("some.user@example.com");
expect(apiToken.isLoggedIn()).to.be.true;
});

it("fills in the blanks if the directory search result is incomplete", async () => {
nock(process.env.NUSSO_BASE_URL)
.get("/validate-with-directory-search-response")
.reply(200, {
results: [{ displayName: [], mail: "" }],
});

const result = await getAuthCallbackHandler.handler(event);

expect(result.statusCode).to.eq(302);
expect(result.headers.location).to.eq("https://example.com");

expect(result.cookies).to.include(
"redirectUrl=null; Expires=Thu, 01 Jan 1970 00:00:00 GMT"
);

const dcApiCookie = helpers.cookieValue(
result.cookies,
process.env.API_TOKEN_NAME
);

const apiToken = new ApiToken(dcApiCookie.value);

expect(apiToken.token.sub).to.eq("uid123");
expect(apiToken.token.name).to.eq("uid123");
expect(apiToken.token.email).to.eq("uid123@e.northwestern.edu");
expect(apiToken.isLoggedIn()).to.be.true;
});

Expand Down
Loading