Skip to content

Instantly share code, notes, and snippets.

@dirkjanfaber
Created June 14, 2021 07:46
Show Gist options
  • Save dirkjanfaber/b5dd8082dda2baced94e9bb752dec9d8 to your computer and use it in GitHub Desktop.
Save dirkjanfaber/b5dd8082dda2baced94e9bb752dec9d8 to your computer and use it in GitHub Desktop.
Fetch the authenticator code so it is usable in a script.
#!/usr/bin/env python3
import authenticator as a
import os
def get_code():
cd = (a.data
.ClientFile(
os.environ['AUTHENTICATOR_PASSWORD'])
.load(
os.path.expanduser('~/.authenticator/authenticator.data')
)[0])
return (a.hotp.HOTP()
.generate_code_from_time(
cd.shared_secret(),
code_length=cd.password_length(),
period=cd.period())[0])
try:
os.environ['AUTHENTICATOR_PASSWORD']
except KeyError:
raise SystemExit("### Please set AUTHENTICATOR_PASSWORD")
print(get_code())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment