API Gateway で作成した REST API を呼び出します。
呼び出し時に必要となる ID トークンを InitiateAuth の呼び出し結果の IdToken
の箇所からコピーします。” は含めず、” と “ に囲まれた文字列をコピーします。
ターミナルや Powershell で以下の内容を < >
部分を書き換えて入力して実行します。<id-token>
は先ほどコピーした ID トークンの文字列を貼り付けます。<api-endpoint>
は2 章で作成した API Gateway REST API の URL を入力します。
curl --location \
--request POST '<api-endpoint>' \
--header 'Authorization: <id-token>'
C:\Windows\System32\curl.exe --location `
--request POST '<api-endpoint>' `
--header 'Authorization: <id-token>'
成功した場合の出力を整形すると以下のような内容となっています。この API の結果としては、呼び出した内容が JSON 構造で返ってきます。例えば、指定した [Authorization] などが含まれていることを確認できます。
{
"body-json": {},
"params": {
"path": {},
"querystring": {},
"header": {
"accept": "*\/*",
"Authorization": "?????.?????.?????",
"Host": "????????.execute-api.ap-northeast-1.amazonaws.com",
"User-Agent": "curl\/7.64.1",
"X-Amzn-Trace-Id": "Root=?-????????-????????????????????????",
"X-Forwarded-For": "???.???.???.???",
"X-Forwarded-Port": "443",
"X-Forwarded-Proto": "https"
}
},
"stage-variables": {},
"context": {
"account-id": "",
"api-id": "????????.",
"api-key": "",
"authorizer-principal-id": "",
"caller": "",
"cognito-authentication-provider": "",
"cognito-authentication-type": "",
"cognito-identity-id": "",
"cognito-identity-pool-id": "",
"http-method": "POST",
"stage": "prod",
"source-ip": "???.???.???.???",
"user": "",
"user-agent": "curl/7.64.1",
"user-arn": "",
"request-id": "????????-????-????-????-????????????",
"resource-id": "5ndtpfnysg",
"resource-path": "/"
}
}
理解を深めるために、設定などを変更して再度 API リクエストしてみてください。
Authorization に指定する文字列を適当に変えてから、API をリクエストするとエラーになることが確認できます。
API Gateway REST API の認可設定を外すと、トークンが無くてもアクセスが可能になります。
60 分経過してから再度試すと、ID トークンの有効期限が切れて API にアクセスできなくなります。
API Gateway で Mock 機能を使うのではなく、実際に Lambda ファンクションを呼び出した場合、Labmda ファンクションからどのユーザがアクセスしたか確認できます。
SignUp の API は追加のユーザ属性を設定してユーザを作成できます。設定された属性は、ID トークンに反映されます。