2016-09-20 80 views
0

以下是我一直收到的錯誤 「CWOAU0062E:由於重定向URI無效,OAuth服務提供者無法重定向請求,因此請聯繫您的系統管理員以解決問題。無法在BlueMix中爲NodeJS獲取SSO

var express = require('express'); 

// Add for SSO 
var cookieParser = require('cookie-parser'); 
var session = require('express-session'); 
var passport = require('passport'); 
var OpenIDConnectStrategy = require('passport-idaas-openidconnect').IDaaSOIDCStrategy; 
var redis = require('redis'); 
var RedisStore = require('connect-redis')(session); 

// cfenv provides access to your Cloud Foundry environment 
// for more info, see: https://www.npmjs.com/package/cfenv 
var cfenv = require('cfenv'); 
// get the app environment from Cloud Foundry 
var appEnv = cfenv.getAppEnv(); 

// create a new express server 
var app = express(); 
var services = JSON.parse(process.env.VCAP_SERVICES || null); 
// get configuration for redis backing service and connect to service 
var redisConfig = appEnv.getService(/Redis.*/); 
if(redisConfig == null) { 
    console.log('ERROR: Failed to create REDDISCONFIG!!!'); 
} else { 
    var redisPort = redisConfig.credentials.port; 
    var redisHost = redisConfig.credentials.hostname; 
    var redisPasswd = redisConfig.credentials.password; 

    var redisclient = redis.createClient(redisPort, redisHost, {no_ready_check: true}); 
    redisclient.auth(redisPasswd, function (err) { 
     if (err) { 
     throw err; 
     } 
    }); 

    redisclient.on('connect', function() { 
     console.log('Connected to Redis'); 
    }); 
} 

// define express session services, etc for SSO 
app.use(cookieParser()); 
// app.use(session({resave: 'true', saveUninitialized: 'true' , secret: 'keyboard cat'})); 
if(redisConfig != null) { 
    app.use(session({ 
    store: new RedisStore({ client: redisclient }), 
    resave: 'true', 
    saveUninitialized: 'true', 
    secret: 'top secr8t' 
    })); 
} 

app.use(passport.initialize()); 
app.use(passport.session()); 

passport.serializeUser(function(user, done) { 
    done(null, user); 
}); 

passport.deserializeUser(function(obj, done) { 
    done(null, obj); 
}); 

// find config object for the SSO services from VCAP_SERVICES through cfenv/appEnv 
var ssoConfig = services.SingleSignOn[0]; 
//appEnv.getService(/Single Sign On.*/) 
if(ssoConfig == null) { 
    console.log('ERROR: Failed to instantiate SSOCONFIG. Its not available!!!'); 
} else { 
    var client_id = ssoConfig.credentials.clientId; 
    var client_secret = ssoConfig.credentials.secret; 
    var authorization_url = ssoConfig.credentials.authorizationEndpointUrl; 
    var token_url = ssoConfig.credentials.tokenEndpointUrl; 
    var issuer_id = ssoConfig.credentials.issuerIdentifier; 
} 
// you MUST change the host route to match your application name 
// var callback_url = 'https://scaleSSO-TOR0815.mybluemix.net/auth/sso/callback'; 
var callback_url = 'https://krishnodejs.mybluemix.net/auth/sso/callback'; 

var OpenIDConnectStrategy = require('passport-idaas-openidconnect').IDaaSOIDCStrategy; 
var Strategy = new OpenIDConnectStrategy({ 
       authorizationURL : authorization_url, 
       tokenURL : token_url, 
       clientID : client_id, 
       scope: 'openid', 
       response_type: 'code', 
       clientSecret : client_secret, 
       callbackURL : appEnv.url + '/auth/sso/callback', 
       // callbackURL : callback_url, 
       skipUserProfile: true, 
       issuer: issuer_id}, 
    function(accessToken, refreshToken, profile, done) { 
       process.nextTick(function() { 
     profile.accessToken = accessToken; 
     profile.refreshToken = refreshToken; 
     done(null, profile); 
      }) 
}); 

passport.use(Strategy); 
app.get('/login', passport.authenticate('openidconnect', {})); 

function ensureAuthenticated(req, res, next) { 
    if(!req.isAuthenticated()) { 
     // req.session.originalUrl = 'https://krishnodejs.mybluemix.net'; 
     res.redirect('/login'); 
    } else { 
     return next(); 
    } 
} 

app.get('/auth/sso/callback',function(req,res,next) { 
     var redirect_url = 'https://krishnodejs.mybluemix.net/hello'; 
     // req.session.originalUrl; 
      passport.authenticate('openidconnect',{ 
       successRedirect: redirect_url, 
       failureRedirect: '/failure', 
      })(req,res,next); 
     }); 


app.get('/hello', ensureAuthenticated, function(req, res) { 
    res.send('Hello, '+ req.user['id'] + '!'); } 
); 

app.get('/failure', function(req, res) { 
      res.send('login failed'); }); 

// serve the files out of ./public as our main files 
app.use(express.static(__dirname + '/public')); 

// start server on the specified port and binding host 
app.listen(appEnv.port, function() { 

    // print a message when the server starts listening 
    console.log("server starting on " + appEnv.url); 
}); 

我在SSO的返回URL設置以下網址「https://krishnodejs.mybluemix.net/hello

任何意見,建議,解決是更受歡迎。

失敗

重定向URL有我的回調網址吧,除了怪異&範圍= OpenID的....但我想,這可能不是一個問題

我看着服務器端日誌錯誤。但沒有。讓我不知道到哪裏的問題

https://ssotest-gx1592z76o-cl12.iam.ibmcloud.com/idaas/oidc/endpoint/default/authorize?response_type=code&client_id=EdzctxPuQ4&redirect_uri=https://krishnodejs.mybluemix.net/auth/sso/callback&scope=openid OpenID的」

回答

0

對於誰也有類似的問題,注意是來自BlueMix重新登錄REDIRECT_URL參數人。

在我來說,我是

  • 「配置應用程序」 一節中設置此URL在兩個地方

    雖然這兩個鏈接都指向相同的URL,但出於某種原因,應用程序會從代碼中選取一個。我能夠弄清楚的方式是從參數callback_url中返回的(如上) 首先,我們在代碼中提供的重定向URL不必是完整的URL。在我的情況下,它應該是'bluemix/callback'。我修正了這一點。沒有用,不行。但事情從一個問題轉移到另一個問題。如果我能這樣說,那就是一個前進的步驟。 下一個問題是什麼? URL回來了,但協議不是。它不是https,而是始終返回http,因此該頁面仍未加載。 最後,我擺脫了從代碼中設置此URL並在UI中驅動URL中的所有內容。我在「配置應用程序」中將URL設置爲「https://krishnodejs.mybluemix.net/auth/bluemix/callback」。 如果你想知道,是什麼樣子我的代碼,這些更改後,在這裏你去

    var OpenIDConnectStrategy = require('passport-idaas-openidconnect').IDaaSOIDCStrategy; 
    var OpenIDStrategy = new OpenIDConnectStrategy({ 
        authorizationURL : authorization_url, 
        tokenURL : token_url, 
        clientID : client_id, 
        scope: 'openid', 
        response_type: 'code', 
        clientSecret : client_secret, 
        // callbackURL : callback_url, 
        skipUserProfile: true, 
        issuer: issuer_id 
    }, function(accessToken, refreshToken, profile, done) { 
        process.nextTick(function() { 
         profile.accessToken = accessToken; 
         profile.refreshToken = refreshToken; 
         done(null, profile); 
        }); 
    }); 
    passport.use(OpenIDStrategy); 
    

    }

    正如你可以看到我的評論指出,經過callback_url的代碼。賓果,一切正常。 獲得的經驗:注意'redirect_url'。如果URL返回錯誤,那麼您錯過了在代碼或用戶界面中設置它的權利。 我的下一次嘗試將是在UI中完全不設置它,並將它從代碼中完全驅動。目前認爲我已經過去了這個問題,我可以繼續前進。 希望這有助於。

  • 0

    CWOAU0062E:由於重定向URI無效,OAuth服務提供者無法重定向請求。請聯繫您的系統管理員以解決問題。 通常表示在單一登錄服務中沒有正確配置「返回URL」。

    因此,請更新服務集成部分下的單一登錄返回URL,以便它與您的代碼中的callback_url匹配,然後重新啓動應用程序