Unable to retrieve auth string from auth endpoint - received status: 500 - node.js

I using react native and backend is node.I trying implement pusher in my app.
Object {
"error": "Unable to retrieve auth string from auth endpoint - received status: 0 from http://10.0.27.124:8070/pusher/auth. Clients must be authenticated to join private or presence channels. See: https://pusher.com/docs/authenticating_users",
"status": 0,
"type": "AuthError",
}
Here is my react native code :
const pusher = new Pusher('73286f08a5b2aeeea398', {
cluster: 'ap1',
authEndpoint: 'http://10.0.27.124:8070/pusher/auth',
});
console.log(pusher)
const presenceChannel = pusher.subscribe('presence-channel');
Here is my node js code :
exports.authPusher = function (req, res) {
const socketId = req.body.socket_id;
const channel = req.body.channel_name;
console.log(req.body);
const presenceData = {
user_id: 'unique_user_id',
user_info: { name: 'Mr Channels', twitter_id: '#pusher' },
};
const auth = pusher.authenticate(socketId, channel, presenceData);
res.send(auth);
};
Thank you for answering.

I think you just forgot to use quote marks around authEndpoint parameter. Pusher is trying to call http instead of http://10.0.27.124:8070/pusher/auth.
const pusher = new Pusher('73286f08a5b2aeeea398', {
cluster: 'ap1',
authEndpoint: 'http://10.0.27.124:8070/pusher/auth',
});
If you open network tab on the web-developers toolbar of your browser, you must be able to see the actual request and debug it from there.

Related

Failed to connect to video room in Twilio

I'm woking on building a web calling application in React + Twilio API. But it failed to post API and this is unable to connect to video room because of the token error.
an error:
POST https://ecs.us1.twilio.com/v2/Configuration 403
Unable to connect to Room: The authorization with Token failed
I have set up Twilio account like below:
1) Get Twilio credentials and API key
2) This time I use TEST Credentials. And I set up ACCOUNT_SID, API_KEY_SID, and API_KEY_SECRET to .env.
REACT_APP_TWILIO_ACCOUNT_SID = 'ACXXXXXXXXXXXXXX'
REACT_APP_TWILIO_AUTH_TOKEN = 'XXXXXXXXXXXXXXXXX'
REACT_APP_TWILIO_API_KEY_SID = 'SKXXXXXXXXXXX'
REACT_APP_TWILIO_API_KEY_SECRET = 'XXXXXXXXXXXXXXXXX'
3) Set up twilio configuration with API document
import twilio from "twilio";
const AccessToken = twilio.jwt.AccessToken;
const VideoGrant = AccessToken.VideoGrant;
// Substitute your Twilio AccountSid and ApiKey details
const ACCOUNT_SID = process.env.REACT_APP_TWILIO_ACCOUNT_SID;
const API_KEY_SID = process.env.REACT_APP_TWILIO_API_KEY_SID;
const API_KEY_SECRET = process.env.REACT_APP_TWILIO_API_KEY_SECRET;
// Create an Access Token
const accessToken = new AccessToken(ACCOUNT_SID, API_KEY_SID, API_KEY_SECRET);
// Set the Identity of this token
accessToken.identity = "my-user";
// Grant access to Video
const grant = new VideoGrant();
grant.room = "cool room";
accessToken.addGrant(grant);
// Serialize the token as a JWT
const twilioToken = accessToken.toJwt();
export default twilioToken;
4) call a room
connect(twilioToken, { name: "my-new-room" }).then(
room => {
console.log(`Successfully joined a Room: ${room}`);
room.on("participantConnected", participant => {
console.log(`A remote Participant connected: ${participant}`);
});
},
error => {
console.error(`Unable to connect to Room: ${error.message}`);
}
);
But it failed. What's wrong with this?
Passed with Live Account. I don't know why but Test account is not working.

TEAMS bot in node.js: 'Authorization has been denied for this request' in CreateConversation method

I have a TEAMS node.js bot running locally (with ngrok). I receive messages from TEAMS client and echo works
context.sendActivity(`You said '${context.activity.text}'`);
Now I want to send a 1to1 message to this user, but I receive
Error: Authorization has been denied for this request
when creating a conversation.
My code:
var sUserId = "29:1shb_5I6CkkerBVq4qPqcv5dGwDfkXx11Jbjc1UnGCIv"
var sServiceUrl = "https://smba.trafficmanager.net/emea/";
var sTenantId = "942369d2-208e-438b-894c-0d0e1510cf61";
var credentials = new BotConnector.MicrosoftAppCredentials({
appId: "xxxxxxx",
appPassword: "yyyyyyyy"
});
var connectorClient = new BotConnector.ConnectorClient(credentials, { baseUri: sServiceUrl });
const parameters = {
members: [ { id: sUserId } ],
isGroup: false,
channelData:
{
tenant: {
id: sTenantId
}
}
};
var conversationResource = await connectorClient.conversations.createConversation(parameters);
// I get the error here, next is not executed
await connectorClient.conversations.sendToConversation(conversationResource.id, {
type: "message",
from: { id: "xxxxxxx" },
recipient: { id: sUserId },
text: 'This a message from Bot Connector Client (NodeJS)'
});
appId & appPassword are valid (from .env file), if they are wrong I can not receive messages from TEAMS client
I have the same code to create a conversation in a .NET bot and it works for me:
var parameters = new ConversationParameters
{
Members = new[] { new ChannelAccount(sUserId) },
ChannelData = new TeamsChannelData
{
Tenant = new TenantInfo(sTenantId),
},
};
retValue = await connectorClient.Conversations.CreateConversationAsync(parameters);
What is wrong in my node.js code?
Thanks,
Diego
Have you trusted the service? It don't think so based on your code, and it's a classic cause of 401 in your case.
In node.js, do the following:
MicrosoftAppCredentials.trustServiceUrl(serviceUrl);
If you want more details around that, have a look to the documentation about getting 401 when sending proactive messages here
And also this SO answer about Teams and Proactive messaging, in particular last block.
Proactive messaging bot in Teams without mentioning the bot beforehand

Is it possible to integrate bot-builder into an existing express app?

I have an existing node/express chatbot application that connects to several chat platforms using ExpressJS' next(), next() middleware design pattern. I send a 200 response at the very beginning to acknowledge the receipt of a message, and send a new POST request to send a message from my last middleware.
app.post("/bots", receiveMsg, doStuff, formatAndSendMsg, catchErrors);
Now I want to integrate Skype as a channel for my bot, but the NodeJS library for bot-framework has a totally different way of doing things, using events and such magic that I haven't fully understood yet:
var connector = new builder.ConsoleConnector();
app.post("/skype", connector.listen());
var bot = new builder.UniversalBot(connector, function (session) {
session.send("You said: %s", session.message.text);
});
It doesn't look like these are compatible ways to do things, so what is the best way to receive a message and then send a response to a user without having to change my express routing to fit bot-builder in? Can I get a Session object with Session.send() to respond to? Will I have to do all the addressing manually? Is there a method that resembles this:
app.post("/skype", (req, res, next) => {
const address = req.body.id;
const message = new builder.Message(address, messageBody).send()
}
Or:
app.post("/skype", connector.listen(), (req, res, next) => {
// (res.locals is available in every express middleware function)
const session = res.locals.botFrameworkSession;
// do stuff
session.send(message);
}
You can register bot application in your existing express applications. Bot builder SDK is also compatible in expressjs framework. You can refer to official sample which is also leveraging express.
Don't forget to modify the messsaging endpoint in your bot registration to your bot's endpoint, e.g.
https://yourdomain/stuff
in your scenario. Please refer to https://learn.microsoft.com/en-us/azure/bot-service/bot-service-quickstart-registration for more info.
Building messages, addressing them, and sending those messages are all possible using the official bot framework NodeJS library. What I couldn't do with that library was receive messages and verify their authenticity on my routes without making major changes to my design (using request middleware - next() - to process the incoming request) which is already in production with other bots and not easy to change.
Here's my workaround: First is this BotFrameworkAuthenticator class that I create based on the Microsoft documentation here: https://learn.microsoft.com/en-us/azure/bot-service/rest-api/bot-framework-rest-connector-authentication
It is instantiated with the appID and appPassword from your Bot Framework app.
import axios from "axios";
import * as jwt from "jsonwebtoken";
import * as jwkToPem from 'jwk-to-pem';
export class BotFrameworkAuthenticator {
private appId: string;
private appPassword: string;
private openIdMetadata: any;
// The response body specifies the document in the JWK format but also includes an additional property for each key: endorsements.
private validSigningKeys: any;
// The list of keys is relatively stable and may be cached for long periods of time (by default, 5 days within the Bot Builder SDK).
private signingKeyRefreshRate: number = 432000; // in seconds (432000 = 5 days)
constructor(appId, appPassword) {
this.appId = appId;
this.appPassword = appPassword;
this.getListOfSigningKeys();
}
// response data should contain "jwks_uri" property that contains address to request list of valid signing keys.
public async getOpenIdMetaData() {
// This is a static URL that you can hardcode into your application. - MS Bot Framework docs
await axios.get("https://login.botframework.com/v1/.well-known/openidconfiguration").then(response => {
this.openIdMetadata = response.data;
logger.info("OpenID metadata document recieved for Bot Framework.");
}).catch(err => {
logger.warn(err.message, "Could not get OpenID metadata document for Bot Framework. Retrying in 15 seconds...");
setTimeout(this.getListOfSigningKeys, 15000);
})
}
public async getListOfSigningKeys() {
await this.getOpenIdMetaData();
if (this.openIdMetadata && this.openIdMetadata.jwks_uri) {
// previous function getOpenIdMetaData() succeeded
await axios.get(this.openIdMetadata.jwks_uri).then(response => {
logger.info(`Signing keys recieved for Bot Framework. Caching for ${this.signingKeyRefreshRate / 86400} days.`);
this.validSigningKeys = response.data.keys;
setTimeout(this.getListOfSigningKeys, (this.signingKeyRefreshRate * 1000));
}).catch(err => {
logger.error(err.message, "Could not get list of valid signing keys for Bot Framework. Retrying in 15 seconds");
setTimeout(this.getListOfSigningKeys, 15000);
});
} else {
// previous function getOpenIdMetaData() failed, but has already queued this function to run again. Will continue until succeeds.
return;
}
}
/**
* Verifies that the message was sent from Bot Framework by checking values as specified in Bot Framework Documentation:
* https://learn.microsoft.com/en-us/azure/bot-service/rest-api/bot-framework-rest-connector-authentication#step-4-verify-the-jwt-token
* Retrieves the Bearer token from the authorization header, decodes the token so we can match the key id (kid) to a key in the OpenID
* document, then converts that key to PEM format so that jwt/crypto can consume it to verify that the bearer token is
* cryptographically signed.
* If the serviceUrl property in the token doe not match the serviceUrl property in the message, it should also be rejected.
*/
public verifyMsgAuthenticity(serviceUrl: string, headers: any) {
try {
const token = headers.authorization.replace("Bearer ", "");
const decoded = jwt.decode(token, { complete: true }) as any;
const verifyOptions = {
issuer: "https://api.botframework.com",
audience: this.appId,
clockTolerance: 300, // (seconds) The token is within its validity period. Industry-standard clock-skew is 5 minutes. (Bot Framework documentation);
}
const jwk = this.lookupKey(decoded.header.kid)
const pem = jwkToPem(jwk);
const verified = jwt.verify(token, pem, verifyOptions) as any;
if (!serviceUrl || serviceUrl !== verified.serviceurl) {
logger.warn("Non-matching serviceUrl in Bot Framework verified token!")
return false;
}
return true;
} catch (err) {
logger.warn("Received invalid/unsigned message on Bot Framework endpoint!", err.message)
return false;
}
}
// Finds the relevant key from the openID list. Does not transform the key.
private lookupKey(kid) {
const jwk = this.validSigningKeys.find((key) => {
return (key.kid === kid);
});
return jwk;
}
}
Use the BotFrameworkAuthenticator class like this at the very beginning of your express route to verify that all incoming requests are valid.
const botFrameworkAuthenticator = new BotFrameworkAuthenticator(appID, appPassword);
router.post("/", (req: Request, res: Response, next: NextFunction) => {
if (botFrameworkAuthenticator.verifyMsgAuthenticity(req.body.serviceUrl, req.headers) === true) {
res.status(200).send();
next();
} else {
// unsafe to process
res.status(403).send();
return;
}
});
And to send messages using the regular Bot Framework library without having a Session object that would normally be created by the Bot Framework library when it receives an incoming message:
import * as builder from "botbuilder";
// instantiate the chatConnector (only once, not in the same function as the sending occurs)
const botFrameworkSender = new builder.ChatConnector({ appId, appPassword });
//---------------------------------------------
const skypeMsg = req.body;
const address = {
channelId: skypeMsg.channelId,
user: skypeMsg.from,
bot: skypeMsg.recipient,
conversation: skypeMsg.conversation
};
const response = new builder.Message().text(someText).address(address).toMessage();
const formattedResponses = [response];
botFrameworkSender.send(formattedResponses, logErrorsToConsole);
Note that all of the builder.Message() -- .attachment(), .images(), etc.. -- functions can be used, not just the text()

node-apn : Provider should be created per notification request or one-time

I am new to node-apn. I have implemented it in nodejs application. Below is my code.
var APN = require('apn')
var apnProvider = new APN.Provider({
token: {
key: "PATH_TO_FILE",
keyId: "KEY",
teamId: "TEAM"
},
production: false
});
module.exports = {
send: function (tokens, message, callBackFn) {
var note = new APN.Notification({
alert: "Breaking News: I just sent my first Push Notification",
});
// The topic is usually the bundle identifier of your application.
note.topic = "BUNDLE";
console.log(`Sending: ${note.compile()} to ${tokens}`);
service.send(note, tokens).then(callBackFn);
}
};
So in some documentation it says we should shutdown apnProvider.
So my question is should i create apnProvider globally (like i have done)?
OR should i create per send request (inside send function) & call shutdown after send notification.
I tried reading online. But i couldn't find any example like my requirements.

Send push notifications with two Firebase projects

I'm using Admin SDK for node.js for sending the push notifications. Followed the tutorial and initialized the multiple projects with like examples given with this link.
I need to know how to send push notifications with two projects using with node.js. Used below methods for sending notifications two projects based its working with default project but another project getting error like below
exports.send_test_mailer = function(req, res) {
// Default project
var registrationToken = ["f-vRsDouUFQ:APA91bGktVzu3WjKGqeXqdiYPI8B0lQXs34TkJS4p7LaMiFGfp5LdfB1ZjEhO3CY5ci92apqgt1hEJY0ml11C4hxYUaPfDl7PeDHhcmDGur0JUx5l3M2mLEj30epwRBWVsE4xMSTls4f"];
var payload = {
notification: {
title: "driver app",
body: "driver app push notfications on the day."
},
data: {
score: "850",
time: "2:45"
}
};
firebaseAdmin.messaging().sendToDevice(registrationToken, payload)
.then(function(response) {
console.log("Successfully sent message driver:", JSON.stringify(response));
})
.catch(function(error) {
console.log("Error sending message driver:", JSON.stringify(error));
});
// Second project
var registrationTokens = ["dzXRXUMIB5w:APA91bHSArtroO8M33IHxaslQTugTcEzJcfkbsXEhwbXbvVzBws-aqG4aqKNr37j8WpZev7lolX7cFQlAKYZ1QV_EgC6zTGeT41n3lvSpcDyBg6t4SZZaoPe7nUO9sbdcXA2KDguxAbk"];
var payloads = {
notification: {
title: "customer app",
body: "customer app push notfications on the day."
},
data: {
score: "850",
time: "2:45"
}
};
firebaseAdmin.messaging().sendToDevice(registrationTokens, payloads)
.then(function(response) {
console.log("Successfully sent message customer:", JSON.stringify(response));
})
.catch(function(error) {
console.log("Error sending message customer:", JSON.stringify(error));
});
};
Error
{"results":[{"error":{"code":"messaging/registration-token-not-registered","message":"The provided registration token is not registered. A previously valid registration token can be unregistered for a variety of reasons. See the error documentation for more details. Remove this registration token and stop using it to send messages."}},{"error":{"code":"messaging/mismatched-credential","message":"The credential used to authenticate this SDK does not have permission to send messages to the device corresponding to the provided registration token. Make sure the credential and registration token both belong to the same Firebase project."}}],"canonicalRegistrationTokenCount":0,"failureCount":2,"successCount":0,"multicastId":9014981858701063000}
Here is my answer
var ServiceAccount = require("./path your default app file.json");
var ServiceAccount1 = require("./path your second app file.json");
var serviceAppConfig = {
credential: firebaseAdmin.credential.cert(ServiceAccount),
databaseURL: "https://your firebase default app url"
};
// Initialize the default app
var serviceApp = firebaseAdmin.initializeApp(serviceAppConfig);
//console.log(serviceApp.name); // "[DEFAULT]"
// Retrieve services via the defaultApp variable...
var serviceAuth = serviceApp.auth();
var serviceDatabase = serviceApp.database();
// Get the Messaging service for the default app
global.serviceMessaging = firebaseAdmin.messaging();
var service1AppConfig = {
credential: firebaseAdmin.credential.cert(ServiceAccount1),
databaseURL: "https://your firebase url second app"
};
// Initialize another app with a different config
var service1App = firebaseAdmin.initializeApp(service1AppConfig, "App2 name");
// Use the otherApp variable to retrieve the other app's services
var service1Auth = service1App.auth();
var service1Database = service1App.database();
// Get the Messaging service for a given app
global.service1Messaging = firebaseAdmin.messaging(service1App);

Resources