{"id":13595459,"url":"https://github.com/waqaskhan540/IdentityServerExternalAuth","last_synced_at":"2025-04-09T13:32:05.657Z","repository":{"id":127332485,"uuid":"104107259","full_name":"waqaskhan540/IdentityServerExternalAuth","owner":"waqaskhan540","description":"A solution for exchanging external (Facebook,Google,Twitter etc) tokens with IdentityServer access token.","archived":false,"fork":false,"pushed_at":"2021-08-25T14:52:26.000Z","size":273,"stargazers_count":137,"open_issues_count":8,"forks_count":37,"subscribers_count":9,"default_branch":"master","last_synced_at":"2024-08-01T16:40:32.618Z","etag":null,"topics":["exchange-external","identityserver-access","identityserver4"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/waqaskhan540.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2017-09-19T17:39:43.000Z","updated_at":"2024-07-06T06:38:43.000Z","dependencies_parsed_at":"2024-01-15T16:29:58.044Z","dependency_job_id":"640861cd-5704-4a7c-85c0-16218894a86c","html_url":"https://github.com/waqaskhan540/IdentityServerExternalAuth","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/waqaskhan540%2FIdentityServerExternalAuth","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/waqaskhan540%2FIdentityServerExternalAuth/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/waqaskhan540%2FIdentityServerExternalAuth/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/waqaskhan540%2FIdentityServerExternalAuth/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/waqaskhan540","download_url":"https://codeload.github.com/waqaskhan540/IdentityServerExternalAuth/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223394407,"owners_count":17138539,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["exchange-external","identityserver-access","identityserver4"],"created_at":"2024-08-01T16:01:50.423Z","updated_at":"2024-11-06T18:30:41.300Z","avatar_url":"https://github.com/waqaskhan540.png","language":"C#","funding_links":[],"categories":["C#","C\\#"],"sub_categories":[],"readme":"\u003e Project Available as Nuget Package , [click here](https://github.com/waqaskhan540/identityserver-token-exchange)\n# Exchanging external Tokens (Google, Twitter, Facebook,LinkedIn) with IdentityServer access tokens using an extension grant\n\n## Supported providers\n\n - [x] Facebook\n - [x] LinkedIn\n - [x] Twitter\n - [x] Google\n - [ ] GitHub \n \n## How to exchange external tokens for IdentityServer access token ?\n* Request authentication using the provider's native library.\n* Exchange external token with IdentityServer token by making following request to IdentityServer.\n\n```\nPOST connect/token\n     \n     client_id = [your_client_id]\n     client_secret = [your_client_secret]\n     scopes = [your_scopes]\n     grant_type = external\n     provider = facebook \n     external_token  = [facebook_access_token]\n```\n * If user is already registered then IdentityServer will return the access token, otherwise it will send the user's data and prompt for an email parameter to be added, in this case make another request with an extra ```email``` parameter.\n \n ```\nPOST connect/token\n     \n     client_id = [your_client_id]\n     client_secret = [your_client_secret]\n     scopes = [your_scopes]\n     grant_type = external\n     provider = facebook \n     email = myemail@abc.com\n     external_token  = [facebook_access_token]\n```\n\nYou can change ```provider``` to ```Facebook``` , ```Google``` , ```Twitter``` and ```LinkedIn``` and provide respective token in the ```external_token``` parameter.\n\n## How to setup an external provider\n\n1. ##### Derive an interface from ```IExternalAuthProvider```\n\n```csharp\n\npublic interface IMyCustomProvider : IExternalAuthProvider {\n    Provider provider {get;}\n}\n```\n2. ##### Add your provider to ```ProviderType``` enum\n\n```csharp\npublic enum ProviderType {\n  \n  Facebook,\n  Twitter,\n  Google,\n  MyCustomProvider\n}\n```\n3. ##### Add provider info to ```ProviderDataSource```\n\n```csharp\n\n public class ProviderDataSource\n    {\n        public static IEnumerable\u003cProvider\u003e GetProviders()\n        {\n            return new List\u003cProvider\u003e\n            {\n                new Provider\n                {\n                    ProviderId = 1,\n                    Name = \"Facebook\",\n                    UserInfoEndPoint = \"https://graph.facebook.com/v2.8/me\"\n                },\n                new Provider\n                {\n                    ProviderId = 2,\n                    Name = \"Google\",\n                    UserInfoEndPoint = \"https://www.googleapis.com/oauth2/v2/userinfo\"\n                },\n                 new Provider\n                {\n                    ProviderId = 3,\n                    Name = \"Twitter\",\n                    UserInfoEndPoint = \"https://api.twitter.com/1.1/account/verify_credentials.json\"\n                },\n                new Provider \n                {\n                    ProviderId = 4,\n                    Name=\"MyCustomProvider\",\n                    UserInfoEndPoint = \"[url to end point which validates the token and returns user data]\"\n                }\n            };\n        }\n    }\n\n```\n\n4. ##### Provide an implementation for ```IMyCustomProvider```\n\n```csharp\npublic class MyCustomProvider : IMyCustomProvider {\n\nprivate readonly HttpClient _httpClient;\npublic MyCustomProvider(HttpClient httpClient) {\n  _httpClient = httpClient;\n}\n\npublic Provider =\u003e_providerRepository.Get()\n                                    .FirstOrDefault(x =\u003e x.Name.ToLower() == ProviderType.MyCustomProvider.ToString().ToLower());\n                                    \npublic JObject GetUserInfo(string accessToken) {\n\n var query = \"[build your request according to your providers configuration]\";\n \n var result = _httpClient.GetAsync(Provider.UserInfoEndPoint + query).Result;\n            if (result.IsSuccessStatusCode)\n            {\n                var infoObject = JObject.Parse(result.Content.ReadAsStringAsync().Result);\n                return infoObject;\n            }\n            return null;\n\n}\n}\n```\n5. ##### Bind ```IMyCustomProvider``` in ```ServiceCollectionExtensions```\n\n```csharp\n public static IServiceCollection AddProviders(this IServiceCollection services)\n        {\n            services.AddTransient\u003cIFacebookAuthProvider, FacebookAuthProvider\u003e();\n            services.AddTransient\u003cITwitterAuthProvider, TwitterAuthProvider\u003e();\n            services.AddTransient\u003cIGoogleAuthProvider, GoogleAuthProvider\u003e();\n            services.AddTransient\u003cIMyCustomProvider,MyCustomProvider\u003e();\n            return services;\n        }\n```\n\n6. ##### Add ```MyCustomProvider``` to ```ExternalAuthenticationGrant```\n```csharp\n  providers = new Dictionary\u003cProviderType, IExternalAuthProvider\u003e();\n            providers.Add(ProviderType.Facebook, _facebookAuthProvider);\n            providers.Add(ProviderType.Google, _googleAuthProvider);\n            providers.Add(ProviderType.Twitter, _twitterAuthProvider);\n            providers.Add(ProviderType.LinkedIn, _linkedAuthProvider);\n            providers.Add(ProviderType.MyCustomProvider, _myCustomProvider);\n```\n7. ##### Make a request to IdentityServer using new provider\n\n```\nPOST connect/token\n     \n     client_id = [your_client_id]\n     client_secret = [your_client_secret]\n     scopes = [your_scopes]\n     grant_type = external\n     provider = mycustomprovider \n     external_token  = [access_token_from_custom_provider]\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwaqaskhan540%2FIdentityServerExternalAuth","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwaqaskhan540%2FIdentityServerExternalAuth","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwaqaskhan540%2FIdentityServerExternalAuth/lists"}