-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7dcf60c
commit dc46e96
Showing
37 changed files
with
551 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,49 @@ | ||
import { sign } from "jsonwebtoken"; | ||
import { adminGraphQLClient as adminClient } from "../../hasura"; | ||
import { gql } from "graphql-request"; | ||
import { $admin } from "../../client"; | ||
import { User, Role } from "@opensystemslab/planx-core/types"; | ||
|
||
export const buildJWT = async (email: string | undefined) => { | ||
const { users } = await adminClient.request( | ||
gql` | ||
query ($email: String!) { | ||
users(where: { email: { _eq: $email } }, limit: 1) { | ||
id | ||
} | ||
} | ||
`, | ||
{ email }, | ||
); | ||
|
||
if (!users.length) return; | ||
|
||
const { id } = users[0]; | ||
|
||
const hasura = { | ||
"x-hasura-allowed-roles": ["platformAdmin", "public"], | ||
"x-hasura-default-role": "platformAdmin", | ||
"x-hasura-user-id": id.toString(), | ||
}; | ||
export const buildJWT = async (email: string): Promise<string | undefined> => { | ||
const user = await $admin.user.getByEmail(email); | ||
if (!user) return; | ||
|
||
const data = { | ||
sub: id.toString(), | ||
"https://hasura.io/jwt/claims": hasura, | ||
sub: user.id.toString(), | ||
"https://hasura.io/jwt/claims": generateHasuraClaimsForUser(user), | ||
}; | ||
|
||
const jwt = sign(data, process.env.JWT_SECRET!); | ||
return jwt; | ||
}; | ||
|
||
const generateHasuraClaimsForUser = (user: User) => ({ | ||
"x-hasura-allowed-roles": getAllowedRolesForUser(user), | ||
"x-hasura-default-role": getDefaultRoleForUser(user), | ||
"x-hasura-user-id": user.id.toString(), | ||
}); | ||
|
||
/** | ||
* Get all possible roles for this user | ||
* Requests made outside this scope will not be authorised by Hasura | ||
*/ | ||
const getAllowedRolesForUser = (user: User): Role[] => { | ||
const teamRoles = user.teams.map((teamRole) => teamRole.role); | ||
const allowedRoles: Role[] = [ | ||
"public", // Allow public access | ||
"teamEditor", // Least privileged role for authenticated users - required for Editor access | ||
...teamRoles, // User specific roles | ||
]; | ||
if (user.isPlatformAdmin) allowedRoles.push("platformAdmin"); | ||
|
||
return [...new Set(allowedRoles)]; | ||
}; | ||
|
||
/** | ||
* The default role is used for all requests | ||
* Can be overwritten on a per-request basis in the client using the x-hasura-role header | ||
* set to a role in the x-hasura-allowed-roles list | ||
* | ||
* This is the role of least privilege for the user | ||
*/ | ||
const getDefaultRoleForUser = (user: User): Role => { | ||
return user.isPlatformAdmin ? "platformAdmin" : "teamEditor"; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.