Skip to content

Commit c26f628

Browse files
committed
Clear tokens
1 parent 89b52aa commit c26f628

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

instance.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff 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{

transport.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
}

0 commit comments

Comments
 (0)