-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement API auth route handlers, tests, and configuration
Co-Authored-By: Brendan Quinn <[email protected]>
- Loading branch information
Showing
12 changed files
with
379 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
const axios = require("axios").default; | ||
const cookie = require("cookie"); | ||
const jwt = require("jsonwebtoken"); | ||
|
||
/** | ||
* NUSSO auth callback | ||
*/ | ||
exports.handler = async (event) => { | ||
const returnPath = cookie.parse(event.headers.Cookie, { | ||
decode: function (token) { | ||
return Buffer.from(token, "base64").toString("utf8"); | ||
}, | ||
})?.redirectUrl; | ||
|
||
const user = await redeemSsoToken(event); | ||
let response; | ||
if (user) { | ||
const token = jwt.sign(user, process.env.API_TOKEN_SECRET); | ||
response = { | ||
statusCode: 302, | ||
headers: { | ||
location: returnPath, | ||
"set-cookie": cookie.serialize("dcApiV2Token", token, { | ||
domain: "library.northwestern.edu", | ||
path: "/", | ||
secure: true, | ||
}), | ||
}, | ||
}; | ||
} | ||
|
||
return response; | ||
}; | ||
|
||
async function redeemSsoToken(event) { | ||
const cookies = cookie.parse(event.headers.Cookie); | ||
|
||
if (cookies.nusso) { | ||
try { | ||
const response = await axios.get( | ||
`${process.env.NUSSO_BASE_URL}validate-with-directory-search-response`, | ||
{ | ||
headers: { | ||
apikey: process.env.NUSSO_API_KEY, | ||
webssotoken: cookies.nusso, | ||
}, | ||
} | ||
); | ||
const user = response.data.results[0]; | ||
return user; | ||
} catch (err) { | ||
console.error(err); | ||
return null; | ||
} | ||
} else { | ||
console.warn("No NUSSO token found in request"); | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
const { dcApiEndpoint } = require("../aws/environment"); | ||
const axios = require("axios").default; | ||
const cookie = require("cookie"); | ||
|
||
/** | ||
* Performs NUSSO login | ||
*/ | ||
exports.handler = async (event) => { | ||
const callbackUrl = `${dcApiEndpoint()}/auth/callback`; | ||
const url = `${process.env.NUSSO_BASE_URL}get-ldap-redirect-url`; | ||
const returnPath = | ||
event.queryStringParameters?.goto || event.headers?.Referer; | ||
|
||
if (!returnPath) { | ||
return { | ||
statusCode: 400, | ||
}; | ||
} | ||
|
||
let resp; | ||
|
||
await axios | ||
.get(url, { | ||
headers: { | ||
apikey: process.env.NUSSO_API_KEY, | ||
goto: callbackUrl, | ||
}, | ||
}) | ||
.then((response) => { | ||
resp = { | ||
statusCode: 302, | ||
headers: { | ||
location: response.data.redirecturl, | ||
"set-cookie": cookie.serialize("redirectUrl", returnPath, { | ||
encode: function (token) { | ||
return Buffer.from(token, "utf8").toString("base64"); | ||
}, | ||
}), | ||
}, | ||
}; | ||
}) | ||
.catch((error) => { | ||
console.error("NUSSO request error", error); | ||
resp = { | ||
statusCode: 401, | ||
}; | ||
}); | ||
|
||
return resp; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
const cookie = require("cookie"); | ||
const jwt = require("jsonwebtoken"); | ||
|
||
/** | ||
* NUSSO whoami - validates JWT and returns user info | ||
*/ | ||
exports.handler = async (event) => { | ||
try { | ||
const token = cookie.parse(event.headers.Cookie)?.dcApiV2Token; | ||
const user = jwt.verify(token, process.env.API_TOKEN_SECRET); | ||
|
||
return { | ||
statusCode: 200, | ||
headers: { | ||
"Access-Control-Allow-Origin": event.headers.Origin, | ||
"Access-Control-Allow-Headers": "*", | ||
"Access-Control-Allow-Methods": "POST, GET, OPTIONS", | ||
"Access-Control-Allow-Credentials": "true", | ||
}, | ||
body: JSON.stringify(user), | ||
}; | ||
} catch (error) { | ||
return { | ||
statusCode: 401, | ||
body: "Error verifying API token: " + error.message, | ||
}; | ||
} | ||
}; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.