| 25 | } |
| 26 | |
| 27 | func (s *Store) CreateOAuth2Client(ctx context.Context, create *OAuth2ClientMessage) (*OAuth2ClientMessage, error) { |
| 28 | configBytes, err := protojson.Marshal(create.Config) |
| 29 | if err != nil { |
| 30 | return nil, errors.Wrap(err, "failed to marshal config") |
| 31 | } |
| 32 | |
| 33 | var workspaceArg any |
| 34 | if create.Workspace != "" { |
| 35 | workspaceArg = create.Workspace |
| 36 | } |
| 37 | |
| 38 | q := qb.Q().Space(` |
| 39 | INSERT INTO oauth2_client (client_id, workspace, client_secret_hash, config, last_active_at) |
| 40 | VALUES (?, ?, ?, ?, NOW()) |
| 41 | RETURNING last_active_at |
| 42 | `, create.ClientID, workspaceArg, create.ClientSecretHash, configBytes) |
| 43 | |
| 44 | query, args, err := q.ToSQL() |
| 45 | if err != nil { |
| 46 | return nil, err |
| 47 | } |
| 48 | |
| 49 | if err := s.GetDB().QueryRowContext(ctx, query, args...).Scan(&create.LastActiveAt); err != nil { |
| 50 | return nil, errors.Wrap(err, "failed to create OAuth2 client") |
| 51 | } |
| 52 | return create, nil |
| 53 | } |
| 54 | |
| 55 | // GetOAuth2Client looks up a client by client_id (the table's primary key). |
| 56 | // Workspace is no longer a lookup key — clients are workspace-agnostic since |