node-oauth2-server icon indicating copy to clipboard operation
node-oauth2-server copied to clipboard

What is the role of redirect_uri in implementation of refresh_token oauth

Open RiteshM opened this issue 13 years ago • 16 comments

i read the provided documentation for node-oauth2-server.i am trying to run this example.i set up all the things:

var express = require('express'); oauthserver = require('node-oauth2-server');

var app = express(); app.configure(function() { var oauth = oauthserver({ model: require('./model'), // See below for specification grants: ['refresh_token'], allow:['/path1'], debug: true }); app.use(express.bodyParser()); // REQUIRED app.use(oauth.handler()); app.use(oauth.errorHandler()); });

app.listen(3000);

Now i get the concept of model object for password.for getting refresh_token what to do ?? for example in google oauth.first we get code parameter on redirect uri then we make a call to token url.how to implement refresh_token oauth.and no role is mentioned of redirect_uri. but its required for 3 legged oauth.Please explain implementation. on which url user have to direct for login.

RiteshM avatar Oct 03 '13 15:10 RiteshM

I think you are referencing the "auth_code" grant type which is not yet implemented in this module, it will land in 2.0 branch in the next couple of weeks, follow #10

thomseddon avatar Oct 03 '13 15:10 thomseddon

what is the flow of refresh_token grant type.please explain a little what are the endpoints and expected response for getting refresh token?? please explain a little !!

RiteshM avatar Oct 03 '13 15:10 RiteshM

A valid request could look like this:

POST /oauth/token HTTP/1.1
Host: server.example.com
Authorization: Basic czZCaGRSa3F0MzpnWDFmQmF0M2JW
Content-Type: application/x-www-form-urlencoded

grant_type=refresh_token&refresh_token=abc123

More details in the test, specifically this one.

thomseddon avatar Oct 03 '13 15:10 thomseddon

perhaps this is czZCaGRSa3F0MzpnWDFmQmF0M2JW access token and this is abc123 refresh token . how to get these parameters for first request when user submits his user name and password ?? in password case we get this by submitting username and password ?Please explain .

RiteshM avatar Oct 03 '13 15:10 RiteshM

As in the test, you need to send four params for a refresh_token grant:

  1. grant_type (must be refresh_token)
  2. refresh_token
  3. client_id
  4. client_secret

You can either send client_id and secret in the body, or you can create an authorisation header (which is the czZCaGRSa3F0MzpnWDFmQmF0M2JW) equal to base64(client_id:client_secret)

The server will reply with an access token response.

These questions are more to do with the OAuth2 protocol as oppose to this module, which is merely an implementation of it in node. Full details on the protocol can be found in the spec: http://tools.ietf.org/html/rfc6749

thomseddon avatar Oct 03 '13 15:10 thomseddon

i try to understand ur point in the http://tools.ietf.org/html/rfc6749 there is a

(A)------- Authorization Grant --------->| | | | | | | |<-(B)----------- Access Token -------------| | | | & Refresh Token

in the starting i will not have refresh_token . i have to send a request for authorization_grant . hw to send this request to your library so that i get refresh_token and after that i will be able to make these requests as u described

POST /oauth/token HTTP/1.1 Host: server.example.com Authorization: Basic czZCaGRSa3F0MzpnWDFmQmF0M2JW Content-Type: application/x-www-form-urlencoded

grant_type=refresh_token&refresh_token=abc123

r u getting my problem ??

RiteshM avatar Oct 03 '13 16:10 RiteshM

Use password grant to get the first access token and refresh token, there is an example in the readme and in the tests. As I said in my first comment, Auth Code grant_type is not yet supported.

thomseddon avatar Oct 03 '13 17:10 thomseddon

when i change grant to this grants: ['refresh_token','password'], then its giving me error on request payload grant_type=password&username=johndoe&password=A3ddj3w .error is 503 Service Unavailable { code: 503 error: "server_error" error_description: "server_error" } .perhaps in model there is no change we have to do for only getting access token .why i am facing it and how to remove it ??

RiteshM avatar Oct 03 '13 17:10 RiteshM

have you implemented saveRefreshToken ?

If not, then do, if you have then try setting debug: true and look in the console

thomseddon avatar Oct 03 '13 17:10 thomseddon

Also, are any of your callback functions passing an error?

thomseddon avatar Oct 03 '13 17:10 thomseddon

i implemented a dummy model for now

model = module.exports,

model.getAccessToken = function (bearerToken, callback) {

callback(false,{'access_token':'anbc','client_id':'1122334r4555','expired':3600, 'user_id':'123'});

};

model.getClient = function (clientId, clientSecret, callback) { callback(false,{'client_id':'1122334r4555','client_secret':'1122334455','redirect_uri':''}) };

var authorizedClientIds = ['abc1', 'def2']; model.grantTypeAllowed = function (clientId, grantType, callback) { callback(false, true); };

model.saveAccessToken = function (accessToken, clientId, userId, expires, callback) {

callback(null);

};

model.saveRefreshToken = function (refreshToken, clientId, userId, expires, callback) { callback(null); };

/*

  • Required to support password grant type */ model.getUser = function (username, password, callback) {

callback({'id' : '123'});

};

in this i implemented a saveRefreshToken and returning null as error and not getting any thing after making debug:true except { id: '123' } .instead of null i tried false in callback(false) .but getting same error

RiteshM avatar Oct 03 '13 17:10 RiteshM

You're indicating an error by passing an argument to the model.getUser callback, it should be:

model.getUser = function (username, password, callback) {
  callback(false, {id : '123'});
};

thomseddon avatar Oct 03 '13 17:10 thomseddon

i successfully get access_token and refresh_token .i extend my dummy model to add this method and get response using grant refresh_token after getting one time from password grant .

model.getRefreshToken = function(refreshToken ,callback){ callback(false,{'refresh_token':'anbc','client_id':'1122334r4555','expired':3600, 'user_id':'123'});

};

i am getting a random response { access_token: "8934da145798c2b8a3c99802854b6bd8f16d184d" refresh_token: "108db4e676a3048b6073e951d3cc028fd8659a19" token_type: "bearer" expires_in: 3600 }

they have no relation from this passed refresh_token .can u please explain is there any relation exist between both.then what to do in these methods if parameters passed to callback doesn't have any effect ??

RiteshM avatar Oct 03 '13 17:10 RiteshM

The tokens are random, YOU should save the relation in you're "saveAccessToken" and "saveRefreshToken" methods, that way, when "getAccessToken" and "getRefreshToken" are called later you should check your database and return an object specifying the user and client to which the token is associated. The module will check this and then issue a token again if everything is ok.

Please please please just look through the postgres example, look at the tests, read the spec and look how OAuth2 actually works.

thomseddon avatar Oct 03 '13 17:10 thomseddon

hey thanks a lot for the help. but only two doubt

  1. i got the point of implementing all the functions but where function getAccessToken and getRefreshToken utilized or called ?? i get the point from your comment How to implement. 2.when ever i pass an error in any function like

model.saveAccessToken = function (accessToken, clientId, userId, expires, callback) {

callback({'error':'invalid response'},false);

};

then it will give the response { code: 503 error: "server_error" error_description: "server_error" } if there is any custom mistake or error then can i change this response to some thing else manually .can i customize error message using this library??

RiteshM avatar Oct 04 '13 00:10 RiteshM

OK:

  1. i got the point of implementing all the functions but where function getAccessToken and getRefreshToken utilized or called ?? i get the point from your comment How to implement.

When you use the access token to access a resource i.e. GET /secret?access_token=THETOKEN, getAccessToken will be called to see if it is a valid token and see to which user the token is associated (set in req.user) getRefreshToken is called when a refresh token is used to create a new refresh token

  1. is there is any custom mistake or error then can i change this response to some thing else manually .can i customize error message using this library??

See #12

thomseddon avatar Oct 04 '13 06:10 thomseddon