openiddict-core icon indicating copy to clipboard operation
openiddict-core copied to clipboard

Individual Accounts & Social Logins for .net Core Web APIs

Open behrooz66 opened this issue 10 years ago • 8 comments

I am trying to figure out whether the following is possible and how can it be done: Implementing individual user accounts login enabled for Web Api in .Net Core using OpenIddict, providing the following capabilities:

  • Generating access tokens,
  • Generating refresh tokens,
  • Enabling social logins (google, facebook, also capable of renewing their tokens using refresh token),
  • Enabling roll-based authorization
  • Using local database for storage (EntityFramework...)

I wasn't able to get this under control using the available documents, I was wondering if anybody has done something similar before or knows where to start.

The thing is, I prefer not to use any server side views, I would rather have a completely disjoint client side app which only communicates to the server(s) using API end points. Therefore, something like the implicit which requires creating server side views and cookies is not ideal.

I know if I exclude the social logins from my list of requirements, it becomes very easy. But that is the problem. I can have a button on my app that redirects to [for example] a google sign in page, and when google returns its access_token, I can verify it on the server side, however, I don't know how can I generate my own access_token and refresh_token afterwards.

Any suggestions?

behrooz66 avatar Sep 27 '16 18:09 behrooz66

You can take a look at this example. https://github.com/openiddict/openiddict-core/blob/dev/samples/Mvc.Server/Controllers/AuthorizationController.cs#L174

You just need to create a new authentication ticket after your custom logic is done.

jabas06 avatar Sep 27 '16 20:09 jabas06

But that is the problem. I can have a button on my app that redirects to [for example] a google sign in page, and when google returns its access_token, I can verify it on the server side, however, I don't know how can I generate my own access_token and refresh_token afterwards.

Though I strongly recommend the approach used by the MVC server sample (i.e the authorization code or the implicit flows) as it's easier to implement, far less error prone and much easier to customize, this flow is supported in the most recent OpenIddict bits, that allow handling custom grants (for more info about the "assertion grant", read http://stackoverflow.com/a/39018048/542757 and the specifications it mentions).

To implement that, you'll have to enable custom flow support by calling services.AddOpenIddict().AllowCustomFlow("urn:ietf:params:oauth:grant-type:google_access_token"). With this snippet, OpenIddict will allow you to handle grant_type=urn:ietf:params:oauth:grant-type:google_access_token requests in your own token endpoint action:

[HttpPost("~/connect/token")]
[Produces("application/json")]
public IActionResult Exchange(OpenIdConnectRequest request) {
    if (request.GrantType == "urn:ietf:params:oauth:grant-type:google_access_token") {
        // Reject the request if the "assertion" parameter is missing.
        if (string.IsNullOrEmpty(request.Assertion)) {
            return BadRequest(new OpenIdConnectResponse {
                Error = OpenIdConnectConstants.Errors.InvalidRequest,
                ErrorDescription = "The mandatory 'assertion' parameter was missing."
            });
        }

        // Create a new ClaimsIdentity containing the claims that
        // will be used to create an id_token and/or an access token.
        var identity = new ClaimsIdentity(OpenIdConnectServerDefaults.AuthenticationScheme);

        // Validate the access token using Google's token validation
        // endpoint and add the user claims you retrieved here.

        // Create a new authentication ticket holding the user identity.
        var ticket = new AuthenticationTicket(
            new ClaimsPrincipal(identity),
            new AuthenticationProperties(),
            OpenIdConnectServerDefaults.AuthenticationScheme);

        ticket.SetScopes(
            OpenIdConnectConstants.Scopes.OpenId,
            OpenIdConnectConstants.Scopes.OfflineAccess);

        return SignIn(ticket.Principal, ticket.Properties, ticket.AuthenticationScheme);
    }

    return BadRequest(new OpenIdConnectResponse {
        Error = OpenIdConnectConstants.Errors.UnsupportedGrantType,
        ErrorDescription = "The specified grant type is not supported."
    });
}

image

kevinchalet avatar Sep 28 '16 01:09 kevinchalet

Thank you @PinpointTownes , much appreciated. Since you mentioned you suggest the implicit flow demonstrated in the MVC.Server sample, would you mind explaining a bit how do I use that for a single page app? More precisely, I mean in a JS app you will need to have the token in hand and the ability to renew the token somehow (but refresh token doesn't exist in implicit flow). How do you obtain the token, and how do you renew it in that case?

behrooz66 avatar Sep 28 '16 17:09 behrooz66

just a long shot, would it even be possible for you to create a sample that does what I have described? I know many people who are struggling to build something like that.

behrooz66 avatar Sep 28 '16 17:09 behrooz66

More precisely, I mean in a JS app you will need to have the token in hand and the ability to renew the token somehow (but refresh token doesn't exist in implicit flow). How do you obtain the token, and how do you renew it in that case?

To "refresh a token" with the implicit flow, you must send an authorization request in a hidden frame, with prompt=none.

just a long shot, would it even be possible for you to create a sample that does what I have described?

@shaunluttin recently published a great sample for Aurelia: https://github.com/openiddict/openiddict-samples/tree/master/samples/ImplicitFlow. Adding a prompt=none demo would be a nice addition.

kevinchalet avatar Sep 29 '16 08:09 kevinchalet

I opened a ticket to track that: https://github.com/openiddict/openiddict-samples/issues/11.

kevinchalet avatar Sep 29 '16 08:09 kevinchalet

Let me see if I am understanding correctly.

You discourage this:

                +----------+ +------------------------> +------------+
                |  Client  |                            |Third Party |
                +----------+ <------------------------+ +------------+
                    ^   +            access_token
                    |   |              id_token
                    |   |
local_access_token  |   | access_token
local_id_token      |   | id_token
                    +   v
                +-----------+
                |OpenIddict |
                +-----------+

You encourage this:

                +----------+
                |  Client  |
                +----------+
                    ^   +
                    |   |
                    |   |
local_access_token  |   |
local_id_token      |   |
                    +   v
                +-----------+ +------------------------> +------------+
                |OpenIddict |                            |Third Party |
                +-----------+ <------------------------+ +------------+
                                      access_token,
                                      id_token

Is that right?

shaunluttin avatar Oct 14 '16 17:10 shaunluttin

@shaunluttin for the record: yep :smile:

kevinchalet avatar Oct 27 '16 12:10 kevinchalet