Skip to content
File

Blob: spec/protocol/keyless.twirp.go

go825 lines
1// Code generated by protoc-gen-twirp v8.1.3, DO NOT EDIT.
2// source: spec/proto/keyless.proto
3 
4package protocol
5 
6import context "context"
7import fmt "fmt"
8import http "net/http"
9import io "io"
10import json "encoding/json"
11import strconv "strconv"
12import strings "strings"
13 
14import protojson "google.golang.org/protobuf/encoding/protojson"
15import twirp "github.com/twitchtv/twirp"
16import 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
22const _ = twirp.TwirpPackageMinVersion_8_1_0
23 
24// ========================
25// KeylessService Interface
26// ========================
27 
28type KeylessService interface {
29 GetCertificate(context.Context, *KeylessGetCertificateRequest) (*KeylessGetCertificateResponse, error)
30 
31 Sign(context.Context, *KeylessSignRequest) (*KeylessSignResponse, error)
32}
33 
34// ==============================
35// KeylessService Protobuf Client
36// ==============================
37 
38type keylessServiceProtobufClient struct {
39 client HTTPClient
40 urls [2]string
41 interceptor twirp.Interceptor
42 opts twirp.ClientOptions
43}
44 
45// NewKeylessServiceProtobufClient creates a Protobuf client that implements the KeylessService interface.
46// It communicates using Protobuf and can be configured with a custom HTTPClient.
47func NewKeylessServiceProtobufClient(baseURL string, client HTTPClient, opts ...twirp.ClientOption) KeylessService {
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", "KeylessService")
68 urls := [2]string{
69 serviceURL + "GetCertificate",
70 serviceURL + "Sign",
71 }
72 
73 return &keylessServiceProtobufClient{
74 client: client,
75 urls: urls,
76 interceptor: twirp.ChainInterceptors(clientOpts.Interceptors...),
77 opts: clientOpts,
78 }
79}
80 
81func (c *keylessServiceProtobufClient) GetCertificate(ctx context.Context, in *KeylessGetCertificateRequest) (*KeylessGetCertificateResponse, error) {
82 ctx = ctxsetters.WithPackageName(ctx, "protocol")
83 ctx = ctxsetters.WithServiceName(ctx, "KeylessService")
84 ctx = ctxsetters.WithMethodName(ctx, "GetCertificate")
85 caller := c.callGetCertificate
86 if c.interceptor != nil {
87 caller = func(ctx context.Context, req *KeylessGetCertificateRequest) (*KeylessGetCertificateResponse, error) {
88 resp, err := c.interceptor(
89 func(ctx context.Context, req interface{}) (interface{}, error) {
90 typedReq, ok := req.(*KeylessGetCertificateRequest)
91 if !ok {
92 return nil, twirp.InternalError("failed type assertion req.(*KeylessGetCertificateRequest) when calling interceptor")
93 }
94 return c.callGetCertificate(ctx, typedReq)
95 },
96 )(ctx, req)
97 if resp != nil {
98 typedResp, ok := resp.(*KeylessGetCertificateResponse)
99 if !ok {
100 return nil, twirp.InternalError("failed type assertion resp.(*KeylessGetCertificateResponse) when calling interceptor")
101 }
102 return typedResp, err
103 }
104 return nil, err
105 }
106 }
107 return caller(ctx, in)
108}
109 
110func (c *keylessServiceProtobufClient) callGetCertificate(ctx context.Context, in *KeylessGetCertificateRequest) (*KeylessGetCertificateResponse, error) {
111 out := new(KeylessGetCertificateResponse)
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 
127func (c *keylessServiceProtobufClient) Sign(ctx context.Context, in *KeylessSignRequest) (*KeylessSignResponse, error) {
128 ctx = ctxsetters.WithPackageName(ctx, "protocol")
129 ctx = ctxsetters.WithServiceName(ctx, "KeylessService")
130 ctx = ctxsetters.WithMethodName(ctx, "Sign")
131 caller := c.callSign
132 if c.interceptor != nil {
133 caller = func(ctx context.Context, req *KeylessSignRequest) (*KeylessSignResponse, error) {
134 resp, err := c.interceptor(
135 func(ctx context.Context, req interface{}) (interface{}, error) {
136 typedReq, ok := req.(*KeylessSignRequest)
137 if !ok {
138 return nil, twirp.InternalError("failed type assertion req.(*KeylessSignRequest) when calling interceptor")
139 }
140 return c.callSign(ctx, typedReq)
141 },
142 )(ctx, req)
143 if resp != nil {
144 typedResp, ok := resp.(*KeylessSignResponse)
145 if !ok {
146 return nil, twirp.InternalError("failed type assertion resp.(*KeylessSignResponse) when calling interceptor")
147 }
148 return typedResp, err
149 }
150 return nil, err
151 }
152 }
153 return caller(ctx, in)
154}
155 
156func (c *keylessServiceProtobufClient) callSign(ctx context.Context, in *KeylessSignRequest) (*KeylessSignResponse, error) {
157 out := new(KeylessSignResponse)
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// KeylessService JSON Client
175// ==========================
176 
177type keylessServiceJSONClient struct {
178 client HTTPClient
179 urls [2]string
180 interceptor twirp.Interceptor
181 opts twirp.ClientOptions
182}
183 
184// NewKeylessServiceJSONClient creates a JSON client that implements the KeylessService interface.
185// It communicates using JSON and can be configured with a custom HTTPClient.
186func NewKeylessServiceJSONClient(baseURL string, client HTTPClient, opts ...twirp.ClientOption) KeylessService {
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", "KeylessService")
207 urls := [2]string{
208 serviceURL + "GetCertificate",
209 serviceURL + "Sign",
210 }
211 
212 return &keylessServiceJSONClient{
213 client: client,
214 urls: urls,
215 interceptor: twirp.ChainInterceptors(clientOpts.Interceptors...),
216 opts: clientOpts,
217 }
218}
219 
220func (c *keylessServiceJSONClient) GetCertificate(ctx context.Context, in *KeylessGetCertificateRequest) (*KeylessGetCertificateResponse, error) {
221 ctx = ctxsetters.WithPackageName(ctx, "protocol")
222 ctx = ctxsetters.WithServiceName(ctx, "KeylessService")
223 ctx = ctxsetters.WithMethodName(ctx, "GetCertificate")
224 caller := c.callGetCertificate
225 if c.interceptor != nil {
226 caller = func(ctx context.Context, req *KeylessGetCertificateRequest) (*KeylessGetCertificateResponse, error) {
227 resp, err := c.interceptor(
228 func(ctx context.Context, req interface{}) (interface{}, error) {
229 typedReq, ok := req.(*KeylessGetCertificateRequest)
230 if !ok {
231 return nil, twirp.InternalError("failed type assertion req.(*KeylessGetCertificateRequest) when calling interceptor")
232 }
233 return c.callGetCertificate(ctx, typedReq)
234 },
235 )(ctx, req)
236 if resp != nil {
237 typedResp, ok := resp.(*KeylessGetCertificateResponse)
238 if !ok {
239 return nil, twirp.InternalError("failed type assertion resp.(*KeylessGetCertificateResponse) when calling interceptor")
240 }
241 return typedResp, err
242 }
243 return nil, err
244 }
245 }
246 return caller(ctx, in)
247}
248 
249func (c *keylessServiceJSONClient) callGetCertificate(ctx context.Context, in *KeylessGetCertificateRequest) (*KeylessGetCertificateResponse, error) {
250 out := new(KeylessGetCertificateResponse)
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 
266func (c *keylessServiceJSONClient) Sign(ctx context.Context, in *KeylessSignRequest) (*KeylessSignResponse, error) {
267 ctx = ctxsetters.WithPackageName(ctx, "protocol")
268 ctx = ctxsetters.WithServiceName(ctx, "KeylessService")
269 ctx = ctxsetters.WithMethodName(ctx, "Sign")
270 caller := c.callSign
271 if c.interceptor != nil {
272 caller = func(ctx context.Context, req *KeylessSignRequest) (*KeylessSignResponse, error) {
273 resp, err := c.interceptor(
274 func(ctx context.Context, req interface{}) (interface{}, error) {
275 typedReq, ok := req.(*KeylessSignRequest)
276 if !ok {
277 return nil, twirp.InternalError("failed type assertion req.(*KeylessSignRequest) when calling interceptor")
278 }
279 return c.callSign(ctx, typedReq)
280 },
281 )(ctx, req)
282 if resp != nil {
283 typedResp, ok := resp.(*KeylessSignResponse)
284 if !ok {
285 return nil, twirp.InternalError("failed type assertion resp.(*KeylessSignResponse) when calling interceptor")
286 }
287 return typedResp, err
288 }
289 return nil, err
290 }
291 }
292 return caller(ctx, in)
293}
294 
295func (c *keylessServiceJSONClient) callSign(ctx context.Context, in *KeylessSignRequest) (*KeylessSignResponse, error) {
296 out := new(KeylessSignResponse)
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// KeylessService Server Handler
314// =============================
315 
316type keylessServiceServer struct {
317 KeylessService
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// NewKeylessServiceServer 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).
328func NewKeylessServiceServer(svc KeylessService, 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 &keylessServiceServer{
342 KeylessService: 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)
353func (s *keylessServiceServer) 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
358func (s *keylessServiceServer) 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// KeylessServicePathPrefix 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
374const KeylessServicePathPrefix = "/twirp/protocol.KeylessService/"
375 
376func (s *keylessServiceServer) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
377 ctx := req.Context()
378 ctx = ctxsetters.WithPackageName(ctx, "protocol")
379 ctx = ctxsetters.WithServiceName(ctx, "KeylessService")
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.KeylessService" {
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 "GetCertificate":
410 s.serveGetCertificate(ctx, resp, req)
411 return
412 case "Sign":
413 s.serveSign(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 
422func (s *keylessServiceServer) serveGetCertificate(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.serveGetCertificateJSON(ctx, resp, req)
431 case "application/protobuf":
432 s.serveGetCertificateProtobuf(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 
440func (s *keylessServiceServer) serveGetCertificateJSON(ctx context.Context, resp http.ResponseWriter, req *http.Request) {
441 var err error
442 ctx = ctxsetters.WithMethodName(ctx, "GetCertificate")
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(KeylessGetCertificateRequest)
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.KeylessService.GetCertificate
463 if s.interceptor != nil {
464 handler = func(ctx context.Context, req *KeylessGetCertificateRequest) (*KeylessGetCertificateResponse, error) {
465 resp, err := s.interceptor(
466 func(ctx context.Context, req interface{}) (interface{}, error) {
467 typedReq, ok := req.(*KeylessGetCertificateRequest)
468 if !ok {
469 return nil, twirp.InternalError("failed type assertion req.(*KeylessGetCertificateRequest) when calling interceptor")
470 }
471 return s.KeylessService.GetCertificate(ctx, typedReq)
472 },
473 )(ctx, req)
474 if resp != nil {
475 typedResp, ok := resp.(*KeylessGetCertificateResponse)
476 if !ok {
477 return nil, twirp.InternalError("failed type assertion resp.(*KeylessGetCertificateResponse) when calling interceptor")
478 }
479 return typedResp, err
480 }
481 return nil, err
482 }
483 }
484 
485 // Call service method
486 var respContent *KeylessGetCertificateResponse
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 *KeylessGetCertificateResponse and nil error while calling GetCertificate. 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 
523func (s *keylessServiceServer) serveGetCertificateProtobuf(ctx context.Context, resp http.ResponseWriter, req *http.Request) {
524 var err error
525 ctx = ctxsetters.WithMethodName(ctx, "GetCertificate")
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(KeylessGetCertificateRequest)
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.KeylessService.GetCertificate
544 if s.interceptor != nil {
545 handler = func(ctx context.Context, req *KeylessGetCertificateRequest) (*KeylessGetCertificateResponse, error) {
546 resp, err := s.interceptor(
547 func(ctx context.Context, req interface{}) (interface{}, error) {
548 typedReq, ok := req.(*KeylessGetCertificateRequest)
549 if !ok {
550 return nil, twirp.InternalError("failed type assertion req.(*KeylessGetCertificateRequest) when calling interceptor")
551 }
552 return s.KeylessService.GetCertificate(ctx, typedReq)
553 },
554 )(ctx, req)
555 if resp != nil {
556 typedResp, ok := resp.(*KeylessGetCertificateResponse)
557 if !ok {
558 return nil, twirp.InternalError("failed type assertion resp.(*KeylessGetCertificateResponse) when calling interceptor")
559 }
560 return typedResp, err
561 }
562 return nil, err
563 }
564 }
565 
566 // Call service method
567 var respContent *KeylessGetCertificateResponse
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 *KeylessGetCertificateResponse and nil error while calling GetCertificate. 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 
602func (s *keylessServiceServer) serveSign(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.serveSignJSON(ctx, resp, req)
611 case "application/protobuf":
612 s.serveSignProtobuf(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 
620func (s *keylessServiceServer) serveSignJSON(ctx context.Context, resp http.ResponseWriter, req *http.Request) {
621 var err error
622 ctx = ctxsetters.WithMethodName(ctx, "Sign")
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(KeylessSignRequest)
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.KeylessService.Sign
643 if s.interceptor != nil {
644 handler = func(ctx context.Context, req *KeylessSignRequest) (*KeylessSignResponse, error) {
645 resp, err := s.interceptor(
646 func(ctx context.Context, req interface{}) (interface{}, error) {
647 typedReq, ok := req.(*KeylessSignRequest)
648 if !ok {
649 return nil, twirp.InternalError("failed type assertion req.(*KeylessSignRequest) when calling interceptor")
650 }
651 return s.KeylessService.Sign(ctx, typedReq)
652 },
653 )(ctx, req)
654 if resp != nil {
655 typedResp, ok := resp.(*KeylessSignResponse)
656 if !ok {
657 return nil, twirp.InternalError("failed type assertion resp.(*KeylessSignResponse) when calling interceptor")
658 }
659 return typedResp, err
660 }
661 return nil, err
662 }
663 }
664 
665 // Call service method
666 var respContent *KeylessSignResponse
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 *KeylessSignResponse and nil error while calling Sign. 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 
703func (s *keylessServiceServer) serveSignProtobuf(ctx context.Context, resp http.ResponseWriter, req *http.Request) {
704 var err error
705 ctx = ctxsetters.WithMethodName(ctx, "Sign")
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(KeylessSignRequest)
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.KeylessService.Sign
724 if s.interceptor != nil {
725 handler = func(ctx context.Context, req *KeylessSignRequest) (*KeylessSignResponse, error) {
726 resp, err := s.interceptor(
727 func(ctx context.Context, req interface{}) (interface{}, error) {
728 typedReq, ok := req.(*KeylessSignRequest)
729 if !ok {
730 return nil, twirp.InternalError("failed type assertion req.(*KeylessSignRequest) when calling interceptor")
731 }
732 return s.KeylessService.Sign(ctx, typedReq)
733 },
734 )(ctx, req)
735 if resp != nil {
736 typedResp, ok := resp.(*KeylessSignResponse)
737 if !ok {
738 return nil, twirp.InternalError("failed type assertion resp.(*KeylessSignResponse) when calling interceptor")
739 }
740 return typedResp, err
741 }
742 return nil, err
743 }
744 }
745 
746 // Call service method
747 var respContent *KeylessSignResponse
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 *KeylessSignResponse and nil error while calling Sign. 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 
782func (s *keylessServiceServer) ServiceDescriptor() ([]byte, int) {
783 return twirpFileDescriptor2, 0
784}
785 
786func (s *keylessServiceServer) 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.
793func (s *keylessServiceServer) PathPrefix() string {
794 return baseServicePath(s.pathPrefix, "protocol", "KeylessService")
795}
796 
797var twirpFileDescriptor2 = []byte{
798 // 391 bytes of a gzipped FileDescriptorProto
799 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xac, 0x52, 0xdf, 0x8b, 0xd3, 0x40,
800 0x10, 0x76, 0xaf, 0xb5, 0xde, 0xcd, 0xc5, 0x12, 0xd6, 0x1f, 0x84, 0xd0, 0x83, 0x90, 0x07, 0x2f,
801 0x78, 0x90, 0x60, 0xea, 0x89, 0x4f, 0xe2, 0xd9, 0x07, 0x0b, 0x07, 0x3d, 0xc9, 0x21, 0x05, 0xdf,
802 0xd6, 0x75, 0xba, 0x5d, 0x9a, 0x66, 0xe3, 0xee, 0x56, 0xf1, 0x3f, 0xf3, 0x3f, 0xf3, 0x55, 0x9a,
803 0xa4, 0xc6, 0x50, 0xab, 0x2f, 0x3e, 0xed, 0xcc, 0xb7, 0x33, 0xdf, 0x7c, 0x33, 0x7c, 0xe0, 0x99,
804 0x12, 0x79, 0x52, 0x6a, 0x65, 0x55, 0xb2, 0xc2, 0x6f, 0x39, 0x1a, 0x13, 0x57, 0x19, 0x3d, 0xae,
805 0x1e, 0xae, 0x72, 0xff, 0xe1, 0x6f, 0x35, 0xa5, 0xfa, 0x5a, 0xff, 0x87, 0x02, 0x46, 0xd7, 0x75,
806 0xc3, 0x5b, 0xb4, 0x13, 0xd4, 0x56, 0x2e, 0x24, 0x67, 0x16, 0x33, 0xfc, 0xbc, 0x41, 0x63, 0xe9,
807 0x05, 0xdc, 0x2d, 0xb5, 0x52, 0x0b, 0x8f, 0x04, 0x24, 0x3a, 0x4d, 0x1f, 0xc5, 0x3b, 0xbe, 0xf8,
808 0xdd, 0x16, 0xbe, 0x59, 0xcc, 0x95, 0x5e, 0x65, 0x75, 0x0d, 0xf5, 0xe1, 0x78, 0xa9, 0x8c, 0x2d,
809 0xd8, 0x1a, 0xbd, 0xa3, 0x80, 0x44, 0x27, 0xd9, 0xaf, 0x3c, 0x9c, 0xc0, 0xd9, 0x81, 0x41, 0xa6,
810 0x54, 0x85, 0x41, 0x1a, 0x82, 0xc3, 0x5b, 0xd8, 0x78, 0x24, 0xe8, 0x45, 0x4e, 0xd6, 0xc1, 0xc2,
811 0x1f, 0x04, 0x68, 0xc3, 0x72, 0x2b, 0x45, 0xf1, 0xbf, 0x45, 0xd2, 0xc7, 0x30, 0xf8, 0x24, 0x05,
812 0x1a, 0xeb, 0xf5, 0x02, 0x12, 0x39, 0x59, 0x93, 0xd1, 0x57, 0xd0, 0x67, 0xb9, 0x50, 0x5e, 0x3f,
813 0x20, 0xd1, 0x30, 0x7d, 0xda, 0xf2, 0xef, 0x8b, 0x89, 0xa7, 0xcc, 0x2c, 0xaf, 0x72, 0xa1, 0xb4,
814 0xb4, 0xcb, 0x75, 0x56, 0xf5, 0x85, 0xaf, 0xe1, 0x7e, 0x07, 0xa6, 0xa7, 0x70, 0xef, 0xfd, 0xec,
815 0x7a, 0x76, 0x33, 0x9f, 0xb9, 0x77, 0x28, 0xc0, 0xe0, 0x76, 0x7a, 0x95, 0x5e, 0xbe, 0x70, 0x49,
816 0x13, 0x8f, 0x5f, 0x3e, 0x77, 0x8f, 0x9a, 0xf8, 0xf2, 0x59, 0xea, 0xf6, 0xc2, 0x31, 0x3c, 0xe8,
817 0xcc, 0x6a, 0x8e, 0x36, 0x82, 0x13, 0x23, 0x45, 0xc1, 0xec, 0x46, 0x63, 0xb5, 0xbd, 0x93, 0xb5,
818 0x40, 0xfa, 0x9d, 0xc0, 0x70, 0xd7, 0x85, 0xfa, 0x8b, 0xe4, 0x48, 0x19, 0x0c, 0xbb, 0xf7, 0xa7,
819 0x4f, 0xf6, 0xb6, 0xf9, 0xa3, 0x13, 0xfc, 0xf3, 0x7f, 0xd6, 0x35, 0x9a, 0x26, 0xd0, 0xdf, 0x6a,
820 0xa4, 0xa3, 0xbf, 0x9d, 0xc9, 0x3f, 0x3b, 0xf0, 0x5b, 0x93, 0xbc, 0xb9, 0x98, 0x92, 0x0f, 0xe7,
821 0x42, 0xc5, 0x6b, 0xa9, 0x99, 0x40, 0x53, 0x32, 0x8e, 0x31, 0x57, 0xc9, 0xd6, 0xc3, 0x16, 0x75,
822 0xd2, 0x7a, 0x99, 0xab, 0xfc, 0xe3, 0xa0, 0x8a, 0xc6, 0x3f, 0x03, 0x00, 0x00, 0xff, 0xff, 0x49,
823 0x8e, 0xc7, 0xa8, 0x07, 0x03, 0x00, 0x00,
824}