-
Notifications
You must be signed in to change notification settings - Fork 151
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ability to set user credentials from strings(s) (#885)
- Loading branch information
Showing
11 changed files
with
298 additions
and
62 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
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,72 @@ | ||
// 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 | ||
{ | ||
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.