public static BasicAuthorizationHeader fromString(final String header) throws InvalidAuthorizationHeaderException {
    try {
      if (StringUtils.isBlank(header)) {
        throw new InvalidAuthorizationHeaderException("Blank header");
      }

      final int spaceIndex = header.indexOf(' ');

      if (spaceIndex == -1) {
        throw new InvalidAuthorizationHeaderException("Invalid authorization header: " + header);
      }

      final String authorizationType = header.substring(0, spaceIndex);

      if (!"Basic".equals(authorizationType)) {
        throw new InvalidAuthorizationHeaderException("Unsupported authorization method: " + authorizationType);
      }

      final String credentials;

      try {
        credentials = new String(Base64.getDecoder().decode(header.substring(spaceIndex + 1)));
      } catch (final IndexOutOfBoundsException e) {
        throw new InvalidAuthorizationHeaderException("Missing credentials");
      }

      if (StringUtils.isEmpty(credentials)) {
        throw new InvalidAuthorizationHeaderException("Bad decoded value: " + credentials);
      }

      final int credentialSeparatorIndex = credentials.indexOf(':');

      if (credentialSeparatorIndex == -1) {
        throw new InvalidAuthorizationHeaderException("Badly-formatted credentials: " + credentials);
      }

      final String usernameComponent = credentials.substring(0, credentialSeparatorIndex);

      final String username;
      final byte deviceId;
      {
        final Pair<String, Byte> identifierAndDeviceId =
            AccountAuthenticator.getIdentifierAndDeviceId(usernameComponent);

        username = identifierAndDeviceId.first();
        deviceId = identifierAndDeviceId.second();
      }

      final String password = credentials.substring(credentialSeparatorIndex + 1);

      if (StringUtils.isAnyBlank(username, password)) {
        throw new InvalidAuthorizationHeaderException("Username or password were blank");
      }

      return new BasicAuthorizationHeader(username, deviceId, password);
    } catch (final IllegalArgumentException | IndexOutOfBoundsException e) {
      throw new InvalidAuthorizationHeaderException(e);
    }
  }
@Test
  void fromString() throws InvalidAuthorizationHeaderException {
    {
      final BasicAuthorizationHeader header =
          BasicAuthorizationHeader.fromString("Basic YWxhZGRpbjpvcGVuc2VzYW1l");

      assertEquals("aladdin", header.getUsername());
      assertEquals("opensesame", header.getPassword());
      assertEquals(Device.PRIMARY_ID, header.getDeviceId());
    }

    {
      final BasicAuthorizationHeader header = BasicAuthorizationHeader.fromString("Basic " +
          Base64.getEncoder().encodeToString("username.7:password".getBytes(StandardCharsets.UTF_8)));

      assertEquals("username", header.getUsername());
      assertEquals("password", header.getPassword());
      assertEquals(7, header.getDeviceId());
    }
  }