[CORL-437] SSO Token Documentation + Updates (#2390)

* feat: updated README, added more SSO functionality

* fix: lint

* fix: lint

* fix: lint

* fix: typos
This commit is contained in:
Wyatt Johnson
2019-07-05 21:49:41 +00:00
committed by GitHub
parent 0754ceb803
commit da1fa9c9fc
12 changed files with 2018 additions and 1807 deletions
@@ -0,0 +1,48 @@
import { LocalProfile, SSOProfile } from "coral-server/models/user";
import { getLocalProfile, hasLocalProfile } from "./helpers";
const localProfile: LocalProfile = {
type: "local",
id: "hans@email.com",
password: "secret",
};
const ssoProfile: SSOProfile = {
type: "sso",
id: "sso-id",
lastIssuedAt: new Date(0),
};
it("will get the local profile", () => {
expect(getLocalProfile({ profiles: [localProfile] })).toEqual(localProfile);
});
it("will return undefined when it can't find the local profile", () => {
expect(getLocalProfile({ profiles: [ssoProfile] })).toBeUndefined();
});
it("will return true when the profile exists", () => {
expect(hasLocalProfile({ profiles: [localProfile] })).toBeTruthy();
});
it("will return true when the profile exists with the right email", () => {
expect(
hasLocalProfile({ profiles: [localProfile] }, localProfile.id)
).toBeTruthy();
});
it("will return false when the profile exists with the wrong email", () => {
expect(
hasLocalProfile({ profiles: [localProfile] }, "max@email.com")
).toBeFalsy();
});
it("will return false when the profile does not exist", () => {
expect(hasLocalProfile({ profiles: [ssoProfile] })).toBeFalsy();
});
it("will return false when the profile does not exist with an email specified", () => {
expect(
hasLocalProfile({ profiles: [ssoProfile] }, "max@email.com")
).toBeFalsy();
});
+52 -2
View File
@@ -1,7 +1,7 @@
import { GQLUSER_ROLE } from "coral-server/graph/tenant/schema/__generated__/types";
import { STAFF_ROLES } from "coral-server/models/user/constants";
import { User } from ".";
import { STAFF_ROLES } from "./constants";
import { LocalProfile, SSOProfile, User } from "./user";
export function roleIsStaff(role: GQLUSER_ROLE) {
if (STAFF_ROLES.includes(role)) {
@@ -14,3 +14,53 @@ export function roleIsStaff(role: GQLUSER_ROLE) {
export function userIsStaff(user: Pick<User, "role">) {
return roleIsStaff(user.role);
}
export function getSSOProfile(user: Pick<User, "profiles">) {
return user.profiles.find(profile => profile.type === "sso") as
| SSOProfile
| undefined;
}
export function needsSSOUpdate(
token: Pick<User, "email" | "username">,
user: Pick<User, "email" | "username">
) {
return user.email !== token.email || user.username !== token.username;
}
/**
* getLocalProfile will get the LocalProfile from the User if it exists.
*
* @param user the User to pull the LocalProfile out of
*/
export function getLocalProfile(
user: Pick<User, "profiles">
): LocalProfile | undefined {
return user.profiles.find(({ type }) => type === "local") as
| LocalProfile
| undefined;
}
/**
* hasLocalProfile will return true if the User has a LocalProfile, optionally
* checking the email on it as well.
*
* @param user the User to pull the LocalProfile out of
* @param withEmail when specified, will ensure that the LocalProfile has the
* specific email provided
*/
export function hasLocalProfile(
user: Pick<User, "profiles">,
withEmail?: string
): boolean {
const profile = getLocalProfile(user);
if (!profile) {
return false;
}
if (withEmail && profile.id !== withEmail) {
return false;
}
return true;
}
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff