Documentation
¶
Index ¶
- Variables
- func BuildAccessToken(key *rsa.PrivateKey, kid string, c IDTokenClaims) (string, error)
- func BuildAgentIdentityJWT(key *rsa.PrivateKey, kid string, c AgentIdentityClaims) (string, error)
- func BuildIDToken(key *rsa.PrivateKey, kid string, c IDTokenClaims) (string, error)
- func GenerateEd25519Key() (privatePKCS8 []byte, public []byte, err error)
- func HashToken(raw string) []byte
- func LoadRSAPrivate(pkcs8 []byte) (*rsa.PrivateKey, error)
- type AgentIdentity
- type AgentIdentityClaims
- type AgentTask
- type DeviceCode
- type IDTokenClaims
- type JwksKey
- type MintAgentIdentityArgs
- type MintAgentIdentityResult
- type PkceRequest
- type RSAKeyPair
- type Server
- type SessionResolver
- type Store
- func (s *Store) ApproveDeviceCode(ctx context.Context, userCode, userID string) error
- func (s *Store) ConsumePkceRequest(ctx context.Context, code string) (*PkceRequest, error)
- func (s *Store) ExchangeDeviceCode(ctx context.Context, deviceAuthID, userCode string) (*DeviceCode, error)
- func (s *Store) GetActiveJwksKey(ctx context.Context) (*JwksKey, error)
- func (s *Store) GetAgentIdentity(ctx context.Context, rid string) (*AgentIdentity, error)
- func (s *Store) GetAgentTask(ctx context.Context, taskID string) (*AgentTask, error)
- func (s *Store) GetDeviceCodeByUserCode(ctx context.Context, userCode string) (*DeviceCode, error)
- func (s *Store) InsertAccessToken(ctx context.Context, tokenHash []byte, userID string, expiresAt time.Time) error
- func (s *Store) InsertAgentIdentity(ctx context.Context, a AgentIdentity) error
- func (s *Store) InsertAgentTask(ctx context.Context, taskID, rid, userID string, expiresAt time.Time) error
- func (s *Store) InsertDeviceCode(ctx context.Context, dc DeviceCode) error
- func (s *Store) InsertJwksKey(ctx context.Context, kid string, kp *RSAKeyPair, active bool) error
- func (s *Store) InsertPkceRequest(ctx context.Context, r PkceRequest) error
- func (s *Store) InsertRefreshToken(ctx context.Context, tokenHash []byte, familyID, userID string, ...) error
- func (s *Store) ListAllJwksKeys(ctx context.Context) ([]JwksKey, error)
- func (s *Store) LookupAccessToken(ctx context.Context, rawToken string) (string, error)
- func (s *Store) RotateRefreshToken(ctx context.Context, oldRaw string, newHash []byte, newExpiry time.Time) (string, error)
Constants ¶
This section is empty.
Variables ¶
var ErrRefreshTokenReuse = errors.New("refresh token unknown or revoked")
ErrRefreshTokenReuse is returned by RotateRefreshToken when the presented token is unknown OR has already been revoked. Callers (the /oauth/token refresh handler) should map this to a 401 with OAuth error `refresh_token_expired` or `refresh_token_reused`.
Functions ¶
func BuildAccessToken ¶
func BuildAccessToken(key *rsa.PrivateKey, kid string, c IDTokenClaims) (string, error)
BuildAccessToken mints the access_token as an RS256 JWT carrying just `{iss, sub, iat, exp}`. Codex never validates the signature (it treats access_token as an opaque bearer), but it *does* parse `exp` to drive proactive refresh (login/src/auth/manager.rs:1793-1812 — is_stale_for_proactive_refresh calls parse_jwt_expiration on access_token). Minting the access_token as a JWT means codex refreshes cleanly at the real expiry instead of waiting for a 401 retry.
func BuildAgentIdentityJWT ¶
func BuildAgentIdentityJWT(key *rsa.PrivateKey, kid string, c AgentIdentityClaims) (string, error)
BuildAgentIdentityJWT returns an RS256 JWT whose claims pass codex's strict aud="codex-app-server" + iss=".../agent-identity" check.
func BuildIDToken ¶
func BuildIDToken(key *rsa.PrivateKey, kid string, c IDTokenClaims) (string, error)
BuildIDToken returns a signed RS256 JWT with the OpenAI-nested claim that codex's `compose_success_url` and `jwt_auth_claims` paths expect (login/src/server.rs:821-907).
func GenerateEd25519Key ¶
GenerateEd25519Key produces an Ed25519 keypair used per-agent for AgentAssertion signing. PrivatePKCS8 is stored on the client (inside the Agent Identity JWT's agent_private_key claim); the public key stays server-side for verification.
func HashToken ¶
HashToken returns sha256(raw) suitable for DB primary key. Tokens are never stored plaintext.
func LoadRSAPrivate ¶
func LoadRSAPrivate(pkcs8 []byte) (*rsa.PrivateKey, error)
LoadRSAPrivate parses a PKCS#8 DER blob (as stored in codex_jwks_keys.private_pkcs8) back into an *rsa.PrivateKey for signing.
Types ¶
type AgentIdentity ¶
type AgentIdentityClaims ¶
type AgentIdentityClaims struct {
AgentRuntimeID string
AgentPrivateKeyPKCS8 []byte
AccountID string
ChatgptUserID string
Email string
PlanType string
ExpiresAt time.Time
}
AgentIdentityClaims is the JWT codex's exec-server --remote --use-agent-identity-auth requires (agent-identity/src/lib.rs:65-78).
type DeviceCode ¶
type IDTokenClaims ¶
IDTokenClaims is the subset of fields we populate for the OpenAI-shaped id_token. Codex never verifies the signature, but it base64-decodes the payload and looks for the nested OpenAI claim.
type MintAgentIdentityArgs ¶
MintAgentIdentityArgs is the input shape used by the UI's "Add connector" path and the test bench.
type MintAgentIdentityResult ¶
type MintAgentIdentityResult struct {
JWT string
// contains filtered or unexported fields
}
MintAgentIdentityResult is what the UI shows the user (JWT) and what tests use (privKey) to assert downstream signature checks pass.
type PkceRequest ¶
type RSAKeyPair ¶
type RSAKeyPair struct {
PrivateKey *rsa.PrivateKey
PrivatePKCS8 []byte
PublicN string
PublicE string
}
RSAKeyPair is an RSA-2048 keypair ready for JWKS exposure (public) and JWT signing (private). PublicN/PublicE are base64url-no-pad encoded for direct JWKS serialization; PrivatePKCS8 is the DER blob stored encrypted at rest.
func GenerateRSAKey ¶
func GenerateRSAKey() (kid string, kp *RSAKeyPair, err error)
GenerateRSAKey mints a fresh RSA-2048 keypair and a deterministic kid derived from the public modulus prefix. kid stability matters because it's embedded in JWT headers and used to look up the verification key in JWKS; a random kid forces full JWKS scans.
type Server ¶
type Server struct {
Store *Store
IssuerURL string // e.g. "https://agent.cs.ac.cn/codex-auth"
SigningKey *rsa.PrivateKey // active RSA key for id_token + Agent Identity JWT
SigningKid string
SessionResolve SessionResolver
LoginRedirectURL string // where /oauth/authorize sends unauth users
}
Server is the user-facing codex-auth HTTP surface (PKCE / device flow / JWKS / agent-identity), all mounted under a single chi subrouter.
func (*Server) HandleValidate ¶
func (s *Server) HandleValidate(w http.ResponseWriter, r *http.Request)
HandleValidate is the internal endpoint codex-exec-gateway calls over X-Internal-Secret to verify a Bearer / AgentAssertion token. Returns 200 with {user_id} or 401 with {error}.
func (*Server) MintAgentIdentity ¶
func (s *Server) MintAgentIdentity(ctx context.Context, args MintAgentIdentityArgs) (*MintAgentIdentityResult, error)
MintAgentIdentity generates an Ed25519 keypair for the new agent, signs the Agent Identity JWT with the active RSA key, and persists (public key, jwt_signed_with) so subsequent AgentAssertion signatures can be verified.
type SessionResolver ¶
SessionResolver returns the user_id for the request's session cookie, or empty string if unauthenticated. agentserver supplies its existing session middleware here.
type Store ¶
type Store struct {
// contains filtered or unexported fields
}
Store is a thin DB facade over all codex_* tables. Each method takes a context and returns a typed value or error; no business logic lives here (compose in pkce.go, agent_identity.go, etc.).
func (*Store) ApproveDeviceCode ¶
func (*Store) ConsumePkceRequest ¶
ConsumePkceRequest atomically deletes and returns the row. Returns nil if the code is missing or expired.
func (*Store) ExchangeDeviceCode ¶
func (s *Store) ExchangeDeviceCode(ctx context.Context, deviceAuthID, userCode string) (*DeviceCode, error)
ExchangeDeviceCode atomically returns the row and marks it 'exchanged'. Only approved rows are returned; subsequent calls return nil.
func (*Store) GetActiveJwksKey ¶
func (*Store) GetAgentIdentity ¶
func (*Store) GetAgentTask ¶
func (*Store) GetDeviceCodeByUserCode ¶
func (*Store) InsertAccessToken ¶
func (*Store) InsertAgentIdentity ¶
func (s *Store) InsertAgentIdentity(ctx context.Context, a AgentIdentity) error
func (*Store) InsertAgentTask ¶
func (*Store) InsertDeviceCode ¶
func (s *Store) InsertDeviceCode(ctx context.Context, dc DeviceCode) error
func (*Store) InsertJwksKey ¶
func (*Store) InsertPkceRequest ¶
func (s *Store) InsertPkceRequest(ctx context.Context, r PkceRequest) error
func (*Store) InsertRefreshToken ¶
func (*Store) ListAllJwksKeys ¶
func (*Store) LookupAccessToken ¶
LookupAccessToken returns the user_id if the token is valid (exists, not expired, not revoked); empty string otherwise.
func (*Store) RotateRefreshToken ¶
func (s *Store) RotateRefreshToken(ctx context.Context, oldRaw string, newHash []byte, newExpiry time.Time) (string, error)
RotateRefreshToken revokes the old refresh token and inserts a new one in the same family. Returns the user_id; errors if the old token is missing or already revoked.