Search code examples
authenticationoauthgoogle-chrome-extensionopenidmoodle

OAuth - Login With Moodle



I am building a chrome extension for a school, and it needs to authenticate with moodle, meaning that I need a OAuth or OpenID like setup, I open a page to moodle, the user then logs in/approves the app, and then a token is returned, which I can then use for normal moodle web service calls. Is this possible?

Thanks, Ari


Solution

  • I has a similar issue when trying to authenticate an extension using oauth, the flow I used was first check if we have tokens stored: if not then open a new tab from a background page, which means it will be ran when the extension is first installed, although if the token is there we go on to run our requests, but I asked the user to close the tab after the approval has been completed, which I set a listener for after it was created, then I knew that the process was complete and I could make requests.

    If you need any code for sections of this, I'd be happy to post.

    Good Luck

    Update:

    check if we have tokens saved :

    if(localStorage.token){
        if(localStorage.secret){
    

    if not :

    else{
        var authURL = bgOauth.requestTokenCall();
        var req = new XMLHttpRequest();
        req.overrideMimeType("application/json");
        req.onload = bgOauth.processRequestTokenData;
        req.open("get", authURL, true);
        req.send();
        console.log('sent')
    }
    

    then I process the response from this, if all good then I open a new tab:

    startPopup : function(destinationurl) {
    
        chrome.tabs.create({url : destinationurl}, 
        function(tab){bgOauth.newTabId = tab.id})
        alert(Instructions)     
        chrome.tabs.onRemoved.addListener(function(tabId)
        {
            console.log('oh no, closed');
            if(tabId == bgOauth.newTabId){
                console.log('freaking sweet!');
                bgOauth.handleApproval()
            }   
            else{
                return false;
            }
        })
    

    Then I can finish the authorization process, and preform requests be sure to save your credentials in localStorage.