/*
 * Copyright 2014 Red Hat, Inc.
 *
 *  All rights reserved. This program and the accompanying materials
 *  are made available under the terms of the Eclipse Public License v1.0
 *  and Apache License v2.0 which accompanies this distribution.
 *
 *  The Eclipse Public License is available at
 *  http://www.eclipse.org/legal/epl-v10.html
 *
 *  The Apache License v2.0 is available at
 *  http://www.opensource.org/licenses/apache2.0.php
 *
 *  You may elect to redistribute this code under either of these licenses.
 */

package io.vertx.ext.web.handler.impl;

import io.vertx.core.http.HttpHeaders;
import io.vertx.core.http.HttpServerRequest;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.RoutingContext;
import io.vertx.ext.auth.AuthProvider;
import io.vertx.ext.auth.User;

import java.util.Base64;

/**
 * @author <a href="http://pmlopes@gmail.com">Paulo Lopes</a>
 * @author <a href="http://tfox.org">Tim Fox</a>
 */
public class BasicAuthHandlerImpl extends AuthHandlerImpl {

  private final String realm;

  public BasicAuthHandlerImpl(AuthProvider authProvider, String realm) {
    super(authProvider);
    this.realm = realm;
  }

  @Override
  public void handle(RoutingContext context) {
    User user = context.user();
    if (user != null) {
      // Already authenticated in, just authorise
      authorise(user, context);
    } else {
      HttpServerRequest request = context.request();
      String authorization = request.headers().get(HttpHeaders.AUTHORIZATION);

      if (authorization == null) {
        handle401(context);
      } else {
        String suser;
        String spass;
        String sscheme;

        try {
          String[] parts = authorization.split(" ");
          sscheme = parts[0];
          String[] credentials = new String(Base64.getDecoder().decode(parts[1])).split(":");
          suser = credentials[0];
          // when the header is: "user:"
          if( credentials.length == 2)
              spass = credentials[1];
          else if( credentials.length >2) {
              spass = joinCredentials(credentials);
          } else {
              spass = null;
          }
        } catch (ArrayIndexOutOfBoundsException e) {
          handle401(context);
          return;
        } catch (IllegalArgumentException | NullPointerException e) {
          // IllegalArgumentException includes PatternSyntaxException
          context.fail(e);
          return;
        }

        if (!"Basic".equals(sscheme)) {
          context.fail(400);
        } else {
          JsonObject authInfo = new JsonObject().put("username", suser).put("password", spass);
          authProvider.authenticate(authInfo, res -> {
            if (res.succeeded()) {
              User authenticated = res.result();
              context.setUser(authenticated);
              authorise(authenticated, context);
            } else {
              handle401(context);
            }
          });
        }
      }
    }
  }

  private void handle401(RoutingContext context) {
    context.response().putHeader("WWW-Authenticate", "Basic realm=\"" + realm + "\"");
    context.fail(401);
  }
  
    private static String joinCredentials(String[] credentials) {
        StringBuilder sb = new StringBuilder();
        for (int i = 1; i < credentials.length -1; i++) {
            sb.append(credentials[i]).append(":");
        }
        sb.append(credentials[credentials.length-1]);
        return sb.toString();        
    }         
}

@@ -1759,30 +1759,24 @@
   String
-%5B%5D credentials
+ decoded
  = new S
@@ -1822,315 +1822,301 @@
 1%5D))
-.split(%22:%22);%0A          suser = credentials%5B0%5D;%0A          // when the header is: %22user:%22%0A          if( credentials.length == 2)%0A              spass = credentials%5B1%5D;%0A          else if( credentials.length %3E2) %7B%0A              spass = joinCredentials(credentials);%0A          %7D else %7B%0A              spass = null;
+;          %0A          int colonIdx = decoded.indexOf(%22:%22);%0A          if(colonIdx!=-1) %7B%0A              suser = decoded.substring(0,colonIdx);%0A              spass = decoded.substring(colonIdx+1);%0A          %7D else %7B%0A              suser = decoded;%0A              spass = null;                      
 %0A   
@@ -2115,32 +2115,42 @@
     %0A          %7D
+          
 %0A        %7D catch
@@ -3127,350 +3127,8 @@
 %0A  %7D
-%0A  %0A    private static String joinCredentials(String%5B%5D credentials) %7B%0A        StringBuilder sb = new StringBuilder();%0A        for (int i = 1; i %3C credentials.length -1; i++) %7B%0A            sb.append(credentials%5Bi%5D).append(%22:%22);%0A        %7D%0A        sb.append(credentials%5Bcredentials.length-1%5D);%0A        return sb.toString();        %0A    %7D       
   %0A%7D
-%0A

Use substring instead of split+join to make it more readable and efficient.