Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GoogleAuthenticator class is thread-safe? #68

Open
s-papa opened this issue Aug 21, 2018 · 1 comment
Open

GoogleAuthenticator class is thread-safe? #68

s-papa opened this issue Aug 21, 2018 · 1 comment

Comments

@s-papa
Copy link

s-papa commented Aug 21, 2018

Is it safe to generate keys or verify passwords in a multi-threaded environment using singleton instance for the GoogleAuthenticator class

@mikhail-nikolaenko
Copy link

Had the same question. I have checked source code and found no global properties usage except configuration, CredentialRepository, and SecureRandom.
Configuration itself is private final, but mutable. Any way even if it is mutable, there is no public getter (actually no getter at all), so no one can change it from outside, and from inside class only reads values. SecureRandom implements Random which is thread safe.
At last, we have CredentialRepository. When we check the following code:

    public ICredentialRepository getCredentialRepository() {
        if (this.credentialRepositorySearched) {
            return this.credentialRepository;
        } else {
            this.credentialRepositorySearched = true;
            ServiceLoader<ICredentialRepository> loader = ServiceLoader.load(ICredentialRepository.class);
            Iterator var2 = loader.iterator();
            if (var2.hasNext()) {
                ICredentialRepository repository = (ICredentialRepository)var2.next();
                this.credentialRepository = repository;
            }

            return this.credentialRepository;
        }
    }

we see that here GoogleAuthenticator tries to load Service and cache it. And unfortunately, at least this method is not thread safe. Problem could happened if several threads will use GoogleAuthenticator straight after class was instantiated. One more issue here is a public setter for CredentialRepository: setCredentialRepository which is also not synchronized with other methods.

So out of the box this class is not a thread safe. So my suggestion is to extend this class and implement custom synchronization for methods. Any way on many *nix systems securerandom will block as it uses /dev/random stream to generate random numbers, so won't be a big penalty for synchronizing business methods. Another question - how faster will be class in genera as we will safe SecureRandom instantiation for every call, which is in this class most expensive. Only profiling on target server could answer this question on 100% :)

wbr,
Mike

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants