From 1ec701f65f8b1c37fe531c5f501aa230267dc60f Mon Sep 17 00:00:00 2001 From: jonaslagoni Date: Fri, 17 Feb 2023 20:27:23 +0100 Subject: [PATCH] fix: credential files not being loaded --- Oxide.Ext.GamingApi/GamingApiNats.cs | 82 ++++++++++++++-------------- 1 file changed, 41 insertions(+), 41 deletions(-) diff --git a/Oxide.Ext.GamingApi/GamingApiNats.cs b/Oxide.Ext.GamingApi/GamingApiNats.cs index 6fef8de..81c7be9 100644 --- a/Oxide.Ext.GamingApi/GamingApiNats.cs +++ b/Oxide.Ext.GamingApi/GamingApiNats.cs @@ -11,6 +11,7 @@ using System.Configuration; using System.IO; using System.Text; +using UnityEngine; namespace Oxide.Ext.GamingApi { @@ -70,31 +71,39 @@ private GamingApiNats(LoggingInterface logger) opts.Url = this.GetNatsHost(); string authenticationType = GetNatsAuthenticationType(); if(authenticationType == "jwt") { - EventHandler jwtEh = (sender, args) => + string NatsJwtUserFile = this.GetNatsJwtUserFile(); + if (NatsJwtUserFile == null) { - // Obtain a user JWT... - string jwt = this.GetNatsJwtUser(); + EventHandler 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 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 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 @@ -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"); @@ -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) {