📔
ServerlessDocs - Serverless File Service
  • Introduction
  • Why Serverless!!
  • Prerequisites
  • User Management
    • Authentication
    • Amazon Cognito
    • Create the User Pool
    • Create an App Client
    • Integrate the app with Cognito User Pool
    • Create the Identity Pool
    • Validate the setup
    • Troubleshooting
  • Manage Documents with AWS S3
    • Authorization
    • Update Cognito Identity Pool Auth Role
    • S3 Documents Bucket Folder Structure
    • Validate the Access Permissions
    • Public and Private files access
    • Upload Files to S3
    • Download Files
    • Delete Files
    • Share Files
  • More Security Configurations
    • Cognito user emails
    • Password policies
    • Allow only Organization and Whitelisted users to Signup
    • Allow admin users to upload Public files
    • Notify Admin users when a new user signup
    • Enable Multi-factor Authentication (MFA)
  • User Operations
    • Password Reset / Forgot Password
    • Resend Verification Code
  • Serverless APIs
    • APIs
    • Secure APIs using API Gateway Authorizer
    • Access Cognito values in Lambda function
    • Authorize APIs with OAuth 2 Scope
    • Fine-grained Access with AssumeRole
    • Notify Admin users on large file uploads
    • Generate a Month-To-Date Usage report
  • Source Code and Setup
    • Source Code
    • IAM Policies
    • S3 Bucket Policies
    • Suggestions / Feedback
    • More References
  • Deployment
    • AWS SAM
    • Serverless Framework
  • Contributors
    • Team
Powered by GitBook
On this page

Was this helpful?

  1. Serverless APIs

Access Cognito values in Lambda function

JWT token that is part of the Authorization header is accessible inside the Lambda functions as well. In the below example, I am decoding the JWT token value and checking the values inside it.

This is only possible as we are using the id_token along with access_token value. ID Tokens follow the JWT structure so it is possible to decode and check the values. Access Tokens are not bound to follow the JWT structure and assume to be opaque.

const res = require('./common/res');
const jwtDecode = require('jwt-decode');

const getToken = (header) => {
    return jwtDecode(new Buffer(header.split(" ")[0]).toString());
}

module.exports.handler = async event => {
    console.log(`Cognito User`, event);
    console.log(getToken(event.headers.Authorization));

    return res({body: {
        message: 'This is a test profile message',
        source: event.cognitoPoolClaims // Cognito Pool Claims
    }})
}

/** 
JWT Payload Data

{
  sub: 'b9f37dec-b5bf-4eb1-989f-ae7026715c3d',
  'cognito:groups': [ 'admins' ],
  email_verified: true,
  'cognito:preferred_role': 'arn:aws:iam::XXXXXXXXXX:role/admin-users-serverless-docs',
  iss: 'https://cognito-idp.ap-southeast-1.amazonaws.com/ap-southeast-1_lBwBP9VEb',
  'cognito:username': 'dhaval_admin',
  'cognito:roles': [ 'arn:aws:iam::XXXXXXXXXX:role/admin-users-serverless-docs' ],
  aud: '7fdcfo50mhcqm5ui0pl0lrc88f',
  event_id: '8f233e4a-cd77-4792-89cd-4a29148ecdc2',
  token_use: 'id',
  auth_time: 1595657572,
  exp: 1595661172,
  iat: 1595657572,
  email: 'dhaval@appgambit.com'
}
**/

PreviousSecure APIs using API Gateway AuthorizerNextAuthorize APIs with OAuth 2 Scope

Last updated 4 years ago

Was this helpful?