/* Copyright 2002-2005 The Apache Software Foundation or its licensors, as
 * applicable.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "apr_strings.h"
//#include "apr_md5.h"            /* for apr_password_validate */
//#include "apr_lib.h"            /* for apr_isspace */
//#include "apr_base64.h"         /* for apr_base64_decode et al */
#define APR_WANT_STRFUNC        /* for strcasecmp */
#include "apr_want.h"

#define CORE_PRIVATE
#include "ap_config.h"
#include "httpd.h"
#include "http_config.h"
#include "http_core.h"
//#include "http_log.h"
//#include "http_protocol.h"
//#include "http_request.h"
#include "ap_provider.h"

#include "mod_auth.h"

typedef struct provider_alias_rec {
    char *provider;
    char *provider_alias;
    ap_conf_vector_t *sec_auth;
} provider_alias_rec;

typedef struct authn_alias_srv_conf {
    apr_array_header_t *alias_rec;
} authn_alias_srv_conf;

module AP_MODULE_DECLARE_DATA authn_alias_module;

static authn_status authn_alias_check_password(request_rec *r, const char *user,
                                              const char *password)
{
    /* Look up the provider alias in the alias list */
    /* Get the the dir_config and call ap_Merge_per_dir_configs() */
    /* Call the real provider->check_password() function */
    /* return the result of the above function call */

/* Q: How can this function get the alias provider name? */

    return AUTH_USER_NOT_FOUND;
}

static void *create_authn_alias_svr_config(apr_pool_t *p, server_rec *s)
{

    authn_alias_srv_conf *authcfg;

    /*
     * As with the x_create_dir_config() reoutine, we allocate and fill
     * in an empty record.
     */
    authcfg = (authn_alias_srv_conf *) apr_pcalloc(p, sizeof(authn_alias_srv_conf));
    authcfg->alias_rec = NULL;

    return (void *) authcfg;
}

static const authn_provider authn_alias_provider =
{
    &authn_alias_check_password,
};

static const char *authaliassection(cmd_parms *cmd, void *mconfig, const char *arg)
{
    int old_overrides = cmd->override;
    const char *endp = ap_strrchr_c(arg, '>');
    const char *args;
    char *provider_alias;
    char *provider;
    const char *errmsg;
    ap_conf_vector_t *new_auth_config = ap_create_per_dir_config(cmd->pool);
    authn_alias_srv_conf *authcfg = 
        (authn_alias_srv_conf *)ap_get_module_config(cmd->server->module_config, 
                                                     &authn_alias_module);

    const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
    if (err != NULL) {
        return err;
    }

    if (endp == NULL) {
        return apr_pstrcat(cmd->pool, cmd->cmd->name,
                           "> directive missing closing '>'", NULL);
    }

    args = apr_pstrndup(cmd->pool, arg, endp - arg);

    if (!args[0]) {
        return apr_pstrcat(cmd->pool, cmd->cmd->name,
                           "> directive requires additional arguments", NULL);
    }

    provider = ap_getword_conf(cmd->pool, &args);
    provider_alias = ap_getword_conf(cmd->pool, &args);

    if (!provider[0] || !provider_alias[0]) {
        return apr_pstrcat(cmd->pool, cmd->cmd->name,
                           "> directive requires additional arguments", NULL);
    }

    if (!authcfg->alias_rec) {
        authcfg->alias_rec = apr_array_make(cmd->pool, 10, sizeof(provider_alias_rec*));
    }

    cmd->override = OR_ALL|ACCESS_CONF;

    errmsg = ap_walk_config(cmd->directive->first_child, cmd, new_auth_config);

    if (!errmsg) {
        provider_alias_rec *prvdraliasrec = apr_array_push(authcfg->alias_rec);

        prvdraliasrec->sec_auth = new_auth_config;
        prvdraliasrec->provider = provider;
        prvdraliasrec->provider_alias = provider_alias;

        ap_register_provider(cmd->pool, AUTHN_PROVIDER_GROUP, provider_alias, "0",
                             &authn_alias_provider);
    }

    cmd->override = old_overrides;

    return errmsg;
}

static const command_rec authn_alias_cmds[] =
{
    AP_INIT_RAW_ARGS("<AuthProviderAlias", authaliassection, NULL, RSRC_CONF,
                     "Container for authentication directives grouped under "
                     "a provider alias"),
    {NULL}
};


static void register_hooks(apr_pool_t *p)
{
//    ap_hook_check_user_id(authenticate_basic_user,NULL,NULL,APR_HOOK_MIDDLE);

}

module AP_MODULE_DECLARE_DATA authn_alias_module =
{
    STANDARD20_MODULE_STUFF,
    NULL,                          /* dir config creater */
    NULL,                          /* dir merger --- default is to override */
    create_authn_alias_svr_config, /* server config */
    NULL,                          /* merge server config */
    authn_alias_cmds,               /* command apr_table_t */
    register_hooks                 /* register hooks */
};
