Example #1
0
        /// <inheritdoc />
        public TData?Unprotect(string?protectedText, string?purpose)
        {
            try
            {
                if (protectedText == null)
                {
                    return(default(TData));
                }

                var protectedData = Base64UrlTextEncoder.Decode(protectedText);
                if (protectedData == null)
                {
                    return(default(TData));
                }

                var protector = _protector;
                if (!string.IsNullOrEmpty(purpose))
                {
                    protector = protector.CreateProtector(purpose);
                }

                var userData = protector.Unprotect(protectedData);
                if (userData == null)
                {
                    return(default(TData));
                }

                return(_serializer.Deserialize(userData));
            }
            catch
            {
                // TODO trace exception, but do not leak other information
                return(default(TData));
            }
        }
        private static string GetSubjectId(OAuthTokenResponse tokens)
        {
            var payloadString = tokens.AccessToken.Split('.')[1];

            payloadString = Encoding.UTF8.GetString(Base64UrlTextEncoder.Decode(payloadString));
            var payload = JsonDocument.Parse(payloadString);

            return(payload.RootElement.GetString(EsiaConstants.SbjIdUrn));
        }
        public void DataOfVariousLengthRoundTripCorrectly()
        {
            for (int length = 0; length != 256; ++length)
            {
                var data = new byte[length];
                for (int index = 0; index != length; ++index)
                {
                    data[index] = (byte)(5 + length + (index * 23));
                }
                string text   = Base64UrlTextEncoder.Encode(data);
                byte[] result = Base64UrlTextEncoder.Decode(text);

                for (int index = 0; index != length; ++index)
                {
                    Assert.Equal(data[index], result[index]);
                }
            }
        }
Example #4
0
        public AuthenticationTicket Unprotect(string protectedText, string purpose)
        {
            AuthenticationTicket tdata;

            try
            {
                if (protectedText == null)
                {
                    tdata = default(AuthenticationTicket);
                }
                else
                {
                    byte[] array = Base64UrlTextEncoder.Decode(protectedText);
                    if (array == null)
                    {
                        tdata = default(AuthenticationTicket);
                    }
                    else
                    {
                        IDataProtector dataProtector = _protector;
                        if (!string.IsNullOrEmpty(purpose))
                        {
                            dataProtector = dataProtector.CreateProtector(purpose);
                        }
                        byte[] array2 = dataProtector.Unprotect(array);
                        if (array2 == null)
                        {
                            tdata = default(AuthenticationTicket);
                        }
                        else
                        {
                            tdata = _serializer.Deserialize(array2);
                        }
                    }
                }
            }
            catch
            {
                tdata = default(AuthenticationTicket);
            }
            return(tdata);
        }