Skip to content

Commit

Permalink
Merge pull request #20 from hull/fix/account-and-user-sub-token
Browse files Browse the repository at this point in the history
use asUser with id in token when subjectType is not a user
  • Loading branch information
michaloo authored Apr 18, 2017
2 parents db3a4b7 + 1703e63 commit 6175abb
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/lib/crypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ module.exports = {
_.reduce(objectClaims, (c, oClaims, objectType) => {
if (_.isObject(oClaims) && !_.isEmpty(oClaims)) {
c[`io.hull.as${_.upperFirst(objectType)}`] = oClaims;
} else if (_.isString(oClaims) && !_.isEmpty(oClaims) && objectType !== subjectType) {
c[`io.hull.as${_.upperFirst(objectType)}`] = { id: oClaims };
}
return c;
}, claims);
Expand Down
19 changes: 18 additions & 1 deletion tests/client-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,23 @@ describe("Hull", () => {
.that.eql({ email: "foo@bar.com" });
});

it("should allow to link a user using its id to an account", () => {
const hull = new Hull({ id: "562123b470df84b740000042", secret: "1234", organization: "test" });

const scoped = hull.asUser("1234").account({ domain: "hull.io" });
const scopedJwtClaims = jwt.decode(scoped.configuration().accessToken, scoped.configuration().secret);

expect(scopedJwtClaims)
.to.have.property("io.hull.subjectType")
.that.eql("account");
expect(scopedJwtClaims)
.to.have.property("io.hull.asAccount")
.that.eql({ domain: "hull.io" });
expect(scopedJwtClaims)
.to.have.property("io.hull.asUser")
.that.eql({ id: "1234" });
});

it("should allow to resolve an existing account user is linked to", () => {
const hull = new Hull({ id: "562123b470df84b740000042", secret: "1234", organization: "test" });

Expand Down Expand Up @@ -126,6 +143,6 @@ describe("Hull", () => {
.to.not.throw(Error);
expect(hull.asAccount.bind(null, { external_id: "1234" }))
.to.not.throw(Error);
})
});
});
});

0 comments on commit 6175abb

Please sign in to comment.