File
Blob: spec/protocol/pki.twirp.go
| 1 | // Code generated by protoc-gen-twirp v8.1.3, DO NOT EDIT. |
| 2 | // source: spec/proto/pki.proto |
| 3 | |
| 4 | package protocol |
| 5 | |
| 6 | import context "context" |
| 7 | import fmt "fmt" |
| 8 | import http "net/http" |
| 9 | import io "io" |
| 10 | import json "encoding/json" |
| 11 | import strconv "strconv" |
| 12 | import strings "strings" |
| 13 | |
| 14 | import protojson "google.golang.org/protobuf/encoding/protojson" |
| 15 | import twirp "github.com/twitchtv/twirp" |
| 16 | import ctxsetters "github.com/twitchtv/twirp/ctxsetters" |
| 17 | |
| 18 | // Version compatibility assertion. |
| 19 | // If the constant is not defined in the package, that likely means |
| 20 | // the package needs to be updated to work with this generated code. |
| 21 | // See https://twitchtv.github.io/twirp/docs/version_matrix.html |
| 22 | const _ = twirp.TwirpPackageMinVersion_8_1_0 |
| 23 | |
| 24 | // ==================== |
| 25 | // PKIService Interface |
| 26 | // ==================== |
| 27 | |
| 28 | type PKIService interface { |
| 29 | RequestCertificate(context.Context, *CertificateRequest) (*CertificateResponse, error) |
| 30 | |
| 31 | RenewCertificate(context.Context, *CertificateRenewalRequest) (*CertificateResponse, error) |
| 32 | } |
| 33 | |
| 34 | // ========================== |
| 35 | // PKIService Protobuf Client |
| 36 | // ========================== |
| 37 | |
| 38 | type pKIServiceProtobufClient struct { |
| 39 | client HTTPClient |
| 40 | urls [2]string |
| 41 | interceptor twirp.Interceptor |
| 42 | opts twirp.ClientOptions |
| 43 | } |
| 44 | |
| 45 | // NewPKIServiceProtobufClient creates a Protobuf client that implements the PKIService interface. |
| 46 | // It communicates using Protobuf and can be configured with a custom HTTPClient. |
| 47 | func NewPKIServiceProtobufClient(baseURL string, client HTTPClient, opts ...twirp.ClientOption) PKIService { |
| 48 | if c, ok := client.(*http.Client); ok { |
| 49 | client = withoutRedirects(c) |
| 50 | } |
| 51 | |
| 52 | clientOpts := twirp.ClientOptions{} |
| 53 | for _, o := range opts { |
| 54 | o(&clientOpts) |
| 55 | } |
| 56 | |
| 57 | // Using ReadOpt allows backwards and forwards compatibility with new options in the future |
| 58 | literalURLs := false |
| 59 | _ = clientOpts.ReadOpt("literalURLs", &literalURLs) |
| 60 | var pathPrefix string |
| 61 | if ok := clientOpts.ReadOpt("pathPrefix", &pathPrefix); !ok { |
| 62 | pathPrefix = "/twirp" // default prefix |
| 63 | } |
| 64 | |
| 65 | // Build method URLs: <baseURL>[<prefix>]/<package>.<Service>/<Method> |
| 66 | serviceURL := sanitizeBaseURL(baseURL) |
| 67 | serviceURL += baseServicePath(pathPrefix, "protocol", "PKIService") |
| 68 | urls := [2]string{ |
| 69 | serviceURL + "RequestCertificate", |
| 70 | serviceURL + "RenewCertificate", |
| 71 | } |
| 72 | |
| 73 | return &pKIServiceProtobufClient{ |
| 74 | client: client, |
| 75 | urls: urls, |
| 76 | interceptor: twirp.ChainInterceptors(clientOpts.Interceptors...), |
| 77 | opts: clientOpts, |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | func (c *pKIServiceProtobufClient) RequestCertificate(ctx context.Context, in *CertificateRequest) (*CertificateResponse, error) { |
| 82 | ctx = ctxsetters.WithPackageName(ctx, "protocol") |
| 83 | ctx = ctxsetters.WithServiceName(ctx, "PKIService") |
| 84 | ctx = ctxsetters.WithMethodName(ctx, "RequestCertificate") |
| 85 | caller := c.callRequestCertificate |
| 86 | if c.interceptor != nil { |
| 87 | caller = func(ctx context.Context, req *CertificateRequest) (*CertificateResponse, error) { |
| 88 | resp, err := c.interceptor( |
| 89 | func(ctx context.Context, req interface{}) (interface{}, error) { |
| 90 | typedReq, ok := req.(*CertificateRequest) |
| 91 | if !ok { |
| 92 | return nil, twirp.InternalError("failed type assertion req.(*CertificateRequest) when calling interceptor") |
| 93 | } |
| 94 | return c.callRequestCertificate(ctx, typedReq) |
| 95 | }, |
| 96 | )(ctx, req) |
| 97 | if resp != nil { |
| 98 | typedResp, ok := resp.(*CertificateResponse) |
| 99 | if !ok { |
| 100 | return nil, twirp.InternalError("failed type assertion resp.(*CertificateResponse) when calling interceptor") |
| 101 | } |
| 102 | return typedResp, err |
| 103 | } |
| 104 | return nil, err |
| 105 | } |
| 106 | } |
| 107 | return caller(ctx, in) |
| 108 | } |
| 109 | |
| 110 | func (c *pKIServiceProtobufClient) callRequestCertificate(ctx context.Context, in *CertificateRequest) (*CertificateResponse, error) { |
| 111 | out := new(CertificateResponse) |
| 112 | ctx, err := doProtobufRequest(ctx, c.client, c.opts.Hooks, c.urls[0], in, out) |
| 113 | if err != nil { |
| 114 | twerr, ok := err.(twirp.Error) |
| 115 | if !ok { |
| 116 | twerr = twirp.InternalErrorWith(err) |
| 117 | } |
| 118 | callClientError(ctx, c.opts.Hooks, twerr) |
| 119 | return nil, err |
| 120 | } |
| 121 | |
| 122 | callClientResponseReceived(ctx, c.opts.Hooks) |
| 123 | |
| 124 | return out, nil |
| 125 | } |
| 126 | |
| 127 | func (c *pKIServiceProtobufClient) RenewCertificate(ctx context.Context, in *CertificateRenewalRequest) (*CertificateResponse, error) { |
| 128 | ctx = ctxsetters.WithPackageName(ctx, "protocol") |
| 129 | ctx = ctxsetters.WithServiceName(ctx, "PKIService") |
| 130 | ctx = ctxsetters.WithMethodName(ctx, "RenewCertificate") |
| 131 | caller := c.callRenewCertificate |
| 132 | if c.interceptor != nil { |
| 133 | caller = func(ctx context.Context, req *CertificateRenewalRequest) (*CertificateResponse, error) { |
| 134 | resp, err := c.interceptor( |
| 135 | func(ctx context.Context, req interface{}) (interface{}, error) { |
| 136 | typedReq, ok := req.(*CertificateRenewalRequest) |
| 137 | if !ok { |
| 138 | return nil, twirp.InternalError("failed type assertion req.(*CertificateRenewalRequest) when calling interceptor") |
| 139 | } |
| 140 | return c.callRenewCertificate(ctx, typedReq) |
| 141 | }, |
| 142 | )(ctx, req) |
| 143 | if resp != nil { |
| 144 | typedResp, ok := resp.(*CertificateResponse) |
| 145 | if !ok { |
| 146 | return nil, twirp.InternalError("failed type assertion resp.(*CertificateResponse) when calling interceptor") |
| 147 | } |
| 148 | return typedResp, err |
| 149 | } |
| 150 | return nil, err |
| 151 | } |
| 152 | } |
| 153 | return caller(ctx, in) |
| 154 | } |
| 155 | |
| 156 | func (c *pKIServiceProtobufClient) callRenewCertificate(ctx context.Context, in *CertificateRenewalRequest) (*CertificateResponse, error) { |
| 157 | out := new(CertificateResponse) |
| 158 | ctx, err := doProtobufRequest(ctx, c.client, c.opts.Hooks, c.urls[1], in, out) |
| 159 | if err != nil { |
| 160 | twerr, ok := err.(twirp.Error) |
| 161 | if !ok { |
| 162 | twerr = twirp.InternalErrorWith(err) |
| 163 | } |
| 164 | callClientError(ctx, c.opts.Hooks, twerr) |
| 165 | return nil, err |
| 166 | } |
| 167 | |
| 168 | callClientResponseReceived(ctx, c.opts.Hooks) |
| 169 | |
| 170 | return out, nil |
| 171 | } |
| 172 | |
| 173 | // ====================== |
| 174 | // PKIService JSON Client |
| 175 | // ====================== |
| 176 | |
| 177 | type pKIServiceJSONClient struct { |
| 178 | client HTTPClient |
| 179 | urls [2]string |
| 180 | interceptor twirp.Interceptor |
| 181 | opts twirp.ClientOptions |
| 182 | } |
| 183 | |
| 184 | // NewPKIServiceJSONClient creates a JSON client that implements the PKIService interface. |
| 185 | // It communicates using JSON and can be configured with a custom HTTPClient. |
| 186 | func NewPKIServiceJSONClient(baseURL string, client HTTPClient, opts ...twirp.ClientOption) PKIService { |
| 187 | if c, ok := client.(*http.Client); ok { |
| 188 | client = withoutRedirects(c) |
| 189 | } |
| 190 | |
| 191 | clientOpts := twirp.ClientOptions{} |
| 192 | for _, o := range opts { |
| 193 | o(&clientOpts) |
| 194 | } |
| 195 | |
| 196 | // Using ReadOpt allows backwards and forwards compatibility with new options in the future |
| 197 | literalURLs := false |
| 198 | _ = clientOpts.ReadOpt("literalURLs", &literalURLs) |
| 199 | var pathPrefix string |
| 200 | if ok := clientOpts.ReadOpt("pathPrefix", &pathPrefix); !ok { |
| 201 | pathPrefix = "/twirp" // default prefix |
| 202 | } |
| 203 | |
| 204 | // Build method URLs: <baseURL>[<prefix>]/<package>.<Service>/<Method> |
| 205 | serviceURL := sanitizeBaseURL(baseURL) |
| 206 | serviceURL += baseServicePath(pathPrefix, "protocol", "PKIService") |
| 207 | urls := [2]string{ |
| 208 | serviceURL + "RequestCertificate", |
| 209 | serviceURL + "RenewCertificate", |
| 210 | } |
| 211 | |
| 212 | return &pKIServiceJSONClient{ |
| 213 | client: client, |
| 214 | urls: urls, |
| 215 | interceptor: twirp.ChainInterceptors(clientOpts.Interceptors...), |
| 216 | opts: clientOpts, |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | func (c *pKIServiceJSONClient) RequestCertificate(ctx context.Context, in *CertificateRequest) (*CertificateResponse, error) { |
| 221 | ctx = ctxsetters.WithPackageName(ctx, "protocol") |
| 222 | ctx = ctxsetters.WithServiceName(ctx, "PKIService") |
| 223 | ctx = ctxsetters.WithMethodName(ctx, "RequestCertificate") |
| 224 | caller := c.callRequestCertificate |
| 225 | if c.interceptor != nil { |
| 226 | caller = func(ctx context.Context, req *CertificateRequest) (*CertificateResponse, error) { |
| 227 | resp, err := c.interceptor( |
| 228 | func(ctx context.Context, req interface{}) (interface{}, error) { |
| 229 | typedReq, ok := req.(*CertificateRequest) |
| 230 | if !ok { |
| 231 | return nil, twirp.InternalError("failed type assertion req.(*CertificateRequest) when calling interceptor") |
| 232 | } |
| 233 | return c.callRequestCertificate(ctx, typedReq) |
| 234 | }, |
| 235 | )(ctx, req) |
| 236 | if resp != nil { |
| 237 | typedResp, ok := resp.(*CertificateResponse) |
| 238 | if !ok { |
| 239 | return nil, twirp.InternalError("failed type assertion resp.(*CertificateResponse) when calling interceptor") |
| 240 | } |
| 241 | return typedResp, err |
| 242 | } |
| 243 | return nil, err |
| 244 | } |
| 245 | } |
| 246 | return caller(ctx, in) |
| 247 | } |
| 248 | |
| 249 | func (c *pKIServiceJSONClient) callRequestCertificate(ctx context.Context, in *CertificateRequest) (*CertificateResponse, error) { |
| 250 | out := new(CertificateResponse) |
| 251 | ctx, err := doJSONRequest(ctx, c.client, c.opts.Hooks, c.urls[0], in, out) |
| 252 | if err != nil { |
| 253 | twerr, ok := err.(twirp.Error) |
| 254 | if !ok { |
| 255 | twerr = twirp.InternalErrorWith(err) |
| 256 | } |
| 257 | callClientError(ctx, c.opts.Hooks, twerr) |
| 258 | return nil, err |
| 259 | } |
| 260 | |
| 261 | callClientResponseReceived(ctx, c.opts.Hooks) |
| 262 | |
| 263 | return out, nil |
| 264 | } |
| 265 | |
| 266 | func (c *pKIServiceJSONClient) RenewCertificate(ctx context.Context, in *CertificateRenewalRequest) (*CertificateResponse, error) { |
| 267 | ctx = ctxsetters.WithPackageName(ctx, "protocol") |
| 268 | ctx = ctxsetters.WithServiceName(ctx, "PKIService") |
| 269 | ctx = ctxsetters.WithMethodName(ctx, "RenewCertificate") |
| 270 | caller := c.callRenewCertificate |
| 271 | if c.interceptor != nil { |
| 272 | caller = func(ctx context.Context, req *CertificateRenewalRequest) (*CertificateResponse, error) { |
| 273 | resp, err := c.interceptor( |
| 274 | func(ctx context.Context, req interface{}) (interface{}, error) { |
| 275 | typedReq, ok := req.(*CertificateRenewalRequest) |
| 276 | if !ok { |
| 277 | return nil, twirp.InternalError("failed type assertion req.(*CertificateRenewalRequest) when calling interceptor") |
| 278 | } |
| 279 | return c.callRenewCertificate(ctx, typedReq) |
| 280 | }, |
| 281 | )(ctx, req) |
| 282 | if resp != nil { |
| 283 | typedResp, ok := resp.(*CertificateResponse) |
| 284 | if !ok { |
| 285 | return nil, twirp.InternalError("failed type assertion resp.(*CertificateResponse) when calling interceptor") |
| 286 | } |
| 287 | return typedResp, err |
| 288 | } |
| 289 | return nil, err |
| 290 | } |
| 291 | } |
| 292 | return caller(ctx, in) |
| 293 | } |
| 294 | |
| 295 | func (c *pKIServiceJSONClient) callRenewCertificate(ctx context.Context, in *CertificateRenewalRequest) (*CertificateResponse, error) { |
| 296 | out := new(CertificateResponse) |
| 297 | ctx, err := doJSONRequest(ctx, c.client, c.opts.Hooks, c.urls[1], in, out) |
| 298 | if err != nil { |
| 299 | twerr, ok := err.(twirp.Error) |
| 300 | if !ok { |
| 301 | twerr = twirp.InternalErrorWith(err) |
| 302 | } |
| 303 | callClientError(ctx, c.opts.Hooks, twerr) |
| 304 | return nil, err |
| 305 | } |
| 306 | |
| 307 | callClientResponseReceived(ctx, c.opts.Hooks) |
| 308 | |
| 309 | return out, nil |
| 310 | } |
| 311 | |
| 312 | // ========================= |
| 313 | // PKIService Server Handler |
| 314 | // ========================= |
| 315 | |
| 316 | type pKIServiceServer struct { |
| 317 | PKIService |
| 318 | interceptor twirp.Interceptor |
| 319 | hooks *twirp.ServerHooks |
| 320 | pathPrefix string // prefix for routing |
| 321 | jsonSkipDefaults bool // do not include unpopulated fields (default values) in the response |
| 322 | jsonCamelCase bool // JSON fields are serialized as lowerCamelCase rather than keeping the original proto names |
| 323 | } |
| 324 | |
| 325 | // NewPKIServiceServer builds a TwirpServer that can be used as an http.Handler to handle |
| 326 | // HTTP requests that are routed to the right method in the provided svc implementation. |
| 327 | // The opts are twirp.ServerOption modifiers, for example twirp.WithServerHooks(hooks). |
| 328 | func NewPKIServiceServer(svc PKIService, opts ...interface{}) TwirpServer { |
| 329 | serverOpts := newServerOpts(opts) |
| 330 | |
| 331 | // Using ReadOpt allows backwards and forwards compatibility with new options in the future |
| 332 | jsonSkipDefaults := false |
| 333 | _ = serverOpts.ReadOpt("jsonSkipDefaults", &jsonSkipDefaults) |
| 334 | jsonCamelCase := false |
| 335 | _ = serverOpts.ReadOpt("jsonCamelCase", &jsonCamelCase) |
| 336 | var pathPrefix string |
| 337 | if ok := serverOpts.ReadOpt("pathPrefix", &pathPrefix); !ok { |
| 338 | pathPrefix = "/twirp" // default prefix |
| 339 | } |
| 340 | |
| 341 | return &pKIServiceServer{ |
| 342 | PKIService: svc, |
| 343 | hooks: serverOpts.Hooks, |
| 344 | interceptor: twirp.ChainInterceptors(serverOpts.Interceptors...), |
| 345 | pathPrefix: pathPrefix, |
| 346 | jsonSkipDefaults: jsonSkipDefaults, |
| 347 | jsonCamelCase: jsonCamelCase, |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | // writeError writes an HTTP response with a valid Twirp error format, and triggers hooks. |
| 352 | // If err is not a twirp.Error, it will get wrapped with twirp.InternalErrorWith(err) |
| 353 | func (s *pKIServiceServer) writeError(ctx context.Context, resp http.ResponseWriter, err error) { |
| 354 | writeError(ctx, resp, err, s.hooks) |
| 355 | } |
| 356 | |
| 357 | // handleRequestBodyError is used to handle error when the twirp server cannot read request |
| 358 | func (s *pKIServiceServer) handleRequestBodyError(ctx context.Context, resp http.ResponseWriter, msg string, err error) { |
| 359 | if context.Canceled == ctx.Err() { |
| 360 | s.writeError(ctx, resp, twirp.NewError(twirp.Canceled, "failed to read request: context canceled")) |
| 361 | return |
| 362 | } |
| 363 | if context.DeadlineExceeded == ctx.Err() { |
| 364 | s.writeError(ctx, resp, twirp.NewError(twirp.DeadlineExceeded, "failed to read request: deadline exceeded")) |
| 365 | return |
| 366 | } |
| 367 | s.writeError(ctx, resp, twirp.WrapError(malformedRequestError(msg), err)) |
| 368 | } |
| 369 | |
| 370 | // PKIServicePathPrefix is a convenience constant that may identify URL paths. |
| 371 | // Should be used with caution, it only matches routes generated by Twirp Go clients, |
| 372 | // with the default "/twirp" prefix and default CamelCase service and method names. |
| 373 | // More info: https://twitchtv.github.io/twirp/docs/routing.html |
| 374 | const PKIServicePathPrefix = "/twirp/protocol.PKIService/" |
| 375 | |
| 376 | func (s *pKIServiceServer) ServeHTTP(resp http.ResponseWriter, req *http.Request) { |
| 377 | ctx := req.Context() |
| 378 | ctx = ctxsetters.WithPackageName(ctx, "protocol") |
| 379 | ctx = ctxsetters.WithServiceName(ctx, "PKIService") |
| 380 | ctx = ctxsetters.WithResponseWriter(ctx, resp) |
| 381 | |
| 382 | var err error |
| 383 | ctx, err = callRequestReceived(ctx, s.hooks) |
| 384 | if err != nil { |
| 385 | s.writeError(ctx, resp, err) |
| 386 | return |
| 387 | } |
| 388 | |
| 389 | if req.Method != "POST" { |
| 390 | msg := fmt.Sprintf("unsupported method %q (only POST is allowed)", req.Method) |
| 391 | s.writeError(ctx, resp, badRouteError(msg, req.Method, req.URL.Path)) |
| 392 | return |
| 393 | } |
| 394 | |
| 395 | // Verify path format: [<prefix>]/<package>.<Service>/<Method> |
| 396 | prefix, pkgService, method := parseTwirpPath(req.URL.Path) |
| 397 | if pkgService != "protocol.PKIService" { |
| 398 | msg := fmt.Sprintf("no handler for path %q", req.URL.Path) |
| 399 | s.writeError(ctx, resp, badRouteError(msg, req.Method, req.URL.Path)) |
| 400 | return |
| 401 | } |
| 402 | if prefix != s.pathPrefix { |
| 403 | msg := fmt.Sprintf("invalid path prefix %q, expected %q, on path %q", prefix, s.pathPrefix, req.URL.Path) |
| 404 | s.writeError(ctx, resp, badRouteError(msg, req.Method, req.URL.Path)) |
| 405 | return |
| 406 | } |
| 407 | |
| 408 | switch method { |
| 409 | case "RequestCertificate": |
| 410 | s.serveRequestCertificate(ctx, resp, req) |
| 411 | return |
| 412 | case "RenewCertificate": |
| 413 | s.serveRenewCertificate(ctx, resp, req) |
| 414 | return |
| 415 | default: |
| 416 | msg := fmt.Sprintf("no handler for path %q", req.URL.Path) |
| 417 | s.writeError(ctx, resp, badRouteError(msg, req.Method, req.URL.Path)) |
| 418 | return |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | func (s *pKIServiceServer) serveRequestCertificate(ctx context.Context, resp http.ResponseWriter, req *http.Request) { |
| 423 | header := req.Header.Get("Content-Type") |
| 424 | i := strings.Index(header, ";") |
| 425 | if i == -1 { |
| 426 | i = len(header) |
| 427 | } |
| 428 | switch strings.TrimSpace(strings.ToLower(header[:i])) { |
| 429 | case "application/json": |
| 430 | s.serveRequestCertificateJSON(ctx, resp, req) |
| 431 | case "application/protobuf": |
| 432 | s.serveRequestCertificateProtobuf(ctx, resp, req) |
| 433 | default: |
| 434 | msg := fmt.Sprintf("unexpected Content-Type: %q", req.Header.Get("Content-Type")) |
| 435 | twerr := badRouteError(msg, req.Method, req.URL.Path) |
| 436 | s.writeError(ctx, resp, twerr) |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | func (s *pKIServiceServer) serveRequestCertificateJSON(ctx context.Context, resp http.ResponseWriter, req *http.Request) { |
| 441 | var err error |
| 442 | ctx = ctxsetters.WithMethodName(ctx, "RequestCertificate") |
| 443 | ctx, err = callRequestRouted(ctx, s.hooks) |
| 444 | if err != nil { |
| 445 | s.writeError(ctx, resp, err) |
| 446 | return |
| 447 | } |
| 448 | |
| 449 | d := json.NewDecoder(req.Body) |
| 450 | rawReqBody := json.RawMessage{} |
| 451 | if err := d.Decode(&rawReqBody); err != nil { |
| 452 | s.handleRequestBodyError(ctx, resp, "the json request could not be decoded", err) |
| 453 | return |
| 454 | } |
| 455 | reqContent := new(CertificateRequest) |
| 456 | unmarshaler := protojson.UnmarshalOptions{DiscardUnknown: true} |
| 457 | if err = unmarshaler.Unmarshal(rawReqBody, reqContent); err != nil { |
| 458 | s.handleRequestBodyError(ctx, resp, "the json request could not be decoded", err) |
| 459 | return |
| 460 | } |
| 461 | |
| 462 | handler := s.PKIService.RequestCertificate |
| 463 | if s.interceptor != nil { |
| 464 | handler = func(ctx context.Context, req *CertificateRequest) (*CertificateResponse, error) { |
| 465 | resp, err := s.interceptor( |
| 466 | func(ctx context.Context, req interface{}) (interface{}, error) { |
| 467 | typedReq, ok := req.(*CertificateRequest) |
| 468 | if !ok { |
| 469 | return nil, twirp.InternalError("failed type assertion req.(*CertificateRequest) when calling interceptor") |
| 470 | } |
| 471 | return s.PKIService.RequestCertificate(ctx, typedReq) |
| 472 | }, |
| 473 | )(ctx, req) |
| 474 | if resp != nil { |
| 475 | typedResp, ok := resp.(*CertificateResponse) |
| 476 | if !ok { |
| 477 | return nil, twirp.InternalError("failed type assertion resp.(*CertificateResponse) when calling interceptor") |
| 478 | } |
| 479 | return typedResp, err |
| 480 | } |
| 481 | return nil, err |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | // Call service method |
| 486 | var respContent *CertificateResponse |
| 487 | func() { |
| 488 | defer ensurePanicResponses(ctx, resp, s.hooks) |
| 489 | respContent, err = handler(ctx, reqContent) |
| 490 | }() |
| 491 | |
| 492 | if err != nil { |
| 493 | s.writeError(ctx, resp, err) |
| 494 | return |
| 495 | } |
| 496 | if respContent == nil { |
| 497 | s.writeError(ctx, resp, twirp.InternalError("received a nil *CertificateResponse and nil error while calling RequestCertificate. nil responses are not supported")) |
| 498 | return |
| 499 | } |
| 500 | |
| 501 | ctx = callResponsePrepared(ctx, s.hooks) |
| 502 | |
| 503 | marshaler := &protojson.MarshalOptions{UseProtoNames: !s.jsonCamelCase, EmitUnpopulated: !s.jsonSkipDefaults} |
| 504 | respBytes, err := marshaler.Marshal(respContent) |
| 505 | if err != nil { |
| 506 | s.writeError(ctx, resp, wrapInternal(err, "failed to marshal json response")) |
| 507 | return |
| 508 | } |
| 509 | |
| 510 | ctx = ctxsetters.WithStatusCode(ctx, http.StatusOK) |
| 511 | resp.Header().Set("Content-Type", "application/json") |
| 512 | resp.Header().Set("Content-Length", strconv.Itoa(len(respBytes))) |
| 513 | resp.WriteHeader(http.StatusOK) |
| 514 | |
| 515 | if n, err := resp.Write(respBytes); err != nil { |
| 516 | msg := fmt.Sprintf("failed to write response, %d of %d bytes written: %s", n, len(respBytes), err.Error()) |
| 517 | twerr := twirp.NewError(twirp.Unknown, msg) |
| 518 | ctx = callError(ctx, s.hooks, twerr) |
| 519 | } |
| 520 | callResponseSent(ctx, s.hooks) |
| 521 | } |
| 522 | |
| 523 | func (s *pKIServiceServer) serveRequestCertificateProtobuf(ctx context.Context, resp http.ResponseWriter, req *http.Request) { |
| 524 | var err error |
| 525 | ctx = ctxsetters.WithMethodName(ctx, "RequestCertificate") |
| 526 | ctx, err = callRequestRouted(ctx, s.hooks) |
| 527 | if err != nil { |
| 528 | s.writeError(ctx, resp, err) |
| 529 | return |
| 530 | } |
| 531 | |
| 532 | buf, err := io.ReadAll(req.Body) |
| 533 | if err != nil { |
| 534 | s.handleRequestBodyError(ctx, resp, "failed to read request body", err) |
| 535 | return |
| 536 | } |
| 537 | reqContent := new(CertificateRequest) |
| 538 | if err = reqContent.UnmarshalVT(buf); err != nil { |
| 539 | s.writeError(ctx, resp, malformedRequestError("the protobuf request could not be decoded")) |
| 540 | return |
| 541 | } |
| 542 | |
| 543 | handler := s.PKIService.RequestCertificate |
| 544 | if s.interceptor != nil { |
| 545 | handler = func(ctx context.Context, req *CertificateRequest) (*CertificateResponse, error) { |
| 546 | resp, err := s.interceptor( |
| 547 | func(ctx context.Context, req interface{}) (interface{}, error) { |
| 548 | typedReq, ok := req.(*CertificateRequest) |
| 549 | if !ok { |
| 550 | return nil, twirp.InternalError("failed type assertion req.(*CertificateRequest) when calling interceptor") |
| 551 | } |
| 552 | return s.PKIService.RequestCertificate(ctx, typedReq) |
| 553 | }, |
| 554 | )(ctx, req) |
| 555 | if resp != nil { |
| 556 | typedResp, ok := resp.(*CertificateResponse) |
| 557 | if !ok { |
| 558 | return nil, twirp.InternalError("failed type assertion resp.(*CertificateResponse) when calling interceptor") |
| 559 | } |
| 560 | return typedResp, err |
| 561 | } |
| 562 | return nil, err |
| 563 | } |
| 564 | } |
| 565 | |
| 566 | // Call service method |
| 567 | var respContent *CertificateResponse |
| 568 | func() { |
| 569 | defer ensurePanicResponses(ctx, resp, s.hooks) |
| 570 | respContent, err = handler(ctx, reqContent) |
| 571 | }() |
| 572 | |
| 573 | if err != nil { |
| 574 | s.writeError(ctx, resp, err) |
| 575 | return |
| 576 | } |
| 577 | if respContent == nil { |
| 578 | s.writeError(ctx, resp, twirp.InternalError("received a nil *CertificateResponse and nil error while calling RequestCertificate. nil responses are not supported")) |
| 579 | return |
| 580 | } |
| 581 | |
| 582 | ctx = callResponsePrepared(ctx, s.hooks) |
| 583 | |
| 584 | respBytes, err := respContent.MarshalVT() |
| 585 | if err != nil { |
| 586 | s.writeError(ctx, resp, wrapInternal(err, "failed to marshal proto response")) |
| 587 | return |
| 588 | } |
| 589 | |
| 590 | ctx = ctxsetters.WithStatusCode(ctx, http.StatusOK) |
| 591 | resp.Header().Set("Content-Type", "application/protobuf") |
| 592 | resp.Header().Set("Content-Length", strconv.Itoa(len(respBytes))) |
| 593 | resp.WriteHeader(http.StatusOK) |
| 594 | if n, err := resp.Write(respBytes); err != nil { |
| 595 | msg := fmt.Sprintf("failed to write response, %d of %d bytes written: %s", n, len(respBytes), err.Error()) |
| 596 | twerr := twirp.NewError(twirp.Unknown, msg) |
| 597 | ctx = callError(ctx, s.hooks, twerr) |
| 598 | } |
| 599 | callResponseSent(ctx, s.hooks) |
| 600 | } |
| 601 | |
| 602 | func (s *pKIServiceServer) serveRenewCertificate(ctx context.Context, resp http.ResponseWriter, req *http.Request) { |
| 603 | header := req.Header.Get("Content-Type") |
| 604 | i := strings.Index(header, ";") |
| 605 | if i == -1 { |
| 606 | i = len(header) |
| 607 | } |
| 608 | switch strings.TrimSpace(strings.ToLower(header[:i])) { |
| 609 | case "application/json": |
| 610 | s.serveRenewCertificateJSON(ctx, resp, req) |
| 611 | case "application/protobuf": |
| 612 | s.serveRenewCertificateProtobuf(ctx, resp, req) |
| 613 | default: |
| 614 | msg := fmt.Sprintf("unexpected Content-Type: %q", req.Header.Get("Content-Type")) |
| 615 | twerr := badRouteError(msg, req.Method, req.URL.Path) |
| 616 | s.writeError(ctx, resp, twerr) |
| 617 | } |
| 618 | } |
| 619 | |
| 620 | func (s *pKIServiceServer) serveRenewCertificateJSON(ctx context.Context, resp http.ResponseWriter, req *http.Request) { |
| 621 | var err error |
| 622 | ctx = ctxsetters.WithMethodName(ctx, "RenewCertificate") |
| 623 | ctx, err = callRequestRouted(ctx, s.hooks) |
| 624 | if err != nil { |
| 625 | s.writeError(ctx, resp, err) |
| 626 | return |
| 627 | } |
| 628 | |
| 629 | d := json.NewDecoder(req.Body) |
| 630 | rawReqBody := json.RawMessage{} |
| 631 | if err := d.Decode(&rawReqBody); err != nil { |
| 632 | s.handleRequestBodyError(ctx, resp, "the json request could not be decoded", err) |
| 633 | return |
| 634 | } |
| 635 | reqContent := new(CertificateRenewalRequest) |
| 636 | unmarshaler := protojson.UnmarshalOptions{DiscardUnknown: true} |
| 637 | if err = unmarshaler.Unmarshal(rawReqBody, reqContent); err != nil { |
| 638 | s.handleRequestBodyError(ctx, resp, "the json request could not be decoded", err) |
| 639 | return |
| 640 | } |
| 641 | |
| 642 | handler := s.PKIService.RenewCertificate |
| 643 | if s.interceptor != nil { |
| 644 | handler = func(ctx context.Context, req *CertificateRenewalRequest) (*CertificateResponse, error) { |
| 645 | resp, err := s.interceptor( |
| 646 | func(ctx context.Context, req interface{}) (interface{}, error) { |
| 647 | typedReq, ok := req.(*CertificateRenewalRequest) |
| 648 | if !ok { |
| 649 | return nil, twirp.InternalError("failed type assertion req.(*CertificateRenewalRequest) when calling interceptor") |
| 650 | } |
| 651 | return s.PKIService.RenewCertificate(ctx, typedReq) |
| 652 | }, |
| 653 | )(ctx, req) |
| 654 | if resp != nil { |
| 655 | typedResp, ok := resp.(*CertificateResponse) |
| 656 | if !ok { |
| 657 | return nil, twirp.InternalError("failed type assertion resp.(*CertificateResponse) when calling interceptor") |
| 658 | } |
| 659 | return typedResp, err |
| 660 | } |
| 661 | return nil, err |
| 662 | } |
| 663 | } |
| 664 | |
| 665 | // Call service method |
| 666 | var respContent *CertificateResponse |
| 667 | func() { |
| 668 | defer ensurePanicResponses(ctx, resp, s.hooks) |
| 669 | respContent, err = handler(ctx, reqContent) |
| 670 | }() |
| 671 | |
| 672 | if err != nil { |
| 673 | s.writeError(ctx, resp, err) |
| 674 | return |
| 675 | } |
| 676 | if respContent == nil { |
| 677 | s.writeError(ctx, resp, twirp.InternalError("received a nil *CertificateResponse and nil error while calling RenewCertificate. nil responses are not supported")) |
| 678 | return |
| 679 | } |
| 680 | |
| 681 | ctx = callResponsePrepared(ctx, s.hooks) |
| 682 | |
| 683 | marshaler := &protojson.MarshalOptions{UseProtoNames: !s.jsonCamelCase, EmitUnpopulated: !s.jsonSkipDefaults} |
| 684 | respBytes, err := marshaler.Marshal(respContent) |
| 685 | if err != nil { |
| 686 | s.writeError(ctx, resp, wrapInternal(err, "failed to marshal json response")) |
| 687 | return |
| 688 | } |
| 689 | |
| 690 | ctx = ctxsetters.WithStatusCode(ctx, http.StatusOK) |
| 691 | resp.Header().Set("Content-Type", "application/json") |
| 692 | resp.Header().Set("Content-Length", strconv.Itoa(len(respBytes))) |
| 693 | resp.WriteHeader(http.StatusOK) |
| 694 | |
| 695 | if n, err := resp.Write(respBytes); err != nil { |
| 696 | msg := fmt.Sprintf("failed to write response, %d of %d bytes written: %s", n, len(respBytes), err.Error()) |
| 697 | twerr := twirp.NewError(twirp.Unknown, msg) |
| 698 | ctx = callError(ctx, s.hooks, twerr) |
| 699 | } |
| 700 | callResponseSent(ctx, s.hooks) |
| 701 | } |
| 702 | |
| 703 | func (s *pKIServiceServer) serveRenewCertificateProtobuf(ctx context.Context, resp http.ResponseWriter, req *http.Request) { |
| 704 | var err error |
| 705 | ctx = ctxsetters.WithMethodName(ctx, "RenewCertificate") |
| 706 | ctx, err = callRequestRouted(ctx, s.hooks) |
| 707 | if err != nil { |
| 708 | s.writeError(ctx, resp, err) |
| 709 | return |
| 710 | } |
| 711 | |
| 712 | buf, err := io.ReadAll(req.Body) |
| 713 | if err != nil { |
| 714 | s.handleRequestBodyError(ctx, resp, "failed to read request body", err) |
| 715 | return |
| 716 | } |
| 717 | reqContent := new(CertificateRenewalRequest) |
| 718 | if err = reqContent.UnmarshalVT(buf); err != nil { |
| 719 | s.writeError(ctx, resp, malformedRequestError("the protobuf request could not be decoded")) |
| 720 | return |
| 721 | } |
| 722 | |
| 723 | handler := s.PKIService.RenewCertificate |
| 724 | if s.interceptor != nil { |
| 725 | handler = func(ctx context.Context, req *CertificateRenewalRequest) (*CertificateResponse, error) { |
| 726 | resp, err := s.interceptor( |
| 727 | func(ctx context.Context, req interface{}) (interface{}, error) { |
| 728 | typedReq, ok := req.(*CertificateRenewalRequest) |
| 729 | if !ok { |
| 730 | return nil, twirp.InternalError("failed type assertion req.(*CertificateRenewalRequest) when calling interceptor") |
| 731 | } |
| 732 | return s.PKIService.RenewCertificate(ctx, typedReq) |
| 733 | }, |
| 734 | )(ctx, req) |
| 735 | if resp != nil { |
| 736 | typedResp, ok := resp.(*CertificateResponse) |
| 737 | if !ok { |
| 738 | return nil, twirp.InternalError("failed type assertion resp.(*CertificateResponse) when calling interceptor") |
| 739 | } |
| 740 | return typedResp, err |
| 741 | } |
| 742 | return nil, err |
| 743 | } |
| 744 | } |
| 745 | |
| 746 | // Call service method |
| 747 | var respContent *CertificateResponse |
| 748 | func() { |
| 749 | defer ensurePanicResponses(ctx, resp, s.hooks) |
| 750 | respContent, err = handler(ctx, reqContent) |
| 751 | }() |
| 752 | |
| 753 | if err != nil { |
| 754 | s.writeError(ctx, resp, err) |
| 755 | return |
| 756 | } |
| 757 | if respContent == nil { |
| 758 | s.writeError(ctx, resp, twirp.InternalError("received a nil *CertificateResponse and nil error while calling RenewCertificate. nil responses are not supported")) |
| 759 | return |
| 760 | } |
| 761 | |
| 762 | ctx = callResponsePrepared(ctx, s.hooks) |
| 763 | |
| 764 | respBytes, err := respContent.MarshalVT() |
| 765 | if err != nil { |
| 766 | s.writeError(ctx, resp, wrapInternal(err, "failed to marshal proto response")) |
| 767 | return |
| 768 | } |
| 769 | |
| 770 | ctx = ctxsetters.WithStatusCode(ctx, http.StatusOK) |
| 771 | resp.Header().Set("Content-Type", "application/protobuf") |
| 772 | resp.Header().Set("Content-Length", strconv.Itoa(len(respBytes))) |
| 773 | resp.WriteHeader(http.StatusOK) |
| 774 | if n, err := resp.Write(respBytes); err != nil { |
| 775 | msg := fmt.Sprintf("failed to write response, %d of %d bytes written: %s", n, len(respBytes), err.Error()) |
| 776 | twerr := twirp.NewError(twirp.Unknown, msg) |
| 777 | ctx = callError(ctx, s.hooks, twerr) |
| 778 | } |
| 779 | callResponseSent(ctx, s.hooks) |
| 780 | } |
| 781 | |
| 782 | func (s *pKIServiceServer) ServiceDescriptor() ([]byte, int) { |
| 783 | return twirpFileDescriptor4, 0 |
| 784 | } |
| 785 | |
| 786 | func (s *pKIServiceServer) ProtocGenTwirpVersion() string { |
| 787 | return "v8.1.3" |
| 788 | } |
| 789 | |
| 790 | // PathPrefix returns the base service path, in the form: "/<prefix>/<package>.<Service>/" |
| 791 | // that is everything in a Twirp route except for the <Method>. This can be used for routing, |
| 792 | // for example to identify the requests that are targeted to this service in a mux. |
| 793 | func (s *pKIServiceServer) PathPrefix() string { |
| 794 | return baseServicePath(s.pathPrefix, "protocol", "PKIService") |
| 795 | } |
| 796 | |
| 797 | var twirpFileDescriptor4 = []byte{ |
| 798 | // 284 bytes of a gzipped FileDescriptorProto |
| 799 | 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x9c, 0x91, 0xcf, 0x4a, 0xc3, 0x40, |
| 800 | 0x10, 0xc6, 0x59, 0x41, 0x2d, 0xa3, 0x48, 0x59, 0x15, 0xda, 0xa2, 0x50, 0xe2, 0xc1, 0x40, 0x21, |
| 801 | 0x81, 0xfa, 0x04, 0xfe, 0x39, 0x28, 0x3d, 0x18, 0x23, 0x28, 0x78, 0x29, 0x71, 0x9d, 0x94, 0xd0, |
| 802 | 0x24, 0xb3, 0x4e, 0xb6, 0xf6, 0xd1, 0x7c, 0x3d, 0xc9, 0x26, 0xb6, 0x09, 0x52, 0x84, 0x9e, 0x76, |
| 803 | 0x98, 0x6f, 0xbf, 0xdf, 0x0c, 0xdf, 0xc0, 0x49, 0xa1, 0x51, 0xf9, 0x9a, 0xc9, 0x90, 0xaf, 0xe7, |
| 804 | 0x89, 0x67, 0x2b, 0xd9, 0xb1, 0x8f, 0xa2, 0x74, 0xd0, 0xd2, 0x69, 0x59, 0xe9, 0xce, 0x35, 0xc8, |
| 805 | 0x5b, 0x64, 0x93, 0xc4, 0x89, 0x8a, 0x0c, 0x86, 0xf8, 0xb9, 0xc0, 0xc2, 0xc8, 0x11, 0xec, 0x6a, |
| 806 | 0x26, 0x8a, 0x7b, 0x62, 0x28, 0xdc, 0x83, 0xf1, 0xa9, 0xf7, 0x4b, 0xf1, 0x82, 0xb2, 0xfd, 0x18, |
| 807 | 0xbf, 0x12, 0xcf, 0xc3, 0xea, 0x8f, 0xc3, 0xd0, 0x6f, 0x21, 0x72, 0x5c, 0x46, 0xe9, 0x36, 0x24, |
| 808 | 0xe9, 0x42, 0x57, 0x2d, 0x98, 0x31, 0x37, 0x53, 0x85, 0x6c, 0xa6, 0x1f, 0xc8, 0xbd, 0x9d, 0xa1, |
| 809 | 0x70, 0x0f, 0xc3, 0xa3, 0xba, 0x5f, 0x0e, 0xba, 0x43, 0x76, 0x26, 0x70, 0xdc, 0x9a, 0x59, 0x68, |
| 810 | 0xca, 0x0b, 0x94, 0x7d, 0xe8, 0xac, 0x8c, 0xc2, 0x1a, 0xf7, 0x55, 0xe5, 0x58, 0x49, 0x1a, 0xb3, |
| 811 | 0x9a, 0x69, 0xa5, 0x00, 0xb3, 0xf1, 0xb7, 0x00, 0x08, 0x26, 0x0f, 0xcf, 0xc8, 0x5f, 0x89, 0x42, |
| 812 | 0xf9, 0x04, 0xb2, 0xde, 0xbe, 0x31, 0x42, 0x9e, 0xad, 0x37, 0xff, 0x1b, 0xd8, 0xe0, 0x7c, 0x83, |
| 813 | 0x5a, 0xef, 0xf5, 0x02, 0x5d, 0x9b, 0x4b, 0x13, 0x78, 0xb1, 0xc1, 0xd2, 0x8c, 0xef, 0x1f, 0xee, |
| 814 | 0xcd, 0xe8, 0x5e, 0xbc, 0x5d, 0xce, 0xc8, 0xcb, 0x12, 0x8e, 0x66, 0x58, 0xe8, 0x48, 0xa1, 0xa7, |
| 815 | 0xc8, 0x2f, 0x2f, 0x6d, 0x90, 0xfd, 0xf5, 0xc5, 0x15, 0xa5, 0xef, 0x7b, 0xb6, 0xba, 0xfa, 0x09, |
| 816 | 0x00, 0x00, 0xff, 0xff, 0xcf, 0x3f, 0xc8, 0x07, 0x29, 0x02, 0x00, 0x00, |
| 817 | } |