My first user

From ISPWiki
Jump to: navigation, search

By default, only a local admin can access the control panel. In this article we'll describe how to login to the control panel as a custom user.

Let's assume that we have the etc/passwd file containing usernames and passwords colon separated (in this example we won't hide password for the sake of simplicity).

The authenticate function handles the user login process. Create the authenticate event handler. For more details please refer to the article My first event handler.

C++ CORE API provides additional tools to simplify creation of a custom authentication method - the isp_api::AuthMethod class. You will need to define two virtual methods - AuthenByName and AuthenByPassword.

Example of myauth.cpp (we won't perform additional checks not to overload our example):

#include <mgr/mgrstr.h>
#include <api/auth_method.h>
#include <api/module.h>
#include <fstream>
#include <sstream>
#define PASSWD  "etc/passwd"
namespace {
using namespace isp_api;
class MyAuth : public AuthMethod {
    MyAuth() : AuthMethod("myauth") {}
    string Lookup(const string &name) const {
        if (!name.empty()) {
            std::ifstream in(PASSWD);
            while (in.good()) {
                std::stringbuf buf; 
                string pass = buf.str();
                string user = str::GetWord(pass, ':');
                if (user == name && !pass.empty())
                    return pass;
        return "";

    void FillupParams(const string &user, mgr_xml::Xml &xml) const {
                .SetProp("level", str::Str(lvAdmin))
                .SetProp("name", user)
                .SetProp("method", "myauth");

    virtual void AuthenByName(mgr_xml::Xml &res, const string &name) const {
        if (!Lookup(name).empty())
            FillupParams(name, res);

    virtual void AuthenByPassword(mgr_xml::Xml &res, const string &name, const string &pass) const {
        string lppass = Lookup(name);
        if (!lppass.empty() && lppass == pass)
            FillupParams(name, res);

MODULE_INIT(myauth, "") {
    new MyAuth();
} // end of private namespace

Now you need to build your components and Upload custom libraries.