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

feat(api): add support for oauth2 token endpoint #376

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

acwrenn
Copy link

@acwrenn acwrenn commented Feb 25, 2023

Cognito supports more features than just its AWS-API calls demonstrate. This service fake simulates just those AWS APIs - except for .well-known/jwks.json.

This PR starts to add support for the token endpoint outlined here: https://docs.aws.amazon.com/cognito/latest/developerguide/token-endpoint.html

However, since we are not serving static data, we need to create a fake target, and then add this to our AWS API Router, so that we can get access to the services objects.

Please give some feedback on the "not real AWS target method" - it seemed the best way to get the shared service objects to the inner handler.

BUILD NOTES:

  1. I could not get the project to build with the newer aws-lamba types - I had to pin those to the lowest minor version originally allowed. Guidance on this would be helpful, I don't generally work on TS projects.
  2. I also could not get the docker image to build without the yarn lock being present - I assumed this was a mistake, as requiring a local yarn before being able to build the docker image with the containerized yarn seemed unnecessary - but hopefully can get some more feedback.

@acwrenn acwrenn force-pushed the add_oauth2_token_flow branch 2 times, most recently from bc4e4d5 to 5533218 Compare February 25, 2023 17:25
@acwrenn acwrenn marked this pull request as ready for review February 25, 2023 17:25
@acwrenn acwrenn force-pushed the add_oauth2_token_flow branch 4 times, most recently from c364b4e to 493572b Compare April 25, 2023 17:21
Comment on lines 63 to 90
let rawBody = "";
req.setEncoding("utf8");
req.on("data", function (chunk) {
rawBody += chunk;
});
req.on("end", function () {
const target = "GetToken";
const route = router(target);
const auth = req.get("Authorization");
if (auth && auth.startsWith("Basic ")) {
const sliced = auth.slice("Basic ".length);
const buff = new Buffer(sliced, "base64");
const decoded = buff.toString("ascii");
const creds = decoded.split(":");
if (creds.length == 2) {
const id = creds[0];
const secret = creds[1];
rawBody += `&client_id=${id}`;
rawBody += `&client_secret=${secret}`;
}
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bit gnarly. 😄

I'm assuming the POST body is form encoded? I'd suggest you either:

  1. Configure the urlencoded body parser and that'll make req.body be key-value pairs if the correct Content-Type is sent on the request, then you'll have a proper object to deal with instead of passing strings around.
  2. Or at least use the raw body parser which'll make req.body into a Buffer you can call toString on, instead of having to concat from a stream.

My preference would be for (1) and then also update the types of GetToken to something more specific too.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok - I adjusted a little, lifted the URL parsing up to the top here, brought it into its own type, then injected the Authorization headers into the new request type instead of smashing it onto the string there.

I might have gone a little overboard with the type things, but I dont write a ton of typescript so got to have a little fun with it :)

@jagregory
Copy link
Owner

I've left a comment on the implementation, but otherwise I think this looks ok. I don't mind it being a bit hacky for these non-API routes, at least while there's only a couple of them. Let me know what you think.

The yarn and docker issues are odd though. There's a yarn.lock in the repo, so the docker image should (and does in GitHub Actions) build fine.

acwrenn added 2 commits July 17, 2023 09:18
Cognito supports more features than just its AWS-API calls demonstrate.
This service fake simulates just those AWS APIs - except for
.well-known/jwks.json.

This PR starts to add support for the token endpoint outlined here:
https://docs.aws.amazon.com/cognito/latest/developerguide/token-endpoint.html

However, since we are not serving static data, we need to create a fake
target, and then add this to our AWS API Router, so that we can get
access to the services objects.
Before, only user creds were supported by the oauth2/token route.
@acwrenn acwrenn force-pushed the add_oauth2_token_flow branch from 493572b to f3adf76 Compare July 17, 2023 16:19
@CurlyDev1
Copy link

Hi, this would be really useful to have locally :) was there a reason the PR was not merged ?

@acwrenn
Copy link
Author

acwrenn commented Mar 7, 2024

We have been using a local fork, but I would like to move off that eventually. But had forgotten about this!

@mario-gazzara
Copy link

When will be merged? I need it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants