I created one front controller (for a REST API) for my okta application. I followed this article and it works fine for machine-to-machine communications. The authenticate()
function in my index.php looks like this:
function authenticate() {
try {
switch(true) {
case array_key_exists('HTTP_AUTHORIZATION', $_SERVER):
$authHeader = $_SERVER['HTTP_AUTHORIZATION'];
break;
case array_key_exists('Authorization', $_SERVER):
$authHeader = $_SERVER['Authorization'];
break;
default;
$authHeader = null;
break;
}
preg_match('/Bearer\s(\S+)/', $authHeader, $matches);
if (!isset($matches[1]))
throw new \Exception('No Bearer Token');
$jwtVerifier = (new \Okta\JwtVerifier\JwtVerifierBuilder())
->setIssuer(getenv('OKTAISSUER'))
->setAudience(getenv('OKTAAUDIENCE'))
->setClientId(getenv('OKTACLIENTID'))
->build();
return $jwtVerifier->verify($matches[1]);
} catch (\Exception $e) {
return false;
}
}
Notice that my jwtVerifier sets the client ID getting the information from my local .env file.
Now I added a second application, to deal with a web-app client, doing simple javascript fetch in a browser. This second application added in my Okta account is of the type SPA / Native, but I want (after I get Okta access token) to consume my REST API being validated by my index.php.
Initially I had not realized that it was not working because the client ID in my index.php authenticate()
function is for the API Services application (gotten from my .env file by my php application). I manually changed the ID in my index.php to be the one from my SPA / Native application and then it works.
So, my question is… to make my authenticate()
function in my index.php work with both my regular clients (machine-to-machine) and my web client (browser), which have their own client ID, how should I send the client ID in my HTTP request to my index.php? It sounds like the client ID should be sent in a header, and in my index.php I parse that and set my jwtVerifier with that client ID (instead of getting it from .env file). If so, what is the best header to send this information? If sending it through the header is not appropriate, what is the recommended approach?