Skip to content

Commit

Permalink
fix: credential files not being loaded
Browse files Browse the repository at this point in the history
  • Loading branch information
jonaslagoni committed Feb 17, 2023
1 parent f85a3b2 commit 1ec701f
Showing 1 changed file with 41 additions and 41 deletions.
82 changes: 41 additions & 41 deletions Oxide.Ext.GamingApi/GamingApiNats.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using System.Configuration;
using System.IO;
using System.Text;
using UnityEngine;

namespace Oxide.Ext.GamingApi
{
Expand Down Expand Up @@ -70,31 +71,39 @@ private GamingApiNats(LoggingInterface logger)
opts.Url = this.GetNatsHost();
string authenticationType = GetNatsAuthenticationType();
if(authenticationType == "jwt") {
EventHandler<UserJWTEventArgs> jwtEh = (sender, args) =>
string NatsJwtUserFile = this.GetNatsJwtUserFile();
if (NatsJwtUserFile == null)
{
// Obtain a user JWT...
string jwt = this.GetNatsJwtUser();
EventHandler<UserJWTEventArgs> jwtEh = (sender, args) =>
{
// Obtain a user JWT...
string jwt = this.GetNatsJwtUser();

this.Logger.Info("NATS: Loading jwt token : " + jwt);
// You must set the JWT in the args to hand off
// to the client library.
args.JWT = jwt;
};
this.Logger.Info("NATS: Loading jwt token : " + jwt);
// You must set the JWT in the args to hand off
// to the client library.
args.JWT = jwt;
};

EventHandler<UserSignatureEventArgs> sigEh = (sender, args) =>
{
// get a private key seed from your environment.
string seed = this.GetNatsJwtSeed();
this.Logger.Info("NATS: Loading jwt seed : " + seed.Substring(0, 3) + "....");
EventHandler<UserSignatureEventArgs> sigEh = (sender, args) =>
{
// get a private key seed from your environment.
string seed = this.GetNatsJwtSeed();
this.Logger.Info("NATS: Loading jwt seed : " + seed.Substring(0, 3) + "....");

// Generate a NkeyPair
NkeyPair kp = Nkeys.FromSeed(seed);
// Generate a NkeyPair
NkeyPair kp = Nkeys.FromSeed(seed);

// Sign the nonce and return the result in the SignedNonce
// args property. This must be set.
args.SignedNonce = kp.Sign(args.ServerNonce);
};
opts.SetUserCredentialHandlers(jwtEh, sigEh);
// Sign the nonce and return the result in the SignedNonce
// args property. This must be set.
args.SignedNonce = kp.Sign(args.ServerNonce);
};
opts.SetUserCredentialHandlers(jwtEh, sigEh);
} else
{
string NatsJwtSeedFile = this.GetNatsJwtSeedFile();
opts.SetUserCredentials(NatsJwtUserFile, NatsJwtSeedFile);
}
} else
{
// NKey authentication
Expand Down Expand Up @@ -187,20 +196,15 @@ public string PrintByteArray(byte[] bytes)
sb.Append("}");
return sb.ToString();
}
private string GetNatsJwtUserFile()
{
var envName = $"GAMINGAPI_NATS_JWT_USER_FILE";
var fileName = Environment.GetEnvironmentVariable(envName);
return fileName;
}
private string GetNatsJwtUser()
{

var envName = $"GAMINGAPI_NATS_JWT_USER";
var envFileName = envName + "_FILE";

var fileName = Environment.GetEnvironmentVariable(envFileName);
if (fileName != null)
{
this.Logger.Info($"NATS: {envFileName} loading from file");
string contents = File.ReadAllText(@fileName);
this.Logger.Info($"NATS: {envFileName} length was - {PrintByteArray(Encoding.UTF8.GetBytes(contents))}");
return contents;
}

var value = Environment.GetEnvironmentVariable(envName);
this.Logger.Info($"NATS: {envName} loading");
Expand All @@ -212,19 +216,15 @@ private string GetNatsJwtUser()
return value;
}

private string GetNatsJwtSeedFile()
{
var envName = $"GAMINGAPI_NATS_JWT_SEED_FILE";
var fileName = Environment.GetEnvironmentVariable(envName);
return fileName;
}
private string GetNatsJwtSeed()
{
var envName = $"GAMINGAPI_NATS_JWT_SEED";
var envFileName = envName + "_FILE";

var fileName = Environment.GetEnvironmentVariable(envFileName);
if (fileName != null)
{
this.Logger.Info($"NATS: {envFileName} loading from file");
string contents = File.ReadAllText(@fileName);
this.Logger.Info($"NATS: {envFileName} length was - {PrintByteArray(Encoding.UTF8.GetBytes(contents))}");
return contents;
}
var value = Environment.GetEnvironmentVariable(envName);
if (value == null)
{
Expand Down

0 comments on commit 1ec701f

Please sign in to comment.