CredentialProvider.cs
1 #if !NET45
2 using Microsoft.Extensions.Configuration;
3 #endif
4 using System;
5 using System.Threading.Tasks;
6 
7 #if NET45
8 using System.Configuration;
9 #endif
10 
11 namespace Microsoft.Bot.Connector
12 {
13  public interface ICredentialProvider
14  {
20  Task<bool> IsValidAppIdAsync(string appId);
21 
27  Task<string> GetAppPasswordAsync(string appId);
28 
33  Task<bool> IsAuthenticationDisabledAsync();
34  }
35 
37  {
38  public string AppId { get; set; }
39 
40  public string Password { get; set; }
41 
42  public Task<bool> IsValidAppIdAsync(string appId)
43  {
44  return Task.FromResult(appId == AppId);
45  }
46 
47  public Task<string> GetAppPasswordAsync(string appId)
48  {
49  return Task.FromResult((appId == this.AppId) ? this.Password : null);
50  }
51 
52  public Task<bool> IsAuthenticationDisabledAsync()
53  {
54  return Task.FromResult(string.IsNullOrEmpty(AppId));
55  }
56  }
57 
62  {
63  public StaticCredentialProvider(string appId, string password)
64  {
65  this.AppId = appId;
66  this.Password = password;
67  }
68  }
69 
70 #if NET45
71  public sealed class SettingsCredentialProvider : SimpleCredentialProvider
75  {
76  public SettingsCredentialProvider(string appIdSettingName = null, string appPasswordSettingName = null)
77  {
78  var appIdKey = appIdSettingName ?? MicrosoftAppCredentials.MicrosoftAppIdKey;
79  var passwordKey = appPasswordSettingName ?? MicrosoftAppCredentials.MicrosoftAppPasswordKey;
80  this.AppId = ConfigurationManager.AppSettings[appIdKey] ?? Environment.GetEnvironmentVariable(appIdKey, EnvironmentVariableTarget.Process);
81  this.Password = ConfigurationManager.AppSettings[passwordKey] ?? Environment.GetEnvironmentVariable(passwordKey, EnvironmentVariableTarget.Process);
82  }
83  }
84 #else
89  {
90  public ConfigurationCredentialProvider(IConfiguration configuration,
91  string appIdSettingName = null,
92  string appPasswordSettingName = null)
93  {
94  var appIdKey = appIdSettingName ?? MicrosoftAppCredentials.MicrosoftAppIdKey;
95  var passwordKey = appPasswordSettingName ?? MicrosoftAppCredentials.MicrosoftAppPasswordKey;
96  this.AppId = configuration.GetSection(appIdKey)?.Value;
97  this.Password = configuration.GetSection(passwordKey)?.Value;
98  }
99  }
100 #endif
101 }
const string MicrosoftAppPasswordKey
The key for Microsoft app Password.
ConfigurationCredentialProvider(IConfiguration configuration, string appIdSettingName=null, string appPasswordSettingName=null)
Task< bool > IsAuthenticationDisabledAsync()
Checks if bot authentication is disabled.
StaticCredentialProvider(string appId, string password)
const string MicrosoftAppIdKey
The key for Microsoft app Id.
Task< bool > IsValidAppIdAsync(string appId)
Validate AppId
Credential provider which uses Microsoft.Extensions.Configuration.IConfiguration to lookup appId and ...
Task< string > GetAppPasswordAsync(string appId)
Get the app password for a given bot appId, if it is not a valid appId, return Null ...
Static credential provider which has the appid and password static