OAuth 2.0 has multiple workflows. I have a few questions regarding the two.
- Authorization code flow - User logs in from client app, authorization server returns an authorization code to the app. The app then exchanges the authorization code for access token.
- Implicit grant flow - User logs in from client app, authorization server issues an access token to the client app directly.
What is the difference between the two approaches in terms of security? Which one is more secure and why?
I don't see a reason why an extra step (exchange authorization code for token) is added in one work flow when the server can directly issue an Access token.
Different websites say that Authorization code flow is used when client app can keep the credentials secure. Why?
Let me summarize the points that I learned from above answers and add some of my own understandings.
Authorization Code Flow!!!
Implicit Grant Flow!!!
The
access_token
is what you need to call a protected resource (an API). In the Authorization Code flow there are 2 steps to get it:code
to the API consumer (called the "Client").code
obtained in #1 for anaccess_token
, authenticating itself with aclient_id
andclient_secret
access_token
.So, there's a double check: the user that owns the resources surfaced through an API and the client using the API (e.g. a web app). Both are validated for access to be granted. Notice the "authorization" nature of OAuth here: user grants access to his resource (through the
code
returned after authentication) to an app, the app get's anaccess_token
, and calls on the user's behalf.In the implicit flow, step 2 is omitted. So after user authentication, an
access_token
is returned directly, that you can use to access the resource. The API doesn't know who is calling that API. Anyone with theaccess_token
can, whereas in the previous example only the web app would (it's internals not normally accessible to anyone).The implicit flow is usually used in scenarios where storing
client id
andclient secret
is not recommended (a device for example, although many do it anyway). That's what the the disclaimer means. People have access to the client code and therefore could get the credentials and pretend to become resource clients. In the implicit flow all data is volatile and there's nothing stored in the app.Both are them are secure, it depends in the environment you are using it.
It is simple. Your client is not secure. Let's see it in details.
Consider you are developing an application against
Instagram API
, so you register your APP withInstagram
and define whichAPI's
you need.Instagram
will provide you withclient_id
andclient_secrect
On you web site you set up a link which says. "Come and Use My Application". Clicking on this your web application should make two calls to
Instagram API
.First
send a request toInstagram Authentication Server
with below parameters.You don't send
client_secret
, You could not trust the client (The user and or his browser which try to use you application). The client can see the url or java script and find yourclient_secrect
easily. This is why you need another step.You receive a
code
andstate
. Thecode
here istemporary
and is not saved any where.Then you make a
second
call toInstagram API
(from your server)As the call is made from our server we can safely use
client_secret
( which shows how we are) withcode
which shows the user have granted outclient_id
to use the resource.In response we will have
access_token
From practical perspective (What I understood), The main reason for having Authz code flow is :
"The authorization server authenticates the resource owner (via the user-agent) and establishes whether the resource owner grants or denies the client's access request"
Apart from that, Using refresh tokens, Apps can get long term access to user data.
I'll add something here which I don't think is made clear in the above answers:
tl;dr don't use implicit flow if you don't trust the users machine to hold tokens but you do trust your own servers.
The implicit grant is similar to the authorization code grant with two distinct differences.
It is intended to be used for user-agent-based clients (e.g. single page web apps) that can’t keep a client secret because all of the application code and storage is easily accessible.
Secondly instead of the authorization server returning an authorization code which is exchanged for an access token, the authorization server returns an access token.
Please find details here http://oauth2.thephpleague.com/authorization-server/which-grant/