Which one is more secure and why?
Both of them are secure, it depends in the environment you are using it.
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.
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 with Instagram
and define which API's
you need. Instagram
will provide you with client_id
and client_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 to Instagram Authentication Server
with below parameters.
1. `response_type` with the value `code`
2. `client_id` you have get from `Instagram`
3. `redirect_uri` this is a url on your server which do the second call
4. `scope` a space delimited list of scopes
5. `state` with a CSRF token.
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 your client_secrect
easily. This is why you need another step.
You receive a code
and state
. The code
here is temporary
and is not saved any where.
Then you make a second
call to Instagram API
(from your server)
1. `grant_type` with the value of `authorization_code`
2. `client_id` with the client identifier
3. `client_secret` with the client secret
4. `redirect_uri` with the same redirect URI the user was redirect back to
5. `code` which we have already received.
As the call is made from our server we can safely use client_secret
( which shows who we are), with code
which shows the user have granted out client_id
to use the resource.
In response we will have access_token