Ejemplo n.º 1
0
        public Jwt(string raw)
        {
            Raw = raw;
            if (string.IsNullOrWhiteSpace(Raw))
            {
                // warning: constructed jwt from empty string
                return;
            }

            string[] sections = Raw.Split('.');
            if (sections.Length != 3)
            {
                throw new ArgumentException("failed jwt decode: wrong number of sections", nameof(Raw));
            }

            Payload  = Base64UrlHelpers.DecodeToString(sections[1]);
            Json     = JObject.Parse(Payload);
            IsSigned = !string.IsNullOrEmpty(sections[2]);
        }
Ejemplo n.º 2
0
        public void DecodeToString_ValidBase64UrlString_ReturnsDecodedString()
        {
            var stringToDecodeNoPadding = "MTIzNDU2";
            var expectedDecodedString   = "123456";

            var actualDecodedString = Base64UrlHelpers.DecodeToString(stringToDecodeNoPadding);

            Assert.Equal(expectedDecodedString, actualDecodedString);

            var stringToDecode1Padding        = "MTIzNDU2Nzg";
            var expectedDecodedString1Padding = "12345678";

            actualDecodedString = Base64UrlHelpers.DecodeToString(stringToDecode1Padding);

            Assert.Equal(expectedDecodedString1Padding, actualDecodedString);

            var stringToDecode2Padding        = "MTIzNDU2Nw";
            var expectedDecodedString2Padding = "1234567";

            actualDecodedString = Base64UrlHelpers.DecodeToString(stringToDecode2Padding);

            Assert.Equal(expectedDecodedString2Padding, actualDecodedString);

            var stringToDecodeWithBase64Plus        = "MTI-MTIz";
            var expectedDecodedStringWithBase64Plus = "12>123";

            actualDecodedString = Base64UrlHelpers.DecodeToString(stringToDecodeWithBase64Plus);

            Assert.Equal(expectedDecodedStringWithBase64Plus, actualDecodedString);

            var stringToDecodeWithBase64Slash        = "MTI_MTIz";
            var expectedEncodedStringWithBase64Slash = "12?123";

            actualDecodedString = Base64UrlHelpers.DecodeToString(stringToDecodeWithBase64Slash);

            Assert.Equal(expectedEncodedStringWithBase64Slash, actualDecodedString);

            var emptyStringToDecode = string.Empty;

            actualDecodedString = Base64UrlHelpers.DecodeToString(emptyStringToDecode);

            Assert.Equal(emptyStringToDecode, actualDecodedString);
        }
        [InlineData("", "")]                    // Empty string
        public void DecodeToString_ValidBase64UrlString_ReturnsDecodedString(string stringToDecode, string expectedDecodedString)
        {
            var actualDecodedString = Base64UrlHelpers.DecodeToString(stringToDecode);

            Assert.Equal(expectedDecodedString, actualDecodedString);
        }
        public override void ExecuteCmdlet()
        {
            base.ExecuteCmdlet();

            string resourceUrlOrId;

            if (ParameterSetName == KnownResourceNameParameterSet)
            {
                if (ResourceTypeName == null)
                {
                    ResourceTypeName = SupportedResourceNames.Arm;
                }
                if (!SupportedResourceNames.ResourceNameMap.ContainsKey(ResourceTypeName))
                {
                    throw new ArgumentException(Properties.Resources.InvalidResourceTypeName.FormatInvariant(ResourceTypeName), nameof(ResourceTypeName));
                }
                resourceUrlOrId = SupportedResourceNames.ResourceNameMap[ResourceTypeName];
            }
            else
            {
                resourceUrlOrId = ResourceUrl;
            }

            IAzureContext context = DefaultContext;

            if (TenantId == null)
            {
                TenantId = context.Tenant?.Id;
            }

            IAccessToken accessToken = AzureSession.Instance.AuthenticationFactory.Authenticate(
                context.Account,
                context.Environment,
                TenantId,
                null,
                ShowDialog.Never,
                null,
                null,
                resourceUrlOrId);

            var result = new PSAccessToken()
            {
                Token    = accessToken.AccessToken,
                TenantId = TenantId,
                UserId   = accessToken.UserId,
            };

            result.ExpiresOn = (accessToken as MsalAccessToken)?.ExpiresOn ?? result.ExpiresOn;
            if (result.ExpiresOn == default(DateTimeOffset))
            {
                try
                {
                    var tokenParts   = accessToken.AccessToken.Split('.');
                    var decodedToken = Base64UrlHelpers.DecodeToString(tokenParts[1]);

                    var tokenDocument = JsonDocument.Parse(decodedToken);
                    int expSeconds    = tokenDocument.RootElement.EnumerateObject()
                                        .Where(p => p.Name == "exp")
                                        .Select(p => p.Value.GetInt32())
                                        .First();
                    result.ExpiresOn = UnixEpoch.AddSeconds(expSeconds);
                }
                catch (Exception e)//Ignore exception if fails to parse exp from token
                {
                    WriteDebug("Unable to parse exp in token: " + e.ToString());
                }
            }

            WriteObject(result);
        }