Hello,
I had to do something similar about 3 weeks ago (authentification with SSPI and fetching/creating a token). I'll add a .zip file with two .php files as attachment (though the comments are in German). The ml version is only fetching the token while the ol version is able to create a token if there's no token for the user. The attribute_1 is a created new column for the token table and is used for storing the user name, id or whatever your user has to be indentified with.
Perhaps this will help you or at least give you an idea how to solve this.