You have something similar in concept to what Play Framework uses for sessions today. Nothing wrong with that per say, but writing secure protocols (session serialization and shared trust) is hard for non trivial implementations.
For example: The security effect of the Hmac SHA-1 that you are using is a function of the key size (5.3.4 Security Effect of the HMAC Key) the user chooses. But you do not mandate what the minimal length is in your implementation. Even with a larger key, using Hmac SHA-256 instead, with a minimal key size of 256 bits is a safer bet.
Your implementation, like Play Framework, limits the signature to a symmetrical pre shared secret (HMAC and AES). This is effective for basic scenarios but limiting if you'd wish to not trust all services that consumes the session, since anyone verifying the session needs your secret and is therefore able to modify the session.
Without steering you off your current path, I just wish to mention JWT (Json Web Token) which is a token format typically sent as a OAuth2 Bearer Token (using the HTTP Authorization header). JWT has security considerations as well as a client side singed session (claims) addressed in a standardized way with support for both symmetrical and asymmetrical signature and encryptions.
I'm not suggesting that you abandon your current path but reading the standards around OpenID Connect might be a source of inspiration as they are well written and talk about many subtle security considerations (scroll down and look at the underpinnings group, read about JWA, JWK, JWS, JWE, JWT)
If you do want to use JWT then know that implementing all of JWT (or OpenID Connect for that matter) from scratch is quite a task, but there are well written and easy to use libraries such as Jose4j available. One caveat is that the client needs to add the JWT itself to a header, that implies a web page where you call APIs via javascript.
Where possible I tried to base the impl on what's in Play, as I trust them to do the "right thing"
I read about JWT, from what I understand it's kind of a combination of how header-based CSRF protection works and keeping the content of the cookie in memory only, (instead in the cookie).
Well the signature is a hash, the optional encryption of the data is symmetrical.
Also, if you want to implement "remember me", I suppose you need to resort to cookies anyway to have some kind of persistent client-side storage?
I recommend just keeping cookies out of the equation, that makes CSRF issues go away which gives you a simpler implementation.
Some answers below, hope you don't mind the long answers :)
Where possible I tried to base the impl on what's in Play, as I trust them to do the "right thing"Well, not a bad starting point, but I imagine Play has legacy and backwards compatibility to consider (cookies) and does server side rendering. I'm suggesting what is right for Play might just be OK for a green field library.
I read about JWT, from what I understand it's kind of a combination of how header-based CSRF protection works and keeping the content of the cookie in memory only, (instead in the cookie).CSRF protection is only needed because of the use of cookies or Basic Auth where then the browser automatically attaches the authentication/cookie to each request for a domain. If you don't have to be backwards compatible with a cookie based client side sessions (as Play have to) then instead of using CSRF protection you might as well do away with cookies altogether, if you are already using header based CSRF then you actually gain simplicity.
With JWT you do commonly send the token as a header (Authorization: Bearer <your jwt>). But this is not in any way mandated by JWT or JOSE (Javascript Object Signing and Encryption). You can send a JWT as a POST body or as a Cookie if you wish, sorry if I was misleading on that. JOSE-JWT is a data format.
Well the signature is a hash, the optional encryption of the data is symmetrical.HMAC uses symmetrical keys. You do have a point in that a MAC is not a digital signature. To verify the HMAC you need the same secret key that was used to create it. That means you can create a new signature for a different message. If you have a monolithic application that is fine (like Play apps) if you have multiple applications or multiple small services you may not wish to distribute that key to all clients that need to verify the session.
Also, if you want to implement "remember me", I suppose you need to resort to cookies anyway to have some kind of persistent client-side storage?Yes browser Local Storage should fit well. The advantage of Local Storage over cookies is again no CSRF issues. In the case of JWT a "remember me" could just be a JWT with a long expiration.
I had the impression that cookies are still "the way things are done", and to be honest didn't really consider not using them at all. But it seems I should revise that :)
So you would need to send the session token (in any data format really, I guess it can also be the same content as the cookie I'm constructing currently) in a custom header, and you would get CSRF protection "for free", though it would only work for AJAX requests.
Ah, microservices, right ;) Although I suppose it's quite common to have a single "orchestrator" service which does the whole frontend job. But of course it doesn't have to be that way.
When using Local Storage, would you still separate the "current session data" - containing the signed/encrypted user id or username (that would go to sessionStorage) and a "remember me token" (stored in localStorage) like you do with cookies? It has some nice properties, like varying access levels based on automatic/manual logins or knowing when users log in (automatically or manually).
Thanks,Adam
Without steering you off your current path, I just wish to mention JWT (Json Web Token) which is a token format typically sent as a OAuth2 Bearer Token (using the HTTP Authorization header). JWT has security considerations as well as a client side singed session (claims) addressed in a standardized way with support for both symmetrical and asymmetrical signature and encryptions.