Search code examples
reactjsidentityserver4oidc-client-js

How to get access_token from Identity Server hitting token endpoint, passing credentials from react client using fetch?


I need to get access token from Identity Server through an API call from react client (fetch). I don't want to load the login UI from Identity Server (implicit flow), enter credentials, redirect to the redirect_uri and then get the access_token from the url. I just want to pass the credentials through an API call (fetch) to Token endpoint and get the access token from react client (similar to http://docs.identityserver.io/en/release/endpoints/token.html).

The endpoint is - http://localhost/identityserver/core/connect/token

What are all the other data should I pass to the fetch call?

Following are the response and grant types supported by ids:

response_types_supported: [ "code", "token", "id_token", "id_token token", "code id_token", "code token", "code id_token token" ], grant_types_supported: [ "authorization_code", "client_credentials", "password", "refresh_token", "implicit" ],

May I know how to achieve this using oidc-client package(https://www.npmjs.com/package/oidc-client)? Please let me know if more details are needed so that I can update the question with more information.


Solution

  • You would need to post to the Token Endpoint using the Password grant type:

    POST /connect/token

    client_id=yourclientid& client_secret=yourclientsecret& grant_type=password& username=yourusername&password=yourusernamespassword

    This will return an Access Token not an Identity Token. If you need access to the user's information then you can obtain this from the UserInfo Endpoint.

    oidc-client helps authenticating a user via the Authorize Endpoint and therefore can't help with the Token Endpoint

    This is what the documentation says about the Password Grant:

    The spec recommends using the resource owner password grant only for “trusted” (or legacy) applications. Generally speaking you are typically far better off using one of the interactive OpenID Connect flows when you want to authenticate a user and request access tokens.