/
MicrosoftAppCredentials.cs
147 lines (134 loc) · 7.06 KB
/
MicrosoftAppCredentials.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Microsoft.IdentityModel.Clients.ActiveDirectory;
using Microsoft.Rest;
using Newtonsoft.Json;
namespace Microsoft.Bot.Connector.Authentication
{
/// <summary>
/// MicrosoftAppCredentials auth implementation and cache.
/// </summary>
public class MicrosoftAppCredentials : AppCredentials
{
/// <summary>
/// The configuration property for the Microsoft app Password.
/// </summary>
public const string MicrosoftAppPasswordKey = "MicrosoftAppPassword";
/// <summary>
/// The configuration property for the Microsoft app ID.
/// </summary>
public const string MicrosoftAppIdKey = "MicrosoftAppId";
/// <summary>
/// An empty set of credentials.
/// </summary>
public static readonly MicrosoftAppCredentials Empty = new MicrosoftAppCredentials(null, null);
/// <summary>
/// Initializes a new instance of the <see cref="MicrosoftAppCredentials"/> class.
/// </summary>
/// <param name="appId">The Microsoft app ID.</param>
/// <param name="password">The Microsoft app password.</param>
public MicrosoftAppCredentials(string appId, string password)
: this(appId, password, null, null, null, null)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="MicrosoftAppCredentials"/> class.
/// </summary>
/// <param name="appId">The Microsoft app ID.</param>
/// <param name="password">The Microsoft app password.</param>
/// <param name="customHttpClient">Optional <see cref="HttpClient"/> to be used when acquiring tokens.</param>
public MicrosoftAppCredentials(string appId, string password, HttpClient customHttpClient)
: this(appId, password, null, customHttpClient)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="MicrosoftAppCredentials"/> class.
/// </summary>
/// <param name="appId">The Microsoft app ID.</param>
/// <param name="password">The Microsoft app password.</param>
/// <param name="customHttpClient">Optional <see cref="HttpClient"/> to be used when acquiring tokens.</param>
/// <param name="logger">Optional <see cref="ILogger"/> to gather telemetry data while acquiring and managing credentials.</param>
public MicrosoftAppCredentials(string appId, string password, HttpClient customHttpClient, ILogger logger)
: this(appId, password, null, customHttpClient, logger)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="MicrosoftAppCredentials"/> class.
/// </summary>
/// <param name="appId">The Microsoft app ID.</param>
/// <param name="password">The Microsoft app password.</param>
/// <param name="customHttpClient">Optional <see cref="HttpClient"/> to be used when acquiring tokens.</param>
/// <param name="logger">Optional <see cref="ILogger"/> to gather telemetry data while acquiring and managing credentials.</param>
/// <param name="oAuthScope">The scope for the token.</param>
public MicrosoftAppCredentials(string appId, string password, HttpClient customHttpClient, ILogger logger, string oAuthScope)
: this(appId, password, null, customHttpClient, logger, oAuthScope)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="MicrosoftAppCredentials"/> class.
/// </summary>
/// <param name="appId">The Microsoft app ID.</param>
/// <param name="password">The Microsoft app password.</param>
/// <param name="channelAuthTenant">Optional. The oauth token tenant.</param>
/// <param name="customHttpClient">Optional <see cref="HttpClient"/> to be used when acquiring tokens.</param>
public MicrosoftAppCredentials(string appId, string password, string channelAuthTenant, HttpClient customHttpClient)
: this(appId, password, channelAuthTenant, customHttpClient, null)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="MicrosoftAppCredentials"/> class.
/// </summary>
/// <param name="appId">The Microsoft app ID.</param>
/// <param name="password">The Microsoft app password.</param>
/// <param name="channelAuthTenant">Optional. The oauth token tenant.</param>
/// <param name="customHttpClient">Optional <see cref="HttpClient"/> to be used when acquiring tokens.</param>
/// <param name="logger">Optional <see cref="ILogger"/> to gather telemetry data while acquiring and managing credentials.</param>
public MicrosoftAppCredentials(string appId, string password, string channelAuthTenant, HttpClient customHttpClient, ILogger logger = null)
: this(appId, password, channelAuthTenant, customHttpClient, logger, null)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="MicrosoftAppCredentials"/> class.
/// </summary>
/// <param name="appId">The Microsoft app ID.</param>
/// <param name="password">The Microsoft app password.</param>
/// <param name="channelAuthTenant">Optional. The oauth token tenant.</param>
/// <param name="customHttpClient">Optional <see cref="HttpClient"/> to be used when acquiring tokens.</param>
/// <param name="logger">Optional <see cref="ILogger"/> to gather telemetry data while acquiring and managing credentials.</param>
/// <param name="oAuthScope">The scope for the token.</param>
public MicrosoftAppCredentials(string appId, string password, string channelAuthTenant, HttpClient customHttpClient, ILogger logger = null, string oAuthScope = null)
: base(channelAuthTenant, customHttpClient, logger, oAuthScope)
{
MicrosoftAppId = appId;
MicrosoftAppPassword = password;
}
/// <summary>
/// Gets or sets the Microsoft app password for this credential.
/// </summary>
/// <value>
/// The Microsoft app password for this credential.
/// </value>
public string MicrosoftAppPassword { get; set; }
/// <inheritdoc/>
protected override Lazy<AdalAuthenticator> BuildAuthenticator()
{
return new Lazy<AdalAuthenticator>(
() =>
new AdalAuthenticator(
new ClientCredential(MicrosoftAppId, MicrosoftAppPassword),
new OAuthConfiguration() { Authority = OAuthEndpoint, Scope = OAuthScope },
this.CustomHttpClient,
this.Logger),
LazyThreadSafetyMode.ExecutionAndPublication);
}
}
}