The OAuth2 protocol knows 4 "flows":
- Client credentials
- Resource owner
- Implicit
- Authorization code
The client credentials
flow and the resource owner
flow are valid to use in a machine 2 machine context. Like a daemon service, for example. Do not use these in a web context!
The implicit flow
and the authorization code flow
are human 2 machine flows. Like Kevin mentioned in the other post: The end-user is supposed to enter his credentials on the OIDC server/site, not on your site.
The implicit flow
is considered not to be secure in a web context anymore.
To answer the question: Use the Authorization Code flow with PKCE
to authenticate webapps. What this is, is explained in more detail here: https://bff.gocloudnative.org/concepts/what-is-pkce/.
Important to realise is that when you are using an OAuth2/OIDC server, the user is not logging in to your app. Instead the user is logging in to the OIDC/OAuth2 server and has a session there. This concept is called SSO.
This means that, when implemented correctly, when you have an unauthenticated user who navigates to app 1
, "logs in there", and then navigates to app 2
, the user is automatically logged in there too.
This works, because when you implement the authorization code flow
, to log the user in, he must navigate to https://youridp.example/authorize?xyz. The user sees a login form here. When the user logs in, a cookie will be set on this domain. That's way your identity provider knows the user has logged in. Next, the identity provider redirects the users' browser back to your site and issues a token which you can use to access resources.
Now, when the user navigates to the second site, here too, the application will redirect to https://youridp.example/authorize?xyz. The identity provider will see a cookie has been set, so redirects automatically to the other site without showing a login form. That's how the other site obtains a token.
Unfortunately, using authorization code with PKCE
in the front-end is not considered secure anymore. (Read this article, for example: https://medium.com/p/490545665125). Instead, you should consider implementing the BFF Security Pattern. Read how to implement it here: https://bff.gocloudnative.org/integration-manuals/quickstarts/identityserver4/quickstart/ and find a working example here: https://github.com/thecloudnativewebapp/GoCloudNative.Bff/tree/main/docs/demos
Hope this helps!