Skip to content

Commit

Permalink
Adding factory methods for creating Credentials
Browse files Browse the repository at this point in the history
  • Loading branch information
slorello89 committed Mar 9, 2020
1 parent 2b953df commit fd97e0a
Showing 1 changed file with 51 additions and 1 deletion.
52 changes: 51 additions & 1 deletion Nexmo.Api/Request/Credentials.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Nexmo.Api.Cryptography;

using System.IO;
namespace Nexmo.Api.Request
{
public class Credentials
Expand Down Expand Up @@ -55,5 +55,55 @@ public Credentials(string nexmoApiKey, string nexmoApiSecret, string nexmoApplic
ApplicationKey = nexmoApplicationPrivateKey;
}

public static Credentials FromApiKeyAndSecret(string apiKey, string apiSecret)
{
return new Credentials(){ApiKey = apiKey, ApiSecret = apiSecret};
}

/// <summary>
/// Create Credentials from AppId and Private Key
/// </summary>
/// <param name="appId"></param>
/// <param name="privateKey"></param>
/// <returns></returns>
public static Credentials FromAppIdAndPrivateKey(string appId, string privateKey)
{
return new Credentials(){ApplicationId = appId, ApplicationKey = privateKey};
}

/// <summary>
/// Opens private key file and reads it
/// </summary>
/// <param name="appId">Your Nexmo Application ID</param>
/// <param name="privateKeyPath">path to your private Key file</param>
/// <exception cref="System.UnauthorizedAccessException">Thrown if app doesn't have requried permission to key file</exception>
/// <exception cref="System.ArgumentException">privateKeyPath is a zero length string, contains only white space or contains one or more invalid charecters</exception>
/// <exception cref="System.ArgumentNullException">privateKeyPath is null</exception>
/// <exception cref="System.IO.PathTooLongException">The specified path, filename, or both exceed the system-defined max length</exception>
/// <exception cref="System.IO.DirectoryNotFoundException">The specified path is invalid, (for example, it is on an unmapped drive).</exception>
/// <exception cref="System.IO.FileNotFoundException">The file specified in path was not found.</exception>
/// <exception cref="System.NotSupportedException">path is in an invalid format.</exception>
/// <returns>CredentialsObject</returns>
public static Credentials FromAppIdAndPrivateKeyPath(string appId, string privateKeyPath)
{
using (var reader = File.OpenText(privateKeyPath))
{
var privateKey = reader.ReadToEnd();
return new Credentials(){ApplicationId = appId, ApplicationKey = privateKey};
}
}

/// <summary>
/// Builds Credentials from
/// </summary>
/// <param name="apiKey"></param>
/// <param name="signatureSecret"></param>
/// <param name="method"></param>
/// <returns></returns>
public static Credentials FromApiKeySignatureSecretAndMethod(string apiKey, string signatureSecret,
SmsSignatureGenerator.Method method)
{
return new Credentials(){ApiKey = apiKey, SecuritySecret = signatureSecret, Method = method};
}
}
}

0 comments on commit fd97e0a

Please sign in to comment.