JWT tokenを加えてcurlしてみるメモ

例えばfirebaseのtoken取得の場合

firebaseでプロジェクトを作った場合など、そのプロジェクトの情報を下記の適当なhtmlファイルにくっつけてブラウザでkickしてみます。

developerコンソールで見てみるとidTokenが返されることを確認しましょう。

このidTokenをコピーしておきましょう。

<!DOCTYPE html>
<html>
<head>
  <title></title>
  <script src="https://www.gstatic.com/firebasejs/5.7.1/firebase.js"></script>
  <script>
    // Initialize Firebase
    var config = {
      apiKey: "XXXXXXXXX",
      authDomain: "XXXXXXXXX",
      databaseURL: "XXXXXXXXX",
      projectId: "XXXXXXXXX",
      storageBucket: "",
      messagingSenderId: "XXXXXXXXX"
    };
    firebase.initializeApp(config);
  </script>
  <script type="text/javascript">
    firebase.auth().signInWithEmailAndPassword("XXXXXXXXX@XXXXXXXXX", "XXXXXXXXX").catch(function(error) {
      console.log(error.message);
    });
    firebase.auth().onAuthStateChanged(function(user) {
      firebase.auth().currentUser.getIdToken(/* forceRefresh */ true).then(function(idToken) {
        console.log("以下idToken:");
        console.log(idToken);
      })
    });
  </script>
</head>
<body>
</body>
</html>

curlしてみる

それではcurlしてみましょう。

put

curl -X PUT -H 'authorization: Bearer {さっきのjwtコピペ}'   -H 'cache-control: no-cache'   -H 'content-type: application/json' -d '{
    "user": "oredayo!!!"
}' "https://XXXXXXXXX/prd/api/v1/user" | jq .

get

curl -X GET -H 'authorization: Bearer {さっきのjwtコピペ}'   -H 'cache-control: no-cache'   -H 'content-type: application/json'  "https://XXXXXXXXX/api/v1/user" | jq .

jwtの内容をチェックしよう

https://jwt.io/

こんな感じの帰ってくるのでuidぶっこむ

{
  "iss": "XXXXXXXXXXXX",
  "aud": "XXXXXXXXXXXX",
  "auth_time": 1550648336,
  "user_id": "XXXXXXXXXXXX",
  "sub": "XXXXXXXXXXXX",
  "iat": 1550648369,
  "exp": 1550651969,
  "email": "XXXXXXXXXXXX",
  "email_verified": false,
  "firebase": {
    "identities": {
      "email": [
        "XXXXXXXXXXXX",
      ]
    },
    "sign_in_provider": "password"
  }
}