2016-10-26 2 views
4

私はこのgithubプロジェクトhttps://github.com/openiddict/openiddict-coreを使用しています。しかし、私は手続きが何であるべきか、またはそれらを実装する方法について固執しています。ユーザーが外部IDプロバイダを使用する場合、この例ではgoogleを使用します。ローカルのopenIdトークン用google idTokenを交換するC#

私は、aspnet core webAPIを使用して、angular2アプリを実行しています。私のローカルログインはすべて正常に動作し、ユーザ名とパスワードでconnect/tokenと呼ばれ、accessTokenが返されます。

今私は外部IDプロバイダとしてgoogleを実装する必要があります。私はすべてのステップhereを踏んで、Googleログインボタンを実装しました。ユーザーがログインするとポップアップが開きます。これはGoogleのボタン用に作成したコードです。

// Angular hook that allows for interaction with elements inserted by the 
// rendering of a view. 
ngAfterViewInit() { 
    // check if the google client id is in the pages meta tags 
    if (document.querySelector("meta[name='google-signin-client_id']")) { 
     // Converts the Google login button stub to an actual button. 
     gapi.signin2.render(
      'google-login-button', 
      { 
       "onSuccess": this.onGoogleLoginSuccess, 
       "scope": "profile", 
       "theme": "dark" 
      }); 
    } 
} 

onGoogleLoginSuccess(loggedInUser) { 
    let idToken = loggedInUser.getAuthResponse().id_token; 

    // here i can pass the idToken up to my server and validate it 
} 

今私はidTokenをGoogleから持っています。 Googleのページの次のステップであるhereは、私が行うことができるGoogleのaccessTokenを検証する必要があると言いますが、私はGoogleから持っているaccessTokenをどのように交換し、自分のアプリケーションで使用できるローカルのaccessTokenを作成しますか? Googleのページの次のステップは私が行うことができます、グーグルaccessTokenを検証する必要がありますが、どのように私はすることができた私はグーグルから持っていることをaccessTokenを交換し、地元accessTokenを作成しないと言っているここで見つける

+0

IDトークンを取得するためにGoogleクライアントライブラリが必要なのかどうか疑問に思っています。https://github.com/openidd/openiddict-core/blob/dev/samples/Mvc.Server/のようなGoogle認証を使用することを検討しましたか? Startup.cs#L119? –

+0

私はクライアント側のアプリケーションを持っているだけなので、私はクライアント上のlocalStorageにストアするので、私のアプリケーションのためにaccessTokenクライアント側が必要です。私はその方法を試してみましたが、もう一度、私はクライアント側のトークンのためにトークンを交換する方法を知らないのですか? – Gillardo

答えて

6

私のアプリケーションで使用されていますか?

実装しようとしているフローは、アサーション許可として知られています。あなたはそれについてthis other SO post for more informationを読むことができます。

OpenIddictは完全にカスタム許可をサポートしているので、これはあなたが簡単にあなたのトークンエンドポイントのアクションで実装することができるものです:あなたはまた、OpenIddictオプションでそれを有効にする必要があります

[HttpPost("~/connect/token")] 
[Produces("application/json")] 
public IActionResult Exchange(OpenIdConnectRequest request) 
{ 
    if (request.IsPasswordGrantType()) 
    { 
     // ... 
    } 

    else 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." 
    }); 
} 

注:

// Register the OpenIddict services, including the default Entity Framework stores. 
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 password flow, the refresh 
    // token flow and a custom grant type. 
    .AllowPasswordFlow() 
    .AllowRefreshTokenFlow() 
    .AllowCustomFlow("urn:ietf:params:oauth:grant-type:google_identity_token") 

    // During development, you can disable the HTTPS requirement. 
    .DisableHttpsRequirement(); 

トークンリクエストを送信する場合は、右のgrant_typeを使用し、id_tokenをassertionパラメータとして送信してください。正しく動作するはずです。ここではポストマンとの一例です(Facebookのアクセストークンのためには、それはまったく同じように動作します):言っ

enter image description here

は、あなたがする必要が極めて慎重このステップとして、トークンの検証ルーチンを実装します特にエラーを起こしやすい。オーディエンス(それ以外の場合はyour server would be vulnerable to confused deputy attacks)を含め、すべてを検証することは本当に重要です。

+3

あなたの答えは毎回私を驚かす!あまりにもありがとう – Gillardo

+0

これはまだ有効な答えですか?最初のリンクでは、次のように書いています。「新しいOAuth2ドラフトは将来的にそれを変えるのに役立ちますが、主要サービスが実装を開始するまでにはしばらく時間がかかるでしょう。 過去数ヶ月間に何か変わりましたか?私はAngular2アプリを持っており、Googleのサインインボタンを使いたいと思います。もちろん、私のパスワードフローはOpenIddictで動作します。 – Makla

+2

@Maklaこの回答はまだ有効で、OpenIddictはこのシナリオを引き続きサポートしています。 「OAuth2トークン交換」のドラフトはまだ標準化されていませんが、[先月新バージョンが公開されました](https://tools.ietf.org/html/draft-ietf-oauth-token-exchange-07)です。間違いなく正しい軌道に乗っています。それは、GoogleやFacebookなどの大手プロバイダがそれを実装し始める(FWIW、Facebookはまだ最終RFCに準拠していない古いOAuth2ドラフトを使用している)までに、長年かかりそうだという。 – Pinpoint

関連する問題