dimanche 26 mars 2017

Postman Oauth2 call works, however same call in Jquery fails (web extension)

I have been using Oauth2 using Postman and it works just fine. I get the token as expected.

Postman Oauth

However when I try this in code, it makes everything but I am never returned the code to use (the code is in my database, but it isn't returned from the jquery call).

function validate(redirectURL) {
  console.log('start validate function');


  var clientID2 = "CLIENTID HERE";
  var redirectURL2 = browser.identity.getRedirectURL();
  let m = redirectURL.match(/[#\?](.*)/);
  if (!m || m.length < 1)
    return null;
  let params = new URLSearchParams(m[1].split("#")[0]);
    let tokenurl = "http://localhost:8000/oauth/access_token";
  // tokenurl += `?client_id=${clientID2}`;
  // tokenurl += `?client_secret=${clientSecret}`;
  // tokenurl += `&grant_type=authorization_code`;
  // tokenurl += `&code=${params.get("code")}`;
  // tokenurl += `&redirect_uri=${encodeURIComponent(redirectURL2)}`;
  console.log('validate function tokenurl '+ tokenurl);
  $.ajax({
    method: "POST",
    url: tokenurl,
    async: true,
    crossDomain: true,
    headers: {
      "content-type": "application/x-www-form-urlencoded",
      "cache-control": "no-cache"
    },
    data : {
      client_id : clientID2,
      client_secret : "CLIENT ID SECRET",
      redirect_uri : redirectURL2,
      grant_type : "authorization_code",
      code : params.get("code"),
    }
  })
    .done(function( msg ) {
      console.log('Done return for validate call');
      // console.log( "Token Save Called: " + msg );
    })
    .fail(function(jqXHR, textStatus, errorThrown){
      console.log('Fail return for validate call');
      if(textStatus){
        console.log('validate call returned textStatus-' + textStatus);
      }
      if(errorThrown){
        console.log('validate call returned errorThrown-'+errorThrown);
      }
      console.log('Fail return for validate call Finish');
    })
    .always(function( xhr, status ) {
       console.log('finish validate call ajax request');
     });
}


function authorize() {
  console.log('authorize2');
  const redirectURL = browser.identity.getRedirectURL();
  console.log('redirectURL');
  console.log(encodeURIComponent(redirectURL));
  console.log(redirectURL);
  const clientID = "CLIENTID HERE";
  const scopes = ["openid", "email", "profile"];
  let authURL = "http://localhost:8000/oauth/authorize";
  authURL += `?client_id=${clientID}`;
  authURL += `&grant_type=authorization_code`;
  authURL += `&client_secret=4c7f6f8fa93d59c45502c0ae8c4a95b`;
  authURL += `&response_type=code`;
  authURL += `&redirect_uri=${encodeURIComponent(redirectURL)}`;
  // authURL += `&scope=${encodeURIComponent(scopes.join(' '))}`;

  console.log('redirectURL');
  console.log(redirectURL);
  console.log('authURL');
  console.log(authURL);

  return browser.identity.launchWebAuthFlow({
    interactive: true,
    url: authURL
  });
}


function getAccessToken() {
  console.log('call authorize');
  return authorize().then(validate);
}

console.log('call getAccessToken');
getAccessToken();

I get no data returned and the console logs put out the word error. The functions are getting called and getting to my backend because it is making and storing the final code in the database.

I am using this laravel oauth package http://ift.tt/17O1EK3 with version 5.1

I am using this in a firefox webextension on Firefox Developer Edition running version 54 which supports Oauth2 calls (based on documentation).



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire