I am facing problem in creating the Sinch auth ticket for Android and IOS client using Nodejs. I tried sinch-ticketgen NPM module but it is generating tickets for javascript only, we can not use this ticket for Android and IOS clients.
Following is the code snippet I am using for the ticket generation but it is not working,
const crypto = require('crypto');
const shasum = crypto.createHash('sha1');
const key = '0b25bb2d-f5dd-4337-9f99-a318196f886a';
const secret = 'm1aur2Q4FUWWNuMlKq3KKg==';
const userId = 'sanket';
const sequence = 0;
const stringToSign = userId + key + sequence + secret;
shasum.update(stringToSign);
const singnature = shasum.digest('utf8'); //utf8
console.log('Signature ', singnature.toString('base64'));
const token = singnature.toString('base64').trim();
console.log(token);
Pseducode for the ticket generation is given in the https://www.sinch.com/docs/voice/ios/#applicationauthentication
I tried java example its working fine, not getting where I am making mistake in Nodejs ticket creation.
Following is the working Nodejs snippet for it.
const crypto = require('crypto');
const shasum = crypto.createHash('sha1');
const key = '0b25bb2d-f5dd-4337-9f99-a318196f886a';
const secret = 'm1aur2Q4FUWWNuMlKq3KKg==';
const userId = 'sanket';
const sequence = 0;
const stringToSign = userId + key + sequence + secret;
shasum.update(stringToSign);
const singnature = shasum.digest();
console.log('Signature ', singnature.toString('base64'));
const token = singnature.toString('base64').trim();
console.log(token);
Related
I'm testing the custom tab on Microsoft Teams app.
It correctly works on Desktop and iPhone, but it doesn't work on the Android mobile app.
The Android mobile debug screenshot
I'd like to know Why it's canceled?
The Android mobile couldn't open the app's web page.
I've captured the packet on the webserver when the android clicked the custom tab.
I could see only the server key exchange process, there is no client key exchange.
Android mobile
The iPhone was working properly and there was the client key exchange process.
iPohne
Please advise me.
Refferred to the below source code:
const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
const morgan = require('morgan');
const env = require('node-env-file');
const path = require('path');
var cookieParser = require('cookie-parser');
env(path.join(__dirname, '../.env'));
const config = require('./config/config');
const { logger } = require('./logger/logger');
const fs = require('fs');
const https = require('https');
const nocache = require('nocache');
const app = express();
const api = require('./api');
app.use(morgan('combined'));
app.enable('trust proxy');
app.use(bodyParser.json({ limit: '5mb' }));
app.use(bodyParser.urlencoded({ limit: '5mb', extended: true, parameterLimit: 50000 }));
app.use(cors());
app.use(nocache());
app.use(cookieParser())
app.use('/api', api);
// Login Authenticate
if (config.loginMode === 'local') {
require('./passport')
}
if (process.env.NODE_ENV === 'production') {
// Vue Router
app.use(require('connect-history-api-fallback')());
app.use(express.static(path.join(__dirname, '../../dist')));
}
// MongoDB Connection
require('./db')
const options = {
key: fs.readFileSync('./keys/evm.ecstel.co.kr.key', 'utf8'),
cert: fs.readFileSync('./keys/evm_ecstel_co_kr.crt', 'utf8')
}
// Server Running
const server = https.createServer(options, app).listen(process.env.PORT || config.port, function () {
logger.info(`Server started on https`)
logger.info(`Server started on port ${config.port} in ${config.host}`)
})
I am not using Azure Active Directory authentication in my React Native application.
I want to keep some keys on the cloud so that a user can easily configure without redeploying the complete application.
I was thinking about Azure KeyVault, so the question is, is it possible to use key vault, can I do without showing the authentication page.
If there is any other solution for this let me know.
You can use script code to get the value of the key. Don't need to showing the authentication page.
Below is a simple console code:
//const { DefaultAzureCredential } = require("#azure/identity");
const { ClientSecretCredential } = require("#azure/identity");
const { SecretClient } = require("#azure/keyvault-secrets");
const readline = require('readline');
function askQuestion(query) {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
return new Promise(resolve => rl.question(query, ans => {
rl.close();
resolve(ans);
}))
}
async function main() {
const keyVaultName = "bowmantest";
const KVUri = "https://" + keyVaultName + ".vault.azure.net";
//const credential = new DefaultAzureCredential();
tenantId = "e4c9ab4e-bdxxxxxx-230ba2a757fb";
clientId = "d340361e-5dxxxxxxbaf4-6e81aed46ed9";
clientSecret = "2X~43qA-~J2xxxxxxnT1a7_O2-dKyTK";
const credential = new ClientSecretCredential(tenantId, clientId, clientSecret);
const client = new SecretClient(KVUri, credential);
const secretName = "testbowmansecret";
console.log("Retrieving your secret from " + keyVaultName + ".");
const retrievedSecret = await client.getSecret(secretName);
console.log("Your secret is '" + retrievedSecret.value + "'.");
}
main()
And I can get: (don't need to go to authentication page, but authentication is still needed.)
By the way, you need to give the AD app the access permission:
This is the api documentation:
https://learn.microsoft.com/en-us/javascript/api/#azure/identity/clientsecretcredential?view=azure-node-latest
https://learn.microsoft.com/en-us/javascript/api/%40azure/keyvault-secrets/secretclient?view=azure-node-latest
(You can find everything about JavaScript with Azure in above documentation).
I am creating an App Where user can buy coins and for that I have been trying to integrate Razorpay into my Android App since a long time now. Razorpay can directly be used in Android. It sends Success or Failure results for payment and I can act accordingly (adding points to database in this case). But the problem with this approach is that I have to write points (after success) to database from the app. Which means I have to give write access for points node to user app which is not a good idea. So I wanted to use Razorpay with Firebase Cloud Functions and searching for a long time I came across this tutorial which is for web. I am quite new to Cloud Functions and hence wanted a little help for Android.
Here is the Index.js code but For Web
const functions = require("firebase-functions");
var express = require("express");
var cors = require("cors");
var request = require("request");
const crypto = require("crypto");
const key = "----insert yout key here----";
const key_secret = "----- insert key secret here ----";
var app = express();
app.use(cors({ origin: true }));
app.post("/", (req, res) => {
const amount = req.body.amount;
//Allow Api Calls from local server
const allowedOrigins = [
"http://127.0.0.1:8080",
"http://localhost:8080",
"https://-------YourFirebaseApp-----.firebaseapp.com/"
];
const origin = req.headers.origin;
if (allowedOrigins.indexOf(origin) > -1) {
res.setHeader("Access-Control-Allow-Origin", origin);
}
var options = {
method: "POST",
url: "https://api.razorpay.com/v1/orders",
headers: {
//There should be space after Basic else you get a BAD REQUEST error
Authorization:
"Basic " + new Buffer(key + ":" + key_secret).toString("base64")
},
form: {
amount: amount,
currency: "INR",
receipt:
"----- create a order in firestore and pass order_unique id here ---",
payment_capture: 1
}
};
request(options, function(error, response, body) {
if (error) throw new Error(error);
res.send(body);
});
});
app.post("/confirmPayment", (req, res) => {
const order = req.body;
const text = order.razorpay_order_id + "|" + order.razorpay_payment_id;
var signature = crypto
.createHmac("sha256", key_secret)
.update(text)
.digest("hex");
if (signature === order.razorpay_signature) {
console.log("PAYMENT SUCCESSFULL");
res.send("PAYMENT SUCCESSFULL");
} else {
res.send("something went wrong!");
res.end();
}
});
exports.paymentApi = functions.https.onRequest(app);
I think this will help you.
In my case, I am accessing items(Array of Product IDs) from the user's cart and reading the current price of the items then passing it as an argument to SendOrderId function which will return an OrderId to proceed.
The important thing to keep in mind is that you must have added razorpay in your dependencies inside package.json. You can do that by simply running
npm i razorpay
inside your functions folder (Which include index.js) which will automatically add the dependency to your project
const functions = require("firebase-functions");
const admin = require('firebase-admin');
const Razorpay = require('razorpay')
const razorpay = new Razorpay({
key_id: 'Your_razorpay_key_id',
key_secret: 'your_secret'
})
admin.initializeApp();
function SendOrderId(amountData, response) {
var options = {
amount: amountData, // amount in the smallest currency unit
currency: "INR",
};
razorpay.orders.create(options, function(err, order) {
console.log(order);
response.send(order);
});
}
exports.getOrderId = functions.https.onRequest((req, res) => {
return admin.firestore().collection('Users').doc(req.query.uid).get().then(queryResult => {
console.log(queryResult.data().Cart);
admin.firestore().collectionGroup("Products").where('ProductId', 'in', queryResult.data().Cart).get().then(result => {
var amount = 0;
result.forEach(element => {
amount += element.data().price;
});
SendOrderId(amount * 100, res);
})
})
});
So basically I want my Cloud Functions to create a new parent node named "Civil" whenever there is a new node in "Agent".
Here is the code I tried:
import * as functions from 'firebase-functions';
export const addCivilData = functions.database
.ref('/Agent/{AgentID}')
.onCreate((snapshot, context) => {
const userData = snapshot.val()
const newUsername = userData.username
const defCivilStatus: string = "new"
return snapshot.ref.parent?.child('Civil').child(`${newUsername}`).update(`${defCivilStatus}`)
})
The ? after parent is automatically generated by VSCODE and shows some rules on hovering the the mouse pointer.
and if I remove that "?" it gives red underline on snapshot.ref.parent.
I want to create a parent node "Civil" and child node with username as fetched and set its value to defCivilStatus.
please help
EDIT:
Additional Error screenshots
You can try this :
const functions = require('firebase-functions');
// The Firebase Admin SDK to access the Firebase Realtime Database.
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.addCivilData = functions.database
.ref('/Agent/{AgentID}')
.onCreate((snapshot, context) => {
const userData = snapshot.val();
console.log('userData', userData);
const newUsername = userData.username;
console.log('newUsername', newUsername);
const defCivilStatus = "new";
return snapshot.ref.parent.parent.child('Civil').child(`${newUsername}`).set(`${defCivilStatus}`);
});
And follow the firebase func. logs
Use the ! symbol is the IDE shows any error on the line return snapshot.ref.... like shown in the below code:
return snapshot.ref.parent!.parent!.child('Civil').child(`${newUsername}`).set(`${defCivilStatus}`);
In my android app when user enters wrong PASSWORD more times then i want send email to user using firebase functions with download link of picture captured so i created function below.
So i push email and download link to firebase and when data gets added following function gets triggered but whenever im trying to deploy this function cli giving me error that mailtransport is unexpected..
exports.sendMails = functions.database.ref('/failedAttemps/{email2}/{attemptsid}')
.onWrite((data, context) =>
{
const email2 = context.params.email2;
const attemptsid = context.params.attemptsid;
//const sender_id = context.params.sender_id;
//const mees = context.params.message_not;
// contentss = mees;
const gmailEmail = functions.config().gmail.email;
const gmailPassword = functions.config().gmail.password;
const mailTransport = nodemailer.createTransport({
service: 'gmail',
auth: {
user: "******#gmail.com",
pass: "****#****",
},
});
const email = admin.database().ref(`/Notification/${email2}/${attemptsid}/email`);
email.once("value", function(snapshot){
emails = snapshot.val();
});
const naam = admin.database().ref(`/Notification/${email2}/${attemptsid}/dlink`);
naam.once("value", function(snapshot){
dlinks = snapshot.val();
});
// console.log('message :' , contentss);
const mailOptions = {
from: '"LetsTalk" <noreply#firebase.com>',
to: emails,
};
// Building Email message.
mailOptions.subject = 'Someone tried to login to you account';
mailOptions.text = `${dlink}Thanks you for subscribing to our newsletter. You will receive our next weekly newsletter.`;
try {
await mailTransport.sendMail(mailOptions);
// console.log(`New ${subscribed ? '' : 'un'}subscription confirmation email sent to:`, val.email);
} catch(error) {
console.error('There was an error while sending the email:', error);
}
return null;
});
Every time i try to deploy function on firebase.This error pops upenter image description here
The problem seems to be you are using await without first indicating it is an async function.
Try replacing your first lines with:
exports.sendMails = functions.database.ref('/failedAttemps/{email2}/{attemptsid}')
.onWrite(async (data, context) =>
{