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