-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #110 from sharpn/develop
Adding JWT authentication for use with auth0
- Loading branch information
Showing
18 changed files
with
916 additions
and
612 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
{ | ||
public enum SupportedAuthenticationProviders | ||
{ | ||
IdentityServer | ||
IdentityServer, | ||
Jwt | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
namespace Ocelot.Configuration.Creator | ||
{ | ||
using Ocelot.Configuration.Builder; | ||
using Ocelot.Configuration.File; | ||
|
||
public class ConfigCreator | ||
{ | ||
public IAuthenticationConfig Create(FileAuthenticationOptions authenticationOptions) | ||
{ | ||
if (authenticationOptions.Provider == "Jwt") | ||
{ | ||
return CreateJwt(authenticationOptions); | ||
} | ||
|
||
return CreateIdentityServer(authenticationOptions); | ||
} | ||
|
||
private JwtConfig CreateJwt(FileAuthenticationOptions authenticationOptions) | ||
{ | ||
return new JwtConfigBuilder() | ||
.WithAudience(authenticationOptions.JwtConfig?.Audience) | ||
.WithAuthority(authenticationOptions.JwtConfig?.Authority) | ||
.Build(); | ||
} | ||
|
||
private IdentityServerConfig CreateIdentityServer(FileAuthenticationOptions authenticationOptions) | ||
{ | ||
return new IdentityServerConfigBuilder() | ||
.WithApiName(authenticationOptions.IdentityServerConfig?.ApiName) | ||
.WithApiSecret(authenticationOptions.IdentityServerConfig?.ApiSecret) | ||
.WithProviderRootUrl(authenticationOptions.IdentityServerConfig?.ProviderRootUrl) | ||
.WithRequireHttps(authenticationOptions.IdentityServerConfig.RequireHttps).Build(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
src/Ocelot/JsonConverters/AuthenticationConfigConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
using System; | ||
using Newtonsoft.Json; | ||
using Ocelot.Configuration; | ||
|
||
namespace Ocelot.AcceptanceTests | ||
{ | ||
using Newtonsoft.Json.Linq; | ||
public class AuthenticationConfigConverter : JsonConverter | ||
{ | ||
public override bool CanWrite => false; | ||
|
||
public override bool CanRead => true; | ||
|
||
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) | ||
{ | ||
throw new InvalidOperationException("Use default serialization."); | ||
} | ||
|
||
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) | ||
{ | ||
var jsonObject = JObject.Load(reader); | ||
var setting = default(IAuthenticationConfig); | ||
|
||
if (jsonObject["Provider"] != null) | ||
{ | ||
switch (jsonObject["Provider"].Value<string>()) | ||
{ | ||
case "Jwt": | ||
setting = new JwtConfig( | ||
jsonObject["Authority"].Value<string>(), | ||
jsonObject["Audience"].Value<string>()); | ||
break; | ||
|
||
default: | ||
setting = new IdentityServerConfig( | ||
jsonObject["ProviderRootUrl"].Value<string>(), | ||
jsonObject["ApiName"].Value<string>(), | ||
jsonObject["RequireHttps"].Value<bool>(), | ||
jsonObject["ApiSecret"].Value<string>()); | ||
break; | ||
} | ||
} | ||
else | ||
{ | ||
setting = new IdentityServerConfig(string.Empty, string.Empty, false, string.Empty); | ||
} | ||
|
||
serializer.Populate(jsonObject.CreateReader(), setting); | ||
return setting; | ||
} | ||
|
||
public override bool CanConvert(Type objectType) | ||
{ | ||
return objectType == typeof(IAuthenticationConfig); | ||
} | ||
} | ||
|
||
|
||
} |
Oops, something went wrong.