-
Notifications
You must be signed in to change notification settings - Fork 151
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ability to set user credentials from strings(s) #885
Merged
Changes from 6 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
677bd5b
Ability to set user credentials from strings(s)
scottf 3f7aedc
Ability to set user credentials from strings(s)
scottf 23b5f61
Ability to set user credentials from strings(s)
scottf 8a4dab8
api doc
scottf 74970d5
Merge branch 'main' into issue-882
scottf a31336e
address review comments
scottf 98ff258
address review comments
scottf 75271c5
address review comments
scottf 17087b7
fixed doc
scottf File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
|
@@ -12,6 +12,7 @@ | |
// limitations under the License. | ||
|
||
using System.IO; | ||
using System.Security; | ||
|
||
namespace NATS.Client | ||
{ | ||
|
@@ -56,33 +57,19 @@ public DefaultUserJWTHandler(string jwtFilePath, string credsFilePath) | |
/// <returns>The encoded JWT</returns> | ||
public static string LoadUserFromFile(string path) | ||
{ | ||
string text = null; | ||
string line = null; | ||
StringReader reader = null; | ||
try | ||
string text = File.ReadAllText(path).Trim(); | ||
if (string.IsNullOrEmpty(text)) | ||
{ | ||
text = File.ReadAllText(path).Trim(); | ||
if (string.IsNullOrEmpty(text)) throw new NATSException("Credentials file is empty"); | ||
|
||
reader = new StringReader(text); | ||
for (line = reader.ReadLine(); line != null; line = reader.ReadLine()) | ||
{ | ||
if (line.Contains("-----BEGIN NATS USER JWT-----")) | ||
{ | ||
return reader.ReadLine(); | ||
} | ||
Nkeys.Wipe(line); | ||
} | ||
throw new NATSException("Credentials file does not contain a JWT"); | ||
throw new NATSException("Credentials file is empty"); | ||
} | ||
finally | ||
string user = JWTHandlerUtils.LoadUser(text); | ||
if (user == null) | ||
{ | ||
Nkeys.Wipe(text); | ||
Nkeys.Wipe(line); | ||
reader?.Dispose(); | ||
throw new NATSException("Credentials file does not contain a JWT"); | ||
} | ||
return user.ToString(); | ||
} | ||
|
||
/// <summary> | ||
/// Generates a NATS Ed25519 keypair, used to sign server nonces, from a | ||
/// private credentials file. | ||
|
@@ -91,47 +78,23 @@ public static string LoadUserFromFile(string path) | |
/// <returns>A NATS Ed25519 KeyPair</returns> | ||
public static NkeyPair LoadNkeyPairFromSeedFile(string path) | ||
{ | ||
NkeyPair kp = null; | ||
string text = null; | ||
string line = null; | ||
string seed = null; | ||
StringReader reader = null; | ||
|
||
try | ||
{ | ||
text = File.ReadAllText(path).Trim(); | ||
if (string.IsNullOrEmpty(text)) throw new NATSException("Credentials file is empty"); | ||
|
||
// if it's a nk file, it only has the nkey | ||
if (text.StartsWith("SU")) | ||
string text = File.ReadAllText(path).Trim(); | ||
if (string.IsNullOrEmpty(text)) | ||
{ | ||
kp = Nkeys.FromSeed(text); | ||
return kp; | ||
throw new NATSException("Credentials file is empty"); | ||
} | ||
|
||
// otherwise assume it's a creds file. | ||
reader = new StringReader(text); | ||
for (line = reader.ReadLine(); line != null; line = reader.ReadLine()) | ||
{ | ||
if (line.Contains("-----BEGIN USER NKEY SEED-----")) | ||
{ | ||
seed = reader.ReadLine(); | ||
kp = Nkeys.FromSeed(seed); | ||
Nkeys.Wipe(seed); | ||
} | ||
Nkeys.Wipe(line); | ||
} | ||
|
||
NkeyPair kp = JWTHandlerUtils.LoadNkeyPair(text); | ||
if (kp == null) | ||
{ | ||
throw new NATSException("Seed not found in credentials file."); | ||
else | ||
return kp; | ||
} | ||
return kp; | ||
} | ||
finally | ||
{ | ||
Nkeys.Wipe(line); | ||
Nkeys.Wipe(text); | ||
Nkeys.Wipe(seed); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FYI, these were removed because the wipe tries to wipe a string which is not possible. The wipe method was recently changed to a no-op. |
||
reader?.Dispose(); | ||
} | ||
} | ||
|
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,79 @@ | ||
// Copyright 2019-2024 The NATS Authors | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
using System.IO; | ||
using System.Security; | ||
|
||
namespace NATS.Client | ||
{ | ||
/// <summary> | ||
/// This class is contains the default handlers for the | ||
/// <see cref="Options.UserJWTEventHandler"/> and the | ||
/// <see cref="Options.UserSignatureEventHandler"/>. This class is | ||
/// not normally used directly, but is provided to extend or use for | ||
/// utility methods to read a private seed or user JWT. | ||
/// </summary> | ||
public class JWTHandlerUtils | ||
{ | ||
public static string LoadUser(string text) | ||
{ | ||
StringReader reader = null; | ||
try | ||
{ | ||
reader = new StringReader(text); | ||
for (string line = reader.ReadLine(); line != null; line = reader.ReadLine()) | ||
{ | ||
if (line.Contains("-----BEGIN NATS USER JWT-----")) | ||
{ | ||
return reader.ReadLine(); | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
finally | ||
{ | ||
reader?.Dispose(); | ||
} | ||
} | ||
|
||
public static NkeyPair LoadNkeyPair(string nkeySeed) | ||
{ | ||
StringReader reader = null; | ||
try | ||
{ | ||
// if it's a nk file, it only has the nkey | ||
if (nkeySeed.StartsWith("SU")) | ||
{ | ||
return Nkeys.FromSeed(nkeySeed); | ||
} | ||
|
||
// otherwise assume it's a creds file. | ||
reader = new StringReader(nkeySeed); | ||
for (string line = reader.ReadLine(); line != null; line = reader.ReadLine()) | ||
{ | ||
if (line.Contains("-----BEGIN USER NKEY SEED-----")) | ||
{ | ||
return Nkeys.FromSeed(reader.ReadLine()); | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
finally | ||
{ | ||
reader?.Dispose(); | ||
} | ||
} | ||
} | ||
} |
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,93 @@ | ||
// Copyright 2024 The NATS Authors | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
using System.IO; | ||
using System.Security; | ||
|
||
namespace NATS.Client | ||
{ | ||
/// <summary> | ||
/// TODO | ||
/// </summary> | ||
public class StringUserJWTHandler | ||
{ | ||
/// <summary> | ||
/// Gets the JWT file. | ||
/// </summary> | ||
public string UserJwt { get; } | ||
|
||
/// <summary> | ||
/// Gets the credentials files. | ||
/// </summary> | ||
public string NkeySeed { get; } | ||
|
||
/// <summary> | ||
/// Creates a static user jwt handler. | ||
/// </summary> | ||
/// <param name="credentialsText">The text containing the "-----BEGIN NATS USER JWT-----" block | ||
/// and the text containing the "-----BEGIN USER NKEY SEED-----" block</param> | ||
public StringUserJWTHandler(string credentialsText) : this(credentialsText, credentialsText) {} | ||
|
||
/// <summary> | ||
/// Creates a static user jwt handler. | ||
/// </summary> | ||
/// <param name="userJwt">The text containing the "-----BEGIN NATS USER JWT-----" block</param> | ||
/// <param name="nkeySeed">The text containing the "-----BEGIN USER NKEY SEED-----" block or the seed begining with "SU". | ||
/// May be the same as the jwt string if they are chained.</param> | ||
public StringUserJWTHandler(string userJwt, string nkeySeed) | ||
{ | ||
UserJwt = JWTHandlerUtils.LoadUser(userJwt); | ||
if (UserJwt == null) | ||
{ | ||
throw new NATSException("Credentials do not contain a JWT"); | ||
} | ||
|
||
if (JWTHandlerUtils.LoadNkeyPair(nkeySeed) == null) | ||
{ | ||
throw new NATSException("Seed not found."); | ||
} | ||
NkeySeed = nkeySeed; | ||
} | ||
|
||
/// <summary> | ||
/// The default User JWT Event Handler. | ||
/// </summary> | ||
/// <param name="sender">Usually the connection.</param> | ||
/// <param name="args">Arguments</param> | ||
public void DefaultUserJWTEventHandler(object sender, UserJWTEventArgs args) | ||
{ | ||
args.JWT = UserJwt; | ||
} | ||
|
||
/// <summary> | ||
/// Utility method to signs the UserSignatureEventArgs server nonce from | ||
/// a private credentials file. | ||
/// </summary> | ||
/// <param name="args">Arguments</param> | ||
public void SignNonce(UserSignatureEventArgs args) | ||
{ | ||
// you have to load this every time b/c signing actually wipes data | ||
args.SignedNonce = JWTHandlerUtils.LoadNkeyPair(NkeySeed).Sign(args.ServerNonce); | ||
} | ||
|
||
/// <summary> | ||
/// The default User Signature event handler. | ||
/// </summary> | ||
/// <param name="sender"></param> | ||
/// <param name="args"></param> | ||
public void DefaultUserSignatureHandler(object sender, UserSignatureEventArgs args) | ||
{ | ||
SignNonce(args); | ||
} | ||
} | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah, that was left over from trying secure.