예제 #1
0
        public void No_Byte_Allocation_Side_Effects()
        {
            var bytes = Encoding.UTF8.GetBytes("This is a byte test");
            var setup = new TotpSetup("IRRELEVANT", bytes);

            Assert.Equal(@"data:image/png;base64,VGhpcyBpcyBhIGJ5dGUgdGVzdA==", setup.QrCodeImage);
            Assert.Equal(bytes, setup.QrCodeImageBytes);
        }
예제 #2
0
        /// <summary>
        ///     生成一个bbbase64二维码图片 以便Authenticator 扫描添加
        /// </summary>
        /// <param name="issuer">应用程序或者公司名称 英文 中文可能无法显示.</param>
        /// <param name="accountIdentity">
        ///     用户名或者有效可以显示到Authenticator中
        /// </param>
        /// <param name="accountSecretKey">
        ///     密钥最好每个人都是不同的随机生成或者GUID,校验TOTP时需传入
        /// </param>
        /// <param name="pixelsPerModule">二维码每个点的像素大小默认5.</param>
        /// <param name="isBa64">是否生成base64</param>
        /// <returns>返回手动设置key和二维码base64获取内容 依据isBa64.</returns>
        private TotpSetup GenerateTopSetup(string issuer, string accountIdentity, string accountSecretKey, int pixelsPerModule = 5, bool isBa64 = false)
        {
            Guard.NotNull(issuer);
            Guard.NotNull(accountIdentity);
            Guard.NotNull(accountSecretKey);

            accountIdentity = accountIdentity.Replace(" ", "");
            var encodedSecretKey = Base32.Encode(accountSecretKey);
            var provisionUrl     = $"otpauth://totp/{accountIdentity}?secret={encodedSecretKey}&issuer={issuer}";

            var totpSetup = new TotpSetup
            {
                QrCodeImageBase64  = isBa64 ? GetQrBase64Imageg(provisionUrl, pixelsPerModule) : null,
                QrCodeImageContent = !isBa64 ? provisionUrl : null,
                ManualSetupKey     = encodedSecretKey
            };

            return(totpSetup);
        }
        /// <summary>
        /// Generates an object you will need so that the user can setup his Google Authenticator to be used with your app.
        /// </summary>
        /// <param name="issuer">Your app name or company for example.</param>
        /// <param name="accountIdentity">Name, Email or Id of the user, without spaces, this will be shown in google authenticator.</param>
        /// <param name="accountSecretKey">A secret key which will be used to generate one time passwords. This key is the same needed for validating a passed TOTP.</param>
        /// <param name="qrCodeWidth">Height of the QR code. Default is 300px.</param>
        /// <param name="qrCodeHeight">Width of the QR code. Default is 300px.</param>
        /// <param name="useHttps">Use Https on google api or not.</param>
        /// <returns>TotpSetup with ManualSetupKey and QrCode.</returns>
        public TotpSetup Generate(string issuer, string accountIdentity, string accountSecretKey, int qrCodeWidth = 300, int qrCodeHeight = 300, bool useHttps = true)
        {
            Guard.NotNull(issuer);
            Guard.NotNull(accountIdentity);
            Guard.NotNull(accountSecretKey);

            accountIdentity = accountIdentity.Replace(" ", "");
            var encodedSecretKey = Base32.Encode(accountSecretKey);
            var provisionUrl     = UrlEncoder.Encode(string.Format("otpauth://totp/{0}?secret={1}&issuer={2}", accountIdentity, encodedSecretKey, UrlEncoder.Encode(issuer)));
            var protocol         = useHttps ? "https" : "http";
            var url = $"{protocol}://chart.googleapis.com/chart?cht=qr&chs={qrCodeWidth}x{qrCodeHeight}&chl={provisionUrl}";

            var totpSetup = new TotpSetup
            {
                QrCodeImage    = this.GetQrImage(url),
                ManualSetupKey = encodedSecretKey
            };

            return(totpSetup);
        }