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

Commit 038bda5

Browse files
JhontSouthTracy Boehrer
andauthored
[#3910][JavaScript] Add Bot Authentication SNI sample bot (#3914)
* add bot sample with sni authentication * fix lint issues * apply PR feedback * remove empty spaces * keep app type value in .env * update botbuilder version * Updated SDK version --------- Co-authored-by: Tracy Boehrer <trboehre@microsoft.com>
1 parent 08f1c45 commit 038bda5

31 files changed

Lines changed: 2355 additions & 2 deletions

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ 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 | [.NET&nbsp;Core][cs#84] |[JavaScript][js#84] | |
79-
|85|Subject name/issuer authentication | Demonstrates how to use the subject name/issuer authentication in a bot | [.NET&nbsp;Core][cs#85] | | |
78+
|84|Certificate authentication | Demonstrates how to use Certificates to authenticate the bot | [.NET&nbsp;Core][cs#84] |[JavaScript][js#84] | |
79+
|85|Subject name/issuer authentication | Demonstrates how to use the subject name/issuer authentication in a bot | [.NET&nbsp;Core][cs#85] | [JavaScript][js#85] | |
8080

8181
### Custom question answering samples
8282

@@ -188,6 +188,7 @@ A [collection of **experimental** samples](./experimental) exist, intended to pr
188188
[js#81]:samples/javascript_nodejs/81.skills-skilldialog
189189
[js#82]:samples/javascript_nodejs/82.skills-sso-cloudadapter
190190
[js#84]:samples/javascript_nodejs/84.bot-authentication-certificate
191+
[js#85]:samples/javascript_nodejs/85.bot-authentication-sni
191192

192193
[py#1]:samples/python/01.console-echo
193194
[py#2]:samples/python/02.echo-bot
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
MicrosoftAppType=
2+
MicrosoftAppId=
3+
MicrosoftAppTenantId=
4+
CertificateThumbprint=
5+
KeyVaultName=
6+
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: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
# Authentication Bot using Subject Name/Issuer
2+
3+
Bot Framework v4 bot authentication using Subject Name/Issuer
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 and the MSAL Subject Name/Issuer configuration 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/85.bot-authentication-sni`
25+
26+
```bash
27+
cd samples/javascript_nodejs/85.bot-authentication-sni
28+
```
29+
30+
- Install modules
31+
32+
```bash
33+
npm install
34+
```
35+
36+
- Set environment variables
37+
38+
- MicrosoftAppType: Type of the App.
39+
40+
- MicrosoftAppId: App Id of your bot.
41+
42+
- MicrosoftAppTenantId: Tenant Id to which your bot belongs.
43+
44+
- CertificateThumbprint: Certificate thumbprint.
45+
46+
- KeyVaultName: Name of the KeyVault containing the certificate.
47+
48+
- CertificateName: Name of the certificate in the KeyVault.
49+
50+
- Start the bot
51+
52+
```bash
53+
npm start
54+
```
55+
56+
## Testing the bot using Bot Framework Emulator
57+
58+
[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.
59+
60+
- Install the latest Bot Framework Emulator from [here](https://github.com/Microsoft/BotFramework-Emulator/releases)
61+
62+
### Connect to the bot using Bot Framework Emulator
63+
64+
- Launch Bot Framework Emulator
65+
66+
- File -> Open Bot
67+
68+
- Enter a Bot URL of `http://localhost:3978/api/messages`
69+
70+
## Interacting with the bot
71+
72+
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.
73+
74+
## SSL/TLS certificate
75+
76+
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.
77+
78+
## How to create an SSL/TLS certificate
79+
80+
There are two possible options to create SSL/TSL certificate. Below is a step-by-step description of each one:
81+
82+
### Using local environment
83+
84+
1. Run the following command in a local PowerShell
85+
86+
```
87+
$cert = New-SelfSignedCertificate -CertStoreLocation "<directory-to-store-certificate>" -Subject "CN=<certificate-name>" -KeySpec KeyExchange
88+
```
89+
90+
1. Then, type _Manage User Certificates_ in the Windows search bar and hit enter
91+
92+
2. The certificate will be located in the _user certificates_ folder, under _personal_ directory.
93+
94+
3. Export the certificate to _pfx_ format including the key(The default location is _system32_ folder).
95+
96+
4. Go to the certificate location and run the following command to generate a _pem_ file:
97+
98+
```
99+
OpenSSL pkcs12 -in <certificate-name>.pfx -out c:\<certificate-name>.pem –nodes
100+
```
101+
102+
5. Upload the generated certificate to the Azure app registration.
103+
104+
### Using KeyVault
105+
106+
1. Create a KeyVault resource and assign _the KeyVault Administrator_ role to have permission to create a new certificate.
107+
108+
2. Under the Certificates section, hit on Generate/Import, complete the form, and create the certificate in PEM format.
109+
110+
3. Go to the details of the certificate that you created and enable it.
111+
112+
4. Download the certificate in CER format and then upload it to the Azure app registration.
113+
114+
## Deploy the bot to Azure
115+
116+
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.
117+
118+
## Further reading
119+
120+
- [Bot Framework Documentation](https://docs.botframework.com)
121+
122+
- [Bot Basics](https://docs.microsoft.com/azure/bot-service/bot-builder-basics?view=azure-bot-service-4.0)
123+
124+
- [Activity processing](https://docs.microsoft.com/en-us/azure/bot-service/bot-builder-concept-activity-processing?view=azure-bot-service-4.0)
125+
126+
- [Azure Bot Service Introduction](https://docs.microsoft.com/azure/bot-service/bot-service-overview-introduction?view=azure-bot-service-4.0)
127+
128+
- [Azure Bot Service Documentation](https://docs.microsoft.com/azure/bot-service/?view=azure-bot-service-4.0)
129+
130+
- [Azure CLI](https://docs.microsoft.com/cli/azure/?view=azure-cli-latest)
131+
132+
- [Azure Portal](https://portal.azure.com)
133+
134+
- [Channels and Bot Connector Service](https://docs.microsoft.com/en-us/azure/bot-service/bot-concepts?view=azure-bot-service-4.0)
135+
136+
- [Restify](https://www.npmjs.com/package/restify)
137+
138+
- [dotenv](https://www.npmjs.com/package/dotenv)
139+
140+
- [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 the Bot with Subject Name/Issuer Authentication';
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)