1

I'm making SDK for a web platform, there is a client class which requires cookie to authorize and there is a function that gets auth token from cookie. So my question is: how to check if function was called outside of class. I need this because i want to protect this function with password and make it so if class called it, it would work without a password. Here is my code:

public function gettoken(?string password = ""): string{
    //check if it's called inside of class
    if (fromClass() == true){
       //code that gets token
    }
    //if it's called outside of class
    if ($password == $this->password){
       //code that gets token
    }
    return "Incorrect password";
}
Glebux
  • 13
  • 3
  • Does [`debug_backtrace()`](https://www.php.net/manual/en/function.debug-backtrace.php) work for you? For [example](https://stackoverflow.com/questions/743686/determine-where-a-function-has-been-called-with-php). – vee Apr 09 '22 at 18:00

1 Answers1

0

This sound very much like a bad idea.

Why not make two functions: One public that requires a password and one private that doesn't. The public function can, of course, call the private function after the password has been verified.

Something like this:

public function getTokenUsingPassword($password)
{
    if ($password == $this->password) {
        return $this->getToken();
    }
    return false;
}

private function getToken()
{
    return //code that gets token
}
KIKO Software
  • 15,283
  • 3
  • 18
  • 33