Genius DM

IdentityServer > Overview of IdentityServer 본문

Architecture

IdentityServer > Overview of IdentityServer

Damon Jung 2018. 7. 26. 22:25

IdentityServer 간략 구조

IdentityServer 의 간략한 구조를 살펴보고자 한다.



Overview

OWIN

Open Web Interface for .NET 의 약자이다. http://owin.org/html/spec/owin-1.0.html 에 인터페이스에 대한 정의 및 프로토콜이 명시되어 있다. 기존 ASP.NET 의 지나친 IIS 결합도, 그리고 너무 무거운 System.Web 의 의존성을 탈피하고자 탄생한 인터페이스이다. OWIN 을 통해서 Console Application 은 물론 원하는 Application 도메인에서 호스팅이 가능하며, Func<Environment, Task> Delegate 를 Middleware 라고 부르는 것이 특징이다. IdentityServer 도 OWIN 인터페이스 규약을 지켜서 Middleware 로써 동작하므로, HTTP 통신 진입점이 있고 인증 서비스가 필요한 모든 Application 에 적용 할 수 있다.


Endpoints

IdentityServer 에는 HTTP/HTTPs 요청 진입점들이 존재하는데, 14개 정도의 Controller 들이 존재한다. 가장 대표적인 것만 간추리면

    • TokenEndPointContoller 
    • AccessTokenValidationController
    • AuthorizeEndpointController

정도가 되겠다.


Decorator

데코레이터 디자인 패턴에 의해 설계된 클래스이며 보통 Base 클래스나 상위 Interface 를 구현하는 어떤 클래스에 대한 추가적인 "꾸밈" 이 필요할 때 유용한 패턴이다. 확장 클래스가 필요한 경우 활용하는 것이 대부분이다. IdentityServer 는 IoC 컨테이너로 Autofac 을 사용하는데, AutofacConfig 를 이용하여 RegisterDecorator 를 통해 Decorator 클래스를 등록하여 활용할 수 있다. EventServiceDecorator 가 유일하게 포함되어 있는 데코레이터 클래스이다. 



Extensions

확장 기능을 부여하기 위해 필요한 객체. 보통 Static 으로 현재 객체를 명시해서 마치 기본적으로 제공되는 기능을 부여한 것 처럼 사용할 때 필요한 객체이다.



Store Interfaces

가장 중요한 인터페이스 중에 하나이다. Store 의미 그대로 보관한 데이터를 관리하는 인터페이스로써 IClientStore 처럼 관리되는 대상 데이터로 네이밍을 구성한다. 만약 Token 이나 Client, AuthorizationCode 따위를 데이터베이스나 파일시스템에서 보관하고자 커스터마이징이 필요하다면 Store Interface 를 구현하는 것이 첫 번째 할 일이다.

    • IScopeStore
    • IClientStore
    • IAuthorizationCodeStore
    • ITokenHandleStore

등이 있고, Signature 만 봐도 목적이 뚜렷하기 때문에 분석 및 구현이 간단하다.



Service Interfaces

필요한 서비스를 구현할 수 있다. 



Validator Interfaces

각 종 인증 절차에서 사용하는 검증 프로세스에 대한 인터페이스이다. 기본 OAuth Flow 에서 필요한 인증 외에 Application 에서 특별히 사용하는 암호화 또는 검증 절차가 추가적으로 존재하는 경우, 이 인터페이스 구현을 통해서 인증 절차를 Middleware 수준에서 처리할 수 있다.



Configuration

Service Factory 를 활용하기 위한 IdentityServer 구성을 의미한다. 가장 중요하고 핵심적인 3가지는 아래와 같다.

    • IdentityServerServiceFactory
    • IdentityServerOptions
    • LoggingConfiguration



Autofac ( IoC Container )

Service Factory 에 등록된 각 종 객체들을 컴파일 타임에서 DI 해주며 Controller 에 지정한 Interface 를 완성시켜준다. Configuration 단계에서 내가 사용하려는 서비스 또는 기능을 선택하여 IoC Container 에 등록시킬 수 있다. IdentityServerServiceFactory 의 멤버들에 명시적으로 사용하려는 클래스를 지정하는 것이 그 방법이다. 이를테면 ServiceFactory 에 SecretValidators 에 Default 클래스를 아래와 같이 지정하고 있다.

SecretValidators = new List<Registration<ISecretValidator>>
{
     new Registration<ISecretValidator, HashedSharedSecretValidator>(),
     new Registration<ISecretValidator, X509CertificateThumbprintSecretValidator>()
};
특정 Controller 생성자에서 ISecretValidator 인터페이스를 인자로 받고 있는데, Autofac 이 해당 클래스를 알아서 DI 해주게 되고, HTTP 요청이 해당 컨트롤러로 진입했을 때 위에서 Registration 처리한 클래스가 사용되게 된다. 참고로 이렇게 Interface 를 선언하고, 필요한 객체를 주입하는 패턴을 Strategy Pattern 이라고 부른다.



Service Factory

IdentityServerServiceFactory 는 의미처럼 Factory 패턴에 의해 설계된 클래스이며, IdentityServer 에서 인증 절차를 수행하기 위해 필요한 모든 공정을 멤버로 지니고 있다.

  • UserService
  • ScopeStore
  • ClientStore
  • AuthorizationCodeStore
  • TokenHandleStore
  • ConsentStore
  • RefreshTokenStore
  • CustomRequestValidator
  • TokenService
  • SecretValidators

멤버들만 보아도 인증을 수행하기 위한 공장이라는 것이 느껴진다. IdentityServer 는 디자인 패턴에 대한 고민을 충분히하고, 해당 클래스의 Naming 또한 디자인 패턴 사상에 맞게 아주 정확하게 지정되어 있다.



















Overview of IdentityServer


Let's see an overview of IdentityServer structure.



Overview

OWIN

It's an abbv for Open Web Interface for .NET. You can find the specification of the interface and protocol in here http://owin.org/html/spec/owin-1.0.html. The interface comes down to break the strong bind between IIS and ASP.NET and the coherence to the heavy System.Web. You can even host IdentityServer on a console application, big thanks to OWIN, and another application domains. What's interesting is that OWIN calls Func<Environment, Task>, a delegate, "Middleware". IdentityServer is built on OWIN so it runs on a server as a middleware. As the result, you can setup IdentityServer on any type of application as long as it has an HTTP window and requires an authentication service.


Endpoints

There're several HTTP/HTTPs request endpoints in IdentityServer and there're approx 14 controllers. The most important three are

    • TokenEndPointContoller 
    • AccessTokenValidationController
    • AuthorizeEndpointController



Decorator

Decorator class is designed under the principle of ' Decorator Pattern ', the design pattern to make an extension class or functionalities on an existing base class or a parent class implementing some interface. IdentityServer uses Autofac for an IoC container and you can register decorator classes by RegisterDecorator in AutofacConfig. EventServiceDecorator is the only decorator class in the project.



Extensions

Literally it's for extensions, usually made upon static keyword to make developer feel like they are built-in functions or something.



Store Interfaces

Probably the most important interfaces among the others. These interfaces are in charge of storing something, as you may guess by the name, the interfaces just name after a target data. IClientStore means it's going to manage Client data. If you are going to need to save your token, client, authorization code in your own database or your file system, implementing these interfaces is the first thing to do.

    • IScopeStore
    • IClientStore
    • IAuthorizationCodeStore
    • ITokenHandleStore

And etc. The signature is so simple that you can get it done within no time.



Service Interfaces

You can implement some services you might want to have.



Validator Interfaces

Interfaces for implementing validators. If your application should be built with special validation procedures such as a cryptograpic job, IP white list, and that sort of things, and if the default OAuth validation flow is not enough for your app, you can implement one of these interfaces and you can perform the additional validation during the middleware level process.



Configuration

It's literally the IdentityServer configuration. Below are the most important, essential three classes.

    • IdentityServerServiceFactory
    • IdentityServerOptions
    • LoggingConfiguration



Autofac ( IoC Container )

Autofac dependency-injects designated classes in the service factory at compile time and eventually complete the interface members in controllers. You can register a choosen class to your IoC container at the configuration stage. One good is example is registering a class to one of the members in IdentityServerServiceFactory class. Below is the default registration behavior by which HashedSharedServretValidator and X509CertificateThumbprintSecretValidator can be ready to be running in the future.

SecretValidators = new List<Registration<ISecretValidator>>
{
     new Registration<ISecretValidator, HashedSharedSecretValidator>(),
     new Registration<ISecretValidator, X509CertificateThumbprintSecretValidator>()
};

Some controller constructor takes ISecretValidator as an argument. Autofac does the heavy lifting for you, the DI jobs, and when HTTP requests comes through to the controller, the registered class will be running in the context. FYI, this kind of pattern is called " Strategy Pattern "



Service Factory

IdentityServerServiceFactory is literally a class designed by the famous factory pattern. This factory has all kind of manufacture processes as class members in order to handle a token.

    • UserService
    • ScopeStore
    • ClientStore
    • AuthorizationCodeStore
    • TokenHandleStore
    • ConsentStore
    • RefreshTokenStore
    • CustomRequestValidator
    • TokenService
    • SecretValidators

Don't these remind you of an actual factory, just looking at those members? I can see the IdentityServer writer seriously took the design patterns into consideration and naming is very precisely and beautifully tailored to meet the design pattern ideas.

































'Architecture' 카테고리의 다른 글

IdentityServer > Basic  (1) 2018.07.26
RabbitMQ > Basic Understanding  (0) 2018.07.10
Comments