I don't think this is exactly what you are asking, but the suggestion by jamesfreddyc
https://community.esri.com/thread/180549#comment-625960 has some suggestions about using an .ini file with the gettoken function to remove the need to enter (or hardcode) credentials. I have successfully modify/implemented this in many of my scripts that require a token.
Although that may not be what you are searching for, it may give you some ideas.
Since you posted this in the ArcGIS for Server forum, when you say "... we want to generate and validate our token from out of the ArcGIS environment", I think you mean the GIS Server, right?
In which case, the GIS Server does not support using custom tokens for authentication.
Hope this helps,