Appsyncgraphqlauthorizationwithcognito
创始人
2024-09-11 13:31:26
0

在AWS AppSync中使用Cognito进行授权验证,需要完成以下步骤:

  1. 在AWS Cognito中创建用户池和身份池。
  2. 在AWS AppSync中创建API和数据源,并设置数据源连接到相应的AWS服务。
  3. 在AppSync中定义数据模型和解析器。
  4. 在AppSync中设置授权验证,使用Cognito User Pool或Identity Pool进行身份验证。

下面是一个使用Cognito User Pool进行授权验证的JavaScript示例代码:

const aws = require('aws-sdk');
const AmazonCognitoIdentity = require('amazon-cognito-identity-js');

const poolData = {
  UserPoolId: 'your_user_pool_id',
  ClientId: 'your_user_pool_client_id'
};

const userPool = new AmazonCognitoIdentity.CognitoUserPool(poolData);

exports.handler = async (event) => {
  const token = event.authorizationToken;
  const refreshToken = event.refreshToken;

  if (!token || !refreshToken) {
    throw new Error('Unauthorized');
  }

  const checkToken = new Promise((resolve, reject) => {
    const userData = {
      Username: 'username',
      Pool: userPool,
      Storage: new AmazonCognitoIdentity.LocalStorage()
    };

    const cognitoUser = new AmazonCognitoIdentity.CognitoUser(userData);
    const sessionData = {
      RefreshToken: new AmazonCognitoIdentity.CognitoRefreshToken({ RefreshToken: refreshToken })
    };

    cognitoUser.refreshSession(sessionData, (err, session) => {
      if (err) {
        reject(err);
      } else {
        resolve(session.isValid());
      }
    });
  });

  const result = await checkToken;
  if (!result) {
    throw new Error('Unauthorized');
  }

  const decodedToken = new AmazonCognitoIdentity.CognitoIdToken({ IdToken: token }).decodePayload();
  const headers = {
    'access_token': token,
    'id_token': token,
    'client_id': decodedToken.aud,
    'username': decodedToken.email
  };

  return {
    principalId: decodedToken.sub,
    policyDocument: {
      Version: '2012-10-17',
      Statement: [{
        Action: '

相关内容

热门资讯

有挂实锤开挂!途游辅助器软件,... 有挂实锤开挂!途游辅助器软件,德扑圈有透视吗(一分钟了解开挂辅助工具);无需打开直接搜索加(薇:13...
有挂秘籍开挂!全民内蒙古辅助,... 有挂秘籍开挂!全民内蒙古辅助,789大菠萝可以控制吗(推荐十款开挂辅助软件)您好:789大菠萝可以控...
确实有挂开挂!川滇互娱app辅... >>您好:川滇互娱app辅助确实是有挂的,很多玩家在这款川滇互娱app辅助游戏中打牌都会发现很多用户...
有挂规律开挂!微信小程序辅助器... 微信小程序辅助器出租 无需打开直接搜索微信:136704302本司针对手游进行,选择我们的四大理由:...
有挂教程开挂!赣州冲关辅助,情... 有挂教程开挂!赣州冲关辅助,情怀宜春辅助(科技新动态开挂辅助软件)您好:情怀宜春辅助这款游戏可以开挂...
有挂教程开挂!关春天开挂,德普... 较多好评“微乐万能挂官网”开挂(透视)辅助教程 了解更多开挂安装加(136704302)微信号是一款...
存在有挂开挂!小程序牵手跑得快... 存在有挂开挂!小程序牵手跑得快辅助器,中至赣州黑科技辅助软件(如何分辨真伪开挂辅助下载)>>您好:软...
有挂技巧辅助!微乐老友广东辅助... 有挂技巧辅助!微乐老友广东辅助器,乐平讨赏挂(透视最新开挂辅助插件)微乐老友广东辅助器ai黑科技系统...
有挂讲解开挂!填大坑辅助器,老... 较多好评“微乐万能挂官网”开挂(透视)辅助教程 了解更多开挂安装加(136704302)微信号是一款...
有挂分析辅助!授权二厅辅助,中... 有挂分析辅助!授权二厅辅助,中至赣州黑科技辅助软件视频(玩家必备科普开挂辅助软件)>>您好:软件加1...