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 18, 2023
1 parent 4203c6c commit a40c7e1
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 @@ -71,39 +71,31 @@ private GamingApiNats(LoggingInterface logger)
opts.Url = this.GetNatsHost();
string authenticationType = GetNatsAuthenticationType();
if(authenticationType == "jwt") {
string NatsJwtUserFile = this.GetNatsJwtUserFile();
if (NatsJwtUserFile == null)
EventHandler<UserJWTEventArgs> jwtEh = (sender, args) =>
{
EventHandler<UserJWTEventArgs> jwtEh = (sender, args) =>
{
// Obtain a user JWT...
string jwt = this.GetNatsJwtUser();
// 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);
} else
{
string NatsJwtSeedFile = this.GetNatsJwtSeedFile();
opts.SetUserCredentials(NatsJwtUserFile, NatsJwtSeedFile);
}
// 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
{
// NKey authentication
Expand Down Expand Up @@ -163,7 +155,7 @@ private string GetNatsAuthenticationType()

private string GetNatsNkeyUser()
{
var envName = $"GAMINGAPI_NATS_NKEY_USER";
var envName = $"GAMINGAPI_NATS_NKEY_USER";
var value = Environment.GetEnvironmentVariable(envName);
this.Logger.Info($"NATS: {envName} loading");
if (value == null)
Expand Down Expand Up @@ -196,15 +188,19 @@ 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.Trim();
}

var value = Environment.GetEnvironmentVariable(envName);
this.Logger.Info($"NATS: {envName} loading");
Expand All @@ -216,15 +212,19 @@ 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.Trim();
}
var value = Environment.GetEnvironmentVariable(envName);
if (value == null)
{
Expand Down

0 comments on commit a40c7e1

Please sign in to comment.