/**
 * Copyright (c) 2016, 2017 Bosch Software Innovations GmbH.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Bosch Software Innovations GmbH - initial creation
 */
package org.eclipse.hono.service.auth;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import io.vertx.core.json.JsonObject;

/**
 * Constants related to authentication.
 */
public final class AuthenticationConstants
{
    /**
     * The vert.x event bus address inbound authentication requests are published on.
     */
    public static final String EVENT_BUS_ADDRESS_AUTHENTICATION_IN = "authentication.in";
    /**
     * The name of the field containing the authorization ID granted as the result of a successful authentication.
     */
    public static final String FIELD_AUTHORIZATION_ID   = "authorization-id";
    /**
     * The name of the field containing the SASL mechanism used for authentication.
     */
    public static final String FIELD_MECHANISM          = "mechanism";
    /**
     * The name of the field containing the SASL response the client has provided.
     */
    public static final String FIELD_SASL_RESPONSE      = "sasl-response";
    /**
     * The name of the field containing the Subject DN of the certificate the client has used for EXTERNAL auth.
     */
    public static final String FIELD_SUBJECT_DN         = "subject-dn";
    /**
     * The PLAIN SASL mechanism name.
     */
    public static final String MECHANISM_PLAIN          = "PLAIN";
    /**
     * The EXTERNAL SASL mechanism name.
     */
    public static final String MECHANISM_EXTERNAL       = "EXTERNAL";

    /**
     * Indicates an unsupported SASL mechanism.
     */
    public static final int    ERROR_CODE_UNSUPPORTED_MECHANISM = 0;
    /**
     * Indicates that authentication failed.
     */
    public static final int    ERROR_CODE_AUTHENTICATION_FAILED = 10;

    private static final Pattern PATTERN_CN = Pattern.compile("^CN=(.+?)(?:,\\s*[A-Z]{1,2}=.+|$)");

    private AuthenticationConstants() {
    }

    /**
     * Creates a message for processing a client's SASL <em>response</em>.
     * 
     * @param mechanism the SASL mechanism that the response is part of.
     * @param response the authentication data provided by the client, encoded according to the mechanism.
     * @return the message to be sent to the {@code AuthenticationService}.
     */
    public static JsonObject getAuthenticationRequest(final String mechanism, final byte[] response) {
        return new JsonObject()
                .put(FIELD_MECHANISM, mechanism)
                .put(FIELD_SASL_RESPONSE, response);
    }

    /**
     * Extracts the <em>Common Name (CN)</em> from a subject Distinguished Name (DN).
     * 
     * @param subject The distinguished name.
     * @return The common name or {@code null} if the subject does not contain a CN.
     */
    public static String getCommonName(final String subject) {
        Matcher matcher = PATTERN_CN.matcher(subject);
        if (matcher.matches()) {
            return matcher.group(1); // return CN field value
        } else {
            return null;
        }
    }
}

@@ -428,16 +428,42 @@
 .auth;%0A%0A
+import java.util.Objects;%0A
 import j
@@ -2301,15 +2301,19 @@
 for 
-process
+authenticat
 ing 
@@ -2324,33 +2324,19 @@
 ient
-'s SASL %3Cem%3Eresponse%3C/em%3E
+ using SASL
 .%0A  
@@ -2365,17 +2365,17 @@
 chanism 
-t
+T
 he SASL 
@@ -2389,59 +2389,89 @@
 sm t
-hat the response is part of.%0A     * @param response
+o use for authentication.%0A     * @param saslResponse The SASL response containing
  the
@@ -2490,12 +2490,19 @@
 ion 
-data
+information
  pro
@@ -2524,44 +2524,8 @@
 ient
-, encoded according to the mechanism
 .%0A  
@@ -2598,16 +2598,90 @@
 rvice%7D.%0A
+     * @throws NullPointerException if any of the params is %7B@code null%7D.%0A
      */%0A
@@ -2771,17 +2771,21 @@
  byte%5B%5D 
-r
+saslR
 esponse)
@@ -2857,16 +2857,39 @@
 HANISM, 
+Objects.requireNonNull(
 mechanis
@@ -2890,16 +2890,17 @@
 chanism)
+)
 %0A       
@@ -2938,17 +2938,44 @@
 SE, 
-r
+Objects.requireNonNull(saslR
 esponse)
 ;%0A  
@@ -2970,16 +2970,17 @@
 esponse)
+)
 ;%0A    %7D%0A

Improve null parameter handling.