2017-05-09 1 views
2

IdentityServer4を使用してAPIを使用するために使用するトークンを配信する認証サーバを開発しています。 MongoDBをデータベースとして使用しています。トークンを取得できるユーザーがあり、より安全なものにするためにトークンを暗号化するカスタム証明書を使用しています。 これはどのように見えるか、私のStartup.cs私AuthenticationServerです:あなたは、私がクライアント認証とパスワードの検証を行い、これらのインタフェースのカスタム実装を持って見ることができるようにIDサーバ4 - ディスカバリエンドポイントを無効にする

public void ConfigureServices(IServiceCollection services) 
{ 
    services.Configure<AppSettings>(Configuration.GetSection("AppSettings")); 

    var cert = new X509Certificate2(Path.Combine(_environment.ContentRootPath, "cert", "whatever.pfx"), "whatever"); 

    services.AddIdentityServer().AddSigningCredential(cert) 
      .AddInMemoryApiResources(Config.Config.GetApiResources()); 

    services.AddTransient<IUserRepository, UserRepository>(); 

    services.AddTransient<IClientStore, ClientStore>(); 
    services.AddTransient<IProfileService, UserProfileService>(); 
    services.AddTransient<IResourceOwnerPasswordValidator, UserResourceOwnerPasswordValidator>(); 
    services.AddTransient<IPasswordHasher<User.Model.User>, PasswordHasher<User.Model.User>>(); 

} 

。これはうまくいきます。

それから私は、生成されたトークンと別のアプリケーションを保護しています、と私はそれがIdentityServerAutheticationを使用する必要があることが規定されている(ローカルホスト:私のAuthenticationServerが実行されている5020がある)

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) 
    { 
     app.UseCors("CorsPolicy"); 

     loggerFactory.AddConsole(Configuration.GetSection("Logging")); 
     loggerFactory.AddDebug(); 

     app.UseIdentityServerAuthentication(new IdentityServerAuthenticationOptions 
     { 
      Authority = "http://localhost:5020", 
      RequireHttpsMetadata = false, 
      ApiName = "MyAPI", 
      RoleClaimType = JwtClaimTypes.Role 
     }); 

     app.UseMvc(); 
    } 

すべてが正常に動作します

のSystem.InvalidOperationException:IDX10803:「:から設定を取得できませんが、私シャットダウンがAuthenticationServerが、私はAPIからこのエラーを取得する私が保護していますならば'。 at Microsoft.IdentityModel.Protocols.ConfigurationManager`1.d__24.MoveNext() ---例外がスローされた前の場所からのスタックトレースの終了--- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(タスクタスク) Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerHandler.d__1.MoveNext() でSystem.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(タスクタスク) では失敗:Microsoft.AspNetCore.Server.Kestrel [13]

それで、APIはディスカバリエンドポイントにアクセスして、トークン(私はuserinfo_endpointだろうと思う)。

私のポイントは次のとおりです。

  • これは、ディスカバリエンドポイントが認証サーバを使用する方法の情報を取得するために使用されているようです(私のためのオープンAPIのために理にかなっている)が、私たちの場合には、我々が開発されていませんオープンAPIなので、私たちのクライアントは合意したものに過ぎず、エンドポイントを事前に伝え、私たちはIPアドレスによって制限する可能性が非常に高いでしょう。
  • ディスカバリエンドポイントを無効にする方法と、トークンを適切に復号化するために証明書をAPIでセットアップする方法はありますか。

多分、私は完全な絵が見つからず、私はばかげたことを言っているかもしれませんが、私はその背後にある概念を理解してうれしいです。 ありがとうございます

答えて

0

access_tokenを検証するには、IdentityServerにX509証明書の公開鍵が必要です。ディスカバリエンドポイントを使用して公開鍵を取得しており、公開鍵を毎回更新しています(公開鍵が変更される可能性があるため)。

IdentityServerにアクセスできない場合、APIでaccess_tokenが有効であることを保証することはできません。確認リクエストの結果のキャッシングを増やすことができます。

IdentityServer4.AccessTokenValidationでは完全にはわかりませんが、IdentityServer3.AccessTokenValidationではValidationModeLocalに設定するだけで公開鍵を一度ダウンロードします。

0

ディスカバリエンドポイントをオフ/無効にして、トークンを検証することは可能です。

IConfigurationManagerを実装し、それをIdentityServerAuthenticationOptions内のJwtBearerOptionsオブジェクトに渡す必要があります。

public class OidcConfigurationManager : IConfigurationManager<OpenIdConnectConfiguration> 
{ 
    public OidcConfigurationManager() 
    { 
     SetConfiguration(); 
    } 
    private OpenIdConnectConfiguration _config; 
    public Task<OpenIdConnectConfiguration> GetConfigurationAsync(CancellationToken cancel) 
    { 
     return Task.FromResult<OpenIdConnectConfiguration>(_config); 
    } 
    public void RequestRefresh() 
    { 
    } 

    private void SetConfiguration() 
    { 
     // Build config from JSON 
     var configJson = 
      @"{""issuer"":""http://localhost/id"",""jwks_uri"":""http://localhost/id/.well-known/openid-configuration/jwks"",""authorization_endpoint"":""http://localhost/id/connect/authorize"",""token_endpoint"":""http://localhost/id/connect/token"",""userinfo_endpoint"":""http://localhost/id/connect/userinfo"",""end_session_endpoint"":""http://localhost/id/connect/endsession"",""check_session_iframe"":""http://localhost/id/connect/checksession"",""revocation_endpoint"":""http://localhost/id/connect/revocation"",""introspection_endpoint"":""http://localhost/id/connect/introspect"",""frontchannel_logout_supported"":true,""frontchannel_logout_session_supported"":true,""scopes_supported"":[""openid"",""profile"",""api1"",""offline_access""],""claims_supported"":[""sub"",""name"",""family_name"",""given_name"",""middle_name"",""nickname"",""preferred_username"",""profile"",""picture"",""website"",""gender"",""birthdate"",""zoneinfo"",""locale"",""updated_at""],""grant_types_supported"":[""authorization_code"",""client_credentials"",""refresh_token"",""implicit"",""password""],""response_types_supported"":[""code"",""token"",""id_token"",""id_token token"",""code id_token"",""code token"",""code id_token token""],""response_modes_supported"":[""form_post"",""query"",""fragment""],""token_endpoint_auth_methods_supported"":[""client_secret_basic"",""client_secret_post""],""subject_types_supported"":[""public""],""id_token_signing_alg_values_supported"":[""RS256""],""code_challenge_methods_supported"":[""plain"",""S256""]}"; 
     _config = new OpenIdConnectConfiguration(configJson); 

     // Add signing keys if not present in json above 
     _config.SigningKeys.Add(new X509SecurityKey(cert)); 
    } 
} 

今すぐあなたのIdentityServerAuthenticationOptionsの内側にいくつかのJwtBearerOptionsにそのconfigオブジェクトを渡す(ビット迷惑が、それは私の知っている唯一の方法です)今

var identityServerOptions = new IdentityServerAuthenticationOptions 
{ 
    Authority = "http://localhost:5020", 
    RequireHttpsMetadata = false, 
    ApiName = "MyAPI", 
    RoleClaimType = JwtClaimTypes.Role, 

}; 
var jwtBearerOptions = new JwtBearerOptions() {ConfigurationManager = new OidcConfigurationManager()}; 
var combinedOptions = CombinedAuthenticationOptions.FromIdentityServerAuthenticationOptions(identityServerOptions); 
combinedOptions.JwtBearerOptions = jwtBearerOptions; 
app.UseIdentityServerAuthentication(combinedOptions); 

:ここ

は、いくつかのサンプルコードですOIDCディスカバリエンドポイントがオフであっても、APIがトークンを受信して​​シグネチャを検証できるようになります。

0

API検証ミドルウェアは、起動時に - そして少なくとも(デフォルトでは) - 24時間ごとに発見文書のコピーをダウンロードします。

署名の検証が失敗した場合(スケジュール外のキーのロールオーバーに対応するため)、ダウンロードを再度開始することがあります。

すべての構成値を静的に定義できますが、動的構成更新の利点がすべて失われています。

検出エンドポイントが利用できない場合、おそらくトークンサービス全体が機能していない可能性があります。これはおそらく大きな問題です。

+0

これは、ディスカバリエンドポイントを使用可能にしてもセキュリティリスクが発生しないことを意味しますか?私はエンドポイントのメタデータを無効にしなければならなかったWCFから来ています。 – Learner

関連する問題