# Path: restapi/utils/validation.py
# def is_valid_ipv4(ip):
#     """Validates IPv4 addresses."""
#     pattern = re.compile(r"""
#         ^
#         (?:
#           # Dotted variants:
#           (?:
#             # Decimal 1-255 (no leading 0's)
#             [3-9]\d?|2(?:5[0-5]|[0-4]?\d)?|1\d{0,2}
#           |
#             0x0*[0-9a-f]{1,2}  # Hexadecimal 0x0 - 0xFF (possible leading 0's)
#           |
#             0+[1-3]?[0-7]{0,2} # Octal 0 - 0377 (possible leading 0's)
#           )
#           (?:                  # Repeat 0-3 times, separated by a dot
#             \.
#             (?:
#               [3-9]\d?|2(?:5[0-5]|[0-4]?\d)?|1\d{0,2}
#             |
#               0x0*[0-9a-f]{1,2}
#             |
#               0+[1-3]?[0-7]{0,2}
#             )
#           ){0,3}
#         |
#           0x0*[0-9a-f]{1,8}    # Hexadecimal notation, 0x0 - 0xffffffff
#         |
#           0+[0-3]?[0-7]{0,10}  # Octal notation, 0 - 037777777777
#         |
#           # Decimal notation, 1-4294967295:
#           429496729[0-5]|42949672[0-8]\d|4294967[01]\d\d|429496[0-6]\d{3}|
#           42949[0-5]\d{4}|4294[0-8]\d{5}|429[0-3]\d{6}|42[0-8]\d{7}|
#           4[01]\d{8}|[1-3]\d{0,9}|[4-9]\d{0,8}
#         )
#         $
#     """, re.VERBOSE | re.IGNORECASE)
#     return pattern.match(ip) is not None
# 
# Path: restapi/components/auth/controllers.py
# def show_all_keys():
#     """
#     Show all API keys for all IPs.
#     @return: void
#     """
#     api_keys = get_all_apiauth_object()
# 
#     if api_keys is None:
#         print "No one has access.. That's kind of sad for an API."
#     else:
#         for o in api_keys:
#             print "The IP " + str(o.ip) + " has access key " + str(
#                 o.key) + " API Id: " + str(o.id) + " Description: " + str(o.description)
# 
# def generate_key(ip, desc):
#     """
#     Generates a key for an IP address that can authenticate with the API.
#     Only one API key for IP.
#     @param ip: ip address
#     @param desc: description of access
#     @return: void
#     """
#     key = generate_hash_key()
#     record = APIAuth(key, ip, desc)
#     db.session.add(record)
#     db.session.commit()
#     print "Issued a key '" + key + "' for the IP '" + ip + "' (" + desc + ") with KEY ID: " + str(record.id)
# 
# def show_key(ip):
#     """
#     Show an API Key for some specific IP.
#     @param ip: ip address
#     @return: void
#     """
#     api_key = get_apiauth_object_by_ip(ip)
# 
#     if api_key is None:
#         print "This IP does not have access.."
#     else:
#         print "The IP " + str(ip) + " can access the API with the key: " + str(api_key.key)
# 
# def delete_key(keyid):
#     """
#     Deletes an API key for some specific IP.
#     @param keyid: KEY ID
#     @return: void
#     """
#     if keyid is None:
#         raise Exception("You need to mention a key identity (keyid)")
# 
#     api_key = get_apiauth_object_by_keyid(keyid)
# 
#     if api_key is not None:
#         db.session.delete(api_key)
#         db.session.commit()
#         print "Revoked API access with the key id " + keyid + " (" + api_key.key + ")"
#     else:
#         print "This keyid does not exist.."


from optparse import OptionParser, OptionGroup
from restapi.utils.validation import is_valid_ipv4
from restapi.components.auth.controllers import show_all_keys, generate_key, show_key, delete_key
parser = OptionParser(usage)

group_auth = OptionGroup(parser, "Authentication Options",
                         "Caution: if you remove keys they are not unrecoverable.")

group_auth.add_option("-g", "--generate",
                      help="Generate API key for an IP address",
                      action="store", type="string", dest="generate")
group_auth.add_option("-c", "--comment",
                      help="Comment for generated entry (OPTIONAL)",
                      action="store", type="string", dest="comment")
group_auth.add_option("-d", "--delete",
                      help="Delete API key identified by API ID",
                      action="store", type="string", dest="delete")
group_auth.add_option("-a", "--showall",
                      help="Show all authorized API keys",
                      action="store_true", dest="showall")

parser.add_option_group(group_auth)

(options, args) = parser.parse_args()

if options.showall:
    show_all_keys()

elif options.generate:
    if is_valid_ipv4(options.generate) is True:
        if options.comment is None:
            generate_key(options.generate, "N/A")
        else:

# Path: restapi/utils/validation.py
# def is_valid_ipv4(ip):
#     """Validates IPv4 addresses."""
#     pattern = re.compile(r"""
#         ^
#         (?:
#           # Dotted variants:
#           (?:
#             # Decimal 1-255 (no leading 0's)
#             [3-9]\d?|2(?:5[0-5]|[0-4]?\d)?|1\d{0,2}
#           |
#             0x0*[0-9a-f]{1,2}  # Hexadecimal 0x0 - 0xFF (possible leading 0's)
#           |
#             0+[1-3]?[0-7]{0,2} # Octal 0 - 0377 (possible leading 0's)
#           )
#           (?:                  # Repeat 0-3 times, separated by a dot
#             \.
#             (?:
#               [3-9]\d?|2(?:5[0-5]|[0-4]?\d)?|1\d{0,2}
#             |
#               0x0*[0-9a-f]{1,2}
#             |
#               0+[1-3]?[0-7]{0,2}
#             )
#           ){0,3}
#         |
#           0x0*[0-9a-f]{1,8}    # Hexadecimal notation, 0x0 - 0xffffffff
#         |
#           0+[0-3]?[0-7]{0,10}  # Octal notation, 0 - 037777777777
#         |
#           # Decimal notation, 1-4294967295:
#           429496729[0-5]|42949672[0-8]\d|4294967[01]\d\d|429496[0-6]\d{3}|
#           42949[0-5]\d{4}|4294[0-8]\d{5}|429[0-3]\d{6}|42[0-8]\d{7}|
#           4[01]\d{8}|[1-3]\d{0,9}|[4-9]\d{0,8}
#         )
#         $
#     """, re.VERBOSE | re.IGNORECASE)
#     return pattern.match(ip) is not None
# 
# Path: restapi/components/auth/controllers.py
# def show_all_keys():
#     """
#     Show all API keys for all IPs.
#     @return: void
#     """
#     api_keys = get_all_apiauth_object()
# 
#     if api_keys is None:
#         print "No one has access.. That's kind of sad for an API."
#     else:
#         for o in api_keys:
#             print "The IP " + str(o.ip) + " has access key " + str(
#                 o.key) + " API Id: " + str(o.id) + " Description: " + str(o.description)
# 
# def generate_key(ip, desc):
#     """
#     Generates a key for an IP address that can authenticate with the API.
#     Only one API key for IP.
#     @param ip: ip address
#     @param desc: description of access
#     @return: void
#     """
#     key = generate_hash_key()
#     record = APIAuth(key, ip, desc)
#     db.session.add(record)
#     db.session.commit()
#     print "Issued a key '" + key + "' for the IP '" + ip + "' (" + desc + ") with KEY ID: " + str(record.id)
# 
# def show_key(ip):
#     """
#     Show an API Key for some specific IP.
#     @param ip: ip address
#     @return: void
#     """
#     api_key = get_apiauth_object_by_ip(ip)
# 
#     if api_key is None:
#         print "This IP does not have access.."
#     else:
#         print "The IP " + str(ip) + " can access the API with the key: " + str(api_key.key)
# 
# def delete_key(keyid):
#     """
#     Deletes an API key for some specific IP.
#     @param keyid: KEY ID
#     @return: void
#     """
#     if keyid is None:
#         raise Exception("You need to mention a key identity (keyid)")
# 
#     api_key = get_apiauth_object_by_keyid(keyid)
# 
#     if api_key is not None:
#         db.session.delete(api_key)
#         db.session.commit()
#         print "Revoked API access with the key id " + keyid + " (" + api_key.key + ")"
#     else:
#         print "This keyid does not exist.."


# Path: manage.py
from optparse import OptionParser, OptionGroup
from restapi.utils.validation import is_valid_ipv4
from restapi.components.auth.controllers import show_all_keys, generate_key, show_key, delete_key

parser = OptionParser(usage)

group_auth = OptionGroup(parser, "Authentication Options",
                         "Caution: if you remove keys they are not unrecoverable.")

group_auth.add_option("-g", "--generate",
                      help="Generate API key for an IP address",
                      action="store", type="string", dest="generate")
group_auth.add_option("-c", "--comment",
                      help="Comment for generated entry (OPTIONAL)",
                      action="store", type="string", dest="comment")
group_auth.add_option("-d", "--delete",
                      help="Delete API key identified by API ID",
                      action="store", type="string", dest="delete")
group_auth.add_option("-a", "--showall",
                      help="Show all authorized API keys",
                      action="store_true", dest="showall")

parser.add_option_group(group_auth)

(options, args) = parser.parse_args()

if options.showall:
    show_all_keys()

elif options.generate:
    if is_valid_ipv4(options.generate) is True:
        if options.comment is None:
            generate_key(options.generate, "N/A")
        else:

            generate_key(options.generate, options.comment)