Rich Twitter Digits/Google Auth with OpenIdDictServer

前端 未结 1 1847
温柔的废话
温柔的废话 2020-12-06 22:19

Our app requires sign-in by either mobile number or Google. We are planning to Twitter Digits for mobile number authentication.

The flow of registration and authenti

相关标签:
1条回答
  • 2020-12-06 22:52

    This works very well, but I am wondering what is the cleanest way to support what I am trying to achieve.

    I'd personally go with a custom grant type:

    [HttpPost("~/connect/token")]
    [Produces("application/json")]
    public IActionResult Exchange(OpenIdConnectRequest request)
    {
        if (request.GrantType == "urn:ietf:params:oauth:grant-type:google_identity_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);
    
            // Manually validate the identity token issued by Google,
            // including the issuer, the signature and the audience.
            // Then, copy the claims you need to the "identity" instance.
    
            // 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."
        });
    }
    

    Note that you'll also have to enable it in the OpenIddict options:

    // Register the OpenIddict services.
    services.AddOpenIddict()
        // Register the Entity Framework stores.
        .AddEntityFrameworkCoreStores<ApplicationDbContext>()
    
        // Register the ASP.NET Core MVC binder used by OpenIddict.
        // Note: if you don't call this method, you won't be able to
        // bind OpenIdConnectRequest or OpenIdConnectResponse parameters.
        .AddMvcBinders()
    
        // Enable the token endpoint.
        .EnableTokenEndpoint("/connect/token")
    
        // Enable the refresh token flow and a custom grant type.
        .AllowRefreshTokenFlow()
        .AllowCustomFlow("urn:ietf:params:oauth:grant-type:google_identity_token")
    
        // During development, you can disable the HTTPS requirement.
        .DisableHttpsRequirement();
    

    When sending a token request, make sure to use the right grant_type and to send your id_token as the assertion parameter, and it should work.

    Here's an example using Facebook access tokens:

    Be extremely careful when implementing the token validation routine, as this step is particularly error-prone. It's really important to validate everything, including the audience (otherwise, your server would be vulnerable to confused deputy attacks).

    0 讨论(0)
提交回复
热议问题