1

I write a rest api with yii2 and i am using oAuth2 , the problem is when user want login , client web application should send request to get token , request should contain client_id and secret_key and username and password in this case user can simply inspect element and click to network and see posted parameter to the server this means user can see client_id and secret_key. client_id and secret_key are signature for each application and server can find out witch application use api. how to handle this security issue?

Kavindu Dodanduwa
  • 12,193
  • 3
  • 33
  • 46
zia
  • 278
  • 1
  • 10
  • Hello, welcome to SO. In your question, do you mean you expose client_id and client_secret of your app to end user (human user).? What is your client like ? Is it a Single page application or a web app ? – Kavindu Dodanduwa Jan 20 '18 at 14:12
  • 1
    Hello , client_id and secret is unique for each app such ass Single page application with angular or react , or android app , etc ... – zia Jan 22 '18 at 13:36

1 Answers1

0

It seems you have missed out one key element of OAuth 2.0, client type.

OAuth 2.0 defines two types of clients, public clients and confidentiatl clients.

2.1. Client Types

confidential

These are the clients which can protect a credential. They have the full potential to use authorization code grant type, which obtain token from backchannel request. Because they use backchannel to obtain tokens, their credentials are never exposed to end user(via user agent)

public

Clients which cannot protect credentials. For example SPA clients and mobile apps comes to this category.

In your case, you seems to have a public client (user agent based application in broswer as it seems). In such case, you should set your client type to a public client. If this is not the case, you are not utilizing a proper back channel call from your web application.

Additionally, public clients which use authorization code flow can use PKCE to avoid authorization code theft attacks. Related RFC can be found from RFC7636

Community
  • 1
  • 1
Kavindu Dodanduwa
  • 12,193
  • 3
  • 33
  • 46
  • do you mean i should not check client_id and secret_id for this type of client in my rest api? and just check end user , username and password? in this approach I can not determine witch application requested to server is it right? – zia Jan 22 '18 at 14:01
  • on the other hand witch grant_type should i use for public client type app , i use password grant_type and send this parameter with post request : client_id,client_secret,username,password and rest give me access_token and refresh token. – zia Jan 22 '18 at 15:19
  • @zia You must always have client_id. But if your client type is public and does not have the ability to do a back channel token call, you cannot utilise client_secret. Reason is the same as you are facing now. Client will expose the secret to end user. – Kavindu Dodanduwa Jan 23 '18 at 02:35
  • @zia It depends on your application. Password grant type is for trusted clients or in cases you have legacy systems. Note that this grant type ruins the necessity of OAuth.! You expose end user credentials to client, which is not what we want from OAuth. So better use implicit flow with your single page app. – Kavindu Dodanduwa Jan 23 '18 at 02:42
  • 1
    thank you but i have another question yet witch grant type have to use for mobile applications? – zia Jan 23 '18 at 06:30
  • @zia Mobile clients are recommended to use PKCE. I have updated answer with content – Kavindu Dodanduwa Jan 23 '18 at 08:05