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: '

相关内容

热门资讯

微扑克德州专用辅助器!impo... 微扑克德州专用辅助器!impoker有挂吗,(来玩德州)原先是有挂(详细辅助必备教程);小薇(透视辅...
wepoke辅助德之星!德扑之... wepoke辅助德之星!德扑之星软件透明软件,德州ai能盈利,扑克教程(有挂方法);人气非常高,ai...
aapoker有外挂!wpk外... aapoker有外挂!wpk外挂是真的还是,(AAPOKEr)好像真的有挂(详细透视科技教程);wp...
微扑克有辅助挂!红龙扑克是不是... 微扑克有辅助挂!红龙扑克是不是有问题,(AaPOKER)好像是有挂(详细透视系统教程)微扑克有辅助挂...
aapoker透明挂!来玩德州... WePoke高级策略深度解析‌;aapoker透明挂!来玩德州扑克约局神器,(wpK)一贯真的有挂(...
aapoker有挂!wopok... aapoker有挂!wopoker手机辅助,微扑克wpk安全,软件教程(有挂技巧)是一款可以让一直输...
wepokeai代打!德州之星... wepokeai代打!德州之星辅助透视,(aapOKER)原来真的是有挂(详细透视力荐教程);超受欢...
wepoke计算辅助!wpk游... wepoke计算辅助!wpk游戏是否有挂,(wepoker)起初有挂(详细透视切实教程);实战中需综...
wpk德州透视辅助!德扑之星禁... wpk德州透视辅助!德扑之星禁止模拟器,pokermaster有挂,线上教程(有挂辅助挂);无聊就玩...
aapoker辅助!aapok... aapoker辅助!aapoker德州线上扑克辅助工具,(德州俱乐部)从来真的有挂(详细辅助规律教程...