microsoft/teams.net

Public

mirrored from https://github.com/microsoft/teams.netAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
pr-134

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

Libraries/Microsoft.Teams.Api/Clients/UserTokenClient.cs

177lines · modecode

1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT License.
3
4using System.Text.Json;
5using System.Text.Json.Serialization;
6
7using Microsoft.Teams.Common.Http;
8
9namespace Microsoft.Teams.Api.Clients;
10
11public class UserTokenClient : Client
12{
13 private readonly JsonSerializerOptions _jsonSerializerOptions = new()
14 {
15 DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
16 };
17
18 public UserTokenClient(CancellationToken cancellationToken = default) : base(cancellationToken)
19 {
20
21 }
22
23 public UserTokenClient(IHttpClient client, CancellationToken cancellationToken = default) : base(client, cancellationToken)
24 {
25
26 }
27
28 public UserTokenClient(IHttpClientOptions options, CancellationToken cancellationToken = default) : base(options, cancellationToken)
29 {
30
31 }
32
33 public UserTokenClient(IHttpClientFactory factory, CancellationToken cancellationToken = default) : base(factory, cancellationToken)
34 {
35
36 }
37
38 public async Task<Token.Response> GetAsync(GetTokenRequest request)
39 {
40 var query = QueryString.Serialize(request);
41 var req = HttpRequest.Get($"https://token.botframework.com/api/usertoken/GetToken?{query}");
42 var res = await _http.SendAsync<Token.Response>(req, _cancellationToken);
43 return res.Body;
44 }
45
46 public async Task<IDictionary<string, Token.Response>> GetAadAsync(GetAadTokenRequest request)
47 {
48 var query = QueryString.Serialize(request);
49 var req = HttpRequest.Post($"https://token.botframework.com/api/usertoken/GetAadTokens?{query}", body: request);
50 var res = await _http.SendAsync<IDictionary<string, Token.Response>>(req, _cancellationToken);
51 return res.Body;
52 }
53
54 public async Task<IList<Token.Status>> GetStatusAsync(GetTokenStatusRequest request)
55 {
56 var query = QueryString.Serialize(request);
57 var req = HttpRequest.Get($"https://token.botframework.com/api/usertoken/GetTokenStatus?{query}");
58 var res = await _http.SendAsync<IList<Token.Status>>(req, _cancellationToken);
59 return res.Body;
60 }
61
62 public async Task SignOutAsync(SignOutRequest request)
63 {
64 var query = QueryString.Serialize(request);
65 var req = HttpRequest.Delete($"https://token.botframework.com/api/usertoken/SignOut?{query}");
66 await _http.SendAsync(req, _cancellationToken);
67 }
68
69 public async Task<Token.Response> ExchangeAsync(ExchangeTokenRequest request)
70 {
71 var query = QueryString.Serialize(new
72 {
73 userId = request.UserId,
74 connectionName = request.ConnectionName,
75 channelId = request.ChannelId
76 });
77
78 // This ensures that the request body is buffered so that when sent the `Content-Length` header is set.
79 // This is required for the Bot Framework Token Service to process the request correctly.
80 var body = JsonSerializer.Serialize(request.GetBody(), _jsonSerializerOptions);
81
82 var req = HttpRequest.Post($"https://token.botframework.com/api/usertoken/exchange?{query}", body);
83 req.Headers.Add("Content-Type", new List<string>() { "application/json" });
84
85 var res = await _http.SendAsync<Token.Response>(req, _cancellationToken);
86 return res.Body;
87 }
88
89 public class GetTokenRequest
90 {
91 [JsonPropertyName("userId")]
92 [JsonPropertyOrder(0)]
93 public required string UserId { get; set; }
94
95 [JsonPropertyName("connectionName")]
96 [JsonPropertyOrder(1)]
97 public required string ConnectionName { get; set; }
98
99 [JsonPropertyName("channelId")]
100 [JsonPropertyOrder(2)]
101 public ChannelId? ChannelId { get; set; }
102
103 [JsonPropertyName("code")]
104 [JsonPropertyOrder(3)]
105 public string? Code { get; set; }
106 }
107
108 public class GetAadTokenRequest
109 {
110 [JsonPropertyName("userId")]
111 [JsonPropertyOrder(0)]
112 public required string UserId { get; set; }
113
114 [JsonPropertyName("connectionName")]
115 [JsonPropertyOrder(1)]
116 public required string ConnectionName { get; set; }
117
118 [JsonPropertyName("channelId")]
119 [JsonPropertyOrder(2)]
120 public required ChannelId ChannelId { get; set; }
121
122 [JsonPropertyName("resourceUrls")]
123 [JsonPropertyOrder(3)]
124 public IList<string> ResourceUrls { get; set; } = [];
125 }
126
127 public class GetTokenStatusRequest
128 {
129 [JsonPropertyName("userId")]
130 [JsonPropertyOrder(0)]
131 public required string UserId { get; set; }
132
133 [JsonPropertyName("channelId")]
134 [JsonPropertyOrder(1)]
135 public required ChannelId ChannelId { get; set; }
136
137 [JsonPropertyName("includeFilter")]
138 [JsonPropertyOrder(2)]
139 public string? IncludeFilter { get; set; }
140 }
141
142 public class SignOutRequest
143 {
144 [JsonPropertyName("userId")]
145 [JsonPropertyOrder(0)]
146 public required string UserId { get; set; }
147
148 [JsonPropertyName("connectionName")]
149 [JsonPropertyOrder(1)]
150 public required string ConnectionName { get; set; }
151
152 [JsonPropertyName("channelId")]
153 [JsonPropertyOrder(2)]
154 public required ChannelId ChannelId { get; set; }
155 }
156
157 public class ExchangeTokenRequest
158 {
159 [JsonPropertyName("userId")]
160 [JsonPropertyOrder(0)]
161 public required string UserId { get; set; }
162
163 [JsonPropertyName("connectionName")]
164 [JsonPropertyOrder(1)]
165 public required string ConnectionName { get; set; }
166
167 [JsonPropertyName("channelId")]
168 [JsonPropertyOrder(2)]
169 public required ChannelId ChannelId { get; set; }
170
171 [JsonPropertyName("exchangeRequest")]
172 [JsonPropertyOrder(3)]
173 public required TokenExchange.Request ExchangeRequest { get; set; }
174
175 internal TokenExchange.Request GetBody() => ExchangeRequest;
176 }
177}