A Passport strategy for authenticating with T-Mobile ID using a method similar to OAuth 2.0 API.
This module lets you authenticate using T-Mobile ID in your Node.js applications. By plugging into Passport, T-Mobile ID authentication can be easily and unobtrusively integrated into any application or framework that supports Connect-style middleware, like Express.
$ npm install passport-tmobileid
The T-Mobile ID authentication strategy authenticates users using their phone number or T-Mobile ID username. Developers wishing to utilize this plugin must first redirect their users to the T-Mobile authorization endpoint with the appropriate params.
var params = {'access_type': 'ONLINE',
'redirect_uri': 'https://localhost:3000/auth/tmoid/callback',
'scope': 'TMO_ID_profile,associated_lines,billing_information,entitlements',
'client_id': TMOBILE_CLIENT_ID,
'response_type' : 'code'};
res.redirect('https://auth.tmus.net/oauth2/v1/auth?' + qs.stringify(params));
The strategy requires four elements in order to properly process the authentication request, these are:
- redirect_uri - The callback URL local to your server
- tokenURL - The URL to the token request server
- clientID - Your client ID provied by T-Mobile
- clientSecret - Your client secret key provided by T-Mobile
Note: The strategy supports other standard passport arguments such as passReqToCallback.
var TMobileIDStrategy = require('passport-tmobileid').Strategy;
passport.use(new TMobileIDStrategy({
redirect_uri : LOCAL_CALLBACK_URL,
tokenURL : 'https://token.tmus.net/oauth2/v1/token',
clientID : TMOBILE_CLIENT_ID,
clientSecret : TMOBILE_CLIENT_SECRET,
passReqToCallback : true //to get the req back from passport
},
function(req, token, expiry, id, done){
if(err){return done(err);}
if(!token){return done(null, false);} //No token could be retrieved from the server
if(id) {
//A T-Mobile access token has been provided
User.findOne({'user.tmobileid' : id}, function(err,user){
if(user) { //a user with this id has been found in your database
user.tmobile.access_token = token; //add the tmobile access token to this user
user.save(function(err){
//handle the error
}
return done(null, user); //success
});
}
};
Use passport.authenticate()
, specifying the 'tmoid'
strategy, to
authenticate requests.
For example, as route middleware in an Express application:
app.get('/auth/tmoid',
res.redirect('https://uat.auth.tmus.net/oauth2/v1/auth?' + qs.stringify(params)));
app.get('/auth/tmoid/callback',
passport.authenticate('tmoid', {
failureRedirect: '/login',
successRedirect: '/profile'
}),
function(req, res) {
// Successful authentication, do nothing.
});
For a complete, working example, refer to the example.
(c) 2014 T-Mobile USA, Inc. All Rights Reserved.