/
openapi_spec_authenticator_refresh_token.go
66 lines (59 loc) · 2.23 KB
/
openapi_spec_authenticator_refresh_token.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package openapi
import (
"fmt"
"github.com/dikhan/http_goclient"
"net/http"
"strings"
)
// Api Key Header Auth
type apiRefreshTokenAuthenticator struct {
terraformConfigurationName string
apiKey
refreshTokenURL string
httpClient http_goclient.HttpClientIface
}
func newAPIRefreshTokenAuthenticator(name, refreshToken, refreshTokenURL, terraformConfigurationName string) apiRefreshTokenAuthenticator {
return apiRefreshTokenAuthenticator{
terraformConfigurationName: terraformConfigurationName,
apiKey: apiKey{
name: name,
value: refreshToken,
},
refreshTokenURL: refreshTokenURL,
httpClient: &http_goclient.HttpClient{HttpClient: &http.Client{}},
}
}
func (a apiRefreshTokenAuthenticator) getContext() interface{} {
return a.apiKey
}
func (a apiRefreshTokenAuthenticator) getType() authType {
return authTypeAPIKeyHeader
}
// prepareAuth will send a post request to the refreshTokenURL and get the access token from the response Authorization
// header. Otherwise, it will fail.
func (a apiRefreshTokenAuthenticator) prepareAuth(authContext *authContext) error {
apiKey := a.getContext().(apiKey)
headers := map[string]string{apiKey.name: apiKey.value}
r, err := a.httpClient.PostJson(a.refreshTokenURL, headers, nil, nil)
if err != nil {
return err
}
if r.StatusCode != http.StatusOK && r.StatusCode != http.StatusNoContent {
return fmt.Errorf("refresh token POST response '%s' status code '%d' not matching expected response status code [%d, %d]", a.refreshTokenURL, r.StatusCode, http.StatusOK, http.StatusNoContent)
}
accessToken := r.Header.Get(authorizationHeader)
if accessToken == "" {
return fmt.Errorf("refresh token POST response '%s' is missing the access token", a.refreshTokenURL)
}
if authContext.headers == nil {
authContext.headers = map[string]string{}
}
authContext.headers[authorizationHeader] = accessToken
return nil
}
func (a apiRefreshTokenAuthenticator) validate() error {
if a.value == "" || strings.Trim(a.value, " ") == "Bearer" {
return fmt.Errorf("required security definition '%s' is missing the value. Please make sure the property '%s' is configured with a value in the provider's terraform configuration", a.terraformConfigurationName, a.terraformConfigurationName)
}
return nil
}