【发布时间】:2020-11-20 01:39:30
【问题描述】:
我使用 aws cognito 和 oauth2 在电子 js 中工作。我需要从云端点中的存储中动态获取 accessToken 以获得获取数据列表的授权。就目前而言,如果我以静态方式指定令牌,我可以获得列表。但我需要它动态。令牌的关键是 CognitoIdentityServiceProvider.COGNITO_CLIENT_ID.username.accessToken 但即使我配置了 Cognito,我似乎也无法得到它
这是我的配置文件代码,其中还包含登录功能:
const { Auth } = require('@aws-amplify/auth');
const { Amplify } = require('aws-amplify');
const AmazonCognitoIdentity = require('amazon-cognito-identity-js');
const CognitoUserPool = require('amazon-cognito-identity-js-node').CognitoUserPool;
const CognitoUserSession = require('amazon-cognito-identity-js-node').CognitoUserSession;
const CognitoUser = require('amazon-cognito-identity-js-node').CognitoUser;
const CognitoIdToken = require('amazon-cognito-identity-js-node').CognitoIdToken;
const CognitoAccessToken = require('@aws-amplify/auth');
const CognitoRefreshToken = require('amazon-cognito-identity-js-node').CognitoRefreshToken;
const COGNITO_USER_POOL_ID = 'eu-west-1_P0Jcr7nig';
const COGNITO_CLIENT_ID = '4m1utu56hjm835dshts9jg63ou';
const AWS_REGION = 'eu-west-1';
Amplify.configure({
Auth: {
// OPTIONAL - Enforce user authentication prior to accessing AWS resources or not
mandatorySignIn: false,
region: AWS_REGION,
userPoolId: COGNITO_USER_POOL_ID,
userPoolWebClientId: COGNITO_CLIENT_ID,
// OPTIONAL - Manually set the authentication flow type. Default is 'USER_SRP_AUTH'
authenticationFlowType: 'USER_PASSWORD_AUTH',
oauth: {
domain: "https://edc-echosens-cloud.auth.eu-west-1.amazoncognito.com",
scope: ["email", "profile", "openid"],
redirectSignIn: "http://localhost:1962/",
redirectSignOut: "http://localhost:1962/",
responseType: "code", // or 'token', note that REFRESH token will only be generated when the responseType is code
},
API: {
endpoints: [
{
name: 'PatientsList',
endpoint: 'https://url',
},
],
},
},
});
Auth.signIn({
username: 'doctoredc@yopmail.com',
password: 'kinG2804*D',
}).then().catch(err => {
console.log(err)});
function getAccessToken() {
const poolData = {
UserPoolId : COGNITO_USER_POOL_ID,
ClientId : COGNITO_CLIENT_ID,
};
const userPool = new CognitoUserPool(poolData);
var authenticationData = {
Username : 'mymail@mail.com', // your username here
Password : 'kinG2804*D', // your password here,
authenticationFlowType: 'USER_PASSWORD_AUTH',
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);
},
});
}
// You can get the current config object
//const currentConfig = Auth.configure();
exports.Auth = Auth;
module.exports.getAccessToken = getAccessToken
我进行了池配置,指定了 api url 和凭据。我还添加了一个 Auth.signIn 函数和一个 getAccessToken 函数。然后这是在我的 main 中调用函数的代码
const API_URL = 'https://url';
const headers = {
"Content-Type": "application/json",
Authorization: theAccessToken.getAccessToken()
};
console.log('Token Value:', theAccessToken.getAccessToken());
const getPatients = async(API_URL) => {
try {
const response = await fetch(API_URL,{
method: 'GET', headers: headers}
);
const json = await response.json();
console.log(json);
} catch (error) {
console.log(error);
}
};
getPatients(API_URL);
问题是使云端点指定 authFlowType 为 USER_PASSWORD_AUTH 的团队,但我收到错误消息 USER_SRP_AUTH 未为客户端启用。他们使用 USER_PASSWORD_AUTH。所以它阻止了我,我不知道没有动态获取访问令牌有什么问题。
请帮助我,我非常接近答案。谢谢
【问题讨论】:
标签: amazon-web-services oauth-2.0 electron amazon-cognito access-token