最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

如何使用Facebook验证使用Cognito的API调用?

IT培训 admin 5浏览 0评论

如何使用Facebook验证使用Cognito的API调用?

我想使用Cognito对用户进行身份验证,并选择使用Facebook。用户可以使用其中任何一个选项sign_in / sign_up。

我创建了Cognito用户池和Cognito联合身份,我也创建了Facebook App进行身份验证。用户池和Facebook应用程序都连接到联合身份。

当我签名并稍后通过Cognito用户池对Cognito用户进行身份验证时,Cognito将返回accessToken,我将其存储在localStorage中,并在需要时进行身份验证。

我有/authenticate端点(快速),它接收用户名和密码,如果一切顺利,则返回accessToken。每当我进行需要auth的API调用时,我都会发送我在本地存储中的accessToken。它或多或少如此:

// POST user/authenticate
const authenticationData = {
  Username: username,
  Password: password
}

authenticationDetails = new AuthenticationDetails(authenticationData)

const userData = {
  Username: username,
  Pool: userPool()
}
cognitoUser = new CognitoUser(userData)

cognitoUser.authenticateUser(authenticationDetails, {
  onSuccess: (res) => resolve(res), // here I get accessToken
  onFailure: (err) => {
    console.log('[authenticateUser error]', err)
    reject(err)
  },
//...

然而

当我使用Facebook时,我没有获得accessToken,我可以以同样的方式使用。我通过FB.login从Facebook获取accessToken,我将其传递给Cognito进行身份验证,然后我不知道该怎么做,因为我无法获得任何可用于验证API调用的令牌,这需要Cognito身份验证。

这是我做的:

await window.FB.login((response) => { 
  props.userFacebookSignIn(response)
})
// ...

call(foo, 'users/facebook_sign_in', { accessToken: payload.facebookAccessToken })
// ...

// users/facebook_sign_in
AWS.config.region = config.AWSRegion
AWS.config.credentials = new AWS.CognitoIdentityCredentials({
  IdentityPoolId: 'foo',
  Logins: {
    'graph.facebook': facebookAccessToken
  }
})

AWS.config.credentials.get((err) => {
  // Here I get no errors, I presume that I have logged Facebook user in 
  const accessKeyId = AWS.config.credentials.accessKeyId
  const secretAccessKey = AWS.config.credentials.secretAccessKey
  const sessionToken = AWS.config.credentials.sessionToken
  // here I can do stuff probably, 
  // but I would like to receive token that would allow me to do stuff, 
  // rather than context I can do stuff in
})

虽然我正在做所有这些,但我有这种感觉,AWS的开发人员将Cognito作为前端解决方案实现,而不是在后端使用。如果我错了,请纠正我。

不过,我希望能够在快速中间件中交替使用Cognito和Facebook验证api调用。

那可能吗?谢谢。

回答如下:

我使用联合身份进行salesforce单点登录,但我想这些步骤是一样的。在使用Facebook进行身份验证后,您将收到来自他们的id_token作为回应。您必须将此作为getId方法中的参数传递:

var params = {
  IdentityPoolId: 'STRING_VALUE', /* required */
  AccountId: 'STRING_VALUE',
  Logins: {
    '<IdentityProviderName>': 'STRING_VALUE',
    /* 'graph.facebook': ... */
  }
};
cognitoidentity.getId(params, function(err, data) {
  if (err) console.log(err, err.stack); // an error occurred
  else     console.log(data);           // successful response
});

在结果中,您将获得一个身份ID,您可以将其存储在某处,以便您在进行身份验证时不必每次都进行此调用。现在拿这个身份ID并进行getCredentialsForIdentity调用:

response = client.get_credentials_for_identity(
    IdentityId='string',
    Logins={
        'string': 'string'
    },
    CustomRoleArn='string'
)

这将最终为您提供所需的临时访问密钥,密钥和会话密钥。

如何使用Facebook验证使用Cognito的API调用?

我想使用Cognito对用户进行身份验证,并选择使用Facebook。用户可以使用其中任何一个选项sign_in / sign_up。

我创建了Cognito用户池和Cognito联合身份,我也创建了Facebook App进行身份验证。用户池和Facebook应用程序都连接到联合身份。

当我签名并稍后通过Cognito用户池对Cognito用户进行身份验证时,Cognito将返回accessToken,我将其存储在localStorage中,并在需要时进行身份验证。

我有/authenticate端点(快速),它接收用户名和密码,如果一切顺利,则返回accessToken。每当我进行需要auth的API调用时,我都会发送我在本地存储中的accessToken。它或多或少如此:

// POST user/authenticate
const authenticationData = {
  Username: username,
  Password: password
}

authenticationDetails = new AuthenticationDetails(authenticationData)

const userData = {
  Username: username,
  Pool: userPool()
}
cognitoUser = new CognitoUser(userData)

cognitoUser.authenticateUser(authenticationDetails, {
  onSuccess: (res) => resolve(res), // here I get accessToken
  onFailure: (err) => {
    console.log('[authenticateUser error]', err)
    reject(err)
  },
//...

然而

当我使用Facebook时,我没有获得accessToken,我可以以同样的方式使用。我通过FB.login从Facebook获取accessToken,我将其传递给Cognito进行身份验证,然后我不知道该怎么做,因为我无法获得任何可用于验证API调用的令牌,这需要Cognito身份验证。

这是我做的:

await window.FB.login((response) => { 
  props.userFacebookSignIn(response)
})
// ...

call(foo, 'users/facebook_sign_in', { accessToken: payload.facebookAccessToken })
// ...

// users/facebook_sign_in
AWS.config.region = config.AWSRegion
AWS.config.credentials = new AWS.CognitoIdentityCredentials({
  IdentityPoolId: 'foo',
  Logins: {
    'graph.facebook': facebookAccessToken
  }
})

AWS.config.credentials.get((err) => {
  // Here I get no errors, I presume that I have logged Facebook user in 
  const accessKeyId = AWS.config.credentials.accessKeyId
  const secretAccessKey = AWS.config.credentials.secretAccessKey
  const sessionToken = AWS.config.credentials.sessionToken
  // here I can do stuff probably, 
  // but I would like to receive token that would allow me to do stuff, 
  // rather than context I can do stuff in
})

虽然我正在做所有这些,但我有这种感觉,AWS的开发人员将Cognito作为前端解决方案实现,而不是在后端使用。如果我错了,请纠正我。

不过,我希望能够在快速中间件中交替使用Cognito和Facebook验证api调用。

那可能吗?谢谢。

回答如下:

我使用联合身份进行salesforce单点登录,但我想这些步骤是一样的。在使用Facebook进行身份验证后,您将收到来自他们的id_token作为回应。您必须将此作为getId方法中的参数传递:

var params = {
  IdentityPoolId: 'STRING_VALUE', /* required */
  AccountId: 'STRING_VALUE',
  Logins: {
    '<IdentityProviderName>': 'STRING_VALUE',
    /* 'graph.facebook': ... */
  }
};
cognitoidentity.getId(params, function(err, data) {
  if (err) console.log(err, err.stack); // an error occurred
  else     console.log(data);           // successful response
});

在结果中,您将获得一个身份ID,您可以将其存储在某处,以便您在进行身份验证时不必每次都进行此调用。现在拿这个身份ID并进行getCredentialsForIdentity调用:

response = client.get_credentials_for_identity(
    IdentityId='string',
    Logins={
        'string': 'string'
    },
    CustomRoleArn='string'
)

这将最终为您提供所需的临时访问密钥,密钥和会话密钥。

发布评论

评论列表 (0)

  1. 暂无评论