wstrange / GoogleAuth

Google Authenticator Server side code

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

not working for me

vimalprakashts opened this issue · comments

The google authenticator app and this code are generating different code not the same one. it always says false . :(

GoogleAuthenticator gAuth = new GoogleAuthenticator();
final GoogleAuthenticatorKey key = gAuth.createCredentials();
System.out.println(key.getKey());

System.out.println("Enter your code: ");
Scanner scanner = new Scanner(System.in);
String code = scanner.nextLine();
int CodInt = Integer.parseInt(code);
System.out.println("Your code is " + code);
boolean isCodeValid = gAuth.authorize(key.getKey(), CodInt);

I assume you seeded the google authenticator with the secret key? My guess is that it did not get registered properly.

I would try the QR code method of registering the secret.

hi @wstrange , I seeded that correctly . definitely something wrong with the library.

Hi @vimalprakashts, unless you provide a test case that actually proves that the library is not working I'm afraid you can't state that there is "definitely" a problem with it. Besides the fact that during the library compile phase a test is run that validates all the test cases specified in the RFC, there's a big number of users that haven't filed a similar bug. I'm personally using it in production on literally dozens of applications and I can say that for me it definitely works.

Please, open a new issue or reopen this one trying to provide useful information. Your code snippet above serves no purpose in this sense since it only says that you manually feed the library a code that does not validate. How is that code generated, I don't know. And it's not relevant to this discussion.

hi @emcrisostomo , thank you for you reply. here is my test app code

import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Scanner;

import com.warrenstrange.googleauth.GoogleAuthenticator;
import com.warrenstrange.googleauth.GoogleAuthenticatorKey;


public class App {
    public static void main(String[] args) throws InvalidKeyException, NoSuchAlgorithmException {

         GoogleAuthenticator gAuth = new GoogleAuthenticator();
         final GoogleAuthenticatorKey key = gAuth.createCredentials();
         System.out.println(key.getKey());

         System.out.println("Enter your code: ");
         Scanner scanner = new Scanner(System.in);
         String code = scanner.nextLine();
         int CodInt = Integer.parseInt(code);
         System.out.println("Your code is " + code);
         boolean isCodeValid = gAuth.authorize(key.getKey(), CodInt);

                if(isCodeValid)
                {
             System.out.println("Your code is valid");
                }
                else{
              System.out.println("Your code is not valid");
                }       
    }
}

I run this code, and take the key and entered in google authenticator

Enter account name : test1

Enter your key : "Key generated from 3 line of main code"

                GoogleAuthenticator gAuth = new GoogleAuthenticator();
         final GoogleAuthenticatorKey key = gAuth.createCredentials();
         System.out.println(key.getKey());

Select the Time-based option , and click the ADD button.

the google authenticator immediately starts the code, I copied that code and enter as input to the next code block.

 System.out.println("Enter your code: ");
         Scanner scanner = new Scanner(System.in);
         String code = scanner.nextLine();
         int CodInt = Integer.parseInt(code);
         System.out.println("Your code is " + code);
         boolean isCodeValid = gAuth.authorize(key.getKey(), CodInt);

                if(isCodeValid)
                {
             System.out.println("Your code is valid");
                }
                else{
              System.out.println("Your code is not valid");
                }   

it prints me Your Code is not valid.

Please check I am doing correct or something wrong ?

First check: is your phone clock properly set? A small difference can result in a failure, especially with the default configuration.

@emcrisostomo @wstrange

Yes, there is issues with my Phone clock. Mine is 2 minutes aged of normal time :( , it is working fine now. thank you so much..