简体   繁体   English

如何从 Electron JS 中的 Cognito OAuth2.0 动态获取 AccessToken

[英]How to Get AccessToken Dynamically from Cognito OAuth2.0 in Electron JS

Hello i use Electron JS for a desktop app which is related to a cloud plateform from which in needto get a list of Patients.您好,我将 Electron JS 用于桌面应用程序,该应用程序与需要从中获取患者列表的云平台相关。

As far as now i can get it but with a static AccessToken.就目前而言,我可以使用 static AccessToken。 I really struggled to get it dynamic, please help.我真的很难让它充满活力,请帮忙。

Here is my code:这是我的代码:

This is my configuration file where i specify Cognito Parameters:这是我指定 Cognito 参数的配置文件:

export default {
s3: {
  REGION: 'YOUR_S3_UPLOADS_BUCKET_REGION',
  BUCKET: 'YOUR_S3_UPLOADS_BUCKET_NAME',
},
apiGateway: {
  REGION: 'YOUR_API_GATEWAY_REGION',
  URL: 'YOUR_API_GATEWAY_URL',
},
cognito: {
  REGION: 'eu-west-1',
  USER_POOL_ID: 'eu-west-1_P0Jcr7nig',
  APP_CLIENT_ID: '4m1utu56hjm835dshts9jg63ou',
  IDENTITY_POOL_ID: 'YOUR_IDENTITY_POOL_ID',
  authenticationFlowType: 'USER_PASSWORD_AUTH',
  AUTHENTICATION_FLOW_TYPE: 'USER_PASSWORD_AUTH',
},
API: {
    endpoints: [
      {
        name: 'PatientsList',
        endpoint: 'https://uo992r7huf.execute-api.eu-west-1.amazonaws.com/Stage/patients',
        //endpoint: 'https://uo992r7huf.execute-api.eu-west-1.amazonaws.com/Stage',
      },
    ],
  },
};

Auth.signIn({
 username: process.env.username,
 password: process.env.password,
}).then().catch(err => {
 console.log(err)});

In another file this is my getaccesstoken function which i export to the main在另一个文件中,这是我导出到主文件的 getaccesstoken function

function getAccessToken() {
 const poolData = { 
  UserPoolId : COGNITO_USER_POOL_ID,
 ClientId : COGNITO_CLIENT_ID,
};
const userPool = new CognitoUserPool(poolData);
var authenticationData = {
      Username : process.env.username, // your username here
      Password : process.env.password, // your password here,
      authenticationFlowType: process.env.AUTHENTICATION_FLOW_TYPE,
      Pool : userPool
  };
  var authenticationDetails = new AmazonCognitoIdentity.AuthenticationDetails(
    authenticationData);
  var cognitoUser = new CognitoUser(authenticationData);
  cognitoUser.authenticateUser(authenticationDetails, {
      onSuccess: function (result) {
        console.log('access token + ' + result.getAccessToken().getJwtToken());
      },
      onFailure: function(err) {
       console.log(err);
      },
  });
}

And finally here is how i get the data in main:最后,这是我获取主要数据的方式:

The declarations:声明:

  const  { Auth } = require('./cognitoAuth');
 const theAccessToken = require('./cognitoAuth');

The code:编码:

  //Get Data From Cloud ECS
  const API_URL = 'https://uo992r7huf.execute-api.eu-west-1.amazonaws.com/Stage/patients';
  const headers = {
    "Content-Type": "application/json",
    //Authorization: theAccessToken.getAccessToken()
    Authorization: "eyJraWQiOiJBbE1DZnBCTHYyVUlNazhXSG4xaTk4RG1QNlFmcFpSSjFaSW1qcVVFZnVBPSIsImFsZyI6IlJTMjU2In0.eyJzdWIiOiI4OWYyZGMxZi1iMTI3LTQzM2QtODJhYS1iMjNkNWJhNzY5NGEiLCJjb2duaXRvOmdyb3VwcyI6WyJkb2N0b3IiXSwiZXZlbnRfaWQiOiI1OTM0ZmIwNC0yYTUzLTQ2NmQtYTU1Ni0zNTM3M2RhZmU1Y2UiLCJ0b2tlbl91c2UiOiJhY2Nlc3MiLCJzY29wZSI6ImF3cy5jb2duaXRvLnNpZ25pbi51c2VyLmFkbWluIiwiYXV0aF90aW1lIjoxNTk1NDI2NjQ2LCJpc3MiOiJodHRwczpcL1wvY29nbml0by1pZHAuZXUtd2VzdC0xLmFtYXpvbmF3cy5jb21cL2V1LXdlc3QtMV9QMEpjcjduaWciLCJleHAiOjE1OTcxNTUxMDUsImlhdCI6MTU5NzE1MTUwNSwianRpIjoiNGRkN2U5ZGUtYmQ2YS00NTg4LWIzZDAtMTVjMWM1NWQxY2Y2IiwiY2xpZW50X2lkIjoiNG0xdXR1NTZoam04MzVkc2h0czlqZzYzb3UiLCJ1c2VybmFtZSI6Ijg5ZjJkYzFmLWIxMjctNDMzZC04MmFhLWIyM2Q1YmE3Njk0YSJ9.LYvzPRBxvKw2P3gHwV8NhYPg_EB3F7ZK2F5HpRWHtBHksr6D4N5Fw56ZVupkRCxVJSq0f93DdljI7BBcBnp9d_hpLzmJLTfBhA3t870omTxalTqpGXN_SvsZmuwcfCX-awn1Um6x_-fhq3zcfPkB9FBljbtwPN-kvCc-Iynei9anVxXI686nIbkfbYfnuRnHrbY0vg8FtyDIDBMv277FPoJ96NwPD4mJvNBxQHi_KfWxQ1DmLiAC6c_l2jP_wawAPBv788CjD_8OlKBbjAHinGEkaL1K9vjI5MhNPyTA5ym1IaWar7Jr8RkUDzQGvqEUPKoOUe9PswmOOxLBjehMgQ"
    };
    //console.log('Token Value:', theAccessToken.getAccessToken());
    const getPatients = async(API_URL) => {
      try {
        //get data from cloud specifiying get method and headers which contain token
        const response = await fetch(API_URL,{
          method: 'GET', headers: headers}
          );
        var listPatients = await response.json();
        listPatients.items.forEach(patient => {
          //Checking what i got
          console.log(patient);
        });
        } catch(err) {
          console.log("Error" + err);
        }
        };
       getPatients(API_URL);

Now when i make it dynamic by specifying theAccessToken.getAccessToken I get this error, USER_SRP is not enabled even if specify it, when i asked team told me the cloud service doesn't want to enable it.现在,当我通过指定 theAccessToken.getAccessToken 使其成为动态时,我收到此错误,即使指定了 USER_SRP 也未启用,当我问团队告诉我云服务不想启用它时。 So how can i get this access token please?那么我怎样才能得到这个访问令牌呢?

For a desktop app it is recommended to do these 2 things, according to security guidance :根据安全指南,对于桌面应用程序,建议执行以下两件事:

  • Use Authorization Code Flow (PKCE)使用授权码流 (PKCE)
  • Login via the system browser, so that the app never sees the user's password通过系统浏览器登录,让应用永远不会看到用户的密码

I have a couple of Electron code samples that use Cognito, which you can easily run - maybe start here:我有几个使用 Cognito 的 Electron 代码示例,您可以轻松运行它们 - 也许从这里开始:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Cognito - 没有为 OAuth2.0 流启用客户端 - Cognito - Client is not enabled for OAuth2.0 flows Google OAuth2.0 + Lambda + S3 授权 - 如何从 S3 引用文件? - Google OAuth2.0 + Lambda + S3 Authorization - How to refer to a file from S3? 在API网关中传递OAUTH2.0访问令牌获取请求 - Passing OAUTH2.0 access token in API gateway get request 从iOS成功登录Amazon Cognito后,如何进入AccessToken和IdToken - How do I get to AccessToken and IdToken following successful Amazon Cognito Login from iOS 如何使用 AWS Cognito 与 Nodejs UI 应用程序和 Oauth 2.0 流使用护照 js? - How to use AWS Cognito with Nodejs UI app and Oauth 2.0 flow using passport js? 在 oauth2.0 授权代码授权流程中获取新的刷新令牌 - Get new refresh token in oauth2.0 authorization code grant flow 如何在 WinUI 3 应用程序中获取 AccessToken 或 Session 字符串以启动 AWS Cognito MFA 的设置 - How to get AccessToken or Session string in WinUI 3 application to initiate set up of AWS Cognito MFA 如何在没有应用程序客户端密钥的情况下使用 Cognito OAuth 2.0 对 API 网关请求进行身份验证? - How can I use Cognito OAuth 2.0 to authenticate a API Gateway request without having an app client secret? 账号关联azure AD后如何从alexa获取accesstoken - How to get the accesstoken from alexa after account linking with azure AD 如何从AWS Cognito获取密码-Android? - How to get password from aws cognito - android?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM