BotService.cs
1 //
2 // Copyright (c) Microsoft. All rights reserved.
3 // Licensed under the MIT license.
4 //
5 // Microsoft Bot Framework: http://botframework.com
6 //
7 // Bot Builder SDK Github:
8 // https://github.com/Microsoft/BotBuilder
9 //
10 // Copyright (c) Microsoft Corporation
11 // All rights reserved.
12 //
13 // MIT License:
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
21 //
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
24 //
25 // THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 //
33 
34 using System;
35 using System.Collections.Generic;
36 using System.Net.Http;
37 using System.Reflection;
38 using System.Runtime.Serialization;
39 using System.Threading;
40 using System.Threading.Tasks;
41 
42 using Autofac;
44 using Microsoft.Bot.Connector;
45 
46 namespace Microsoft.Bot.Builder.Azure
47 {
51  public static class BotService
52  {
56  public static BotAuthenticator Authenticator => authenticator.Value;
57 
63  public static BotServiceScope Initialize(Assembly assembly = null)
64  {
65  var resolveAssembly = assembly ?? Assembly.GetCallingAssembly();
66 
67  // update the container with azure module components
68  var builder = new ContainerBuilder();
69  builder.RegisterModule(new AzureModule(resolveAssembly));
70  builder.Update(Conversation.Container);
71 
72  return new BotServiceScope(ResolveAssembly.Create(resolveAssembly));
73  }
74 
81  public static async Task AuthenticateAsync(HttpRequestMessage request, IEnumerable<Activity> activities, CancellationToken token = default(CancellationToken))
82  {
83  if (!await Authenticator.TryAuthenticateAsync(request, activities, token))
84  {
85  throw new UnauthorizedAccessException("Bot authentication failed!");
86  }
87  }
88 
89  internal static readonly Lazy<string> stateApi = new Lazy<string>(() => Utils.GetStateApiUrl());
90 
91  private static readonly Lazy<BotAuthenticator> authenticator = new Lazy<BotAuthenticator>(() => new BotAuthenticator(new StaticCredentialProvider(Utils.GetAppSetting(AppSettingKeys.AppId), Utils.GetAppSetting(AppSettingKeys.Password)),
93  }
94 
98  public sealed class BotServiceScope : IDisposable
99  {
100  private readonly IEnumerable<IDisposable> disposables;
101 
106  public BotServiceScope(params IDisposable[] disposables)
107  {
108  this.disposables = disposables;
109  }
110 
111  void IDisposable.Dispose()
112  {
113  foreach (var disposable in this.disposables)
114  {
115  disposable?.Dispose();
116  }
117  }
118  }
119 }
A utility class for bots running on Azure.
async Task< bool > TryAuthenticateAsync(HttpRequestMessage request, IEnumerable< IActivity > activities, CancellationToken token)
Authenticates the incoming request and add the IActivity.ServiceUrl for each activities to MicrosoftA...
Root namespace for the Microsoft Bot Connector SDK.
Definition: ActionTypes.cs:7
The top level composition root for the SDK.
static async Task AuthenticateAsync(HttpRequestMessage request, IEnumerable< Activity > activities, CancellationToken token=default(CancellationToken))
Authenticate the request and add the service url in the activities to MicrosoftAppCredentials.TrustedHostNames.
Definition: BotService.cs:81
const string Password
The Microsoft app password key.
static BotServiceScope Initialize(Assembly assembly=null)
Initialize bot service by updating the Conversation.Container with AzureModule.
Definition: BotService.cs:63
A helper class responsible for resolving the calling assembly
static string GetAppSetting(string key)
Get value corresponding to the key from application settings.
static string GetStateApiUrl(string key=AppSettingKeys.StateEndpoint)
Get the state api endpoint.
static ResolveAssembly Create(Assembly assembly)
Creates an instance of ResovelCallingAssembly
BotServiceScope(params IDisposable[] disposables)
Creates an instance of BotServiceScope
Definition: BotService.cs:106
static string GetOpenIdConfigurationUrl(string key=AppSettingKeys.OpenIdMetadata)
Get the open Id configuration url from application settings.
Static credential provider which has the appid and password static
Core namespace for Dialogs and associated infrastructure.
Definition: Address.cs:40
The scope for the BotService
Definition: BotService.cs:98
Root namespace for the Microsoft Bot Builder SDK.
Autofac module for azure bot components.
Definition: AzureModule.cs:52