Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for Bearer token authentication #29

Open
mcquaas opened this issue Sep 1, 2024 · 0 comments
Open

Support for Bearer token authentication #29

mcquaas opened this issue Sep 1, 2024 · 0 comments

Comments

@mcquaas
Copy link

mcquaas commented Sep 1, 2024

Restful requests can currently be made sending the token via a simple "Authentication: {your_token}" header.

But many applications, including Postman and OpenApi compliant integrations expect the token to be sent in the form of: "Authentication: Bearer {your_token}".

A simple add-on to the get_wstoken($headers) can do the work:

private function get_wstoken($headers) {
    $wstoken = '';

    if (isset($headers['HTTP_AUTHORIZATION'])) {
        $authorizationHeader = $headers['HTTP_AUTHORIZATION'];
        $authorizationHeaderParts = explode(' ', $authorizationHeader);
        if (count($authorizationHeaderParts) === 2 && $authorizationHeaderParts[0] === 'Bearer') {
            $wstoken = $authorizationHeaderParts[1];
        } else {
            $wstoken = $authorizationHeader;
        }
    }

    if ($wstoken === '') {
        // Raise an error if auth header not supplied.
        $ex = new \moodle_exception('noauthheader', 'webservice_restful', '');
        $this->send_error($ex, 401);
    }

    return $wstoken;
}

Hope this helps.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant