Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions peopleportal/server/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ import { logger, requestLogger } from "./utils/logger";
import { agendaClient } from "./clients/AgendaClient";
import { authenticateGiteaWebhook } from "./utils/gitea-webhook-auth";
import { describeUnknownError } from "./utils/errors";
import { portfolioPageHandler } from "./utils/portfolio-share";
import { loadPublicPortfolio } from "./utils/portfolio-loader";

assertRequiredEnvironment();
logger.info(
Expand Down Expand Up @@ -147,6 +149,22 @@ app.get(["/apply", "/apply/*splat"], (req, res) => {
res.sendFile(path.join(__dirname, "ui", "index.html"));
});

/* Member portfolios (#150) are public, so they skip the login redirect below.
The page itself only shows what /api/portfolio returns for opted-in members,
and the served HTML carries share-preview tags only for those members. */
app.get(
"/u/:username",
portfolioPageHandler({
indexHtmlPath: path.join(__dirname, "ui", "index.html"),
baseUrl: process.env.PEOPLEPORTAL_BASE_URL,
load: (username) =>
loadPublicPortfolio(new AuthentikClient(), username),
}),
);
app.get("/u/*splat", (req, res) => {
res.sendFile(path.join(__dirname, "ui", "index.html"));
});

app.use(express.static(path.join(__dirname, "ui"), { index: false }));

app.get("*splat", NativeExpressOIDCAuthPort, (req, res) => {
Expand Down
69 changes: 69 additions & 0 deletions peopleportal/server/src/clients/AuthentikClient/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1374,6 +1374,75 @@ export class AuthentikClient {
}
};

/**
* Looks up an onboarded People Portal user by exact username. Unlike
* getUserInfoFromEmail, a missing user surfaces as USER_NOT_FOUND rather
* than a generic failure, so a caller can answer "not found" without
* mistaking an Authentik outage for an absent user.
*
* @param username Exact People Portal username
* @returns User Information
*/
public getUserInfoFromUsername = async (
username: string,
): Promise<UserInformationBrief> => {
const ONBOARDED_USER_PATH = "peopleportal.atheesh.org/onboardeduser";
var RequestConfig: any = {
...this.AxiosBaseConfig,
method: "get",
url: `/api/v3/core/users/`,
params: {
username,
path: ONBOARDED_USER_PATH,
type: "internal",
},
};

let users: any[];
try {
const res = await axios.request(RequestConfig);
users = res.data.results;
} catch (e) {
log.error(
AuthentikClient.TAG,
"Get User by Username Request Failed with Error: ",
e,
);
throw new AuthentikClientError(
AuthentikClientErrorType.USERINFO_REQUEST_FAILED,
);
}

/* Filter for Exact Match; never trust the API filters alone */
const exactMatches = users.filter(
(u: any) =>
u.username === username && u.path === ONBOARDED_USER_PATH,
);

if (exactMatches.length < 1)
throw new AuthentikClientError(
AuthentikClientErrorType.USER_NOT_FOUND,
);

if (exactMatches.length > 1)
throw new AuthentikClientError(
AuthentikClientErrorType.MULTIPLE_RESULTS_RETURNED,
);

const user = exactMatches[0];
return {
pk: user.pk,
username: user.username,
name: user.name,
email: user.email,
memberSince: user.date_joined,
active: user.is_active,
attributes: user.attributes,
is_superuser: user.is_superuser,
avatar: user.avatar,
};
};

public createNewUser = async (
request: CreateUserRequest,
): Promise<boolean> => {
Expand Down
50 changes: 50 additions & 0 deletions peopleportal/server/src/clients/AuthentikClient/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,56 @@ export interface UserAttributeDefinition {
[key: string]: string;
};
avatar?: string;
/* Absent means the member never opted in, so the portfolio is private */
portfolio?: PortfolioSettings;
/* Roles the member held in teams they have since left (#150) */
roleHistory?: RoleHistoryEntry[];
/* Recognition granted by team leads or admins (#150) */
awards?: MemberAward[];
}

/* Member portfolio (#150). Stored on the user so it needs no new collection. */
export interface PortfolioSettings {
isPublic: boolean;
/* Role ids left off the public page: a current membership's group pk
(the key of `roles`) or a role history entry id */
hiddenRoleIds: string[];
/* Award ids left off the public page */
hiddenAwardIds: string[];
/* Member-written, shown apart from the verified record. Plain text. */
headline?: string;
githubUrl?: string;
linkedinUrl?: string;
}

/**
* A role kept after the member left the team. Copied from the team at the
* moment of removal, so it survives the team being renamed or deleted.
*/
export interface RoleHistoryEntry {
id: string;
teamPk: string;
teamName: string;
subteamName?: string;
roleTitle: string;
teamType: TeamType;
seasonType: SeasonType | ServiceSeasonType;
seasonYear: number;
/* ISO date the membership ended: the end marker */
endedAt: string;
}

/* Granted and revoked only by leads or admins; members can only hide one */
export interface MemberAward {
id: string;
title: string;
reason?: string;
/* YYYY-MM-DD */
awardedOn: string;
/* Root team the award was granted through, which may revoke it */
teamPk: string;
grantedByPk: number;
grantedAt: string;
}

export interface AuthentikFilterCursor {
Expand Down
27 changes: 26 additions & 1 deletion peopleportal/server/src/controllers/OrgController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ import {
describeUnknownError,
} from "../utils/errors";
import { ExpressRequestBindleExtension } from "../types/express";
import { appendRoleHistory, makeRoleHistoryEntry } from "../utils/portfolio";
import {
validateS3FileSignature,
FILE_SIGNATURES,
Expand Down Expand Up @@ -3177,15 +3178,39 @@ export class OrgController extends Controller {
teamName = parentInfo.attributes.friendlyName ?? parentInfo.name;
}

/* Remove the Role Attribute from Authentik if it exists */
/* Remove the Role Attribute from Authentik if it exists, keeping a
copy in the member's role history so the role stays on their
portfolio (#150) after they leave. Written in the same update. */
if (
userInfo.attributes.roles &&
userInfo.attributes.roles[request.groupId]
) {
const updatedRoles = { ...userInfo.attributes.roles };
delete updatedRoles[request.groupId];

const historyEntry = makeRoleHistoryEntry({
teamPk: request.groupId,
rootTeamName: teamName,
subteamName: groupInfo.parentPk
? groupInfo.attributes.friendlyName
: undefined,
roleTitle: userInfo.attributes.roles[request.groupId],
teamType: groupInfo.attributes.teamType,
seasonType: groupInfo.attributes.seasonType,
seasonYear: groupInfo.attributes.seasonYear,
endedAt: new Date(),
});

await this.authentikClient.updateUserAttributes(request.userPk, {
roles: updatedRoles,
...(historyEntry
? {
roleHistory: appendRoleHistory(
userInfo.attributes.roleHistory,
historyEntry,
),
}
: {}),
});
}

Expand Down
Loading
Loading