2014-03-25 18 views
0

過去6個小時我一直在嘗試在互聯網上發現的大量事情,而我似乎無法解決這個問題。NodeJS OAuth2與AngularJS的CORS問題

  1. 我有一個本地測試角的應用程序,在http://tok.client

  2. 我有FrankHassanabad的OAuth2orize食譜(https://github.com/FrankHassanabad/Oauth2orizeRecipes)下http://localhost:3000

對於我的生活,我不能讓向OAuth2服務器發送令牌請求。

客戶端代碼:

.config(function($stateProvider, $urlRouterProvider, $httpProvider) { //Reset headers to avoid OPTIONS request (aka preflight) $httpProvider.defaults.headers.common = {}; $httpProvider.defaults.headers.post = {}; $httpProvider.defaults.headers.put = {}; $httpProvider.defaults.headers.patch = {}; $httpProvider.defaults.useXDomain = true; delete $httpProvider.defaults.headers.common['X-Requested-With']; console.log($httpProvider.defaults.useXDomain); console.log($httpProvider.defaults.headers.common); })

$scope.doLogin = function() { 
      console.log('DUMMY - doLogin'); 
      return $http.post('https://localhost:3000/oauth/token', 'grant_type=password&username=bob&password=secret&scope=offline_access',{ 
        headers: { 
         'Authorization': 'Basic YWJjMTIzOnNzaC1zZWNyZXQ', 
         'Access-Control-Allow-Origin': 'https://localhost:3000', 
         'Accept': 'application/x-www-form-urlencoded', 
         'Content-Type': 'application/x-www-form-urlencoded' 
        } 
       } 
      ) 
       .success(function(data, status, headers, config) { 
        // this callback will be called asynchronously 
        // when the response is available 
       }) 
       .error(function(dataX, status, headersX, config) { 
        console.log(dataX); 
        console.log(status); 
        console.log(headersX); 
        console.log(config); 
        // called asynchronously if an error occurs 
        // or server returns response with an error status. 
       }); 


      //fetchService(); 

     }; 

服務器端代碼

//Session Configuration 
app.use(function(req, res, next){ 
    res.header('Access-Control-Allow-Credentials', true); 
    res.header('Access-Control-Allow-Origin', req.headers.origin); 
    res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE'); 
    res.header('Access-Control-Allow-Headers', 'Access-Control-Allow-Origin, Accept, Authorization, Content-Type'); 
    if ('OPTIONS' == req.method) { 
     res.send(200); 
    } else { 
     next(); 
    } 

},express.session({ 
    secret: config.session.secret, 
    store: sessionStorage, 
    key: "authorization.sid", 
    cookie: {maxAge: config.session.maxAge } 
})); 

我嘗試添加所有CORS正常運作的要求,我能找到,它不工作。

這是我在瀏覽器中得到的響應:https://www.dropbox.com/s/zcujugh2m4i0xml/Screenshot%202014-03-26%2000.46.51.png

我甚至不知道我在臨時標題設置的標題,我讀了一噸的文章和StackOverflow上的問題,我只是想不通出來。

在某些時候,我雖然是某種https問題,但我在本地有https,OAuth2演示完美地工作,Chrome中的REST控制檯也工作得非常好,只有AngularJS決定讓我花很多錢小時試圖解決這個:)。

可能是什麼問題?

+0

顯然,通過使用信息從這個問題http://stackoverflow.com/questions/3102819/disable-same-origin- policy-in-chrome/6083677#6083677 ...禁用Chrome的安全性我可以成功發佈帖子。這不是一個解決方法,但爲了目前的測試目的,它現在可以工作了,現在我只需要看看它是如何在科爾多瓦的網絡工作者中工作的。 –

回答

0

不知道是否會幫助,但它看起來像你試圖使用密碼授予類型。此授權類型需要知識或用戶憑證以及OAuth2客戶端憑證(您將所有這些憑證交換爲訪問令牌)。

該授予並不意味着被用於客戶端,因爲這意味着您的OAuth消費者憑據泄露給每個人。在JavaScript中,您可以使用值「Basic YWJjMTIzOnNzaC1zZWNyZXQ」將請求標頭添加到您的請求中。這只是您的客戶端憑據base64編碼(解碼爲abc123/ssh-secret)。

對於客戶端(即JavaScript)應用程序,通常建議隱式授權流程(http://tools.ietf.org/html/rfc6749#section-1.3.2)。

有一些Angular庫支持它: https://github.com/enginous/angular-oauth

(使用這些庫我還沒有嘗試過,所以我不能爲他們提供擔保)