-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enhancement to add scoped account keys to the account. e.g. ``` claimsAccount.Account.SigningKeys.Add(new NatsAccountScopedSigningKey { Key = kpScopedAccountSigningKey.GetPublicKey(), Role = "chat_user", Template = new NatsUser() { } }); ``` The list of signing keys still also supports strings via NatsAccountSigningKey which has implicit string operators. Tested using $SYS.REQ.CLAIMS.UPDATE to push the new account, and then using the new SetScoped method from NatsUserClaims signing with the scoped account signing key.
- Loading branch information
1 parent
5a29473
commit b7be217
Showing
7 changed files
with
250 additions
and
4 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 |
---|---|---|
@@ -0,0 +1,104 @@ | ||
// Copyright (c) The NATS Authors. | ||
// Licensed under the Apache License, Version 2.0. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
using System.Text.Json.Serialization.Metadata; | ||
using NATS.Jwt.Models; | ||
|
||
namespace NATS.Jwt.Internal | ||
{ | ||
/// <summary> | ||
/// . | ||
/// </summary> | ||
internal class NatsAccountSigningKeyConverter : JsonConverter<List<NatsAccountSigningKey>> | ||
{ | ||
/// <summary> | ||
/// Converts a JSON representation of SigningKeys to their correct type. | ||
/// </summary> | ||
/// <param name="reader">The Utf8JsonReader used to read the JSON data.</param> | ||
/// <param name="typeToConvert">The type of the object to be converted.</param> | ||
/// <param name="options">The JsonSerializerOptions to be used during serialization.</param> | ||
/// <returns>A list of NatsAccountSigningKey.</returns> | ||
/// <exception cref="JsonException">yeah, this isn't done yet.</exception> | ||
public override List<NatsAccountSigningKey>? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
{ | ||
if (reader.TokenType == JsonTokenType.Null) | ||
{ | ||
return default; | ||
} | ||
else if (reader.TokenType != JsonTokenType.StartArray) | ||
{ | ||
throw new JsonException("Expected Null or Array"); | ||
} | ||
|
||
List<NatsAccountSigningKey> results = []; | ||
|
||
while (reader.Read()) | ||
{ | ||
if (reader.TokenType == JsonTokenType.EndArray) | ||
{ | ||
return results; | ||
} | ||
|
||
if (reader.TokenType == JsonTokenType.String) | ||
{ | ||
string? simpleSigningKey = reader.GetString(); | ||
if (simpleSigningKey != null) | ||
{ | ||
results.Add(simpleSigningKey); | ||
} | ||
} | ||
else if (reader.TokenType == JsonTokenType.StartObject) | ||
{ | ||
NatsAccountScopedSigningKey? scopedSigningKey = JsonSerializer.Deserialize(ref reader, JsonContext.Default.NatsAccountScopedSigningKey); | ||
if (scopedSigningKey != null) | ||
{ | ||
results.Add(scopedSigningKey); | ||
} | ||
} | ||
else | ||
{ | ||
throw new JsonException(); | ||
} | ||
} | ||
|
||
throw new JsonException(); | ||
} | ||
|
||
/// <summary> | ||
/// Writes the List of SigningKeys to its JSON representation. | ||
/// </summary> | ||
/// <param name="writer">The Utf8JsonWriter used to write the JSON data.</param> | ||
/// <param name="value">The List of NatsAccountSigningKeys to be written.</param> | ||
/// <param name="options">The JsonSerializerOptions to be used during serialization.</param> | ||
public override void Write(Utf8JsonWriter writer, List<NatsAccountSigningKey> value, JsonSerializerOptions options) | ||
{ | ||
if (value == null) | ||
{ | ||
writer.WriteNullValue(); | ||
} | ||
else | ||
{ | ||
writer.WriteStartArray(); | ||
|
||
foreach (NatsAccountSigningKey sk in value) | ||
{ | ||
if (sk.GetType() == typeof(NatsAccountSigningKey)) | ||
{ | ||
writer.WriteStringValue((string)sk); | ||
} | ||
else if (sk.GetType() == typeof(NatsAccountScopedSigningKey)) | ||
{ | ||
JsonSerializer.Serialize(writer, (NatsAccountScopedSigningKey)sk, JsonContext.Default.NatsAccountScopedSigningKey); | ||
} | ||
} | ||
|
||
writer.WriteEndArray(); | ||
} | ||
} | ||
} | ||
} |
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,44 @@ | ||
// Copyright (c) The NATS Authors. | ||
// Licensed under the Apache License, Version 2.0. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace NATS.Jwt.Models | ||
{ | ||
/// <summary> | ||
/// Represents an Account Scoped Signing Key. | ||
/// </summary> | ||
public record NatsAccountScopedSigningKey : NatsAccountSigningKey | ||
{ | ||
/// <summary> | ||
/// Gets or sets the kind of scoped key. | ||
/// </summary> | ||
[JsonPropertyName("kind")] | ||
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)] | ||
public string Kind { get; set; } = "user_scope"; | ||
|
||
/// <summary> | ||
/// Gets or sets the Key, usually the public key. | ||
/// </summary> | ||
[JsonPropertyName("key")] | ||
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)] | ||
public string Key { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets Role. | ||
/// </summary> | ||
[JsonPropertyName("role")] | ||
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)] | ||
public string Role { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the User Template to use. | ||
/// </summary> | ||
[JsonPropertyName("template")] | ||
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)] | ||
public NatsUser Template { get; set; } = new(); | ||
} | ||
} |
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,41 @@ | ||
// Copyright (c) The NATS Authors. | ||
// Licensed under the Apache License, Version 2.0. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace NATS.Jwt.Models | ||
{ | ||
/// <summary> | ||
/// Represents an simple signing Key. | ||
/// </summary> | ||
public record NatsAccountSigningKey | ||
{ | ||
private string _signingKey; | ||
|
||
/// <summary> | ||
/// An implicit operator to convert to a string. | ||
/// </summary> | ||
/// <param name="sk">A signing key.</param> | ||
public static implicit operator string(NatsAccountSigningKey sk) => sk._signingKey; | ||
|
||
/// <summary> | ||
/// An implicit operator to convert from a string. | ||
/// </summary> | ||
/// <param name="value">A signing key.</param> | ||
public static implicit operator NatsAccountSigningKey(string value) | ||
{ | ||
return new NatsAccountSigningKey() { _signingKey = value }; | ||
} | ||
|
||
/// <summary> | ||
/// Returns the signing key as a string. | ||
/// </summary> | ||
/// <returns>The basic signing key.</returns> | ||
public override string ToString() | ||
{ | ||
return _signingKey; | ||
} | ||
} | ||
} |
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