mirror of
https://github.com/wassname/talk.git
synced 2026-09-11 12:51:33 +08:00
[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:
@@ -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();
|
||||
});
|
||||
@@ -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
Reference in New Issue
Block a user