CodeSampleX

샘플

google.golang.org/genproto/googleapis/rpc v0.0.0-20260825221802-da73d73af1c5: attribute_context.AttributeContext_Auth

검증된 샘플 — golang google.golang.org/genproto/googleapis/rpc v0.0.0-20260825221802-da73d73af1c5: attribute_context.AttributeContext_Auth. go 1.26 · linux…

sha256:2951fd8a2fb265f801ae1707b2719c867852b9144ce9998650bc1fad9cd1d5d9

이 네트워크가 제공하는 것은 하나입니다. 빌드되는 샘플. 샌드박스에서 돌리고 서명된 영수증을 보관합니다. 등급을 매기지 않고 무엇도 보증하지 않습니다 — 같은 코드가 당신 환경에서 빌드되는지는 측정한 적이 없습니다. 통과한 계약 영수증을 낸 서로 다른 서명 키의 수입니다. 하나면 작성자 혼자이고, 둘 이상이면 다른 사람도 빌드했다는 뜻입니다. 키는 스스로 만드는 것이고 뒤에 등록된 신원이 없으므로, 세는 것은 사람이 아니라 키입니다. MIT-0

실행 증거

선언된 환경과 서명된 실행을 분리해 두었습니다. 이 샘플이 무엇을 어디서 실행했는지 그대로 볼 수 있습니다.

증거 기준
서명된 컨트랙트 통과
검증 영수증
1
빌드한 서명 키
1
선언된 환경 linux 24 · ubuntu · glibc 2.39 x64 go go

검증 실행 환경

환경 컨트랙트 단계 실행일
go 1.26 · linux debian/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · golang@1golang:1.26@sha256:e30143be198a…
2026-09-03

케이스

HOW
목표
verify google.golang.org/genproto/googleapis/rpc/context/attribute_context.AttributeContext_Auth in pkg:golang/google.golang.org/genproto/googleapis/rpc@v0.0.0-20260825221802-da73d73af1c5
패키지
심벌
  • google.golang.org/genproto/googleapis/rpc/context/attribute_context.AttributeContext_Auth
생성일
2026-09-03T03:46:41Z

컨트랙트

  1. AttributeContext_Auth initializes with principal, audiences, presenter, claims, and access levels
  2. AttributeContext_Auth getters return expected principal, audiences, and presenter values
  3. AttributeContext_Auth GetClaims returns structured claims and nil when unset
  4. AttributeContext_Auth GetAccessLevels returns configured access level resource names
  5. AttributeContext_Auth Reset clears all fields to zero values

파일

  • PROMPT.md
  • csx.json
  • go.mod
  • go.sum
  • sample.go
  • spec.json
  • test/contract.go

소스 아티팩트 내려받기 (tar.gz)

소스

PROMPT.md
Clean-room public code sample — generation instructions

Write a brand-new, minimal, self-contained code sample in this clean-room directory.
Do not copy, paraphrase, or reference any existing project source. Work only from this spec.

A csx.json manifest scaffold already exists. Do not recreate it from memory. Preserve its case.goal, packages and symbols; fill its empty case.contract with exact assertions and correct its environment, commands and verifierAdapter for the files you generate.

Goal: verify google.golang.org/genproto/googleapis/rpc/context/attribute_context.AttributeContext_Auth in pkg:golang/google.golang.org/genproto/googleapis/rpc@v0.0.0-20260825221802-da73d73af1c5
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/google.golang.org/genproto/googleapis/rpc@v0.0.0-20260825221802-da73d73af1c5
Demonstrate these symbols/APIs:
  - google.golang.org/genproto/googleapis/rpc/context/attribute_context.AttributeContext_Auth

Rules:
  - One focused purpose; the smallest project that proves the goal.
  - Include a contract test (test/contract.*) that runs OFFLINE and exits 0 exactly when the goal behavior works.
  - Pin every dependency with a lockfile so resolution is reproducible.
  - No secrets, credentials, or tokens. No real URLs (only example.com or localhost). No absolute paths.
  - No personal names, emails, company names, or project identifiers of any kind.
  - No binaries and no generated output (node_modules, dist, target, venv, .git, .env).
  - Keep it under 200 files and 256KB packed.
csx.json
{"case":{"caseId":"case:sha256:0d1affe3484cd57ac273ea25aa10cd56e56b5f58b416371edb43627c76fc38da","contract":["AttributeContext_Auth initializes with principal, audiences, presenter, claims, and access levels","AttributeContext_Auth getters return expected principal, audiences, and presenter values","AttributeContext_Auth GetClaims returns structured claims and nil when unset","AttributeContext_Auth GetAccessLevels returns configured access level resource names","AttributeContext_Auth Reset clears all fields to zero values"],"goal":"verify google.golang.org/genproto/googleapis/rpc/context/attribute_context.AttributeContext_Auth in pkg:golang/google.golang.org/genproto/googleapis/rpc@v0.0.0-20260825221802-da73d73af1c5","kind":"HOW","packages":["pkg:golang/google.golang.org/genproto/googleapis/rpc@v0.0.0-20260825221802-da73d73af1c5"],"schemaVersion":1,"symbols":["google.golang.org/genproto/googleapis/rpc/context/attribute_context.AttributeContext_Auth"]},"contractCommand":["go","run","./test"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"golang","language":"go","libc":"glibc","libcVersion":"2.39","os":"linux","osVersionBucket":"24","packageManager":"go","schemaVersion":1},"license":"MIT-0","packages":["pkg:golang/google.golang.org/genproto/googleapis/rpc@v0.0.0-20260825221802-da73d73af1c5"],"schemaVersion":1,"subject":"pkg:golang/google.golang.org/genproto/googleapis/rpc@v0.0.0-20260825221802-da73d73af1c5","symbols":["google.golang.org/genproto/googleapis/rpc/context/attribute_context.AttributeContext_Auth"],"verifierAdapter":"golang@1"}
go.mod
module sample

go 1.26.6

require (
	google.golang.org/genproto/googleapis/rpc v0.0.0-20260825221802-da73d73af1c5
	google.golang.org/protobuf v1.36.12
)
go.sum
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
google.golang.org/genproto/googleapis/rpc v0.0.0-20260825221802-da73d73af1c5 h1:1VUiZAXyC+zmiFYi+WLtBzr68Cj8wOofHjjrA/kkizc=
google.golang.org/genproto/googleapis/rpc v0.0.0-20260825221802-da73d73af1c5/go.mod h1:DjtHYE8FKJLivXcBEjGwndXfIC23G0VpXiXKqG179uA=
google.golang.org/protobuf v1.36.12 h1:pJOKDDOyeXErUroCihFAd5LQuwXBSpVnKGrj5o/fwxc=
google.golang.org/protobuf v1.36.12/go.mod h1:HTf+CrKn2C3g5S8VImy6tdcUvCska2kB7j23XfzDpco=
sample.go
package sample

import (
	"errors"
	"fmt"

	"google.golang.org/genproto/googleapis/rpc/context/attribute_context"
	"google.golang.org/protobuf/types/known/structpb"
)

// NewAuthContext creates a new AttributeContext_Auth message with the specified fields.
func NewAuthContext(principal string, presenter string, audiences []string, accessLevels []string, claims map[string]any) (*attribute_context.AttributeContext_Auth, error) {
	if principal == "" {
		return nil, errors.New("principal must not be empty")
	}

	auth := &attribute_context.AttributeContext_Auth{
		Principal:    principal,
		Presenter:    presenter,
		Audiences:    audiences,
		AccessLevels: accessLevels,
	}

	if claims != nil {
		structClaims, err := structpb.NewStruct(claims)
		if err != nil {
			return nil, fmt.Errorf("failed to construct claims: %w", err)
		}
		auth.Claims = structClaims
	}

	return auth, nil
}

// ValidateAuthContext verifies that the authentication context satisfies minimal requirements.
func ValidateAuthContext(auth *attribute_context.AttributeContext_Auth) error {
	if auth == nil {
		return errors.New("auth context is nil")
	}
	if auth.GetPrincipal() == "" {
		return errors.New("missing principal in auth context")
	}
	return nil
}

// HasAudience checks whether the auth context contains the specified audience.
func HasAudience(auth *attribute_context.AttributeContext_Auth, target string) bool {
	if auth == nil {
		return false
	}
	for _, aud := range auth.GetAudiences() {
		if aud == target {
			return true
		}
	}
	return false
}

// HasAccessLevel checks whether the auth context contains the specified access level.
func HasAccessLevel(auth *attribute_context.AttributeContext_Auth, target string) bool {
	if auth == nil {
		return false
	}
	for _, level := range auth.GetAccessLevels() {
		if level == target {
			return true
		}
	}
	return false
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify google.golang.org/genproto/googleapis/rpc/context/attribute_context.AttributeContext_Auth in pkg:golang/google.golang.org/genproto/googleapis/rpc@v0.0.0-20260825221802-da73d73af1c5",
  "kind": "HOW",
  "packages": [
    "pkg:golang/google.golang.org/genproto/googleapis/rpc@v0.0.0-20260825221802-da73d73af1c5"
  ],
  "symbols": [
    "google.golang.org/genproto/googleapis/rpc/context/attribute_context.AttributeContext_Auth"
  ]
}
test/contract.go
package main

import (
	"fmt"
	"os"

	"google.golang.org/genproto/googleapis/rpc/context/attribute_context"
	"sample"
)

func main() {
	if err := runTests(); err != nil {
		fmt.Fprintf(os.Stderr, "Contract test failed: %v\n", err)
		os.Exit(1)
	}
	fmt.Println("All AttributeContext_Auth contract tests passed successfully.")
}

func runTests() error {
	principal := "https://accounts.example.com/user123"
	presenter := "client-app-123.apps.example.com"
	audiences := []string{"https://api.example.com/service-a", "https://api.example.com/service-b"}
	accessLevels := []string{"//accesscontextmanager.googleapis.com/accessPolicies/1/accessLevels/level-basic"}
	claimsData := map[string]any{
		"iss": "https://accounts.example.com",
		"sub": "user123",
	}

	// 1. AttributeContext_Auth initializes with principal, audiences, presenter, claims, and access levels
	auth, err := sample.NewAuthContext(principal, presenter, audiences, accessLevels, claimsData)
	if err != nil {
		return fmt.Errorf("failed to create auth context: %w", err)
	}
	if auth == nil {
		return fmt.Errorf("expected non-nil AttributeContext_Auth")
	}

	// 2. AttributeContext_Auth getters return expected principal, audiences, and presenter values
	if got := auth.GetPrincipal(); got != principal {
		return fmt.Errorf("expected principal %q, got %q", principal, got)
	}
	if got := auth.GetPresenter(); got != presenter {
		return fmt.Errorf("expected presenter %q, got %q", presenter, got)
	}
	if auds := auth.GetAudiences(); len(auds) != 2 || auds[0] != audiences[0] || auds[1] != audiences[1] {
		return fmt.Errorf("unexpected audiences: %v", auds)
	}
	if !sample.HasAudience(auth, "https://api.example.com/service-a") {
		return fmt.Errorf("expected audience https://api.example.com/service-a to be present")
	}
	if sample.HasAudience(auth, "https://api.example.com/service-c") {
		return fmt.Errorf("did not expect audience https://api.example.com/service-c to be present")
	}

	// 3. AttributeContext_Auth GetClaims returns structured claims and nil when unset
	claims := auth.GetClaims()
	if claims == nil {
		return fmt.Errorf("expected non-nil claims")
	}
	fields := claims.GetFields()
	if fields["sub"] == nil || fields["sub"].GetStringValue() != "user123" {
		return fmt.Errorf("expected claim sub='user123', got %v", fields["sub"])
	}

	var unsetAuth *attribute_context.AttributeContext_Auth
	if unsetAuth.GetClaims() != nil {
		return fmt.Errorf("expected nil claims on nil AttributeContext_Auth")
	}
	emptyAuth := &attribute_context.AttributeContext_Auth{}
	if emptyAuth.GetClaims() != nil {
		return fmt.Errorf("expected nil claims on empty AttributeContext_Auth")
	}

	// 4. AttributeContext_Auth GetAccessLevels returns configured access level resource names
	levels := auth.GetAccessLevels()
	if len(levels) != 1 || levels[0] != accessLevels[0] {
		return fmt.Errorf("unexpected access levels: %v", levels)
	}
	if !sample.HasAccessLevel(auth, accessLevels[0]) {
		return fmt.Errorf("expected access level to be present")
	}

	// Validation checks
	if err := sample.ValidateAuthContext(auth); err != nil {
		return fmt.Errorf("validation failed: %w", err)
	}
	if err := sample.ValidateAuthContext(emptyAuth); err == nil {
		return fmt.Errorf("expected validation failure for empty auth context")
	}

	// 5. AttributeContext_Auth Reset clears all fields to zero values
	auth.Reset()
	if auth.GetPrincipal() != "" {
		return fmt.Errorf("expected empty principal after Reset")
	}
	if len(auth.GetAudiences()) != 0 {
		return fmt.Errorf("expected empty audiences after Reset")
	}
	if auth.GetPresenter() != "" {
		return fmt.Errorf("expected empty presenter after Reset")
	}
	if auth.GetClaims() != nil {
		return fmt.Errorf("expected nil claims after Reset")
	}
	if len(auth.GetAccessLevels()) != 0 {
		return fmt.Errorf("expected empty access levels after Reset")
	}

	return nil
}

오리진 시더

익명