I am trying to call the API /v1/revoke from an Angular 6 client with OKTA implicit flow (using library angular-oauth2-oidc). From the sample program, I add a new function revokeToken() as the library doesn’t implement a revoke-token function. I include “http://localhost:8080” as a trusted origin but still get the following error:
PS: Revoking an access token with Postman works. It seems the OKTA trusted origin is not taken into account when the /v1/revoke responses (during browser pre-flight check).
The content-type needs to be set to “application/x-www-form-urlencoded” instead of “application/x-www-form-urlencoded/json”. This change applies also for the body of the request
If this is a Single Page App, the best solution would be to send the client_id in the body of the request instead of authorization header
Make sure to send withCredentials xHR attribute
You can find here a working example using XMLHttpRequest.
If I set the content-type to ‘application/x-www-form-urlencoded’, I will get an error saying ‘token’ is required. Therefore, I set to ‘application/json’
If I move the client_id into the request body, I will get Error 401 (not authorized)
I added header ‘withCredentials’ with ‘true’ => still not working
By the way, the link you provided cannot be accessed.
/revoke endpoint does not accept the parameters in JSON.
You can find below the example.
Revoke token example using XMLHttpRequest
<!DOCTYPE html>
<html>
<body>
<script type=“text/javascript”>
var baseUrl = ‘https://org.okta.com’;
var requestPath = ‘/oauth2/v1/revoke’;
var xhr = new XMLHttpRequest();
if (“withCredentials” in xhr) {
xhr.onerror = function() {
alert(‘Invalid URL or Cross-Origin Request Blocked. You must explicitly add this site (’ + window.location.origin + ‘) to the list of allowed websites in the administrator UI’);
}
xhr.onload = function() {
alert(this.responseText);
};
xhr.open(‘POST’, baseUrl + requestPath, true);
xhr.setRequestHeader(“Accept”, “application/json”);
xhr.setRequestHeader(“Content-Type”, “application/x-www-form-urlencoded”);
xhr.withCredentials = true;
xhr.send(“client_id=CLIENT_ID_HERE&token_type_hint=access_token&token=ACCESS_TOKEN_HERE”);
} else {
alert(“CORS is not supported for this browser!”)
}
</script>
</body>
</html>