File tree Expand file tree Collapse file tree 2 files changed +53
-2
lines changed
Expand file tree Collapse file tree 2 files changed +53
-2
lines changed Original file line number Diff line number Diff line change @@ -105,11 +105,11 @@ func (f *Instance) GetAPIInstance() (*client.FortifiAPI, error) {
105105 return nil , fmt .Errorf ("Failed to authenticate with Fortifi: %s" , err .Error ())
106106 }
107107
108- f .apiInstance = client .New (transport , strfmt .Default )
108+ f .apiInstance = client .New (NewTransport ( transport , f . getNewToken ) , strfmt .Default )
109109 return f .apiInstance , nil
110110}
111111
112- func (f * Instance ) getNewToken (transport * httptransport. Runtime ) error {
112+ func (f * Instance ) getNewToken (transport runtime. ClientTransport ) error {
113113 c := client .New (transport , strfmt .Default )
114114 p := authentication .NewGetServiceAuthTokenParams ()
115115 p .Payload = & models.ServiceAccountCredentialsPayload {
Original file line number Diff line number Diff line change 1+ package api
2+
3+ import (
4+ "encoding/json"
5+ "net/http"
6+
7+ "github.com/go-openapi/runtime"
8+ )
9+
10+ type errorResponse struct {
11+ Payload struct {
12+ Data []interface {} `json:"data"`
13+ Meta struct {
14+ Code int `json:"code"`
15+ Message string `json:"message"`
16+ RequestId string `json:"requestId"`
17+ Type string `json:"type"`
18+ } `json:"meta"`
19+ } `json:"Payload"`
20+ }
21+
22+ type Transport struct {
23+ original runtime.ClientTransport
24+ clear func (transport runtime.ClientTransport ) error
25+ }
26+
27+ func NewTransport (transport runtime.ClientTransport , clear func (transport runtime.ClientTransport ) error ) * Transport {
28+ return & Transport {original : transport , clear : clear }
29+ }
30+
31+ func (t * Transport ) Submit (operation * runtime.ClientOperation ) (interface {}, error ) {
32+ resp , err := t .original .Submit (operation )
33+
34+ if err != nil {
35+ if rawR , ok := err .(runtime.ClientResponseStatus ); ok && rawR .IsCode (http .StatusForbidden ) {
36+ env := & errorResponse {}
37+ if bytes , mErr := json .Marshal (err ); mErr == nil {
38+ err = json .Unmarshal (bytes , env )
39+ }
40+
41+ if env .Payload .Meta .Type == "InvalidTokenException" || env .Payload .Meta .Message == "A valid access token must be supplied" {
42+ clearErr := t .clear (t .original )
43+ if clearErr == nil {
44+ return t .original .Submit (operation )
45+ }
46+ }
47+ }
48+ }
49+
50+ return resp , err
51+ }
You can’t perform that action at this time.
0 commit comments