Webservice pass-through authentication

Webservice pass-through authentication

by David North -
Number of replies: 3

I'm setting up a webservice that is sent the user's username and password. The function then has to log the user into moodle.


My idea was to just set up a form on the clients side that posts to /login/index.php but for some reason that isn't viable, and it has to be a webservice.


What is the best way for me to auto login the user within the webservice function?

Average of ratings: -
In reply to David North

Re: Webservice pass-through authentication

by Darko Miletić -

Could you be a bit clearer? I do not understand what is the exact work-flow here.

In reply to Darko Miletić

Re: Webservice pass-through authentication

by David North -

Sorry about that.


I need to set up an external webservice function that logs the user into the system.


Currently this is what I've done and it works but is there a better way to do this?



/**
     * Returns description of method parameters
     *
     * @return external_function_parameters
     * @since Moodle 2.2
     */
    public static function autologin_parameters() {

        return new external_function_parameters(
                array('username' => new external_value(PARAM_RAW, 'user name'))
        );
    }	


/**
     * Logs the user into into the system
     *
     * @return external_function_parameters
     * @since Moodle 2.2
     */
    public static function autologin($username){
        require_once('../../config.php');
        global $CFG;

        ?>

        <form id="loginform" action="<?php echo $CFG->wwwroot.'/login/index.php' ?>" method="post" style="display:none">
            <input type="hidden" name="username" value="<?php echo $username ?>"> <br>
            <input type="hidden" name="password" value="pass1234"> <br>
        </form>
        
        <script type="text/javascript">
            document.getElementById('loginform').submit(); // SUBMIT FORM
        </script>

        <?php

    }


        /**
     * Returns description of method result value
     *
     * @return external_description
     * @since Moodle 2.2
     */
    public static function autologin_returns() {
        return new external_value(PARAM_RAW, 'The username of the user to be logged in');
    }

What I've done is added a form into the function and submitted the form using javascript and then the user is logged in and redirected into moodle. The return is pretty irrelevant as the user gets redirected into the system.

I get my desired results but I'm sure this isn't the most effective/best way of doing this, or is it?