1

我想知道是否有人圍繞如何解決API網關/ Lambda上的以下問題提出了一個好主意 - API的使用者只需要一個令牌,通過認證授權者附加到API網關,API必須處理所有事情,並僅僅回覆令牌。AWS:使用開發人員身份驗證身份的安全API網關

所以用戶發送我的登錄LAMBDA他們的用戶名和密碼,以便按該文檔我做了以下(我砍傷了錯誤處理出來爲簡潔):

const cognitoidentity = new AWS.CognitoIdentity({ region: 'eu-west-1' }) 
    const userId = authenticationService.authorise(username, password) 
    const params = { 
     IdentityPoolId: 'eu-west-1:my-identity-pool-id', 
     Logins: { 
      'myapp.mydomain.com': userId, 
     }, 
    } 
    cognitoidentity.getOpenIdTokenForDeveloperIdentity(params, (err, data) => { 

    // Now I have an OpenId token and an identity Id 


}) 

起初我以爲那我應該叫GetCredentialsForIdentity但是這並不會使我返回訪問令牌,我是否正在往這裏走,或者我錯過了什麼?

更新:爲了清楚起見,我不希望消費者必須實施AWS客戶端sdk,基本上我希望他們能夠添加一個頭部,以使他們能夠像傳統api一樣提出請求。

+0

如果消費者不需要實施AWS SDK,他們如何獲得令牌以訪問API?你有一個自定義的令牌生成器或類似的東西給你的消費者? –

+0

那麼對於用戶池他們得到一個JWT回來這是很好 - 從進一步的研究,我認爲身份池是浪費時間,所以是的,我想創建自己的JWT實現與聯合身份使用,我會更新我已經證明了答案。 –

回答

0

更新基於評論: 檢查這個代碼

var data = { 
     UserPoolId : '...', // Your user pool id here 
     ClientId : '...' // Your client id here 
    }; 
    var userPool = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserPool(data); 
    var cognitoUser = userPool.getCurrentUser(); 

    if (cognitoUser != null) { 
     cognitoUser.getSession(function(err, session) { 
      if (err) { 
       alert(err); 
       return; 
      } 
      console.log('session validity: ' + session.isValid()); 

      AWS.config.credentials = new AWS.CognitoIdentityCredentials({ 
       IdentityPoolId : '...' // your identity pool id here 
       Logins : { 
        // Change the key below according to the specific region your user pool is in. 
        'cognito-idp.<region>.amazonaws.com/<YOUR_USER_POOL_ID>' : session.getIdToken().getJwtToken() 
       } 
      }); 

      // Instantiate aws sdk service objects now that the credentials have been updated. 
      // example: var s3 = new AWS.S3(); 

     }); 
    } 

session.getAccessToken().getJwtToken()會給你的accessToken

此鏈接http://docs.aws.amazon.com/cognito/latest/developerguide/amazon-cognito-user-pools-using-tokens-with-identity-providers.html可能會幫助你。

您必須在用戶使用用戶名和密碼登錄後生成訪問令牌。並且您可以稍後驗證該密鑰。 否則,當用戶通過驗證後,您可以使用STS生成憑證並通過該憑證。

對於API網關,檢查是否可以在代碼中創建API密鑰並使用它。

+0

嘿,謝謝你,我擁有的問題實際上是獲取訪問令牌,身份令牌等一旦我有openId令牌。 –

+0

@MrkFldig檢查更新。鏈接:http://docs.aws.amazon.com/cognito/latest/developerguide/using-amazon-cognito-user-identity-pools-javascript-examples.html – skarfa

+0

感謝Skarfa,但那是一個用戶池,而不是身份池。 –