コード例 #1
0
        public async Task <IHttpActionResult> GoogleAuthConfirm(GoogleAuthConfirmationModel confirmationModel)
        {
            Logger.InfoWithIp(CurrentClassName, nameof(GoogleAuthConfirm), $"Google auth confirm request for user {User.Identity.GetUserName()}");
            await googleAuthModule.Confirm(confirmationModel, User.Identity.GetUserId());

            return(Ok());
        }
コード例 #2
0
        public async Task <GoogleAuthConfirmationModel> GetSecret(string userName)
        {
            var secretKey  = KeyGeneration.GenerateRandomKey(20);
            var barcodeUrl = KeyUrl.GetTotpUrl(secretKey, userName) + $"&issuer={issuerName}";

            var model = new GoogleAuthConfirmationModel
            {
                Barcode   = QrCodeImageGeneratorUrlPrefix + HttpUtility.UrlEncode(barcodeUrl),
                SecretKey = Base32Encoder.Encode(secretKey)
            };

            return(model);
        }
コード例 #3
0
        public async Task Confirm(GoogleAuthConfirmationModel confirmationModel, string userId)
        {
            Logger.Debug(CurrentClassName, nameof(Confirm), $"Decoding secret key '{confirmationModel.SecretKey}'");
            var secretKey = Base32Encoder.Decode(confirmationModel.SecretKey);

            long timeStepMatched = 0;

            Logger.Debug(CurrentClassName, nameof(Confirm), $"Generating TOTP-key");
            var otp = new Totp(secretKey);

            if (otp.VerifyTotp(confirmationModel.InputCode, out timeStepMatched))
            {
                var user = await Repository.FindById(userId);

                user.IsGoogleAuthenticatorEnabled = true;
                user.GoogleAuthenticatorSecretKey = confirmationModel.SecretKey;
                await Repository.UpdateUser(user);
            }

            throw new Exception("Code is not valid");
        }