awslabs / cognito-at-edge

Serverless authentication solution to protect your website or Amplify application
Apache License 2.0
168 stars 54 forks source link

Make handler methods public #66

Closed vikas-reddy closed 11 months ago

vikas-reddy commented 1 year ago

What would you like to be added:

Rename handler methods to make them public. Method names starting with underscore _ are considered private by convention.

Even though these methods are accessible from outside now(Javascript language doesn't understand this _ prefix convention), we think it makes more sense for them to be explicitly public for our use case.

_fetchTokensFromRefreshToken -> fetchTokensFromRefreshToken
_fetchTokensFromCode -> fetchTokensFromCode
_getRedirectToCognitoUserPoolResponse -> getRedirectToCognitoUserPoolResponse

Why is this needed:

We are planning to use this library for our new authentication gateway application. As opposed to the intended use case of this library, which is to use the handle method to place static S3 files behind an authentication gate, we are planning to use the individual handler methods directly in our app. This auth gateway app will be a set of Lambda@Edge handlers that work as an intermediary between React frontend clients and AWS Cognito to do

  1. authentication duties,
  2. exchange code for tokens, and
  3. sending tokens as HttpOnly cookies, which clients can use to communicate with some Amazon internal API's

Handlers

  1. /signIn: Mapped to the existing method _getRedirectToCognitoUserPoolResponse
  2. /parseAuth: Mapped to existing method _fetchTokensFromCode
  3. /refreshToken: Mapped to existing method _fetchTokensFromRefreshToken

In our Cloudfront distribution setup, we'd do something like this (notice no underscore prefixes)

// signIn Lambda@Edge handler
const authenticator = new Authenticator({...})
exports.handler = async (request) => authenticator.getRedirectToCognitoUserPoolResponse(request, redirectUri)

// parseAuth Lambda@Edge handler
const authenticator = new Authenticator({...})
exports.handler = async (request) => authenticator.fetchTokensFromCode(redirectUri, code)

Slack or email me on vikred@amazon.com for additional details