← PHP / Laravel · Contents

PHP-056 / Intermediate

Storing API tokens in plaintext

A02:2021 Cryptographic Failures · CWE-256 Plaintext Storage of a Password

Why is this a problem?

If the DB leaks, all API tokens can be used immediately.

How to fix it

php
$token = $user->createToken('app');   // Sanctum: store the hash; return plaintext only once
return $token->plainTextToken;

Further reading