waqaskhan540 / identityserver-token-exchange

A solution for exchanging external (Facebook,Google,Twitter etc) tokens with IdentityServer access token.
GNU General Public License v3.0
15 stars 20 forks source link

Exchanging external Tokens (Google, Twitter, Facebook,LinkedIn) with IdentityServer access tokens using an extension grant

Installation

Package Manager

PM> Install IdentityServer.External.TokenExchange

Setup

By default the package contains implementations for Google , Facebook , Twitter & LinkedIn and can be configured using the AddDefaultExternalTokenProviders method.

 services.AddIdentityServer()

                /** identity server configs **/

                .AddDeveloperSigningCredential()
                .AddInMemoryClients(IdentityServerConfig.GetClients())
                .AddInMemoryIdentityResources(IdentityServerConfig.GetIdentityResources())
                .AddInMemoryApiResources(IdentityServerConfig.GetApiResources())
                .AddTestUsers(IdentityServerConfig.GetUsers())

               /** token exchange configs **/

                .AddTokenExchangeForExternalProviders()  //registers an extension grant
                .AddDefaultTokenExchangeProviderStore()  //registers default in-memory store for providers info
                .AddDefaultExternalTokenProviders()      //registers providers auth implementations
                .AddDefaultTokenExchangeProfileService() //registers default profile service
                .AddDefaultExternalUserStore();          //registers default in-memory user's store

Usage

POST connect/token

     client_id = [your_client_id]
     client_secret = [your_client_secret]
     scope = [your_scopes]
     grant_type = external
     provider = facebook 
     external_token  = [facebook_access_token]

You can change provider to Facebook , Google , Twitter and LinkedIn and provide respective token in the external_token parameter.

Customization

Adding Custom Provider

Step: 1 Provide external authentication provider

Provide an implementation of IExternalTokenProvider. This class will be responsible for talking to your external provider for retrieving user's info.

The name of the class must follow the naming convention (Add "AuthProvider" at the end of your your class name) otherwise the DI would be unable to resolve it.

Step: 2 Provide a custom provider store

Add a custom provider store by implementing ITokenExchangeProviderStore. This class will be responsible for managing all information abour all the providers i.e. facebook , google and custom providers.

Step: 3 Register external provider

Register your service in Startup.cs

 .AddCustomExternalTokenProvider<MyCustomAuthProvider>();
Step: 4 Register external provider store

Register your custom providers store.

  .AddCustomTokenExchangeProviderStore<MyCustomProviderStore>();

License

Please see LICENSE for licensing information.