MMaschie
MMaschie

Reputation: 9

Identity Server 4, External providers and Web API

I’m in process of developing system which consists from such parts: - Some services under gateway (Ocelot) - Mobile client (iOS) - Identity Server 4

Mobile client hasn’t been prepared yet, so I use Postman for emulating requests from it. My problem is implementation of Authentication with External providers, like Google. It’s my first experience of using IS 4, so I have some misunderstanding and difficulties. Excuse me, if my question is too abstract or if I miss smth obvious. I successfully deployed IS 4 using all this tutorials and it works with Password Credentials flow in a proper way: I request IS for access token, sending user credentials, it returns token and I can successfully use it for access to my API methods.

Situation with External Providers are different. I’ve overviewed this tutorial (https://learn.microsoft.com/en-us/aspnet/core/security/authentication/social/google-logins?view=aspnetcore-3.1) and some other and add code from it to the IS project. I can successfully log in with Google, using a button on that IS4 web-page which goes with IS 4 Quickstart UI template. But no chance to work with API. As I understand in such workflow client-app should go for a token not to my IS as in example with a local user, but to the Google Auth provider. And I emulated it with Postman and got a strange access_token which has no data and it_token which contains username, email and so on. I try to use this id_token with requests to my API. The result is always 401.

Where I’m wrong? How should I build requests to API with token from Google? Or I have misunderstanding and there should be another flow: client goes to IS with specific request, IS goes to Google and then returns proper token to Client?

Here is configuration of authecation on the side of Web API app:

private void ConfigAuthentication(IServiceCollection services)
{
    services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
            .AddJwtBearer(JwtBearerDefaults.AuthenticationScheme, options =>
            {
                options.Authority = "http://localhost:5000";
                options.RequireHttpsMetadata = false;
                options.Audience = "k_smart_api";
            });
}

Here is config of Google-Auth on the side of IdentityServer:

services.AddAuthentication().AddGoogle(opts => {
   opts.ClientId = "My google client Id";
   opts.ClientSecret = "my google client secret";
   opts.SignInScheme = IdentityConstants.ExternalScheme;
   opts.SaveTokens = true;
});

This is how I get Access Token:

postman exampple

Upvotes: 1

Views: 1457

Answers (1)

Tore Nestenius
Tore Nestenius

Reputation: 19961

The tokens you get back from Google, is only used to Authenticate the user in Identity Server. Then after Identity Server receives those tokens, it sign-in the user and create new tokens (ID+access) that are passed to your client. you should look at using the authorization code flow in your client to authenticate the user and to get the tokens. then use the access token received to access your API.

do remember that the tokens received from Google are not used to give access to your APIs.

Upvotes: 2

Related Questions