MCPcopy Index your code
hub / github.com/coder/coder / APIKey

Function APIKey

coderd/database/dbgen/dbgen.go:522–567  ·  view source on GitHub ↗
(t testing.TB, db database.Store, seed database.APIKey, munge ...func(*database.InsertAPIKeyParams))

Source from the content-addressed store, hash-verified

520}
521
522func APIKey(t testing.TB, db database.Store, seed database.APIKey, munge ...func(*database.InsertAPIKeyParams)) (key database.APIKey, token string) {
523 id, _ := cryptorand.String(10)
524 secret, hashed, err := apikey.GenerateSecret(22)
525 require.NoError(t, err)
526
527 ip := seed.IPAddress
528 if !ip.Valid {
529 ip = pqtype.Inet{
530 IPNet: net.IPNet{
531 IP: net.IPv4(127, 0, 0, 1),
532 Mask: net.IPv4Mask(255, 255, 255, 255),
533 },
534 Valid: true,
535 }
536 }
537
538 // It does not make sense for the created_at to be after the expires_at.
539 // So if expires is set, change the default created_at to be 24 hours before.
540 var createdAt time.Time
541 if !seed.ExpiresAt.IsZero() && seed.CreatedAt.IsZero() {
542 createdAt = seed.ExpiresAt.Add(-24 * time.Hour)
543 }
544
545 params := database.InsertAPIKeyParams{
546 ID: takeFirst(seed.ID, id),
547 // 0 defaults to 86400 at the db layer
548 LifetimeSeconds: takeFirst(seed.LifetimeSeconds, 0),
549 HashedSecret: takeFirstSlice(seed.HashedSecret, hashed),
550 IPAddress: ip,
551 UserID: takeFirst(seed.UserID, uuid.New()),
552 LastUsed: takeFirst(seed.LastUsed, dbtime.Now()),
553 ExpiresAt: takeFirst(seed.ExpiresAt, dbtime.Now().Add(time.Hour)),
554 CreatedAt: takeFirst(seed.CreatedAt, createdAt, dbtime.Now()),
555 UpdatedAt: takeFirst(seed.UpdatedAt, dbtime.Now()),
556 LoginType: takeFirst(seed.LoginType, database.LoginTypePassword),
557 Scopes: takeFirstSlice([]database.APIKeyScope(seed.Scopes), []database.APIKeyScope{database.ApiKeyScopeCoderAll}),
558 AllowList: takeFirstSlice(seed.AllowList, database.AllowList{{Type: policy.WildcardSymbol, ID: policy.WildcardSymbol}}),
559 TokenName: takeFirst(seed.TokenName),
560 }
561 for _, fn := range munge {
562 fn(&params)
563 }
564 key, err = db.InsertAPIKey(genCtx, params)
565 require.NoError(t, err, "insert api key")
566 return key, fmt.Sprintf("%s-%s", key.ID, secret)
567}
568
569func WorkspaceAgentPortShare(t testing.TB, db database.Store, orig database.WorkspaceAgentPortShare) database.WorkspaceAgentPortShare {
570 ps, err := db.UpsertWorkspaceAgentPortShare(genCtx, database.UpsertWorkspaceAgentPortShareParams{

Callers 15

TestUserLogoutFunction · 0.92
TestCustomNotificationFunction · 0.92
TestAPIKeyFunction · 0.92
TestTaskParamFunction · 0.92
TestOrganizationParamFunction · 0.92
TestRateLimitFunction · 0.92
TestChatParamFunction · 0.92
TestUserParamFunction · 0.92
TestWorkspaceBuildParamFunction · 0.92

Calls 9

StringFunction · 0.92
GenerateSecretFunction · 0.92
NowFunction · 0.92
takeFirstSliceFunction · 0.85
takeFirstFunction · 0.70
AddMethod · 0.65
NewMethod · 0.65
InsertAPIKeyMethod · 0.65
IsZeroMethod · 0.45

Tested by 15

TestUserLogoutFunction · 0.74
TestCustomNotificationFunction · 0.74
TestAPIKeyFunction · 0.74
TestTaskParamFunction · 0.74
TestOrganizationParamFunction · 0.74
TestRateLimitFunction · 0.74
TestChatParamFunction · 0.74
TestUserParamFunction · 0.74
TestWorkspaceBuildParamFunction · 0.74