It provides an OAuth2 server so that a user can use its Moodle account to log in to your application. Oauth2 Library has been taken from https://github.com/bshaffer/oauth2-server-php
-
Clone this repository in a directory named "oauth".
$ git clone https://github.com/projectestac/moodle-local_oauth.git oauth -
Compress it to a .zip file.
-
Log in to Moodle as an administrator.
-
Search a block named Administration and look for Site Administration > Plugins > Install Plugins.
-
Choose the .zip file and hit the button Install Plugin from the ZIP file.
-
Make sure the directory path_to_moodle/local/ has writing permissions for moodle. If the validation is ok, install it.
-
Go to Site Administration > Server > OAuth provider settings
-
Click Add new client
-
Fill in the form. Your Client Identifier and Client Secret (which will be given later) will be used for you to authenticate. The Redirect URL must be the URL mapping to your client that will be used.
-
From your application, redirect the user to this URL:
https://moodledomain.com/local/oauth/login.php?client_id=EXAMPLE&response_type=code&redirect_uri=https%3A%2F%2Fyourapplicationdomain.com%2Ffoo&scope=user_info&state=RANDOM(remember to replace the URL domain with the domain of Moodle and replace EXAMPLE with the Client Identifier given in the form.) The parametersredirect_uri,scopeandstateare optional but recommended:redirect_urimust match exactly the Redirect URL given in the form. If it is omitted, the registered one is used (only possible when the client has a single Redirect URL).scopemust be one of the scopes given in the form.stateis a random value that your application must check when the user is redirected back, to prevent CSRF attacks.
-
The user must log in to Moodle and authorize your application to use its basic info.
-
If it went all ok, the plugin should redirect the user to something like:
https://yourapplicationdomain.com/foo?code=55c057549f29c428066cbbd67ca6b17099cb1a9e&state=RANDOM(that's a GET request to the Redirect URL given with the code parameter and, if it was sent, the state parameter). The code expires after 30 seconds and can be used only once. -
Using the code given, your application must send a POST request to
https://moodledomain.com/local/oauth/token.phphaving the following parameters:{'code': '55c057549f29c428066cbbd67ca6b17099cb1a9e', 'client_id': 'EXAMPLE', 'client_secret': 'codeGivenAfterTheFormWasFilled', 'grant_type': 'authorization_code', 'redirect_uri': 'https://yourapplicationdomain.com/foo'}. Theredirect_uriparameter is required if it was sent in step 1, and must have the same value. Instead of sendingclient_idandclient_secretas parameters, your application can send them in anAuthorization: Basicheader. -
If the correct credentials were given, the response should be a JSON like this:
{"access_token":"79d687a0ea4910c6662b2e38116528fdcd65f0d1","expires_in":3600,"token_type":"Bearer","scope":"user_info","refresh_token":"c1de730eef1b2072b48799000ec7cde4ea6d2af0"}. The access token expires after one hour.The
refresh_tokenis only issued if the Grant Types of the client includerefresh_token(e.g.authorization_code refresh_token). It expires after 14 days. To get a new access token without authorizing the user again, send a POST request tohttps://moodledomain.com/local/oauth/token.phpwith the parameters{'grant_type': 'refresh_token', 'refresh_token': 'c1de730eef1b2072b48799000ec7cde4ea6d2af0', 'client_id': 'EXAMPLE', 'client_secret': 'codeGivenAfterTheFormWasFilled'}(or with the credentials in anAuthorization: Basicheader). The response has the same format, with a newrefresh_token: each refresh token can be used only once, so your application must store the new one. -
Finally, send a request to
https://moodledomain.com/local/oauth/user_info.phppassing the access token in one of these ways (only one at a time):- In an
Authorization: Bearer 79d687a0ea4910c6662b2e38116528fdcd65f0d1header (recommended). - As a parameter of a POST request, like:
{'access_token':'79d687a0ea4910c6662b2e38116528fdcd65f0d1'}. - As a parameter of a GET request:
https://moodledomain.com/local/oauth/user_info.php?access_token=79d687a0ea4910c6662b2e38116528fdcd65f0d1.
Note: if Apache runs PHP through PHP-FPM (
mod_proxy_fcgi), theAuthorizationheader is not passed to PHP by default. AddCGIPassAuth Onto the Apache configuration of the Moodle directory. - In an
-
If the token given is valid, a JSON containing the user information is returned. Ex:
{"id":"22","username":"foobar","idnumber":"","firstname":"Foo","lastname":"Bar","email":"foo@bar.com","lang":"en","phone1":"5551619192","auth":"manual","country":"foo","description":"bar"}The token must have at least one of the scopes given in the form for the client (if the client has no scope,
user_infois required). Otherwise, a JSON with an OAuth2 error is returned:401 invalid_token: the token is missing, invalid or expired, is not associated with a user (e.g. it was obtained with theclient_credentialsgrant type), or its client no longer exists.403 insufficient_scope: the token has none of the scopes of the client.404 user_not_found: the user associated with the token does not exist or has been deleted.
Note: If testing in Postman, you need to set encoding to x-www-form-urlencoded for POST requests.
This plugin has been tested on Moodle 2.8, Moodle 3.0 and Moodle 4.5
Apart from people in this repository, also have contributed: