microsoft/teams.net
Publicmirrored fromhttps://github.com/microsoft/teams.netAvailable
Libraries/Microsoft.Teams.Api/Auth/JsonWebToken.cs
94lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | using System.IdentityModel.Tokens.Jwt; |
| 5 | using System.Text.Json.Serialization; |
| 6 | |
| 7 | namespace Microsoft.Teams.Api.Auth; |
| 8 | |
| 9 | public class JsonWebToken : IToken |
| 10 | { |
| 11 | [JsonPropertyName("appid")] |
| 12 | public string? AppId => Token.Payload.TryGetValue("appid", out var value) ? (string?)value : null; |
| 13 | |
| 14 | [JsonPropertyName("app_displayname")] |
| 15 | public string? AppDisplayName => Token.Payload.TryGetValue("app_displayname", out var value) ? (string?)value : null; |
| 16 | |
| 17 | [JsonPropertyName("tid")] |
| 18 | public string? TenantId => Token.Payload.TryGetValue("tid", out var value) ? (string?)value : null; |
| 19 | |
| 20 | [JsonPropertyName("serviceurl")] |
| 21 | public string ServiceUrl |
| 22 | { |
| 23 | get |
| 24 | { |
| 25 | var serviceUrl = Token.Payload.TryGetValue("serviceurl", out var value) ? (string?)value : null; |
| 26 | |
| 27 | if (serviceUrl is null) |
| 28 | { |
| 29 | serviceUrl = "https://smba.trafficmanager.net/teams"; |
| 30 | } |
| 31 | |
| 32 | if (!serviceUrl.EndsWith("/")) |
| 33 | { |
| 34 | serviceUrl += '/'; |
| 35 | } |
| 36 | |
| 37 | return serviceUrl; |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | [JsonPropertyName("from")] |
| 42 | public CallerType From |
| 43 | { |
| 44 | get => AppId is null ? CallerType.Azure : CallerType.Bot; |
| 45 | } |
| 46 | |
| 47 | [JsonPropertyName("fromId")] |
| 48 | public string FromId |
| 49 | { |
| 50 | get => From.IsBot ? $"urn:botframework:aadappid:{AppId}" : "urn:botframework:azure"; |
| 51 | } |
| 52 | |
| 53 | [JsonPropertyName("expiration")] |
| 54 | public DateTime? Expiration |
| 55 | { |
| 56 | get => Token.ValidTo; |
| 57 | } |
| 58 | |
| 59 | [JsonIgnore] |
| 60 | public bool IsExpired |
| 61 | { |
| 62 | get => Token.ValidTo <= DateTime.UtcNow.AddMilliseconds(1000 * 60 * 5); |
| 63 | } |
| 64 | |
| 65 | [JsonPropertyName("scopes")] |
| 66 | public IEnumerable<string> Scopes |
| 67 | { |
| 68 | get |
| 69 | { |
| 70 | var claim = Token.Claims.FirstOrDefault(c => c.Type == "scope" || c.Type == "scp"); |
| 71 | if (claim is null) return []; |
| 72 | return claim.Value.Split(' '); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | public JwtSecurityToken Token { get; } |
| 77 | private readonly string _tokenAsString; |
| 78 | |
| 79 | public JsonWebToken(string token) |
| 80 | { |
| 81 | var handler = new JwtSecurityTokenHandler(); |
| 82 | Token = handler.ReadJwtToken(token); |
| 83 | _tokenAsString = token; |
| 84 | } |
| 85 | |
| 86 | public JsonWebToken(Token.Response response) |
| 87 | { |
| 88 | var handler = new JwtSecurityTokenHandler(); |
| 89 | Token = handler.ReadJwtToken(response.Token); |
| 90 | _tokenAsString = response.Token; |
| 91 | } |
| 92 | |
| 93 | public override string ToString() => _tokenAsString; |
| 94 | } |