To prevent unauthorized user creation and protect the integrity of user data
defmodule InsecureToken do
alias Guardian.JWT
def decode_and_verify(token) do
{:ok, claims} = JWT.decode_and_verify(token)
IO.inspect(claims)
end
end
In this code snippet, a JWT token is decoded without verifying its signature, thus allowing tampering. An attacker can modify the token header, to change the algorithm used for signature and create users without the token being properly validated.
defmodule SecureToken do
alias Guardian.JWT
@secret_key "Your secret key"
def decode_and_verify(token) do
{:ok, claims} = JWT.decode_and_verify(token, key: @secret_key)
IO.inspect(claims)
end
end
In the secure version of the code, the token is decoded and verified with a predefined secret key. This ensures the token's integrity and authenticity, thus preventing the creation of unauthorized users.