public string ToJson()
        {
            var jsWriter = new JsonWriter();

            jsWriter.WriteObjectStart();
            jsWriter.WritePropertyName("alg");
            jsWriter.Write(Algorithm.ToString());

            if (null != KeyUri)
            {
                switch (KeyFormat)
                {
                case KeyFormat.Json:
                    jsWriter.WritePropertyName("jku");
                    break;

                case KeyFormat.X509:
                    jsWriter.WritePropertyName("xku");
                    break;

                case KeyFormat.Rfc4050:
                    jsWriter.WritePropertyName("xdu");
                    break;
                }
                jsWriter.Write(KeyUri.ToString());
            }

            if (false == string.IsNullOrEmpty(KeyId))
            {
                jsWriter.WritePropertyName("kid");
                jsWriter.Write(KeyId);
            }
            jsWriter.WriteObjectEnd();
            return(jsWriter.ToString());
        }
Beispiel #2
0
        public void LabelAndIssuerAreCorrectlyParsed(string uriString, string expectedLabel, string expectedIssuer)
        {
            var model = new KeyUri(new Uri(uriString));

            model.Label.Should().Be(expectedLabel);
            model.Issuer.Should().Be(expectedIssuer);
        }
Beispiel #3
0
        public void MinimalUrl_ShouldDefaultAlgorithmToSha1()
        {
            var uri = new Uri(MinimalKeyUri);

            var keyUri = new KeyUri(uri);

            keyUri.Algorithm.Should().Be("SHA1");
        }
Beispiel #4
0
        public void MinimalUrl_ShouldDefaultDigitsTo6()
        {
            var uri = new Uri(MinimalKeyUri);

            var keyUri = new KeyUri(uri);

            keyUri.Digits.Should().Be(6);
        }
Beispiel #5
0
        public void MinimalUrl_ShouldDefaultPeriodTo30()
        {
            var uri = new Uri(MinimalKeyUri);

            var keyUri = new KeyUri(uri);

            keyUri.Period.Should().Be(30);
        }
Beispiel #6
0
            /// <inheritdoc/>
            public override Boolean Equals(Object obj)
            {
                KeyUri other = obj as KeyUri;

                if (other == null)
                {
                    return(false);
                }
                return(Object.Equals(_uri, other._uri) && Object.Equals(_endpoint, other._endpoint));
            }
Beispiel #7
0
        public void TestFullKeyUri()
        {
            var model        = new KeyUri(new Uri("otpauth://totp/ACME%20Co:[email protected]?secret=HXDMVJECJJWSRB3HWIZR4IFUGFTMXBOZ&issuer=ACME%20Co&algorithm=SHA1&digits=6&period=30"));
            var modelUri     = model.GetUri();
            var generatedUri = new KeyUri(modelUri);

            model.Issuer.Should().Be(generatedUri.Issuer);
            model.Label.Should().Be(generatedUri.Label);
            model.Secret.Should().Be(generatedUri.Secret);
            model.Digits.Should().Be(generatedUri.Digits);
            model.Algorithm.Should().Be(generatedUri.Algorithm);
            model.Period.Should().Be(generatedUri.Period);
        }
Beispiel #8
0
        public void Ctor_ShouldInitializeValidTotpKeyUri()
        {
            var uri = new Uri("otpauth://totp/ACME%20Co:[email protected]?secret=HXDMVJECJJWSRB3HWIZR4IFUGFTMXBOZ&issuer=ACME%20Co&algorithm=SHA1&digits=6&period=30");

            var keyUri = new KeyUri(uri);

            keyUri.Type.Should().Be("totp");
            keyUri.Secret.Should().Be("HXDMVJECJJWSRB3HWIZR4IFUGFTMXBOZ");
            keyUri.Issuer.Should().Be("ACME Co");
            keyUri.Algorithm.Should().Be("SHA1");
            keyUri.Digits.Should().Be(6);
            keyUri.Period.Should().Be(30);
        }
Beispiel #9
0
        public void GetUri_ShouldContainDefaultParameters()
        {
            var uri = new Uri(MinimalKeyUri);

            var keyUri = new KeyUri(uri);

            keyUri.Algorithm = "SHA1";
            keyUri.Digits    = 6;
            keyUri.Period    = 30;

            var absoluteUri = keyUri.GetUri().AbsoluteUri;

            absoluteUri.Should().NotContain("period=");
            absoluteUri.Should().NotContain("digits=");
            absoluteUri.Should().NotContain("algorithm=");
        }
Beispiel #10
0
        public void GetUri_ShouldContainNonDefaultParameters()
        {
            var uri = new Uri(MinimalKeyUri);

            var keyUri = new KeyUri(uri);

            keyUri.Algorithm = "SHA256";
            keyUri.Digits    = 8;
            keyUri.Period    = 60;

            var absoluteUri = keyUri.GetUri().AbsoluteUri;

            absoluteUri.Should().Contain("period=60");
            absoluteUri.Should().Contain("digits=8");
            absoluteUri.Should().Contain("algorithm=SHA256");
        }
Beispiel #11
0
        private void btnGenerate_Click(object sender, EventArgs e)
        {
            // get a 16 character random string that is base32 for secret
            // then decode the base32 to the real byte array secret key
            _encodedKey = RandomKey.GetRandomEncodedKey();
            _secretKey  = Base32.Decode(_encodedKey);

            // display
            lblSecretKey.Text = string.Format("{0} {1} {2} {3}",
                                              _encodedKey.Substring(0, 4), _encodedKey.Substring(4, 4),
                                              _encodedKey.Substring(8, 4), _encodedKey.Substring(12, 4));

            // get qrcode
            IBarcodeWriter barcodeWriter = new BarcodeWriter
            {
                Format  = BarcodeFormat.QR_CODE,
                Options = new QrCodeEncodingOptions
                {
                    Width  = 250,
                    Height = 250,
                    Margin = 1
                }
            };

            pbQRCode.Image = new Bitmap(barcodeWriter.Write(KeyUri.GetToptUri("TwoFactorNet", "TestUser", _encodedKey)));

            // generate code
            var totp = new Totp(_secretKey, 6, 30);

            lblCurrentVerificationCode.Text = totp.GeneratePassword(UnixTime.GetUnixTime());

            // get next cycle time
            _nextCycleTime = TimeHelper.GetNextCycleTimeUtc();

            // move progressbar to correct value
            pbCurrentVerificationCode.Value = (_nextCycleTime.Second == 0 ? 60 - DateTime.Now.Second : DateTime.Now.Second);

            // setup timer to display
            timer.Enabled = true;
        }
Beispiel #12
0
            /// <inheritdoc/>
            public override Boolean Equals(Object obj)
            {
                KeyUri other = obj as KeyUri;

                if (other == null)
                {
                    return(false);
                }
#if INCLUDE_OSCOAP
                if (Object.Equals(_uri, other._uri) && Object.Equals(_endpoint, other._endpoint))
                {
                    if (_oscoap != null)
                    {
                        if (other._oscoap == null)
                        {
                            return(false);
                        }
                        if (_oscoap.Length != other._oscoap.Length)
                        {
                            return(false);
                        }
                        for (int i = 0; i < _oscoap.Length; i++)
                        {
                            if (_oscoap[i] != other._oscoap[i])
                            {
                                return(false);
                            }
                        }
                        return(true);
                    }
                    return(other._oscoap == null);
                }
                return(false);
#else
                return(Object.Equals(_uri, other._uri) && Object.Equals(_endpoint, other._endpoint));
#endif
            }
Beispiel #13
0
        private void pbQRCode_MouseHover(object sender, EventArgs e)
        {
            var tt = new ToolTip();

            tt.SetToolTip((Control)sender, KeyUri.GetToptUri("TwoFactorNet", "TestUser", _encodedKey));
        }