@Test(expected = IllegalArgumentException.class) public void fromUriStringShouldFailWhenURIContainsAnUnsupportedParameter() { OTPAuthURIBuilder.fromUriString("otpauth: }
public static OTPAuthURIBuilder fromUriString(String uri) { Preconditions.checkNotNull(uri); Matcher m = OTP_AUTH_URI_PATTERN.matcher(uri); if (!m.matches()) { throw new IllegalArgumentException("[" + uri + "] is not a valid OTP Auth URI"); } final OTPType otpType = OTPType.from(m.group(2).toUpperCase(Locale.US)); String label = null; try { label = new URI(uri).getPath().substring(1); } catch (URISyntaxException e) { throw new IllegalArgumentException(e); } if (Strings.nullToEmpty(label).trim().isEmpty()) { throw new IllegalArgumentException("[" + uri + "] is not a valid otp auth URI: 'label' component is missing!"); } String secret = null; String issuer = null; Integer digits = null; Long counter = null; Long period = null; final String query = m.group(4); final Map<String, String> decoder = Splitter.on('&').withKeyValueSeparator("=").split(query); for (Entry<String, String> queryParam : decoder.entrySet()) { if ("secret".equals(queryParam.getKey())) { secret = queryParam.getValue(); if (Strings.nullToEmpty(secret).trim().isEmpty()) { throw new IllegalArgumentException("[" + uri + "] is not a valid otp auth URI: 'secret' parameter value is missing!"); } } else if ("issuer".equals(queryParam.getKey())) { if (Strings.nullToEmpty(queryParam.getValue()).trim().isEmpty()) { throw new IllegalArgumentException("[" + uri + "] is not a valid otp auth URI: 'issuer' parameter value is missing!"); } try { issuer = URLDecoder.decode(queryParam.getValue(), StandardCharsets.UTF_8.name()); } catch (UnsupportedEncodingException e) { throw new RuntimeException("Unexpected error - underlying platform does not support UTF-8 charset!", e); } } else if ("digits".equals(queryParam.getKey())) { if (Strings.nullToEmpty(queryParam.getValue()).trim().isEmpty()) { throw new IllegalArgumentException("[" + uri + "] is not a valid otp auth URI: 'digits' parameter value is missing!"); } digits = Integer.valueOf(queryParam.getValue()); } else if ("counter".equals(queryParam.getKey())) { if (!otpType.equals(OTPType.HOTP)) { throw new IllegalArgumentException("[" + uri + "] is not a valid otp auth URI: 'counter' is not a valid totp parameter!"); } if (Strings.nullToEmpty(queryParam.getValue()).trim().isEmpty()) { throw new IllegalArgumentException("[" + uri + "] is not a valid otp auth URI: 'counter' parameter value is missing!"); } counter = Long.valueOf(queryParam.getValue()); } else if ("period".equals(queryParam.getKey())) { if (!otpType.equals(OTPType.TOTP)) { throw new IllegalArgumentException("[" + uri + "] is not a valid otp auth URI: 'period' is not a valid hotp parameter!"); } if (Strings.nullToEmpty(queryParam.getValue()).trim().isEmpty()) { throw new IllegalArgumentException("[" + uri + "] is not a valid otp auth URI: 'period' parameter value is missing!"); } period = Long.valueOf(queryParam.getValue()); } else { throw new IllegalArgumentException("[" + uri + "] is not a valid otp auth URI: '" + queryParam.getKey() + "' is not a supported parameter!"); } } if (secret == null) { throw new IllegalArgumentException("[" + uri + "] is not a valid otp auth URI: 'secret' parameter is missing!"); } if (digits == null) { throw new IllegalArgumentException("[" + uri + "] is not a valid otp auth URI: 'digits' parameter is missing!"); } if (otpType.equals(OTPType.HOTP) && counter == null) { throw new IllegalArgumentException("[" + uri + "] is not a valid otp auth URI: 'counter' parameter is missing!"); } if (otpType.equals(OTPType.TOTP) && period == null) { throw new IllegalArgumentException("[" + uri + "] is not a valid otp auth URI: 'period' parameter is missing!"); } OTPAuthURIBuilder builder = new OTPAuthURIBuilder(new OTPKey(secret, otpType)).label(label).issuer(issuer).digits(digits); if (counter != null) { builder.counter(counter); } if (period != null) { builder.timeStep(TimeUnit.SECONDS.toMillis(period)); } return builder; }
OTPAuthURIBuilder { public static OTPAuthURIBuilder fromUriString(String uri) { Preconditions.checkNotNull(uri); Matcher m = OTP_AUTH_URI_PATTERN.matcher(uri); if (!m.matches()) { throw new IllegalArgumentException("[" + uri + "] is not a valid OTP Auth URI"); } final OTPType otpType = OTPType.from(m.group(2).toUpperCase(Locale.US)); String label = null; try { label = new URI(uri).getPath().substring(1); } catch (URISyntaxException e) { throw new IllegalArgumentException(e); } if (Strings.nullToEmpty(label).trim().isEmpty()) { throw new IllegalArgumentException("[" + uri + "] is not a valid otp auth URI: 'label' component is missing!"); } String secret = null; String issuer = null; Integer digits = null; Long counter = null; Long period = null; final String query = m.group(4); final Map<String, String> decoder = Splitter.on('&').withKeyValueSeparator("=").split(query); for (Entry<String, String> queryParam : decoder.entrySet()) { if ("secret".equals(queryParam.getKey())) { secret = queryParam.getValue(); if (Strings.nullToEmpty(secret).trim().isEmpty()) { throw new IllegalArgumentException("[" + uri + "] is not a valid otp auth URI: 'secret' parameter value is missing!"); } } else if ("issuer".equals(queryParam.getKey())) { if (Strings.nullToEmpty(queryParam.getValue()).trim().isEmpty()) { throw new IllegalArgumentException("[" + uri + "] is not a valid otp auth URI: 'issuer' parameter value is missing!"); } try { issuer = URLDecoder.decode(queryParam.getValue(), StandardCharsets.UTF_8.name()); } catch (UnsupportedEncodingException e) { throw new RuntimeException("Unexpected error - underlying platform does not support UTF-8 charset!", e); } } else if ("digits".equals(queryParam.getKey())) { if (Strings.nullToEmpty(queryParam.getValue()).trim().isEmpty()) { throw new IllegalArgumentException("[" + uri + "] is not a valid otp auth URI: 'digits' parameter value is missing!"); } digits = Integer.valueOf(queryParam.getValue()); } else if ("counter".equals(queryParam.getKey())) { if (!otpType.equals(OTPType.HOTP)) { throw new IllegalArgumentException("[" + uri + "] is not a valid otp auth URI: 'counter' is not a valid totp parameter!"); } if (Strings.nullToEmpty(queryParam.getValue()).trim().isEmpty()) { throw new IllegalArgumentException("[" + uri + "] is not a valid otp auth URI: 'counter' parameter value is missing!"); } counter = Long.valueOf(queryParam.getValue()); } else if ("period".equals(queryParam.getKey())) { if (!otpType.equals(OTPType.TOTP)) { throw new IllegalArgumentException("[" + uri + "] is not a valid otp auth URI: 'period' is not a valid hotp parameter!"); } if (Strings.nullToEmpty(queryParam.getValue()).trim().isEmpty()) { throw new IllegalArgumentException("[" + uri + "] is not a valid otp auth URI: 'period' parameter value is missing!"); } period = Long.valueOf(queryParam.getValue()); } else { throw new IllegalArgumentException("[" + uri + "] is not a valid otp auth URI: '" + queryParam.getKey() + "' is not a supported parameter!"); } } if (secret == null) { throw new IllegalArgumentException("[" + uri + "] is not a valid otp auth URI: 'secret' parameter is missing!"); } if (digits == null) { throw new IllegalArgumentException("[" + uri + "] is not a valid otp auth URI: 'digits' parameter is missing!"); } if (otpType.equals(OTPType.HOTP) && counter == null) { throw new IllegalArgumentException("[" + uri + "] is not a valid otp auth URI: 'counter' parameter is missing!"); } if (otpType.equals(OTPType.TOTP) && period == null) { throw new IllegalArgumentException("[" + uri + "] is not a valid otp auth URI: 'period' parameter is missing!"); } OTPAuthURIBuilder builder = new OTPAuthURIBuilder(new OTPKey(secret, otpType)).label(label).issuer(issuer).digits(digits); if (counter != null) { builder.counter(counter); } if (period != null) { builder.timeStep(TimeUnit.SECONDS.toMillis(period)); } return builder; } }
OTPAuthURIBuilder { public static OTPAuthURIBuilder fromUriString(String uri) { Preconditions.checkNotNull(uri); Matcher m = OTP_AUTH_URI_PATTERN.matcher(uri); if (!m.matches()) { throw new IllegalArgumentException("[" + uri + "] is not a valid OTP Auth URI"); } final OTPType otpType = OTPType.from(m.group(2).toUpperCase(Locale.US)); String label = null; try { label = new URI(uri).getPath().substring(1); } catch (URISyntaxException e) { throw new IllegalArgumentException(e); } if (Strings.nullToEmpty(label).trim().isEmpty()) { throw new IllegalArgumentException("[" + uri + "] is not a valid otp auth URI: 'label' component is missing!"); } String secret = null; String issuer = null; Integer digits = null; Long counter = null; Long period = null; final String query = m.group(4); final Map<String, String> decoder = Splitter.on('&').withKeyValueSeparator("=").split(query); for (Entry<String, String> queryParam : decoder.entrySet()) { if ("secret".equals(queryParam.getKey())) { secret = queryParam.getValue(); if (Strings.nullToEmpty(secret).trim().isEmpty()) { throw new IllegalArgumentException("[" + uri + "] is not a valid otp auth URI: 'secret' parameter value is missing!"); } } else if ("issuer".equals(queryParam.getKey())) { if (Strings.nullToEmpty(queryParam.getValue()).trim().isEmpty()) { throw new IllegalArgumentException("[" + uri + "] is not a valid otp auth URI: 'issuer' parameter value is missing!"); } try { issuer = URLDecoder.decode(queryParam.getValue(), StandardCharsets.UTF_8.name()); } catch (UnsupportedEncodingException e) { throw new RuntimeException("Unexpected error - underlying platform does not support UTF-8 charset!", e); } } else if ("digits".equals(queryParam.getKey())) { if (Strings.nullToEmpty(queryParam.getValue()).trim().isEmpty()) { throw new IllegalArgumentException("[" + uri + "] is not a valid otp auth URI: 'digits' parameter value is missing!"); } digits = Integer.valueOf(queryParam.getValue()); } else if ("counter".equals(queryParam.getKey())) { if (!otpType.equals(OTPType.HOTP)) { throw new IllegalArgumentException("[" + uri + "] is not a valid otp auth URI: 'counter' is not a valid totp parameter!"); } if (Strings.nullToEmpty(queryParam.getValue()).trim().isEmpty()) { throw new IllegalArgumentException("[" + uri + "] is not a valid otp auth URI: 'counter' parameter value is missing!"); } counter = Long.valueOf(queryParam.getValue()); } else if ("period".equals(queryParam.getKey())) { if (!otpType.equals(OTPType.TOTP)) { throw new IllegalArgumentException("[" + uri + "] is not a valid otp auth URI: 'period' is not a valid hotp parameter!"); } if (Strings.nullToEmpty(queryParam.getValue()).trim().isEmpty()) { throw new IllegalArgumentException("[" + uri + "] is not a valid otp auth URI: 'period' parameter value is missing!"); } period = Long.valueOf(queryParam.getValue()); } else { throw new IllegalArgumentException("[" + uri + "] is not a valid otp auth URI: '" + queryParam.getKey() + "' is not a supported parameter!"); } } if (secret == null) { throw new IllegalArgumentException("[" + uri + "] is not a valid otp auth URI: 'secret' parameter is missing!"); } if (digits == null) { throw new IllegalArgumentException("[" + uri + "] is not a valid otp auth URI: 'digits' parameter is missing!"); } if (otpType.equals(OTPType.HOTP) && counter == null) { throw new IllegalArgumentException("[" + uri + "] is not a valid otp auth URI: 'counter' parameter is missing!"); } if (otpType.equals(OTPType.TOTP) && period == null) { throw new IllegalArgumentException("[" + uri + "] is not a valid otp auth URI: 'period' parameter is missing!"); } OTPAuthURIBuilder builder = new OTPAuthURIBuilder(new OTPKey(secret, otpType)).label(label).issuer(issuer).digits(digits); if (counter != null) { builder.counter(counter); } if (period != null) { builder.timeStep(TimeUnit.SECONDS.toMillis(period)); } return builder; } private  OTPAuthURIBuilder(OTPKey key); }
OTPAuthURIBuilder { public static OTPAuthURIBuilder fromUriString(String uri) { Preconditions.checkNotNull(uri); Matcher m = OTP_AUTH_URI_PATTERN.matcher(uri); if (!m.matches()) { throw new IllegalArgumentException("[" + uri + "] is not a valid OTP Auth URI"); } final OTPType otpType = OTPType.from(m.group(2).toUpperCase(Locale.US)); String label = null; try { label = new URI(uri).getPath().substring(1); } catch (URISyntaxException e) { throw new IllegalArgumentException(e); } if (Strings.nullToEmpty(label).trim().isEmpty()) { throw new IllegalArgumentException("[" + uri + "] is not a valid otp auth URI: 'label' component is missing!"); } String secret = null; String issuer = null; Integer digits = null; Long counter = null; Long period = null; final String query = m.group(4); final Map<String, String> decoder = Splitter.on('&').withKeyValueSeparator("=").split(query); for (Entry<String, String> queryParam : decoder.entrySet()) { if ("secret".equals(queryParam.getKey())) { secret = queryParam.getValue(); if (Strings.nullToEmpty(secret).trim().isEmpty()) { throw new IllegalArgumentException("[" + uri + "] is not a valid otp auth URI: 'secret' parameter value is missing!"); } } else if ("issuer".equals(queryParam.getKey())) { if (Strings.nullToEmpty(queryParam.getValue()).trim().isEmpty()) { throw new IllegalArgumentException("[" + uri + "] is not a valid otp auth URI: 'issuer' parameter value is missing!"); } try { issuer = URLDecoder.decode(queryParam.getValue(), StandardCharsets.UTF_8.name()); } catch (UnsupportedEncodingException e) { throw new RuntimeException("Unexpected error - underlying platform does not support UTF-8 charset!", e); } } else if ("digits".equals(queryParam.getKey())) { if (Strings.nullToEmpty(queryParam.getValue()).trim().isEmpty()) { throw new IllegalArgumentException("[" + uri + "] is not a valid otp auth URI: 'digits' parameter value is missing!"); } digits = Integer.valueOf(queryParam.getValue()); } else if ("counter".equals(queryParam.getKey())) { if (!otpType.equals(OTPType.HOTP)) { throw new IllegalArgumentException("[" + uri + "] is not a valid otp auth URI: 'counter' is not a valid totp parameter!"); } if (Strings.nullToEmpty(queryParam.getValue()).trim().isEmpty()) { throw new IllegalArgumentException("[" + uri + "] is not a valid otp auth URI: 'counter' parameter value is missing!"); } counter = Long.valueOf(queryParam.getValue()); } else if ("period".equals(queryParam.getKey())) { if (!otpType.equals(OTPType.TOTP)) { throw new IllegalArgumentException("[" + uri + "] is not a valid otp auth URI: 'period' is not a valid hotp parameter!"); } if (Strings.nullToEmpty(queryParam.getValue()).trim().isEmpty()) { throw new IllegalArgumentException("[" + uri + "] is not a valid otp auth URI: 'period' parameter value is missing!"); } period = Long.valueOf(queryParam.getValue()); } else { throw new IllegalArgumentException("[" + uri + "] is not a valid otp auth URI: '" + queryParam.getKey() + "' is not a supported parameter!"); } } if (secret == null) { throw new IllegalArgumentException("[" + uri + "] is not a valid otp auth URI: 'secret' parameter is missing!"); } if (digits == null) { throw new IllegalArgumentException("[" + uri + "] is not a valid otp auth URI: 'digits' parameter is missing!"); } if (otpType.equals(OTPType.HOTP) && counter == null) { throw new IllegalArgumentException("[" + uri + "] is not a valid otp auth URI: 'counter' parameter is missing!"); } if (otpType.equals(OTPType.TOTP) && period == null) { throw new IllegalArgumentException("[" + uri + "] is not a valid otp auth URI: 'period' parameter is missing!"); } OTPAuthURIBuilder builder = new OTPAuthURIBuilder(new OTPKey(secret, otpType)).label(label).issuer(issuer).digits(digits); if (counter != null) { builder.counter(counter); } if (period != null) { builder.timeStep(TimeUnit.SECONDS.toMillis(period)); } return builder; } private  OTPAuthURIBuilder(OTPKey key); static OTPAuthURIBuilder fromKey(OTPKey key); static OTPAuthURIBuilder fromUriString(String uri); OTPAuthURIBuilder label(String label); OTPAuthURIBuilder issuer(String issuer); OTPAuthURIBuilder digits(int digits); OTPAuthURIBuilder counter(long counter); OTPAuthURIBuilder timeStep(long timeStep); OTPAuthURI build(); }
OTPAuthURIBuilder { public static OTPAuthURIBuilder fromUriString(String uri) { Preconditions.checkNotNull(uri); Matcher m = OTP_AUTH_URI_PATTERN.matcher(uri); if (!m.matches()) { throw new IllegalArgumentException("[" + uri + "] is not a valid OTP Auth URI"); } final OTPType otpType = OTPType.from(m.group(2).toUpperCase(Locale.US)); String label = null; try { label = new URI(uri).getPath().substring(1); } catch (URISyntaxException e) { throw new IllegalArgumentException(e); } if (Strings.nullToEmpty(label).trim().isEmpty()) { throw new IllegalArgumentException("[" + uri + "] is not a valid otp auth URI: 'label' component is missing!"); } String secret = null; String issuer = null; Integer digits = null; Long counter = null; Long period = null; final String query = m.group(4); final Map<String, String> decoder = Splitter.on('&').withKeyValueSeparator("=").split(query); for (Entry<String, String> queryParam : decoder.entrySet()) { if ("secret".equals(queryParam.getKey())) { secret = queryParam.getValue(); if (Strings.nullToEmpty(secret).trim().isEmpty()) { throw new IllegalArgumentException("[" + uri + "] is not a valid otp auth URI: 'secret' parameter value is missing!"); } } else if ("issuer".equals(queryParam.getKey())) { if (Strings.nullToEmpty(queryParam.getValue()).trim().isEmpty()) { throw new IllegalArgumentException("[" + uri + "] is not a valid otp auth URI: 'issuer' parameter value is missing!"); } try { issuer = URLDecoder.decode(queryParam.getValue(), StandardCharsets.UTF_8.name()); } catch (UnsupportedEncodingException e) { throw new RuntimeException("Unexpected error - underlying platform does not support UTF-8 charset!", e); } } else if ("digits".equals(queryParam.getKey())) { if (Strings.nullToEmpty(queryParam.getValue()).trim().isEmpty()) { throw new IllegalArgumentException("[" + uri + "] is not a valid otp auth URI: 'digits' parameter value is missing!"); } digits = Integer.valueOf(queryParam.getValue()); } else if ("counter".equals(queryParam.getKey())) { if (!otpType.equals(OTPType.HOTP)) { throw new IllegalArgumentException("[" + uri + "] is not a valid otp auth URI: 'counter' is not a valid totp parameter!"); } if (Strings.nullToEmpty(queryParam.getValue()).trim().isEmpty()) { throw new IllegalArgumentException("[" + uri + "] is not a valid otp auth URI: 'counter' parameter value is missing!"); } counter = Long.valueOf(queryParam.getValue()); } else if ("period".equals(queryParam.getKey())) { if (!otpType.equals(OTPType.TOTP)) { throw new IllegalArgumentException("[" + uri + "] is not a valid otp auth URI: 'period' is not a valid hotp parameter!"); } if (Strings.nullToEmpty(queryParam.getValue()).trim().isEmpty()) { throw new IllegalArgumentException("[" + uri + "] is not a valid otp auth URI: 'period' parameter value is missing!"); } period = Long.valueOf(queryParam.getValue()); } else { throw new IllegalArgumentException("[" + uri + "] is not a valid otp auth URI: '" + queryParam.getKey() + "' is not a supported parameter!"); } } if (secret == null) { throw new IllegalArgumentException("[" + uri + "] is not a valid otp auth URI: 'secret' parameter is missing!"); } if (digits == null) { throw new IllegalArgumentException("[" + uri + "] is not a valid otp auth URI: 'digits' parameter is missing!"); } if (otpType.equals(OTPType.HOTP) && counter == null) { throw new IllegalArgumentException("[" + uri + "] is not a valid otp auth URI: 'counter' parameter is missing!"); } if (otpType.equals(OTPType.TOTP) && period == null) { throw new IllegalArgumentException("[" + uri + "] is not a valid otp auth URI: 'period' parameter is missing!"); } OTPAuthURIBuilder builder = new OTPAuthURIBuilder(new OTPKey(secret, otpType)).label(label).issuer(issuer).digits(digits); if (counter != null) { builder.counter(counter); } if (period != null) { builder.timeStep(TimeUnit.SECONDS.toMillis(period)); } return builder; } private  OTPAuthURIBuilder(OTPKey key); static OTPAuthURIBuilder fromKey(OTPKey key); static OTPAuthURIBuilder fromUriString(String uri); OTPAuthURIBuilder label(String label); OTPAuthURIBuilder issuer(String issuer); OTPAuthURIBuilder digits(int digits); OTPAuthURIBuilder counter(long counter); OTPAuthURIBuilder timeStep(long timeStep); OTPAuthURI build();  }