I'm building a Single-Page App (SPA) and a RESTful API. The API needs security - certain users can only make calls to certain endpoints. I have an external Identity Provider (IdP (Okta)) that I want the user to authenticate with using the OpenId Connect protocol. I'm trying to clarify the correct steps for authentication and authorization of the SPA to the RESTful API. The two flows I've been looking at are the Authorization code flow and the Implicit flow.
If I was to go with Implicit flow, then the steps would be:
- The user visits the SPA, which redirects the user to the IdP to sign-in.
- After the user signs in, the IdP returns the user to the SPA with an access token and ID token.
- (This is the step I'm unsure about) Each time the SPA makes a request to the RESTful API, it passes the access token and ID token along with the request, which the RESTful API validates and then checks the user has authority to access the particular endpoint. If it does it returns the result, otherwise the user is unauthorized.
If I was to go with the Authorization Code flow, then the steps would be:
- Same as above step 1.
- After the user signs in, the IdP returns the user to the SPA with an authorisation code.
- (Again, the step I'm unsure is correct) Each time the SPA makes a request to the RESTful API, it passes the authorization code along with the request, which the RESTful API then exchanges (along with a client secret) with the IdP for an access token and ID token. It uses these to check if the user can access the particular endpoint. If it does it returns the result, otherwise they're unauthorized.
I think the implicit flow is the one to use in this scenario, but do I have the steps correct? Particularly step 3, sending two tokens in every request, doesn't seem right. But I think I need both tokens to validate and determine the user. Help appreciated!