Skip to content
Closed
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
4 changes: 2 additions & 2 deletions packages/components/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/components/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@labkey/components",
"version": "7.45.2",
"version": "7.45.3-fb-role-restricted-api-keys.2",
"description": "Components, models, actions, and utility functions for LabKey applications and pages",
"sideEffects": false,
"files": [
Expand Down
4 changes: 4 additions & 0 deletions packages/components/releaseNotes/components.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# @labkey/components
Components, models, actions, and utility functions for LabKey applications and pages

### version 7.45.3
*Released*: TBD
- Add role restriction option to API key dialog

### version 7.45.2
*Released*: 29 June 2026
- GitHub Issue #1023: Add redirect() helper that uses core-safeRedirect when necessary to check url before redirecting
Expand Down
20 changes: 17 additions & 3 deletions packages/components/src/internal/components/security/APIWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ export interface RemoveGroupMembersResponse {
removed: number[];
}

export interface ApiKeyRole {
displayName: string;
uniqueName: string;
}

export interface AuthenticationConfiguration {
description: string;
reauthUrl: string;
Expand All @@ -68,7 +73,8 @@ interface AuthenticationConfigurationResponse {

export interface SecurityAPIWrapper {
addGroupMembers: (groupId: number, principalIds: number[], projectPath: string) => Promise<AddGroupMembersResponse>;
createApiKey: (type?: string, description?: string) => Promise<string>;
createApiKey: (type?: string, description?: string, role?: string) => Promise<string>;
getApiKeyRoles: () => Promise<ApiKeyRole[]>;
createGroup: (groupName: string, projectPath: string) => Promise<Security.CreateGroupResponse>;
deleteApiKeys: (selections: Set<string>) => Promise<QueryCommandResponse>;
deleteContainer: (options: DeleteContainerOptions) => Promise<Record<string, unknown>>;
Expand Down Expand Up @@ -133,17 +139,24 @@ export class ServerSecurityAPIWrapper implements SecurityAPIWrapper {
});
};

createApiKey = async (type = 'apikey', description?: string): Promise<string> => {
createApiKey = async (type = 'apikey', description?: string, role?: string): Promise<string> => {
const response = await request<{ apikey: string }>({
url: ActionURL.buildURL('security', 'createApiKey.api'),
method: 'POST',
jsonData: { type, description },
jsonData: { type, description, role },
errorLogMsg: 'Problem generating the apiKey for this user.',
});

return response.apikey;
};

getApiKeyRoles = (): Promise<ApiKeyRole[]> => {
return request<ApiKeyRole[]>({
url: ActionURL.buildURL('security', 'getApiKeyRoles.api'),
errorLogMsg: 'Failed to load API key roles.',
});
};

deleteApiKeys(selections: Set<string>): Promise<QueryCommandResponse> {
const rows = [];
selections.forEach(selection => {
Expand Down Expand Up @@ -453,6 +466,7 @@ export function getSecurityTestAPIWrapper(
return {
addGroupMembers: mockFn(),
createApiKey: mockFn(),
getApiKeyRoles: mockFn(),
deleteApiKeys: mockFn(),
createGroup: mockFn(),
deleteContainer: mockFn(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ describe('KeyGeneratorModal', () => {
api: {
security: {
createApiKey: apiKeyFn,
getApiKeyRoles: jest.fn().mockResolvedValue([]),
},
},
},
Expand Down
35 changes: 33 additions & 2 deletions packages/components/src/internal/components/user/APIKeysPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import { GridPanel } from '../../../public/QueryModel/GridPanel';
import { Modal } from '../../Modal';
import { getHelpLink, HelpLink } from '../../util/helpLinks';
import { biologicsIsPrimaryApp } from '../../app/products';
import { ApiKeyRole } from '../security/APIWrapper';

const API_KEYS_QUERY_HREF = ActionURL.buildURL('query', 'executeQuery.view', '/', {
schemaName: 'core',
Expand Down Expand Up @@ -101,19 +102,21 @@ interface ModalProps extends KeyGeneratorProps {
export const KeyGeneratorModal: FC<ModalProps> = props => {
const { type, afterCreate, noun, onClose } = props;
const [description, setDescription] = useState<string>();
const [role, setRole] = useState<string>();
const [roles, setRoles] = useState<ApiKeyRole[]>([]);
const { api } = useAppContext<AppContext>();
const [error, setError] = useState<boolean>(false);
const [keyValue, setKeyValue] = useState<string>(undefined);

const onGenerateKey = useCallback(async () => {
try {
const key = await api.security.createApiKey(type, description);
const key = await api.security.createApiKey(type, description, role);
setKeyValue(key);
afterCreate?.();
} catch (e) {
setError(true);
}
}, [api.security, type, afterCreate, description]);
}, [api.security, type, afterCreate, description, role]);

useEffect(() => {
(async () => {
Expand All @@ -123,6 +126,12 @@ export const KeyGeneratorModal: FC<ModalProps> = props => {
})();
}, [type, onGenerateKey]);

useEffect(() => {
if (type === 'apikey') {
api.security.getApiKeyRoles().then(setRoles).catch(() => {});
}
}, [api.security, type]);

const onCopyKey = useCallback(() => {
const handleCopy = (event: ClipboardEvent): void => {
setCopyValue(event, keyValue);
Expand All @@ -137,6 +146,10 @@ export const KeyGeneratorModal: FC<ModalProps> = props => {
setDescription(event.target.value);
}, []);

const changeRole = useCallback((event: ChangeEvent<HTMLSelectElement>) => {
setRole(event.target.value || undefined);
}, []);

return (
<Modal
title={noun}
Expand All @@ -157,6 +170,24 @@ export const KeyGeneratorModal: FC<ModalProps> = props => {
autoFocus
placeholder="Enter description of key usage (optional)"
/>
{roles.length > 0 && (
<div className="top-padding">
<label htmlFor="keyRole">Restrict Permissions</label>
<select
className="form-control"
id="keyRole"
onChange={changeRole}
value={role ?? ''}
>
<option value="">No Restrictions</option>
{roles.map(r => (
<option key={r.uniqueName} value={r.uniqueName}>
{r.displayName}
</option>
))}
</select>
</div>
)}
</div>
)}
{!!keyValue && (
Expand Down