microsoft/teams.net
Publicmirrored fromhttps://github.com/microsoft/teams.netAvailable
Libraries/Microsoft.Teams.Plugins/Microsoft.Teams.Plugins.AspNetCore.DevTools/Controllers/ActivityController.cs
93lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | using System.Text; |
| 5 | using System.Text.Json; |
| 6 | using System.Text.Json.Nodes; |
| 7 | |
| 8 | using Microsoft.AspNetCore.Http; |
| 9 | using Microsoft.AspNetCore.Mvc; |
| 10 | using Microsoft.Extensions.DependencyInjection; |
| 11 | using Microsoft.IdentityModel.Tokens; |
| 12 | using Microsoft.Teams.Api; |
| 13 | using Microsoft.Teams.Api.Activities; |
| 14 | using Microsoft.Teams.Api.Auth; |
| 15 | |
| 16 | namespace Microsoft.Teams.Plugins.AspNetCore.DevTools.Controllers; |
| 17 | |
| 18 | [ApiController] |
| 19 | [Obsolete("Use Minimal APIs instead.")] |
| 20 | public class ActivityController : ControllerBase |
| 21 | { |
| 22 | private readonly DevToolsPlugin _plugin; |
| 23 | private readonly SecurityKey _securityKey; |
| 24 | |
| 25 | public ActivityController(DevToolsPlugin plugin) |
| 26 | { |
| 27 | _plugin = plugin; |
| 28 | _securityKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("secret_devtools_signing_key_123456")); |
| 29 | } |
| 30 | |
| 31 | [HttpPost("/v3/conversations/{conversationId}/activities")] |
| 32 | public async Task<IResult> Create(string conversationId, [FromBody] JsonNode body, CancellationToken cancellationToken) |
| 33 | { |
| 34 | var isClient = HttpContext.Request.Headers.TryGetValue("x-teams-devtools", out var strings) && strings.Any(h => h == "true"); |
| 35 | body["id"] ??= Guid.NewGuid().ToString(); |
| 36 | |
| 37 | if (!isClient) |
| 38 | { |
| 39 | return Results.Json(new { id = body["id"] }, statusCode: 201); |
| 40 | } |
| 41 | |
| 42 | body["from"] ??= JsonSerializer.SerializeToNode(new Account() |
| 43 | { |
| 44 | Id = "devtools", |
| 45 | Name = "devtools", |
| 46 | }); |
| 47 | |
| 48 | body["conversation"] = JsonSerializer.SerializeToNode(new Conversation() |
| 49 | { |
| 50 | Id = conversationId, |
| 51 | Type = ConversationType.Personal, |
| 52 | Name = "default" |
| 53 | }); |
| 54 | |
| 55 | body["recipient"] = JsonSerializer.SerializeToNode(new Account() |
| 56 | { |
| 57 | Id = _plugin.AppId ?? string.Empty, |
| 58 | Name = _plugin.AppName, |
| 59 | }); |
| 60 | |
| 61 | var activity = JsonSerializer.Deserialize<Activity>(JsonSerializer.Serialize(body)); |
| 62 | |
| 63 | if (activity is null) |
| 64 | { |
| 65 | return Results.BadRequest(); |
| 66 | } |
| 67 | |
| 68 | var descriptor = new SecurityTokenDescriptor |
| 69 | { |
| 70 | Claims = new Dictionary<string, object>() |
| 71 | { |
| 72 | { "serviceurl", $"http://localhost:{HttpContext.Request.Host.Port}/" } |
| 73 | }, |
| 74 | SigningCredentials = new SigningCredentials(_securityKey, SecurityAlgorithms.HmacSha256Signature) |
| 75 | }; |
| 76 | |
| 77 | var tokenHandler = new IdentityModel.JsonWebTokens.JsonWebTokenHandler |
| 78 | { |
| 79 | SetDefaultTimesOnTokenCreation = false |
| 80 | }; |
| 81 | |
| 82 | var tokenString = tokenHandler.CreateToken(descriptor); |
| 83 | var token = new JsonWebToken(tokenString); |
| 84 | var _ = await _plugin.Do(new() |
| 85 | { |
| 86 | Token = token, |
| 87 | Activity = activity, |
| 88 | Services = HttpContext.RequestServices.CreateAsyncScope().ServiceProvider, |
| 89 | }, cancellationToken).ConfigureAwait(false); |
| 90 | |
| 91 | return Results.Json(new { id = body["id"] }, statusCode: 201); |
| 92 | } |
| 93 | } |