Ops: Sign-in and Permissions
This page is for the administrators of a Source Graph instance. Every request is treated as exactly one kind of caller:
- Anonymous: no
Authorizationheader, while public reading is on. Can run read-only operations on public repositories, within the anonymous rate limit. - Signed-in user: a browser session after Google or GitHub sign-in. Can read public repositories and the private repositories they created or manage, create personal API keys and save personal GitHub or custom Git tokens.
- Administrator: a signed-in user with the administrator role. Can read every repository, register and maintain every repository and manage site settings, users and global credentials.
- Repository manager: a signed-in user listed as a manager of a repository. Can read and maintain that repository, as described in Who may register repositories.
- Personal API key (
sgk_…): for scripts and Agents acting as its owner; covered under Sessions and API keys. - Service token: the instance's service credential, seeded from
SOURCEGRAPH_API_TOKENon first start and then kept in the database, for automation you control. Grants every HTTP and MCP operation on every repository.
Which repositories each caller can see is described in Public and private repositories. Any other Authorization value is rejected with 401 AUTHENTICATION_REQUIRED; an invalid credential never silently becomes anonymous access. For how callers obtain and send each credential, see Choose a credential in Connecting.
Set up sign-in
Source Graph signs users in with Google (OpenID Connect) or GitHub (OAuth). A provider is enabled when both its client ID and client secret are set; with neither configured, the sign-in screen explains what is missing and the site stays anonymous and read-only.
| Provider | Environment variables | Callback URL | Scopes |
|---|---|---|---|
GOOGLE_CLIENT_ID, GOOGLE_CLIENT_SECRET | <origin>/sourcegraph/auth/google/callback | openid, profile, email | |
| GitHub | GITHUB_CLIENT_ID, GITHUB_CLIENT_SECRET | <origin>/sourcegraph/auth/github/callback | read:user |
- Register the callback.
<origin>is the value ofSOURCEGRAPH_ORIGIN, the public origin without a path. For Google, create an OAuth client of type Web application and add the callback as an authorized redirect URI; for GitHub, create an OAuth App and use it as the authorization callback URL. - Store the secrets in your platform's secret store and restart the service. On first start with both values of a provider, Source Graph imports them into encrypted database storage; after that the saved configuration is used even without the environment values, and database records take precedence over them.
- Sign in. The first person to sign in becomes the administrator, as described below.
Sign-in must start on the configured site address, because the provider always returns there and the login cookie belongs to one host. A sign-in started on another address, such as the platform's own hostname, is first sent to the sign-in screen on the site address; a proxy in front of the service forwards the original host in X-Forwarded-Host.
When the public origin changes, for example when the service moves behind another domain, update SOURCEGRAPH_ORIGIN and both provider callbacks together.
GitHub sign-in only identifies users; it does not grant repository access. Tokens for private repositories are configured separately for each code source, as described in Registering and Managing Repositories.
The first administrator
Users are identified by provider and that provider's stable subject ID. Matching email addresses never merge identities or grant administrator rights, so the same person signing in with Google and with GitHub becomes two separate users.
The first verified sign-in on a new instance atomically becomes the administrator. Configure one provider, complete the owner's first sign-in, and only then add a second provider or make the site public. Administrators can promote other users on the Users page; at least one enabled administrator must always remain, and administrators cannot demote themselves.
Who may sign in
The site setting Allow other users to sign in or register is off by default. While it is off, only administrators can sign in. Anyone else is refused with a 403 page that shows the configurable notice in both languages, links to the project and returns to the previous page after five seconds. Turning the setting off also cuts off existing non-administrator sessions, and their personal API keys are refused with REGISTRATION_CLOSED.
Anonymous reading is independent of this setting: visitors can still read public repositories while public reading is enabled in the site's access settings. Anonymous traffic is rate-limited; the limits are listed in Rate limits in Operations.
A separate setting, Let signed-in users register repositories, is also off by default; settings saved before it existed read as off. When it is on, any signed-in user can register a repository and becomes its manager. Managers and administrators can change that repository's settings, index and clean it up, cancel its jobs, delete it and add or remove managers, up to 50; everyone else gets 403 REPOSITORY_MANAGEMENT_DENIED. Users, site settings and global code-source tokens stay administrator-only. Repositories a user registers are fetched with that user's own code-source token first, then the site-wide one, so turning the setting on lets signed-in users register any repository the site-wide token can read.
Public and private repositories
Each repository is public or private, chosen with Public repository when it is registered or later in its settings (public_read, default true). Repositories registered before this choice existed stay public.
| Caller | Public repositories | Private repositories |
|---|---|---|
| Anonymous | Readable while public reading is on | Not visible |
| Signed-in user | Readable | Only those they created or manage |
| Administrator, signed in on the website | Readable | All |
| Personal API key | Readable | Only those its owner created or manages, even when the owner is an administrator |
| Service token | Readable | All |
A private repository a caller cannot see behaves exactly like an unregistered name: details, search, read, list, diff, plugins, prepare and the MCP tools return 404 REPOSITORY_NOT_FOUND, catalogs leave it out, and statistics omit its name. To give someone access, add them as a manager of the repository or make it public.
Registering a name that is already registered returns 409 REPOSITORY_EXISTS with the registrant in creator. The website shows who registered it and suggests asking them to add you as a manager, or a site administrator when no registrant is recorded.
Background synchronization, indexing and cleanup run for every repository regardless of visibility.
Sessions and API keys
A browser session lasts 90 days in an HTTP-only, same-site cookie scoped to /sourcegraph, marked secure on HTTPS origins. Signing in again replaces the browser's previous session.
Personal API keys are for scripts and Agents acting as you. Create them in your user settings with a name and an optional expiry; each key starts with sgk_ and can be copied later from the same page. Keys stop working when they expire, when you delete them, when your account is disabled, or when registration is closed and you are not an administrator.
A key reads what its owner can read as a signed-in user, except that an administrator's key does not see every repository: it sees public repositories and the private ones its owner created or manages. Besides reading, a key can call only index preparation, over MCP prepare or POST /sourcegraph/api/admin/v1/repo/prepare. It may prepare repositories its owner manages, or any repository it can see when its owner is currently an administrator; this is checked on every call. MCP lists the prepare tool only for keys whose owner is an administrator or manages at least one repository, refreshed within a minute. Keys are limited to 10 prepare calls a minute, as listed in Ops: Day-to-Day Operations; every other management route returns 403 ACCESS_DENIED.
The service token, set with SOURCEGRAPH_API_TOKEN and at least 32 characters long, is compared in constant time and grants full access. Treat it like a root credential and keep it out of shared configuration.
Access errors
These codes are specific to authentication and permissions. For rate limits and all other errors, see Common errors in Operations.
| Code | Status | Meaning |
|---|---|---|
AUTHENTICATION_REQUIRED | 401 | No usable credential, or an invalid, expired or disabled one |
ACCESS_DENIED | 403 | The credential is valid but not allowed here, for example a personal key on a management route other than prepare |
REGISTRATION_CLOSED | 403 | Sign-in is restricted to administrators |
REGISTRATION_DISABLED | 403 | A non-administrator tried to register a repository while user repositories are off |
REPOSITORY_MANAGEMENT_DENIED | 403 | The caller is neither a manager of this repository nor an administrator |
PREPARATION_NOT_ALLOWED | 403 | Anonymous callers can never request indexing |
REPOSITORY_NOT_FOUND | 404 | The repository is not registered, or it is private and the caller cannot see it |
REPOSITORY_EXISTS | 409 | The name is already registered; creator names the registrant to ask for access |
运维:登录与权限
本页面向 Source Graph 实例的管理员。每个请求都会被认定为恰好一类调用方:
- 匿名访客:开启公开阅读时,请求不带
Authorization头。可在匿名限流范围内,对公开仓库执行只读操作。 - 已登录用户:通过 Google 或 GitHub 登录后的浏览器会话。可读取公开仓库以及自己创建或管理的私有仓库,创建个人 API Key,并保存个人 GitHub 或自定义 Git 令牌。
- 管理员:拥有管理员角色的已登录用户。可读取所有仓库,登记和维护所有仓库,并管理站点设置、用户和全局凭据。
- 仓库管理者:被列为某个仓库管理者的已登录用户。可以读取和维护该仓库,详见 谁可以登录。
- 个人 API Key(
sgk_…):供以其所有者身份运行的脚本和 Agent 使用;详见 会话与 API Key。 - 服务令牌:实例的服务凭据,首次启动时由
SOURCEGRAPH_API_TOKEN提供初始值,之后保存在数据库中,供你掌控的自动化使用。可对所有仓库执行所有 HTTP 和 MCP 操作。
每类调用方能看到哪些仓库,见 公开仓库与私有仓库。其他任何 Authorization 值都会被拒绝并返回 401 AUTHENTICATION_REQUIRED;无效凭据绝不会悄悄降级为匿名访问。调用方如何获取和发送各类凭据,见“接入”中的 选择凭据。
配置登录
Source Graph 支持通过 Google(OpenID Connect)或 GitHub(OAuth)登录。某个登录提供方只有在客户端 ID 和客户端密钥都已设置时才会启用;如果两者都未配置,登录页会说明缺少什么,站点保持匿名只读状态。
| 提供方 | 环境变量 | 回调 URL | 授权范围 |
|---|---|---|---|
GOOGLE_CLIENT_ID、GOOGLE_CLIENT_SECRET | <origin>/sourcegraph/auth/google/callback | openid、profile、email | |
| GitHub | GITHUB_CLIENT_ID、GITHUB_CLIENT_SECRET | <origin>/sourcegraph/auth/github/callback | read:user |
- 登记回调地址。
<origin>是SOURCEGRAPH_ORIGIN的值,即不含路径的公开源地址。在 Google 中创建类型为 Web application 的 OAuth 客户端,并将回调地址添加为已授权的重定向 URI;在 GitHub 中创建 OAuth App,并将其用作授权回调 URL。 - 保存密钥到你所用平台的密钥存储中,然后重启服务。某个提供方的两个值都存在时,Source Graph 会在首次启动时把它们导入加密的数据库存储;此后即使没有这些环境变量也会使用已保存的配置,且数据库记录优先于环境变量。
- 登录。 第一个登录的人会成为管理员,详见下文。
登录必须从配置的站点地址开始,因为提供方总是回到这个地址,登录 Cookie 也只属于一个主机。从其他地址(例如平台自带的主机名)发起的登录,会先被送到站点地址上的登录页;位于服务前面的代理通过 X-Forwarded-Host 转发原始主机。
当公开源地址变化时(例如服务迁移到另一个域名之后),请同时更新 SOURCEGRAPH_ORIGIN 和两个提供方的回调地址。
GitHub 登录只用于识别用户,并不授予仓库访问权限。私有仓库的令牌需要按代码源另行配置,详见 登记和管理仓库。
首位管理员
用户通过登录提供方及该提供方下稳定的主体 ID 来识别。邮箱地址相同既不会合并身份,也不会授予管理员权限,因此同一个人分别用 Google 和 GitHub 登录,会成为两个独立的用户。
新实例上第一次通过验证的登录会以原子方式成为管理员。请先只配置一个提供方,完成所有者的首次登录,之后再添加第二个提供方或将站点设为公开。管理员可以在 Users 页面提升其他用户;系统中必须始终保留至少一名启用状态的管理员,且管理员不能降低自己的权限。
谁可以登录
站点设置 Allow other users to sign in or register(允许其他用户登录或注册)默认关闭。关闭期间只有管理员可以登录。其他人会被拒绝,并看到一个 403 页面:页面以两种语言显示可配置的提示,附有项目链接,并在五秒后返回上一页。关闭该设置还会切断现有非管理员用户的会话,他们的个人 API Key 也会被拒绝并返回 REGISTRATION_CLOSED。
匿名阅读与该设置相互独立:只要站点的访问设置中开启了公开阅读,访客仍可读取公开仓库。匿名流量受限流约束,具体限额见“运维”中的 限流。
另一项设置 Let signed-in users register repositories(允许已登录用户登记仓库)同样默认关闭;在它出现之前保存的设置读取为关闭。开启后,任何已登录用户都可以登记仓库并成为其管理者。管理者和管理员可以修改该仓库的设置、建索引和清理、取消其任务、删除它,以及添加或移除管理者(最多 50 位);其他人会得到 403 REPOSITORY_MANAGEMENT_DENIED。用户、站点设置和全局代码源令牌仍仅限管理员。用户登记的仓库先使用该用户自己的代码源令牌拉取,再使用站点级令牌,因此开启这项设置意味着已登录用户可以登记站点级令牌能读取的任何仓库。
公开仓库与私有仓库
每个仓库要么公开,要么私有,在登记时或之后在其设置中通过 Public repository(公开仓库)选择(public_read,默认为 true)。在这个选项出现之前登记的仓库保持公开。
| 调用方 | 公开仓库 | 私有仓库 |
|---|---|---|
| 匿名访客 | 开启公开阅读时可读取 | 不可见 |
| 已登录用户 | 可读取 | 仅限自己创建或管理的仓库 |
| 在网站上登录的管理员 | 可读取 | 全部 |
| 个人 API Key | 可读取 | 仅限其所有者创建或管理的仓库,即使所有者是管理员也是如此 |
| 服务令牌 | 可读取 | 全部 |
调用方看不到的私有仓库,其表现与未登记的名称完全相同:详情、搜索、读取、列目录、差异、插件、prepare 和 MCP 工具都返回 404 REPOSITORY_NOT_FOUND,仓库目录中不会列出它,统计信息也会略去它的名称。要让某人获得访问权限,请把他们添加为该仓库的管理者,或将仓库设为公开。
登记一个已被登记的名称会返回 409 REPOSITORY_EXISTS,并在 creator 中给出登记人。网站会显示是谁登记的,并建议请对方把你添加为管理者;没有记录登记人时,则建议联系站点管理员。
后台同步、建索引和清理对所有仓库都会运行,与可见性无关。
会话与 API Key
浏览器会话有效期为 90 天,保存在作用域为 /sourcegraph 的 HTTP-only、same-site Cookie 中,在 HTTPS 源上会标记为 secure。再次登录会替换该浏览器之前的会话。
个人 API Key 供以你的身份运行的脚本和 Agent 使用。在用户设置中创建 Key 时需填写名称,可选设置过期时间;每个 Key 都以 sgk_ 开头,之后也可以在同一页面再次复制。Key 在以下情况会失效:到期、被你删除、你的账号被停用,或注册已关闭且你不是管理员。
Key 能读取的内容与其所有者以已登录用户身份能读取的相同,但管理员的 Key 例外,它看不到所有仓库:只能看到公开仓库以及其所有者创建或管理的私有仓库。除读取外,Key 只能调用建索引准备,即 MCP 的 prepare 或 POST /sourcegraph/api/admin/v1/repo/prepare。它可以为其所有者管理的仓库执行准备;当所有者当前是管理员时,可以为它能看到的任何仓库执行准备;每次调用都会校验这一点。只有所有者是管理员或至少管理一个仓库的 Key,MCP 才会列出 prepare 工具,该状态在一分钟内刷新。Key 每分钟最多调用 10 次 prepare,见 运维:日常运维与排障;其他所有管理路由都返回 403 ACCESS_DENIED。
服务令牌通过 SOURCEGRAPH_API_TOKEN 设置,长度至少 32 个字符,以恒定时间比较,并授予完全访问权限。请像对待 root 凭据一样对待它,不要放进共享配置中。
访问错误
以下错误码专用于认证和权限。限流及其他所有错误,见“运维”中的 常见错误。
| 错误码 | 状态码 | 含义 |
|---|---|---|
AUTHENTICATION_REQUIRED | 401 | 没有可用凭据,或凭据无效、已过期、已停用 |
ACCESS_DENIED | 403 | 凭据有效但无权访问此处,例如用个人 Key 访问 prepare 以外的管理路由 |
REGISTRATION_CLOSED | 403 | 登录仅限管理员 |
REGISTRATION_DISABLED | 403 | 未开启用户登记仓库时,非管理员尝试登记仓库 |
REPOSITORY_MANAGEMENT_DENIED | 403 | 调用方既不是该仓库的管理者,也不是管理员 |
PREPARATION_NOT_ALLOWED | 403 | 匿名调用方永远不能请求建索引 |
REPOSITORY_NOT_FOUND | 404 | 仓库未登记,或者是私有仓库且调用方看不到它 |
REPOSITORY_EXISTS | 409 | 该名称已被登记;creator 给出可以申请访问权限的登记人 |