Skip to content

Commit

Permalink
Merge pull request #50 from tidepool-org/jhbate/oauth-header
Browse files Browse the repository at this point in the history
Jhbate/oauth login
  • Loading branch information
jebeck committed May 22, 2015
2 parents 48895dc + d5cf9e8 commit 7842829
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,7 @@ module.exports = function (config, deps) {
getUserId: user.getUserId,
isLoggedIn: user.isLoggedIn,
login: user.login,
oauthLogin: user.oauthLogin,
logout: user.logout,
signup: user.signup,
updateCurrentUser: user.updateCurrentUser,
Expand Down
22 changes: 22 additions & 0 deletions test/integration/tidepoolPlatform_integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -789,4 +789,26 @@ describe('platform client', function () {
});
});
});
describe('handles oauth token login', function () {
it('an invalid token returns 401', function(done){
async.series([
//ensure we are logged out
function(callback){ pwdClient.logout(callback);},
//do oauthLogin but with an invalid token
function(callback){
pwdClient.oauthLogin('fakeToken',function(err, data){
expect(err).to.exist;
expect(err.status).to.exist;
expect(err.status).to.equal(401);
callback(null);
});
},
//we do a legit login for cleanup in this instance
function(callback){ pwdClient.login(a_PWD,{}, callback);},
],
function(err, details) {
done(); //finish up
});
});
});
});
31 changes: 31 additions & 0 deletions user.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,36 @@ module.exports = function (common, config, deps) {
function getUserToken(){
return myToken;
}
/**
* Login user to the Tidepool platform using the provided oauth token
*
* @param provided oauth token
* @param cb
* @returns {cb} cb(err, response)
*/
function oauthLogin(oauthToken, cb) {

superagent
.post(common.makeAPIUrl('/auth/oauthlogin'))
.set('Authorization', 'bearer '+oauthToken)
.end(
function (err, res) {

if (err != null) {
return cb(err, null);
}

if (res.status !== 200) {
return common.handleHttpError(res, cb);
}

var oauthUserId = res.body.oauthUser.userid;
var theToken = res.headers[common.SESSION_TOKEN_HEADER];
//save the session and remember by default
saveSession(oauthUserId, theToken, {remember:true});
return cb(null,{userid: oauthUserId, user: res.body.oauthUser, target: res.body.oauthTarget});
});
}
/**
* Login user to the Tidepool platform
*
Expand Down Expand Up @@ -416,6 +446,7 @@ module.exports = function (common, config, deps) {
getUserToken : getUserToken,
isLoggedIn: isLoggedIn,
login : login,
oauthLogin : oauthLogin,
logout : logout,
signup : signup,
initialize : initialize,
Expand Down

0 comments on commit 7842829

Please sign in to comment.