Skip to content

Instantly share code, notes, and snippets.

@mikeboers
Created August 27, 2012 13:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikeboers/3488682 to your computer and use it in GitHub Desktop.
Save mikeboers/3488682 to your computer and use it in GitHub Desktop.
How to use Google Authenticator links with OATH Token
import base64
import re
import sys
if len(sys.argv) > 1:
code = ''.join(sys.argv[1:])
else:
code = sys.stdin.read()
# Parse the Google URL.
m = re.match(r'otpauth://(totp|hotp)/(.+?)\?secret=([\w\s]+)', code)
if m:
type_ = m.group(1)
name = m.group(2)
code = m.group(3)
else:
type_ = 'totp'
name = 'Converted'
# Remove all whitespace.
code = re.sub(r'\s', '', code)
# Fix the padding.
code = code.rstrip('=')
if len(code) % 8:
code += '=' * (8 - len(code) % 8)
code = base64.b16encode(base64.b32decode(code, casefold=True))
# Print the new URL.
print 'oathtoken:///addToken?name=%s%s&key=%s' % (name, '&timeBased=true' if type_ == 'totp' else '', code)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment