Skip to content

Custom Authentication Method - Passing Error Messages #34

Description

@HappyAmos

How would you pass back a custom error message from a custom authenticator? Obviously my example below is pulled completely out of thin air and would never work, but I think you get what I mean? Somehow, there must be a way to pass a message from here, back to the error handler, or alter the $arguments?

class DbAuthenticator implements AuthenticatorInterface {
    private $settings;
 
    public function __construct($settings) {
        $this->settings = $settings;
    }
 
    public function __invoke(array $arguments) {
        if (wrongPassword($user, $pass)) {
            $returnAmessageSomeHow = "Invalid password";
            $failedLogins = $failedLogins + 1;
            return false;
        } 
        if (invalidUsername($user, $pass)) {
            $returnAmessageSomeHow = "Invalid username";
            return false;
        }
    }

}

then...

$container["HttpBasicAuthentication"] = function ($container) {
    return new HttpBasicAuthentication([
        "path" => [ ... ],
        "authenticator" => new DbAuthenticator($container['settings']),
        "error" => function ($request, $response, $arguments) {
            $data = [];
            $data["status"] = "error";
            $data["message"] = $messageReturnedFromAuthenticatorSomehow;
            return $response->write(json_encode($data, JSON_UNESCAPED_SLASHES));
        }
    ]);
};

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions