Hi Bruno,
Have you considered using the JWT Token component from Forge? I think it would help you a lot.
It provides you already with a library for creating, decoding and validating JSON Web Tokens, an open, industry standard RFC 7519 method for representing claims securely between two parties.
Hope it helps.
Regards,
João