def get_hd_domain(username, default_domain='default'):
  """Returns the domain associated with an email address.

  Intended for use with the OAuth hd parameter for Google.

  Args:
    username: Username to parse.
    default_domain: Domain to set if '@suchandsuch.huh' is not part of the
        username. Defaults to 'default' to specify a regular Google account.

  Returns:
    String of the domain associated with username.
  """
  name, at_sign, domain = username.partition('@')
  # If user specifies gmail.com, it confuses the hd parameter
  # (thanks, bartosh!)
  if domain == 'gmail.com' or domain == 'googlemail.com':
    return 'default'
  return domain or default_domain