/*
** Module   : BasicAuth.java
** Abstract : The Basic authorization configuration for the web service test.
**
** Copyright (c) 2022, Golden Code Development Corporation.
**
** -#- -I- --Date-- ---------------------------------------Description----------------------------------------
** 001 CA  20220117 Created initial version.
*/

/*
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU Affero General Public License as
** published by the Free Software Foundation, either version 3 of the
** License, or (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU Affero General Public License for more details.
**
** You should have received a copy of the GNU Affero General Public License
** along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

package com.goldencode.harness.test;

import java.util.*;

import org.apache.http.client.methods.*;

import com.goldencode.harness.*;

/**
 * This implements basic authentication, which just passes a base64-encoded string of the "user:password".
 */
public class BasicAuth
implements WebAuthorization
{
   /** The password to use for authentication. */
   private static final String PASSWORD = "password";

   /** The username to use for authentication. */
   private static final String USERNAME = "username";

   /** The authorization header. */
   private static final String HEADER_NAME = "Authorization";
   
   /** Map with the override basic parameters. */
   public static final Map<String, String> overrideParameters = new HashMap<>();

   /** The username. */
   private String user;
   
   /** The password. */
   private String password;
   
   /**
    * Copy the default authorization settings from the configuration.
    * 
    * @param    cfg
    *           The session configuration.
    */
   public BasicAuth(WebSessionSettings cfg)
   {
      String type = "basic";
      
      setParameter(USERNAME, cfg.getAuthorizationParameter(type, USERNAME));
      setParameter(PASSWORD, cfg.getAuthorizationParameter(type, PASSWORD));
   }
   
   /**
    * Configure the request with the authorization.
    * 
    * @param    request
    *           The HTTP request.
    */
   @Override
   public void setAuthorization(RequestBuilder request)
   {
      overrideParameters.forEach((k, v) -> setParameter(k, v));

      String token = user + ":" + password;
      
      byte[] bytes = Base64.getEncoder().encode(token.getBytes());
      
      token = new String(bytes);
      
      request.addHeader(HEADER_NAME, "Basic " + token);
   }

   /**
    * Set an authorization parameter.  This may be a HTTP header or anything else required to build the
    * authorization.
    * 
    * @param    name
    *           The parameter name.
    * @param    value
    *           The parameter value.
    */
   @Override
   public void setParameter(String name, String value)
   {
      if (value == null || value.length() == 0)
      {
         return;
      }
      
      switch (name.toLowerCase())
      {
         case USERNAME:
            this.user = value;
            break;
         case PASSWORD:
            this.password = value;
            break;
      }
   }
}
