Note:

If you want to create a new page for developers, you should create it on the Moodle Developer Resource site.

Oauth2 authentication: Difference between revisions

From MoodleDocs
(Remove stale info.)
 
(18 intermediate revisions by 2 users not shown)
Line 1: Line 1:
This document describes the functional and technical specification of Moodle Oauth2 plugin(s). Oauth2 plugin(s) will allow a user to authenticate in Moodle with a identity provider that implement Oauth2, like Google or Facebook.
This information on this page has been archived because it is no longer current. For information on OAuth2 see [[OAuth2_Services]].


= What is Oauth2 =
To see the infomation previously on this page check the history.
OAuth is a security protocol that enables users to grant third-party access (i.e. Moodle) to their web resources (i.e Google/Facebook/...'s user data) without sharing their passwords.
 
==== See also ====
* [http://oauth.net/2/ Oauth2 specification]
 
= What are the user's benefits =
* The user can connect in Moodle with a Google/Facebook/... account.
* Login is fast: 1 click on a Google/Facebook/... button.
* The user does not need to remember a password for Moodle.
* The user can link an existing Moodle account to an Oauth2 identity provider. Thus the user benefits of the above points.
* When the user is new, the creation process is faster: in the best case, which is the case for Facebook/Google, every mandatory user fields are pre filled.
 
=Why implementing Oauth2 which is still in Draft version=
The main reason we choose Oauth2 is that massive identity providers as Google already started to [https://developers.google.com/accounts/docs/OAuth deprecate Oauth1] or only use Oauth2 (Facebook). Oauth2 has also for benefit over Oauth1 to be simpler to implement for the client. However at the moment, Oauth2 is on a [http://hueniverse.com/2012/07/oauth-2-0-and-the-road-to-hell/ rocky road]. The specification are still marked as Draft, and major providers as Facebook or Google implement their own version. But at the end these implementations are globally similar, and we will be able to reuse a major part of our code for each providers.
 
==== See also ====
* [https://developers.google.com/accounts/docs/OAuth2 Google Oauth2]
* [https://developers.facebook.com/docs/authentication/ Facebook Oauth2]
* [http://msdn.microsoft.com/en-us/library/live/hh243647.aspx Microsoft Live Oauth2]
* [https://dev.twitter.com/docs/anywhere/welcome Twitter JS authentication]
* [https://developer.linkedin.com/documents/getting-started-javascript-api Linkedin JS authentication]
 
=What about Oauth1=
Actually we will have to implement it as well as some big identity provider as Twitter or Linkedin only offer Oauth1.
 
==== See also ====
* [https://developer.linkedin.com/documents/authentication LinkedinOauth1]
* [https://dev.twitter.com/docs/auth/oauth Twitter Oauth1]
 
= Functional specification =
 
== User ==
=== User authenticates for the first time in Moodle ===
# user clicks on Google button
# A Google page is opened asking the user to authorize  the Moodle site (happens only once)
# Pre-filled profile page is displayed. The user can update the information.
=== User already has an account ===
==== The email address sent by the identity provider is known by Moodle ====
# user clicks on Google button
# A Google page is opened asking the user to authorize  the Moodle site (happens only once)
# user is logged in
==== The email address is unknown ====
# user clicks on Google button
# A Google page is opened asking the user to authorize  the Moodle site (happens only once)
# a page asks if the user already have an account. There is one "create account / continue" button, and some authentication forms similar to the ones on the login page.
#* user has no accounts: the user clicks on continue. The pre-filled profile page is displayed. The user can update the information
#* user has an account: the user authenticates with the forms. The user arrive on a page asking to link Google account. The user clicks on Google account. The user is logged in. The account has been linked.
=== User already authenticated with his/her Oauth2 provider ===
The user only sees the provider icon (s)he previously used. This information is stored in a cookie. A link for the user to see the full providers list should be displayed.
 
== Administration ==
The administrator wants to:
* enable the plugin(s)
* set Oauth2 client id / Oauth2 client secret
* allow user account creation
* set lockable fields
* deny email addresses
* bypass the deny email addresses global settings (for example allow hotmail registration by Oauth2 but disallow it for manual to avoid spammer)
* allow email addresses
* allow the user to remove the authentication method (if not primary)
 
= Technical specification =
 
== One plugin by provider or One plugin for all providers ==
There were some talk about having one plugin by provider (one for Google, one for Moodle, one for Facebook) or one plugin with all provider (Google/Moodle(s)/facebook in one page). Both choice can support multiple instances (For example when Moodle sites will be Oauth2 providers). 
=== One plugin by provider ===
As we go for multiple authentication, each providers should have it's own lockable fields. Lockable fields will be ignored if the Oauth2 auth method is not the primary auth method of the user.
==== Pros ====
* it works nice with lang file and Amos without additional code. Each providers use its own vocabulary (client id/app id) and need its own explanation on how to set it up.
* clean setting page by providers
* can enable/disable each plugin without losing the settings
* easy overview of which authentication method is enabled
 
==== Cons ====
 
=== One plugin for all providers ===
==== Pros ====
* less click to setup for the administrator. Only enable one plugin.
 
==== Cons ====
* the settings page could become bigger and messier.
* need an additional "enable" checkbox for each providers (so the administrator can disable the plugin without losing the settings)
* administratosr do not know which Oauth2 provider is enabled without looking into the Oauth2 plugin.
* does not work well with AMOS
 
== Proposed design ==
<code language=php>
auth/oauth2SHORTNAME/auth.php
/**
* Facebook Oauth2 authentication plugin.
*/
class auth_plugin_oauth2SHORTNAME extends auth_plugin_oauth2 {
 
    public function __construct() {
        global $CFG;
 
        $this->name = 'Human Name';
        $this->shortname = SHORTNAME;
        $this->clientlibpath = $CFG->libdir . '/facebookapi.php';
        $this->clientclassname = 'shortname_oauth';
        $this->clientscope = 'email';
 
        parent::__construct();
    }
}
 
lib/SHORTNAMEapi.php
/** OAuth 2.0 client for SHORTNAME Services */
class SHORTNAME_oauth extends oauth2_client {
   
    /** Returns the auth url for OAuth 2.0 request */
    protected function auth_url() {
        // ...
    }
 
    /** Returns the token url for OAuth 2.0 request */
    protected function token_url() {
        // ...
    }
 
    /** Resets headers and response for multiple requests */
    public function reset_state() {
        // ...
    }
   
    /** Retrieve user info from SHORTNAME api */
    public function retrieve_auth_user_info() {
        // Oauth2 API call to get user info
 
        // Store result into $this->oauth2user
    }
}
 
lib/oauth2/auth.php
/** Oauth2 authentication plugin. */
abstract class auth_plugin_oauth2 extends auth_plugin_base {
 
    public $name;
    public $shortname;
    public $logourl;
    public $oauth2client;
    public $clientclassname;
    public $clientlibpath;
    public $clientscope;
 
    public function __construct() {
        // - Check that the extending class is correctly located, named, constructed.
        // - Instanciate the oauth2 client
        // - Normal auth_plugin_base settings
    }
 
    function prevent_local_passwords() { return true;}
 
    function user_login($username, $password) {
        //retrieve the user matching username
        //username must exist
    }
 
    function is_internal() {return false;}
 
    function can_change_password() {return false;}
 
    /**
    * Authentication hook - is called every time user hit the login page
    * The code is run only if the param code is mentionned.
    */
    function loginpage_hook() {
 
        //check the authorization code and that we are the right oauth2 plugin
     
                // Load the authenticated user info
             
                //throw an error if the email address is not verified
             
                // Prohibit login if email belongs to the prohibited domain
 
                // Detect it's a linking call
                // If yes then redirect to the profile page
             
                    // Link the account
                 
                        // Redirect back to edit profile page
                // Check if a linked account already exist
         
                // Avoid to commit an incomplete new user if ever a crash appears
           
                //create the user if it doesn't exist
                                 
                    // Check the provider allow user creation                 
                   
                    // If the user didn't confirm creation, then redirect to confirmation page
                 
                    //get following incremented username
                 
                    //check the user doesn't exist
                 
                    //retrieve more information from the provider
                 
                    //retrieve country and city if the provider failed to give it
                 
                    // create user record
 
                    // link account
 
                //authenticate the user
               
                    //set a cookie to remember what auth provider was selected
               
                    //prefill more user information if new user
                 
                    // Set URL redirection to the user edit page
               
                //We are going to redirect, it's time to commit the new user           
    }
 
    function loginpage_idp_list($wantsurl) {
        // List the different instance (mainly for Oauth2 Moodle)
    }
 
}
</code>
 
== Multiple authentication support ==
In order to support account linking we need a table to support multiple authentication.
=== New table:  mdl_user_auths ===
 
{| class="wikitable"
! id !! component !!  class="unsortable" |userid !!  class="unsortable" | provideruserid
|-
|  1 || auth_manual || 2 || null
|-
|  1 || auth_oauth2google || 2 || 85134984
|}
 
=== See also ===
* [https://docs.moodle.org/dev/Multi_authentication Multiple Authentication] (old)
 
= See also =
* MDL-34426

Latest revision as of 01:58, 23 May 2017

This information on this page has been archived because it is no longer current. For information on OAuth2 see OAuth2_Services.

To see the infomation previously on this page check the history.