0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

FlutterでCognitoのIdTokenやAccessTokenをログ出力すると署名が壊れる?

Last updated at Posted at 2023-06-10

ググるとズレた話題の議論をぐるぐる巡ることになり、数時間ハマったので。
ChatGPTさんも知らなかったです。

問題

Amazon Cognito から取得したトークンを Flutter アプリ内から debugPrintで ログに出力し、コピペして認証に使おうとすると、認証に失敗します。jwt.io でトークンを検証してみると署名が壊れています。なんで?

原因

dart の debugPrint はどんなに長い文字列でも省略せずに出力してくれるのですが、Flutterのログ(flutter logs)でコンソールに表示される文字列は1,024文字が上限です。

Cognitoのトークンは1,024文字をちょっと超えているので、ログ上は一見正常に見えるトークンでも末尾が切れており、署名が壊れたトークンとなってしまいます。

解決法

1024文字を超える文字列はFlutter DevTools の logging ビューを使うと正常に表示できるのでこちらを使います。別解としては、Flutterアプリ内にデバッグ機能としてトークン文字列を表示したり、クリップボードにコピーする機能を付けるなどがあると思います。

おわりに

意外と気が付きにくいよ!

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?