HEX
Server: LiteSpeed
System: Linux houston.panomity.com 6.8.0-100-generic #100-Ubuntu SMP PREEMPT_DYNAMIC Tue Jan 13 16:40:06 UTC 2026 x86_64
User: nudepix (1011)
PHP: 7.4.33
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: //opt/agentcloud/webapp/src/db/credential.ts
'use strict';

import * as db from 'db/index';
import toObjectId from 'misc/toobjectid';
import { ObjectId } from 'mongodb';
import { CredentialType } from 'struct/credential';
import { InsertResult } from 'struct/db';

export type TokenExchangeMethod = 'post' | 'basic';

export type Credential = {
	_id?: ObjectId;
	orgId: ObjectId;
	teamId: ObjectId;
	type: CredentialType;
	credentials: {
		key?: string;
		endpointURL?: string;
		clientId?: string;
		clientSecret?: string;
		authURL?: string;
		tokenURL?: string;
		scope?: string;
		tokenExchangeMethod?: TokenExchangeMethod;
	};
    name: string;
    createdDate: Date;
}

export function CredentialCollection(): any {
	return db.db().collection('credentials');
}

export function getCredentialById(teamId: db.IdOrStr, credentialId: db.IdOrStr): Promise<Credential> {
	return CredentialCollection().findOne({
		_id: toObjectId(credentialId),
		teamId: toObjectId(teamId),
	}, {
		projection: {
			'credentials.key': 0,
		}
	});
}

export function getCredentialsByTeam(teamId: db.IdOrStr): Promise<Credential> {
	return CredentialCollection().find({
		teamId: toObjectId(teamId),
	}, {
		projection: {
			'credentials.key': 0,
		}
	}).toArray();
}

export function getCredentialsById(teamId: db.IdOrStr, credentialIds: db.IdOrStr[]): Promise<Credential[]> {
	return CredentialCollection().find({
		_id: {
			$in: credentialIds.map(toObjectId),
		},
		teamId: toObjectId(teamId),
	}, {
		projection: {
			'credentials.key': 0,
		}
	}).toArray();
}

export async function addCredential(credential: Credential): Promise<InsertResult> {
	return CredentialCollection().insertOne(credential);
}

export function deleteCredentialById(teamId: db.IdOrStr, credentialId: db.IdOrStr): Promise<any> {
	return CredentialCollection().deleteOne({
		_id: toObjectId(credentialId),
		teamId: toObjectId(teamId),
	});
}