Skip to content
This repository was archived by the owner on Jan 5, 2026. It is now read-only.

Commit 366d52c

Browse files
[JavaScript] Add Bot Authentication Certificate sample (#3908)
* add bot-authentication-certificate sample * fix lint issues * Include sample link in list of samples * Add documentation details --------- Co-authored-by: JhontSouth <jhonatan.sandoval@southworks.com> Co-authored-by: Jhonatan Sandoval Velasco <122501764+JhontSouth@users.noreply.github.com>
1 parent d991983 commit 366d52c

31 files changed

Lines changed: 2357 additions & 0 deletions

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ Samples are designed to illustrate functionality you'll need to implement to bui
7575
|18|OAuth authentication | Bot that demonstrates how to integrate OAuth providers. |[.NET&nbsp;Core][cs#18]|[JavaScript][js#18]|[Python][py#18]|[Java][java#18]
7676
|24|MSGraph&nbsp;authentication | Demonstrates bot authentication capabilities of Azure Bot Service. Demonstrates utilizing the Microsoft Graph API to retrieve data about the user.|[.NET&nbsp;Core][cs#24] |[JavaScript][js#24] |[Python][py#24]|[Java][java#24]
7777
|46|Teams authentication | Demonstrates how to use authentication for a bot running in Microsoft Teams. | [.NET&nbsp;Core](https://github.com/OfficeDev/Microsoft-Teams-Samples/tree/main/samples/bot-teams-authentication/csharp) | [JavaScript](https://github.com/OfficeDev/Microsoft-Teams-Samples/tree/main/samples/bot-conversation-sso-quickstart/js) |[Python](https://github.com/OfficeDev/Microsoft-Teams-Samples/tree/main/samples/bot-teams-authentication/python)|[Java](https://github.com/OfficeDev/Microsoft-Teams-Samples/tree/main/samples/bot-teams-authentication/java)
78+
|84|Certificate authentication | Demonstrates how to use Certificates to authenticate the bot | |[JavaScript][js#84] | |
7879

7980
### Custom question answering samples
8081

@@ -183,6 +184,7 @@ A [collection of **experimental** samples](./experimental) exist, intended to pr
183184
[js#80]:samples/javascript_nodejs/80.skills-simple-bot-to-bot
184185
[js#81]:samples/javascript_nodejs/81.skills-skilldialog
185186
[js#82]:samples/javascript_nodejs/82.skills-sso-cloudadapter
187+
[js#84]:samples/javascript_nodejs/84.bot-authentication-certificate
186188

187189
[py#1]:samples/python/01.console-echo
188190
[py#2]:samples/python/02.echo-bot
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
MicrosoftAppTenantId=
2+
MicrosoftAppId=
3+
CertificateThumbprint=
4+
KeyVaultName=
5+
CertificateName=
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/* eslint-disable */
2+
module.exports = {
3+
"extends": "standard",
4+
"rules": {
5+
"semi": [2, "always"],
6+
"indent": [2, 4],
7+
"no-return-await": 0,
8+
"space-before-function-paren": [2, {
9+
"named": "never",
10+
"anonymous": "never",
11+
"asyncArrow": "always"
12+
}],
13+
"template-curly-spacing": [2, "always"]
14+
}
15+
};
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# Authentication Bot using SSL/TLS certificates
2+
3+
Bot Framework v4 bot authentication using Certificate
4+
5+
This bot has been created using [Bot Framework](https://dev.botframework.com/), is shows how to use the bot authentication capabilities of Azure Bot Service. In this sample, we use a local or KeyVault certificate to create the Bot Framework Authentication.
6+
7+
## Prerequisites
8+
9+
- [Node.js](https://nodejs.org) version 10.14 or higher
10+
11+
```bash
12+
# determine node version
13+
node --version
14+
```
15+
16+
## To try this sample
17+
18+
- Clone the repository
19+
20+
```bash
21+
git clone https://github.com/microsoft/botbuilder-samples.git
22+
```
23+
24+
- In a terminal, navigate to `samples/javascript_nodejs/84.bot-authentication-certificate`
25+
26+
```bash
27+
cd samples/javascript_nodejs/84.bot-authentication-certificate
28+
```
29+
30+
- Install modules
31+
32+
```bash
33+
npm install
34+
```
35+
36+
- Start the bot
37+
38+
```bash
39+
npm start
40+
```
41+
42+
## Testing the bot using Bot Framework Emulator
43+
44+
[Bot Framework Emulator](https://github.com/microsoft/botframework-emulator) is a desktop application that allows bot developers to test and debug their bots on localhost or running remotely through a tunnel.
45+
46+
- Install the latest Bot Framework Emulator from [here](https://github.com/Microsoft/BotFramework-Emulator/releases)
47+
48+
### Connect to the bot using Bot Framework Emulator
49+
50+
- Launch Bot Framework Emulator
51+
- File -> Open Bot
52+
- Enter a Bot URL of `http://localhost:3978/api/messages`
53+
54+
## Interacting with the bot
55+
56+
This sample uses the bot authentication capabilities of Azure Bot Service, providing features to make it easier to develop a bot that authenticates users using digital security certificates. You just need to provide the certificate data linked to the managed identity and run the bot, then communicate with it to validate its correct authentication.
57+
58+
## SSL/TLS certificate
59+
60+
An SSL/TLS certificate is a digital object that allows systems to verify identity and subsequently establish an encrypted network connection with another system using the Secure Sockets Layer/Transport Layer Security (SSL/TLS) protocol. Certificates are issued using a cryptographic system known as public key infrastructure (PKI). PKI allows one party to establish the identity of another through the use of certificates if they both trust a third party, known as a certificate authority. SSL/TLS certificates therefore function as digital identity documents that protect network communications and establish the identity of websites on the Internet as well as resources on private networks.
61+
62+
## Deploy the bot to Azure
63+
64+
To learn more about deploying a bot to Azure, see [Deploy your bot to Azure](https://aka.ms/azuredeployment) for a complete list of deployment instructions.
65+
66+
## Further reading
67+
68+
- [Bot Framework Documentation](https://docs.botframework.com)
69+
70+
- [Bot Basics](https://docs.microsoft.com/azure/bot-service/bot-builder-basics?view=azure-bot-service-4.0)
71+
72+
- [Activity processing](https://docs.microsoft.com/en-us/azure/bot-service/bot-builder-concept-activity-processing?view=azure-bot-service-4.0)
73+
74+
- [Azure Bot Service Introduction](https://docs.microsoft.com/azure/bot-service/bot-service-overview-introduction?view=azure-bot-service-4.0)
75+
76+
- [Azure Bot Service Documentation](https://docs.microsoft.com/azure/bot-service/?view=azure-bot-service-4.0)
77+
78+
- [Azure CLI](https://docs.microsoft.com/cli/azure/?view=azure-cli-latest)
79+
80+
- [Azure Portal](https://portal.azure.com)
81+
82+
- [Channels and Bot Connector Service](https://docs.microsoft.com/en-us/azure/bot-service/bot-concepts?view=azure-bot-service-4.0)
83+
84+
- [Restify](https://www.npmjs.com/package/restify)
85+
86+
- [dotenv](https://www.npmjs.com/package/dotenv)
87+
88+
- [SSL/TLS certificates](https://www.digicert.com/tls-ssl/tls-ssl-certificates)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
const { ActivityHandler, MessageFactory } = require('botbuilder');
5+
6+
class AuthBot extends ActivityHandler {
7+
constructor() {
8+
super();
9+
// See https://aka.ms/about-bot-activity-message to learn more about the message and other activity types.
10+
this.onMessage(async (context, next) => {
11+
const replyText = 'Running dialog with bot authenticated';
12+
const message = MessageFactory.text(replyText, replyText);
13+
await context.sendActivity(message);
14+
// By calling next() you ensure that the next BotHandler is run.
15+
await next();
16+
});
17+
18+
this.onMembersAdded(async (context, next) => {
19+
const membersAdded = context.activity.membersAdded;
20+
const welcomeText = 'Welcome to Authentication Bot with SSL/TLS Certificate.';
21+
for (let cnt = 0; cnt < membersAdded.length; ++cnt) {
22+
if (membersAdded[cnt].id !== context.activity.recipient.id) {
23+
await context.sendActivity(MessageFactory.text(welcomeText, welcomeText));
24+
}
25+
}
26+
// By calling next() you ensure that the next BotHandler is run.
27+
await next();
28+
});
29+
}
30+
}
31+
32+
module.exports.AuthBot = AuthBot;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[config]
2+
command = ./deploy.sh
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
#!/bin/bash
2+
3+
# ----------------------
4+
# KUDU Deployment Script
5+
# Version: 1.0.17
6+
# ----------------------
7+
8+
# Helpers
9+
# -------
10+
11+
exitWithMessageOnError () {
12+
if [ ! $? -eq 0 ]; then
13+
echo "An error has occurred during web site deployment."
14+
echo $1
15+
exit 1
16+
fi
17+
}
18+
19+
# Prerequisites
20+
# -------------
21+
22+
# Verify node.js installed
23+
hash node 2>/dev/null
24+
exitWithMessageOnError "Missing node.js executable, please install node.js, if already installed make sure it can be reached from current environment."
25+
26+
# Setup
27+
# -----
28+
29+
SCRIPT_DIR="${BASH_SOURCE[0]%\\*}"
30+
SCRIPT_DIR="${SCRIPT_DIR%/*}"
31+
ARTIFACTS=$SCRIPT_DIR/../artifacts
32+
KUDU_SYNC_CMD=${KUDU_SYNC_CMD//\"}
33+
34+
if [[ ! -n "$DEPLOYMENT_SOURCE" ]]; then
35+
DEPLOYMENT_SOURCE=$SCRIPT_DIR
36+
fi
37+
38+
if [[ ! -n "$NEXT_MANIFEST_PATH" ]]; then
39+
NEXT_MANIFEST_PATH=$ARTIFACTS/manifest
40+
41+
if [[ ! -n "$PREVIOUS_MANIFEST_PATH" ]]; then
42+
PREVIOUS_MANIFEST_PATH=$NEXT_MANIFEST_PATH
43+
fi
44+
fi
45+
46+
if [[ ! -n "$DEPLOYMENT_TARGET" ]]; then
47+
DEPLOYMENT_TARGET=$ARTIFACTS/wwwroot
48+
else
49+
KUDU_SERVICE=true
50+
fi
51+
52+
if [[ ! -n "$KUDU_SYNC_CMD" ]]; then
53+
# Install kudu sync
54+
echo Installing Kudu Sync
55+
npm install kudusync -g --silent
56+
exitWithMessageOnError "npm failed"
57+
58+
if [[ ! -n "$KUDU_SERVICE" ]]; then
59+
# In case we are running locally this is the correct location of kuduSync
60+
KUDU_SYNC_CMD=kuduSync
61+
else
62+
# In case we are running on kudu service this is the correct location of kuduSync
63+
KUDU_SYNC_CMD=$APPDATA/npm/node_modules/kuduSync/bin/kuduSync
64+
fi
65+
fi
66+
67+
# Node Helpers
68+
# ------------
69+
70+
selectNodeVersion () {
71+
NPM_CMD=npm
72+
NODE_EXE=node
73+
}
74+
75+
##################################################################################################################################
76+
# Deployment
77+
# ----------
78+
79+
echo Handling node.js deployment.
80+
81+
# 1. KuduSync
82+
if [[ "$IN_PLACE_DEPLOYMENT" -ne "1" ]]; then
83+
"$KUDU_SYNC_CMD" -v 50 -f "$DEPLOYMENT_SOURCE" -t "$DEPLOYMENT_TARGET" -n "$NEXT_MANIFEST_PATH" -p "$PREVIOUS_MANIFEST_PATH" -i ".git;.hg;.deployment;deploy.sh"
84+
exitWithMessageOnError "Kudu Sync failed"
85+
fi
86+
87+
# 2. Select node version
88+
selectNodeVersion
89+
90+
# 3. Install npm packages
91+
if [ -e "$DEPLOYMENT_TARGET/package.json" ]; then
92+
cd "$DEPLOYMENT_TARGET"
93+
echo "Running $NPM_CMD install --production"
94+
eval $NPM_CMD install --production
95+
exitWithMessageOnError "npm failed"
96+
cd - > /dev/null
97+
fi
98+
99+
##################################################################################################################################
100+
echo "Finished successfully."
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[config]
2+
command = deploy.cmd

0 commit comments

Comments
 (0)