vonage-dotnet-sdk icon indicating copy to clipboard operation
vonage-dotnet-sdk copied to clipboard

Messages client does not support Basic auth

Open dragonmantank opened this issue 2 years ago • 2 comments

Describe the bug With the release of Messages API v1, we added support for the API. Unfortunately it looks like it is hardcoded to use a JWT, but the final GA API allows Basic auth as well. We need to have it check the supplied credentials and switch based on the available creds.

To Reproduce Steps to reproduce the behavior:

  1. Configure a new Vonage object using just an API key and Secret
  2. Attempt to send a message through the Messages API

Expected behavior The message should be accepted by the API

Desktop (please complete the following information):

  • OS: [e.g. iOS] N/A
  • Browser [e.g. chrome, safari] N/A
  • Version [e.g. 22] 6.x

Additional context https://github.com/Vonage/vonage-dotnet-sdk/blob/4189dab236817de8afdf73098cc8842b3cb25908/Vonage/Messages/MessagesClient.cs#L20 is the offending line. Probably need to wrap this an an if statement and call it with either ApiRequest.AuthType.Bearer if the user supplied an app ID + private key, and ApiRequest.AuthType.Basic if they only supplied a key and secret.

dragonmantank avatar Jul 07 '22 19:07 dragonmantank

I am getting "VonageAuthenticationException: AppId or Private Key Path missing." when calling the following line of code after suppling the API Key and API secret. Is there any known workaround?

var response = await vonageClient.MessagesClient.SendAsync(request);

mbcrump avatar Aug 04 '22 22:08 mbcrump

@mbcrump I resolve the problem. You need add your private key for application in the appsettings. Cheers

yanielbf avatar Sep 10 '22 22:09 yanielbf

@dragonmantank Indeed, no matter how you set up your credentials, it seems Messages will only use Bearer auth... I'll keep an eye on this one, I'll keep you all updated.

Tr00d avatar Dec 01 '22 12:12 Tr00d

I just ran into the same issue, using .NET here.

I already had a private key for my app in app settings, so I regenerate it but no luck.

I also injected the private key contents into my vonage client (Vonage.Request.Credentials.ApplicationKey) but still throws "VonageAuthenticationException: AppId or Private Key Path missing.".

I don't see how I can provide a private key path to this, any ideas?

var MMSRequest = new Vonage.Messages.Mms.MmsImageRequest
{
  To = Number,
  From = API_SENDER,
  Image = new Vonage.Messages.Attachment
  {
    Url = AttachmentUrl
  }
};
                               
var Client = new Vonage.VonageClient(new Vonage.Request.Credentials(vonageApiKey: API_KEY, vonageApiSecret: API_SECRET));
 
Client.Credentials.ApplicationKey = @"-----BEGIN PUBLIC KEY-----CONTENTS HERE-----END PUBLIC KEY-----";
                                
//Throws "VonageAuthenticationException: AppId or Private Key Path missing."
Client.MessagesClient.SendAsync(MMSRequest).Result;

Cereal-Killa avatar Jan 31 '23 13:01 Cereal-Killa

@Cereal-Killa MessagesClient only uses the bearer authentication. You can't use the couple ApiKey/ApiSecret to send a message. It's a missing feature, and I need to work on it. At the moment, you need to provide a couple ApplicationId/PrivateKeyPath.

Looking at your example, the client's credentials have ApiKey, ApiSecret and ApplicationKey. The ApplicationId is missing. Can you give it a try? You can generate credentials easily using Credentials.FromAppIdAndPrivateKey

Tr00d avatar Jan 31 '23 13:01 Tr00d

@Cereal-Killa MessagesClient only uses the bearer authentication. You can't use the couple ApiKey/ApiSecret to send a message. It's a missing feature, and I need to work on it. At the moment, you need to provide a couple ApplicationId/PrivateKeyPath.

Looking at your example, the client's credentials have ApiKey, ApiSecret and ApplicationKey. The ApplicationId is missing. Can you give it a try? You can generate credentials easily using Credentials.FromAppIdAndPrivateKey

True, I was reusing SMS code for the client. I gave it a try, providing the path to the private.key, the credential did instantiate with the file contents and app id but now I'm getting:

System.AggregateException
  HResult=0x80131500
  Message=One or more errors occurred.
  Source=mscorlib
  StackTrace:
   at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)

  This exception was originally thrown at this call stack:
    [External Code]

Inner Exception 1:
MissingMethodException: Method not found: 'System.String Jose.JWT.Encode(System.Object, System.Object, Jose.JwsAlgorithm, System.Collections.Generic.IDictionary`2<System.String,System.Object>, Jose.JwtSettings)'.

Update

var MMSCredentials = Credentials.FromAppIdAndPrivateKeyPath(APP_ID, "...\\private.key");

...

Client.Credentials = MMSCredentials;
Client.MessagesClient.SendAsync(MMSRequest).Result;

Cereal-Killa avatar Jan 31 '23 13:01 Cereal-Killa

Never mind, updated all my packages, including JWT and Nexmo and it worked, thank you 👍

Cereal-Killa avatar Jan 31 '23 13:01 Cereal-Killa

It has been fixed in PR #384 and released in v6.1.0. Messages will use Bearer auth if provided, and will fallback on Basic auth if not.

Tr00d avatar Apr 17 '23 09:04 Tr00d