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

Refactor authorization #3529

Merged
merged 1 commit into from
Aug 13, 2019
Merged

Refactor authorization #3529

merged 1 commit into from
Aug 13, 2019

Conversation

jannyHou
Copy link
Contributor

@jannyHou jannyHou commented Aug 9, 2019

Some refactor for #1205

It contains subsequent changes to make the e2e tests in PR loopbackio/loopback4-example-shopping#231 work.

Checklist

👉 Read and sign the CLA (Contributor License Agreement) 👈

  • npm test passes on your machine
  • New tests added or existing tests modified to cover all changes
  • Code conforms with the style guide
  • API Documentation in code was updated
  • Documentation in /docs/site was updated
  • Affected artifact templates in packages/cli were updated
  • Affected example projects in examples/* were updated

👉 Check out how to submit a PR 👈

@jannyHou
Copy link
Contributor Author

jannyHou commented Aug 9, 2019

Tests pass on my local. The CI error is same as #3520. Fixing it...

@@ -26,7 +27,7 @@ describe('Authorization', () => {
let events: string[];

before(givenApplicationAndAuthorizer);
beforeEach(givenRequestContext);
beforeEach(() => givenRequestContext());
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is this change needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@raymondfeng I am seeing this weird error...

Screen Shot 2019-08-09 at 3 29 35 PM

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh, I think that's because givenRequestContext takes an optional user argument.

@@ -161,6 +165,7 @@ describe('Authorization', () => {
__dirname,
'../../../fixtures/casbin/rbac_policy.csv',
);
return await casbin.newEnforcer(conf, policy);
const result = await casbin.newEnforcer(conf, policy);
return result;
Copy link
Contributor

Choose a reason for hiding this comment

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

return casbin.newEnforcer(conf, policy); should be good enough.

});

// retrieve it from authentication module
const user = await invocationCtx.get<{name: string}>(
Copy link
Contributor

Choose a reason for hiding this comment

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

Use UserProfile instead of {name: string}?

debug('Security context for %s', description, authorizationCtx);
let authorizers = await loadAuthorizers(
invocationCtx,
metadata.voters || [],
);

// pass `currentUser` to authorizers
Object.assign(metadata, {currentUser: user});
Copy link
Contributor

Choose a reason for hiding this comment

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

currentUser should be set to authorizationContext if necessary instead of metadata.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

#3529 is also updated accordingly.

}
}
return await next();
const result = await next();
Copy link
Contributor

Choose a reason for hiding this comment

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

return next(); should be good.

@@ -27,6 +27,7 @@
"devDependencies": {
"@loopback/build": "^2.0.3",
"@loopback/testlab": "^1.6.3",
"@loopback/authentication": "^2.1.8",
Copy link
Contributor

Choose a reason for hiding this comment

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

Please add some comments about this temporary workaround.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@raymondfeng I added an entry in the README.md file to explain the user injection.

Copy link
Contributor

Choose a reason for hiding this comment

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

BTW, let's change the initial version to be 0.0.1 so that the first release will be 0.1.0.

@jannyHou jannyHou force-pushed the cleanup/authorization branch 3 times, most recently from 5269dd3 to 968ef91 Compare August 12, 2019 19:06
@jannyHou
Copy link
Contributor Author

loopbackio/loopback4-example-shopping#231 is also updated.

email: user.email,
type: 'USER'
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Please move this inline function to the module scope.

@jannyHou jannyHou force-pushed the cleanup/authorization branch from 0b09eee to ba1e319 Compare August 13, 2019 15:12
@jannyHou jannyHou merged commit 72250ed into authorization Aug 13, 2019
@delete-merged-branch delete-merged-branch bot deleted the cleanup/authorization branch August 13, 2019 15:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants