Skip to content

Commit

Permalink
Handle comma separated allowed roles (#391)
Browse files Browse the repository at this point in the history
* fix: Fix issue where hasura-auth crashes when adding allowed roles upon signin with provider

* chore: Run prettier

* chore: Update .changeset/strange-seas-whisper.md

Co-authored-by: Szilárd Dóró <[email protected]>

---------

Co-authored-by: Szilárd Dóró <[email protected]>
  • Loading branch information
iangabrielsanchez and szilarddoro authored May 31, 2023
1 parent fe87694 commit 0cc9d36
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/strange-seas-whisper.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'hasura-auth': patch
---

fix: don't crash when adding allowed roles upon sign-in with a provider
19 changes: 14 additions & 5 deletions src/routes/oauth/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,27 @@ export const transformOauthProfile = async (

const emailVerified = !!normalised.emailVerified;

let allowedRoles: string[] = ENV.AUTH_USER_DEFAULT_ALLOWED_ROLES;

if (options?.allowedRoles) {
if (Array.isArray(options.allowedRoles)) {
allowedRoles = options.allowedRoles;
} else if (typeof options.allowedRoles === 'string') {
//if for some reason it comes as a string, split it
allowedRoles = (options.allowedRoles as string).split(',');
}
}

return {
passwordHash: null,
metadata: options?.metadata || {},
email,
emailVerified,
defaultRole: options?.defaultRole || ENV.AUTH_USER_DEFAULT_ROLE,
roles: {
data: (options?.allowedRoles || ENV.AUTH_USER_DEFAULT_ALLOWED_ROLES).map(
(role) => ({
role,
})
),
data: allowedRoles.map((role) => ({
role,
})),
},
locale,
displayName,
Expand Down
46 changes: 46 additions & 0 deletions test/oauth/__snapshots__/transform-profile.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,51 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`OAuth helpers should handle an array of allowed roles 1`] = `
Object {
"avatarUrl": "https://platform-lookaside.fbsbx.com/platform/profilepic/?asid=1234567890123456&height=50&width=50&ext=1234567894&hash=Qdiofewu-OPO",
"defaultRole": "user",
"displayName": "Bob Smith",
"email": "[email protected]",
"emailVerified": false,
"locale": "en",
"metadata": Object {},
"passwordHash": null,
"roles": Object {
"data": Array [
Object {
"role": "user",
},
Object {
"role": "me",
},
],
},
}
`;

exports[`OAuth helpers should handle comma separated allowedRoles 1`] = `
Object {
"avatarUrl": "https://platform-lookaside.fbsbx.com/platform/profilepic/?asid=1234567890123456&height=50&width=50&ext=1234567894&hash=Qdiofewu-OPO",
"defaultRole": "user",
"displayName": "Bob Smith",
"email": "[email protected]",
"emailVerified": false,
"locale": "en",
"metadata": Object {},
"passwordHash": null,
"roles": Object {
"data": Array [
Object {
"role": "user",
},
Object {
"role": "me",
},
],
},
}
`;

exports[`OAuth helpers should transform a Facebook profile 1`] = `
Object {
"avatarUrl": "https://platform-lookaside.fbsbx.com/platform/profilepic/?asid=1234567890123456&height=50&width=50&ext=1234567894&hash=Qdiofewu-OPO",
Expand Down
46 changes: 46 additions & 0 deletions test/oauth/transform-profile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,50 @@ describe('OAuth helpers', () => {
const output = await transformOauthProfile(normalisedProfile);
expect(output).toMatchSnapshot();
});

it('should handle an array of allowed roles', async () => {
const facebookProfile = {
id: '1234567890123456',
name: 'Bob Smith',
email: '[email protected]',
picture: {
data: {
height: 50,
is_silhouette: false,
url: 'https://platform-lookaside.fbsbx.com/platform/profilepic/?asid=1234567890123456&height=50&width=50&ext=1234567894&hash=Qdiofewu-OPO',
width: 50,
},
},
};
const normalisedProfile = await normaliseProfile('facebook', {
profile: facebookProfile,
});
const output = await transformOauthProfile(normalisedProfile, {
allowedRoles: ['user', 'me'],
});
expect(output).toMatchSnapshot();
});

it('should handle comma separated allowedRoles', async () => {
const facebookProfile = {
id: '1234567890123456',
name: 'Bob Smith',
email: '[email protected]',
picture: {
data: {
height: 50,
is_silhouette: false,
url: 'https://platform-lookaside.fbsbx.com/platform/profilepic/?asid=1234567890123456&height=50&width=50&ext=1234567894&hash=Qdiofewu-OPO',
width: 50,
},
},
};
const normalisedProfile = await normaliseProfile('facebook', {
profile: facebookProfile,
});
const output = await transformOauthProfile(normalisedProfile, {
allowedRoles: 'user,me' as any,
});
expect(output).toMatchSnapshot();
});
});

0 comments on commit 0cc9d36

Please sign in to comment.