Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for pass-through authentication #430

Merged
merged 1 commit into from
Dec 5, 2017

Conversation

Traksewt
Copy link
Contributor

Description

Allow the rest connector to call other loopback services by passing on the user authorization from the options where available. This is the fallback if no other authorization is specified.

This allows loopback servers to communicate securely with other loopback servers by passing on the auth token. Useful for using loopback as microservices.

Related issues

Checklist

  • New tests added or existing tests modified to cover all changes
  • Code conforms with the style
    guide

@slnode
Copy link

slnode commented Oct 19, 2017

Can one of the admins verify this patch?

2 similar comments
@slnode
Copy link

slnode commented Oct 19, 2017

Can one of the admins verify this patch?

@slnode
Copy link

slnode commented Oct 19, 2017

Can one of the admins verify this patch?

@Traksewt
Copy link
Contributor Author

recreated #421 to be based on master branch

@bajtos
Copy link
Member

bajtos commented Oct 19, 2017

@Traksewt great, thank you for the updated patch. I'm short on time today and off work tomorrow, I'll take a look next week.

@bajtos
Copy link
Member

bajtos commented Oct 19, 2017

@slnode ok to test

@bajtos bajtos self-assigned this Oct 19, 2017
@Traksewt
Copy link
Contributor Author

@bajtos I'm not sure how to fix the PR Builder as I don't have access to see the Details on the build machine.

@bajtos
Copy link
Member

bajtos commented Oct 27, 2017

Hey, sorry for the delay, I am going to review this right now.

I'm not sure how to fix the PR Builder as I don't have access to see the Details on the build machine.

Unfortunately those CI machines are accessible only from the internal IBM network :(

The error seems unrelated to your changes to me, I think we can safely ignore it.

Installing dependencies in preparation for tests and possibly publishing...
+ npm install --ignore-scripts
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@^1.0.0 (node_modules/chokidar/node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for [email protected]: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})
npm ERR! Linux 4.4.0-89-generic
npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "install" "--ignore-scripts"
npm ERR! node v6.11.1
npm ERR! npm  v3.10.10
npm ERR! code E404

npm ERR! 404 no such package available : restore-cursor
npm ERR! 404 
npm ERR! 404  'restore-cursor' is not in the npm registry.
npm ERR! 404 You should bug the author to publish it (or use the name yourself!)
npm ERR! 404 It was specified as a dependency of 'cli-cursor'
npm ERR! 404 
npm ERR! 404 Note that you can also install from a
npm ERR! 404 tarball, folder, http url, or git url.

cc @rmg is this a known problem of our CI?

@bajtos
Copy link
Member

bajtos commented Oct 27, 2017

The changes look good to me at high level. I cleaned the code a bit for you, PTAL at b7c3564 and let me know if this is ok with you.

Copy link
Member

@bajtos bajtos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See my comments below for an explanation of the changes I made.

In addition to that, I changed var to let and const per our style guide (http://loopback.io/doc/en/contrib/style-guide.html#variable-declarations) and reworked test functions to use () => { notation.

}
} else if (auth.accessToken) {
req.headers = req.headers || {};
req.headers.Authorization = auth.accessToken.id;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find this structure difficult to comprehend, there are too many different code paths possible. I reworked it by moving the branch where we are setting req.headers to the top and preserving the old code mostly intact.

// check for the options to pass along to the rest endpoint.
// It may have the access token that can be used
if (auth || !invokeOptions) return auth;
if (this.options && this.options.passAccessToken && invokeOptions &&
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of checking if this.options is set everywhere, it's usually easier to ensure it's at least an empty object in the constructor.

I have also rearranged the code in this function to make it easier to follow for me (hopefully it still looks clear to you too!)

* @param args
* @private
*/
RestAdapter.prototype._extractAuth = function(remotes, invokeOptions) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to pass remotes, since they are already available as this.remotes.

}, {
arg: 'argName2',
type: String,
}];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reformatted the code to follow the convention we are using in LoopBack, where each arg definition is on a single line.

[
 {arg: 'argName1', /*...*/},
 {arg: 'argName2', /*...*/},
]

I have also renamed the variable to make the intent more clear.

it('should find the first arg', function() {
var method = givenRestStaticMethod({accepts: anArg});
expect(method.getArgByName('argName1',
['firstArg', 'secondArg'])).to.equal('firstArg');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Breaking the line in the middle of argument list make the code difficult to follow. Here is a better solution that we are already using in other parts of our codebase:

expect(method.getArgByName('argName1', ['firstArg', 'secondArg']))
  .to.equal('firstArg');

@Traksewt
Copy link
Contributor Author

thanks @bajtos looks good. Good to hear we can use let & const. I was used to the 2.x codebase.

Allow the rest connector to call other loopback services by passing
on the user authorization from the options where available.
This is the fallback if no other authorization is specified.

This allows loopback servers to communicate securely with other loopback
servers by passing on the auth token. Useful for using loopback as
microservices.

This new options is disabled by default, it can be enabled by adding
the following "options" to the datasource configuration:

    {
      "connector": "remote",
      // ...
      "options": {
        "passAccessToken": "true"
      }
    }
@bajtos bajtos force-pushed the allow-access-token branch from 4a3cbef to e8ef11e Compare December 5, 2017 13:30
@bajtos
Copy link
Member

bajtos commented Dec 5, 2017

[cis-jenkins] x64 && linux && nvm,4 — Failed! (e8ef11e)

This failure is unrelated to the changes made by this patch. I am going to ignore it.

@bajtos bajtos changed the title Allow access token Add support for pass-through authentication Dec 5, 2017
@bajtos bajtos merged commit c8fe671 into strongloop:master Dec 5, 2017
@bajtos
Copy link
Member

bajtos commented Dec 5, 2017

Landed, thank you for the contribution!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants