I want to build an (Angular) SPA with a (Asp.Net Core) back-end and I want to use security best practices. My plan is this:
- The SPA displays a Login button
- Clicking the Login button initiates an authorization code openid-connect flow with some identity provider (Google, Microsoft, etc.)
- The user authenticates with the identity provider and then is redirected back to the SPA with the authorization code
- The SPA then makes a login request to the back-end and passes the authorization code
- The back-end makes a request to the identity provider, exchanging the code for an id_token, and creates some kind of session ticket for the user
Now this is where I get bogged down by my lack of proper understanding
Issue 1 - Best Practice
Is the above procedure even considered best practice? Or am I doing it wrong?
Issue 2 - Authentication Ticket
My back-end is API only, it doesn't render HTML pages for the user, only provides Json responses. What kind of authentication ticket should I create? Should it be a JWT or a Cookie? I assume it can't be a cookie since API cannot return a cookie that the browser will store properly. So it must be some kind of token, preferably a JWT. How should the SPA store it, local / session storage?
Issue 3 - Expiration and Refresh
Assuming the back-end returns a JWT to the SPA, what should this JWT contain? What should be its expiration time? How should I handle automatic refreshing?
Issue 4 - Required Packages & Libraries
Also, specific to programming (since this is a programming question after all), what are the required libraries to use for all of this. The question is both for the SPA and for the back-end. I assume I need some kind of openid-connect client for the SPA, and I need some library on the back-end which can complete the authorization code flow (exchanging the code for the id_token) is this done by IdentityServer4
? Or is there some other library.
UPDATE
I've prepared a solution which strives to showcase the proper way to do everything. It is a work in progress, messy and unclean, but it is minimal, and it works and it strives to follow best practices.