0
I’m having trouble getting the token from Oauth2
I’m trying by Postman to get the token.
I have registered in the user who is trying to get a token
{
"statusCode": 400,
"status": 400,
"code": 400,
"message": "Invalid client: cannot retrieve client credentials",
"name": "invalid_client"
}
Oauth2model.js
/**
* Module dependencies.
*/
const debug = require('debug')('OAuth2Model');
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
/**
* Schema definitions.
*/
mongoose.model('OAuthTokens', new Schema({
accessToken: { type: String },
accessTokenExpiresAt: { type: Date },
client : { type: Object }, // `client` and `user` are required in multiple places, for example `getAccessToken()`
clientId: { type: String },
refreshToken: { type: String },
refreshTokenExpiresAt: { type: Date },
user : { type: Object },
userId: { type: String },
}));
mongoose.model('OAuthClients', new Schema({
clientId: { type: String },
clientSecret: { type: String },
redirectUris: { type: Array }
}));
mongoose.model('OAuthUsers', new Schema({
email: { type: String, default: '' },
firstname: { type: String },
lastname: { type: String },
password: { type: String },
username: { type: String }
}));
var OAuthTokensModel = mongoose.model('OAuthTokens');
var OAuthClientsModel = mongoose.model('OAuthClients');
var OAuthUsersModel = mongoose.model('OAuthUsers');
/**
* Get access token.
*/
module.exports.getAccessToken = async function(bearerToken) {
const tokenObj = await OAuthTokensModel.findOne({ accessToken: bearerToken }).lean();
return tokenObj;
};
/**
* Get client.
*/
module.exports.getClient = async function(clientId, clientSecret) {
const clientDetails = await OAuthClientsModel.findOne({ clientId: clientId, clientSecret: clientSecret }).lean();
return clientDetails;
};
/**
* Get refresh token.
*/
module.exports.getRefreshToken = async function(refreshToken) {
return await OAuthTokensModel.findOne({ refreshToken: refreshToken }).lean();
};
/**
* Get user.
*/
module.exports.getUser = async function(username, password) {
return await OAuthUsersModel.findOne({ username: username, password: password }).lean();
};
/**
* Save token.
*/
module.exports.saveToken = function(token, client, user) {
var accessToken = new OAuthTokensModel({
accessToken: token.accessToken,
accessTokenExpiresAt: new Date(token.accessTokenExpiresAt),
client : client,
clientId: client.clientId,
refreshToken: token.refreshToken,
refreshTokenExpiresAt: new Date(token.refreshTokenExpiresAt),
user : user,
userId: user._id,
});
console.log("accesss token",accessToken)
return new Promise( function(resolve,reject){
accessToken.save(function(err,data){
if( err ) reject( err );
else resolve( data );
}) ;
}).then(function(saveResult){
saveResult = saveResult && typeof saveResult == 'object' ? saveResult.toJSON() : saveResult;
var data = new Object();
for( var prop in saveResult ) data[prop] = saveResult[prop];
data.client = data.clientId;
data.user = data.userId;
return data;
});
};
Oauth2service
const debug = require('debug')('OAuth2Service');
const OAuth2Model = require('../model/OAuth2Model.js');
const OAuth2Server = require('oauth2-server'), //Represents an OAuth2 server instance.
Request = OAuth2Server.Request,
Response = OAuth2Server.Response;
var instance;
/**
* Instantiates OAuth2Server using the supplied model.
*/
var oAuth2 = new OAuth2Server({
model:OAuth2Model,
accessTokenLifetime: 86500,
allowBearerTokensInQueryString: true
});
/**
* Creating constructor
*/
function OAuth2Service(){
}
/**
* Define the shared properties and methods using the prototype
*/
/**
* Obtaine OAuth token with Basic Authentication
*/
OAuth2Service.prototype.obtainToken = function(req, res) {
var request = new Request(req);
var response = new Response(res);
return oAuth2.token(request, response)
.then(function(token) {
debug("obtainToken: token %s obtained successfully",token);
res.json(token);
}).catch(function(err) {
res.status(err.code || 500).json(err);
});
}
/**
* Authenticates a request.
*/
OAuth2Service.prototype.authenticateRequest = function(req, res, next) {
var request = new Request(req);
var response = new Response(res);
return oAuth2.authenticate(request, response)
.then(function(token) {
debug("the request was successfully authenticated")
next();
}).catch(function(err) {
res.status(err.code || 500).json(err);
});
}
/**
* Export an Instance
*/
module.exports = {
getInstance: function () {
if (!instance) {
instance = new OAuth2Service();
}
return instance;
}
};