Skip to content

Commit

Permalink
Merge pull request #242 from nulib/5148-use-netid-email-when-blank
Browse files Browse the repository at this point in the history
Use netid-derived name & email if directory response succeeds but is empty
  • Loading branch information
mbklein authored Aug 12, 2024
2 parents 3f4d030 + 85aea16 commit bf1cfd0
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 10 deletions.
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

0 comments on commit bf1cfd0

Please sign in to comment.