MCPcopy Create free account
hub / github.com/pollinations/pollinations / createApiKeyForUser

Function createApiKeyForUser

shared/auth/api-key-creation.ts:227–326  ·  view source on GitHub ↗
({
    authClient,
    dbBinding,
    userId,
    name,
    type,
    expiresIn,
    allowedModels,
    pollenBudget,
    accountPermissions,
    metadata,
    allowAccountKeysPermission,
    defaultCreatedVia,
}: CreateApiKeyForUserInput)

Source from the content-addressed store, hash-verified

225}
226
227export async function createApiKeyForUser({
228 authClient,
229 dbBinding,
230 userId,
231 name,
232 type,
233 expiresIn,
234 allowedModels,
235 pollenBudget,
236 accountPermissions,
237 metadata,
238 allowAccountKeysPermission,
239 defaultCreatedVia,
240}: CreateApiKeyForUserInput) {
241 const db = drizzle(dbBinding, { schema });
242 const attribution = await validateClientRedirectBinding(
243 authClient,
244 db,
245 metadata,
246 );
247
248 const isPublishable = type === "publishable";
249 const callerMetadata = pickCallerMetadata(metadata, isPublishable);
250 if (Array.isArray(callerMetadata.redirectUris)) {
251 for (const uri of callerMetadata.redirectUris as string[]) {
252 validateRedirectUriFormat(uri);
253 }
254 }
255
256 const sanitizedAccountPerms =
257 sanitizeAuthorizeAccountPermissions(accountPermissions) ?? null;
258 const safeAccountPerms = allowAccountKeysPermission
259 ? sanitizedAccountPerms
260 : (sanitizedAccountPerms?.filter((p) => p !== "keys") ?? null);
261
262 const permissions: Record<string, string[]> = {};
263 if (allowedModels) permissions.models = allowedModels;
264 if (safeAccountPerms && safeAccountPerms.length > 0) {
265 permissions.account = safeAccountPerms;
266 }
267
268 const prefix = isPublishable ? "pk" : "sk";
269 const baseMetadata = {
270 ...callerMetadata,
271 keyType: type,
272 createdVia: defaultCreatedVia,
273 };
274
275 const created = await authClient.api.createApiKey({
276 body: {
277 name,
278 prefix,
279 userId,
280 ...(expiresIn != null && { expiresIn }),
281 metadata: baseMetadata,
282 permissions:
283 Object.keys(permissions).length > 0 ? permissions : undefined,
284 },

Callers 3

createTestApiKeyFunction · 0.90
api-keys.tsFile · 0.90
account.tsFile · 0.90

Calls 7

pickCallerMetadataFunction · 0.85
stringifyMethod · 0.80
updateMethod · 0.80
setMethod · 0.65

Tested by

no test coverage detected