Skip to content

Instantly share code, notes, and snippets.

@thomsbg
Last active February 26, 2020 21:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thomsbg/8b35e185967e8b03c248113f8ce10ed5 to your computer and use it in GitHub Desktop.
Save thomsbg/8b35e185967e8b03c248113f8ce10ed5 to your computer and use it in GitHub Desktop.
ApiClient plain class
class ApiClient {
constructor(jwt) {
// parse + verify jwt
// set properties based on jwt claims
}
user() {
if (this.user_id) {
return towerRequest(USER_INFO, { user_id: this.user_id });
} else {
return null;
}
}
rolesFor(context) {
// filter roles in jwt to those that apply to a given context
// (organization, network, community, etc)
}
permittedTo(operation, context) {
return this.rolesFor(context).some(role => {
roleConfig[role].operations.include(operation);
});
}
}
{
"aud": "https://api.{org}.usechorus.com",
"iss": "https://login.usechorus.com",
"full_name": "Blake Thomson",
"https://usechorus.com/user_id": 12345,
"https://usechorus.com/roles": {
"global": [
"role_a",
"role_b",
],
"network:123": [
"role_f"
],
"community:456": [
"role_c",
"role_d"
],
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment