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
No edit summary
Line 7: Line 7:
}}
}}


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 document describes the functional and technical specification of the Moodle Oauth2 plugin(s). Oauth2 plugin(s) will allow a user to authenticate in Moodle with an identity provider that implement Oauth2, like Google or Facebook.


= What is Oauth2 =
= What is Oauth2 =
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.
OAuth2 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 ====
==== See also ====
Line 16: Line 16:


= What are the user's benefits =
= What are the user's benefits =
* The user can connect in Moodle with a Google/Facebook/... account.
* Fast account creation: the new user form is pre-filled with information from the Google/Facebook account.
* Login is fast: 1 click on a Google/Facebook/... button.
* Fast login: 1 click on a Google/Facebook button.
* The user does not need to remember a password for Moodle.
* 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=
=Why implementing Oauth2 which is still in Draft version=
Line 33: Line 31:


=What about Oauth1=
=What about Oauth1=
Actually we will have to implement it as well as some big identity provider as Twitter or Linkedin only offer Oauth1.
Actually we will have to implement it as well as some big identity provider as Twitter only offer Oauth1.


==== See also ====
==== See also ====

Revision as of 07:52, 12 August 2013

Oauth2 authentication
Project state Specification consultation
Tracker issue MDL-34426
Discussion http://moodle.org/mod/forum/discuss.php?d=212430
Assignee Jerome mouneyrac


This document describes the functional and technical specification of the Moodle Oauth2 plugin(s). Oauth2 plugin(s) will allow a user to authenticate in Moodle with an identity provider that implement Oauth2, like Google or Facebook.

What is Oauth2

OAuth2 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

What are the user's benefits

  • Fast account creation: the new user form is pre-filled with information from the Google/Facebook account.
  • Fast login: 1 click on a Google/Facebook button.
  • The user does not need to remember a password for Moodle.

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 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 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

What about Oauth1

Actually we will have to implement it as well as some big identity provider as Twitter only offer Oauth1.

See also

Functional specification

User

User already has a linked Google account

  1. user clicks on Google button - Note: 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.
  2. A Google page is opened asking the user to authorize the Moodle site (happens only once)
  3. user is logged in

User authenticates for the first time with Google

  1. user clicks on Google button
  2. A Google page is opened asking the user to authorize the Moodle site (happens only once)
  3. 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 user is logged in.
    • 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.

Administration

The administrator wants to:

  • enable the plugin(s)
  • set Oauth2 client id / Oauth2 client secret
  • allow user account creation
  • set lockable fields
  • bypass the deny/allow email addresses global settings (for example allow hotmail registration by Oauth2 but disallow it for manual to avoid spammer)

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

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)
   }

}

Multiple authentication support

In order to support account linking we need a table to support multiple authentication.

New table: mdl_user_auths

id component userid provideruserid
1 auth_manual 2 null
1 auth_oauth2google 2 85134984

See also

See also