Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

876
Visualizações
How to verify accessToken in node/express using aws-amplify?

I am using AWS amplify for user authentication on my front-end React app. My React app directly communicates with amplify without any backend(node server) interaction.

I have a REST API written in node/express. I want to secure that API using amplify.

Currently, I am planning to pass the access token from my react app to my node server. But I am unable to find a way through which I can verify this token on the backend using amplify.

Does aws-amplify package provide any function in which I can pass the access token to verify it?

Something like Auth.verifyToken(<access_token>)

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

Unfortunately, there is no such method available in official aws-amplify SDK. After doing a lot of research I had to write my own middleware for that. This is not that difficult as it may seem but the only difficult part is to gather the right information from the huge AWS documentation.

I 've written this middleware to achieve the same, Hope this helps

import axios from 'axios'
import awsconfig from '../../aws-exports';

const COGNITO_URL = `https://cognito-idp.${awsconfig.aws_project_region}.amazonaws.com/`;

const authentication = async (req, res, next) => {
    try {
        const accessToken = req.headers.authorization.split(" ")[1];

        const { data } = await axios.post(
            COGNITO_URL,
            {
                AccessToken: accessToken
            },
            {
                headers: {
                    "Content-Type": "application/x-amz-json-1.1",
                    "X-Amz-Target": "AWSCognitoIdentityProviderService.GetUser"
                }
            }
        )

        req.user = data;
        next();
    } catch (error) {
        return res.status(401).json({
            message: 'Auth failed'
        });
    }
};

export default authentication;

This middleware takes the authorization header and verifies the incoming accessToken using AWS Cognito REST API.

In order to get accessToken on your front-end you can do something like this:

const { accessToken: { jwtToken } } = await Auth.currentSession();

This jwtToken is your accessToken you can send this in your Authorization header and then verify this in the backend using the middleware I've written.

over 4 years ago · Santiago Trujillo Relatório

0

AWS actually has this pretty well documented. I wrote a gist in a middleware that I wrote to validate AWS Cognito tokens on my express.js server.

Basically, when you create a user pool on Amazon, AWS creates a JSON Web Key (JWK). The JWT contains a public key that you can use to verify the signature of a JWT.

At a high level in Javascript:

 import jwt from "jsonwebtoken"; const authToken = getJwtFromHeader(request); // please remember to verify the "iss" claim and "exp" claim! validateAuthToken(authToken); // convert a jwk to a PEM for use by OpenSSL or crypto const jwk = getJwkFromAWS(); const pem = jwkToPem(jwk); // verify the signature of the token using the public key from AWS await jwt.verify(authToken, pem, {algorithms: ['RS256']}, (err, decoded) =>{ console.log('decoded', decoded); // TODO -- verify claims from decoded token });

My GIST for a full express.js implementation: https://gist.github.com/fourgates/92dc769468497863168417c3524e24dd

AWS Resources:

https://github.com/awslabs/aws-support-tools/tree/master/Cognito/decode-verify-jwt https://docs.aws.amazon.com/cognito/latest/developerguide/amazon-cognito-user -groups-using-tokens-verifying-a-jwt.html

over 4 years ago · Santiago Trujillo Relatório

0

Unfortunately, the aws-amplify SDK doesn't provide that functionality. For that reason, I've created an npm package to handle it.

How it works

Authentication flow

The package exposes:

  • authenticate: An Express middleware that can be added to any route that needs to be authenticated against a Cognito User Pool.
  • authenticationError: An Express error handler that takes care of any authentication errors generated by the authenticate middleware.
  • JWTValidator: A class that can be instantiated to validate JWTs issued by Cognito. This is useful if you need a custom authentication and error handling logic. The authenticate middleware uses it internally.

Features included

  • JWT signature verification.
  • JWT claims verification.
  • Verify that the token is not expired.
  • Verify that the audience (aud) claim matches one of the valid audiences provided in the configuration.
  • Verify that the issuer (iss) claim is valid for the configured user pool.
  • Verify that the token_use claim matches one of the valid token uses provided in the configuration.
  • Support for JWKs rotation as per described in the JWT signing key rotation thread.
  • Ability to set custom pems for local testing without the need of creating a User Pool.

Basic usage

// app.js
'use strict';

const express = require('express');
const { authenticate, authenticationError } = require('aws-cognito-express');

const app = express();

// Add the authentication middleware.
app.use(authenticate({
  region: 'us-east-2',
  userPoolId: 'us-east-2_6IfDT7ZUq',
  tokenUse: ['id', 'access'],
  audience: ['55plsi2cl0o267lfusmgaf67pf']
}));

// Protected route.
app.get('/articles', (req, res, next) => {
  console.log('JWT payload: ', req.cognito);
});

// Add the authentication error handler.
app.use(authenticationError());

module.exports = app;

For more advanced use cases, please check the docs here.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda