| 1 |
|
| 2 |
|
| 3 |
from __future__ import print_function |
| 4 |
|
| 5 |
import sys |
| 6 |
from base64 import b64decode |
| 7 |
from optparse import OptionParser |
| 8 |
|
| 9 |
from yubiotp.client import YubiClient10, YubiClient11, YubiClient20 |
| 10 |
|
| 11 |
|
| 12 |
def main(): |
| 13 |
options, args = parse_args() |
| 14 |
|
| 15 |
api_key = b64decode(options.api_key.encode()) if (options.api_key is not None) else None |
| 16 |
|
| 17 |
if options.version == '1.0': |
| 18 |
client = YubiClient10(options.api_id, api_key, options.ssl) |
| 19 |
elif options.version == '1.1': |
| 20 |
client = YubiClient11(options.api_id, api_key, options.ssl, options.timestamp) |
| 21 |
elif options.version == '2.0': |
| 22 |
client = YubiClient20(options.api_id, api_key, options.ssl, options.timestamp, options.sl, options.timeout) |
| 23 |
|
| 24 |
if options.base_url: |
| 25 |
client.base_url = options.base_url |
| 26 |
|
| 27 |
is_valid = True |
| 28 |
|
| 29 |
for token in args: |
| 30 |
if options.noexec: |
| 31 |
print(client.url(token)) |
| 32 |
else: |
| 33 |
if not verify_otp(client, token, options.verbose): |
| 34 |
is_valid = False |
| 35 |
|
| 36 |
sys.exit(0 if is_valid else 2) |
| 37 |
|
| 38 |
|
| 39 |
def parse_args(): |
| 40 |
parser = OptionParser( |
| 41 |
usage='%prog [options] otp ...', |
| 42 |
description="Verifies one or more YubiKey OTP tokens against a YubiCloud service. If you don't supply an API id and key, signatures will be ignored." |
| 43 |
) |
| 44 |
|
| 45 |
parser.add_option('-v', '--verbose', action='store_true', dest='verbose', help="Print request URL and full responst to stderr.") |
| 46 |
parser.add_option('-n', '--noexec', action='store_true', dest='noexec', help="Don't send the request, just print the URL.") |
| 47 |
parser.add_option('-V', '--version', dest='version', type='choice', choices=['1.0', '1.1', '2.0'], default='2.0', help="The API version to use (1.0, 1.1, 2.0). [%default]") |
| 48 |
parser.add_option('-u', '--base-url', dest='base_url', help="Base URL of the api. Defaults to a version-appropriate URL on api.yubico.com.") |
| 49 |
parser.add_option('-s', '--ssl', action='store_true', dest='ssl', help="Use an https url by default.") |
| 50 |
parser.add_option('-i', '--api-id', dest='api_id', default='1', help="Your API ID. [%default]") |
| 51 |
parser.add_option('-k', '--api-key', dest='api_key', help="Your base64-encoded API key.") |
| 52 |
parser.add_option('-t', '--timestamp', action='store_true', dest='timestamp', help="(Version 1.1+) include timestamp and counter information in response.") |
| 53 |
parser.add_option('--sl', dest='sl', help="(Version 2.0+) Request server syncing.") |
| 54 |
parser.add_option('--timeout', dest='timeout', help="(Version 2.0+) Seconds to wait for sync response.") |
| 55 |
|
| 56 |
options, args = parser.parse_args() |
| 57 |
|
| 58 |
return options, args |
| 59 |
|
| 60 |
|
| 61 |
def verify_otp(client, otp, verbose=False): |
| 62 |
response = client.verify(otp) |
| 63 |
|
| 64 |
if verbose: |
| 65 |
print(client.url(otp), file=sys.stderr) |
| 66 |
print(file=sys.stderr) |
| 67 |
|
| 68 |
for line in filter(None, response.raw.splitlines()): |
| 69 |
print(line, file=sys.stderr) |
| 70 |
print(file=sys.stderr) |
| 71 |
|
| 72 |
if response.public_id is not None: |
| 73 |
print('public_id: {0}'.format(response.public_id), file=sys.stderr) |
| 74 |
print(file=sys.stderr) |
| 75 |
|
| 76 |
if response.is_ok(): |
| 77 |
is_valid = True |
| 78 |
print('{0}: OK (strict)'.format(otp)) |
| 79 |
else: |
| 80 |
is_valid = False |
| 81 |
print('{0}: {1}'.format(otp, response.status())) |
| 82 |
|
| 83 |
return is_valid |
| 84 |
|
| 85 |
|
| 86 |
if __name__ == '__main__': |
| 87 |
main() |