mirror of
https://github.com/JanDeDobbeleer/oh-my-posh.git
synced 2024-12-31 13:57:26 -08:00
fix(http): correct logic for authentication header
This commit is contained in:
parent
53e5fad486
commit
04f7528f55
|
@ -86,25 +86,26 @@ func (o *OAuthRequest) refreshToken(refreshToken string) (string, error) {
|
|||
}
|
||||
|
||||
func OauthResult[a any](o *OAuthRequest, url string, body io.Reader, requestModifiers ...environment.HTTPRequestModifier) (a, error) {
|
||||
addToken := func() error {
|
||||
accessToken, err := o.getAccessToken()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// add token to header for authentication
|
||||
addAuthHeader := func(request *http.Request) {
|
||||
request.Header.Add("Authorization", "Bearer "+accessToken)
|
||||
}
|
||||
|
||||
if requestModifiers == nil {
|
||||
requestModifiers = []environment.HTTPRequestModifier{}
|
||||
}
|
||||
|
||||
requestModifiers = append(requestModifiers, addAuthHeader)
|
||||
|
||||
return nil
|
||||
if data, err := getCacheValue[a](&o.Request, url); err == nil {
|
||||
return data, nil
|
||||
}
|
||||
|
||||
return do[a](&o.Request, url, body, addToken, requestModifiers...)
|
||||
accessToken, err := o.getAccessToken()
|
||||
if err != nil {
|
||||
var data a
|
||||
return data, err
|
||||
}
|
||||
|
||||
// add token to header for authentication
|
||||
addAuthHeader := func(request *http.Request) {
|
||||
request.Header.Add("Authorization", "Bearer "+accessToken)
|
||||
}
|
||||
|
||||
if requestModifiers == nil {
|
||||
requestModifiers = []environment.HTTPRequestModifier{}
|
||||
}
|
||||
|
||||
requestModifiers = append(requestModifiers, addAuthHeader)
|
||||
|
||||
return do[a](&o.Request, url, body, requestModifiers...)
|
||||
}
|
||||
|
|
|
@ -19,41 +19,34 @@ func (r *Request) Init(env environment.Environment, props properties.Properties)
|
|||
}
|
||||
|
||||
func Do[a any](r *Request, url string, requestModifiers ...environment.HTTPRequestModifier) (a, error) {
|
||||
return do[a](r, url, nil, nil, requestModifiers...)
|
||||
if data, err := getCacheValue[a](r, url); err == nil {
|
||||
return data, nil
|
||||
}
|
||||
return do[a](r, url, nil, requestModifiers...)
|
||||
}
|
||||
|
||||
func do[a any](r *Request, url string, body io.Reader, preRequestFunc func() error, requestModifiers ...environment.HTTPRequestModifier) (a, error) {
|
||||
func getCacheValue[a any](r *Request, key string) (a, error) {
|
||||
var data a
|
||||
|
||||
getCacheValue := func(key string) (a, error) {
|
||||
if val, found := r.env.Cache().Get(key); found {
|
||||
err := json.Unmarshal([]byte(val), &data)
|
||||
if err != nil {
|
||||
r.env.Log(environment.Error, "OAuth", err.Error())
|
||||
return data, err
|
||||
}
|
||||
return data, nil
|
||||
}
|
||||
err := errors.New("no data in cache")
|
||||
r.env.Log(environment.Error, "OAuth", err.Error())
|
||||
return data, err
|
||||
}
|
||||
|
||||
httpTimeout := r.props.GetInt(properties.HTTPTimeout, properties.DefaultHTTPTimeout)
|
||||
|
||||
// No need to check more than every 30 minutes by default
|
||||
cacheTimeout := r.props.GetInt(properties.CacheTimeout, 30)
|
||||
if cacheTimeout > 0 {
|
||||
if data, err := getCacheValue(url); err == nil {
|
||||
return data, nil
|
||||
}
|
||||
if cacheTimeout <= 0 {
|
||||
return data, errors.New("no cache needed")
|
||||
}
|
||||
|
||||
if preRequestFunc != nil {
|
||||
if err := preRequestFunc(); err != nil {
|
||||
if val, found := r.env.Cache().Get(key); found {
|
||||
err := json.Unmarshal([]byte(val), &data)
|
||||
if err != nil {
|
||||
r.env.Log(environment.Error, "OAuth", err.Error())
|
||||
return data, err
|
||||
}
|
||||
return data, nil
|
||||
}
|
||||
err := errors.New("no data in cache")
|
||||
r.env.Log(environment.Error, "OAuth", err.Error())
|
||||
return data, err
|
||||
}
|
||||
|
||||
func do[a any](r *Request, url string, body io.Reader, requestModifiers ...environment.HTTPRequestModifier) (a, error) {
|
||||
var data a
|
||||
httpTimeout := r.props.GetInt(properties.HTTPTimeout, properties.DefaultHTTPTimeout)
|
||||
|
||||
responseBody, err := r.env.HTTPRequest(url, body, httpTimeout, requestModifiers...)
|
||||
if err != nil {
|
||||
|
@ -67,6 +60,7 @@ func do[a any](r *Request, url string, body io.Reader, preRequestFunc func() err
|
|||
return data, err
|
||||
}
|
||||
|
||||
cacheTimeout := r.props.GetInt(properties.CacheTimeout, 30)
|
||||
if cacheTimeout > 0 {
|
||||
r.env.Cache().Set(url, string(responseBody), cacheTimeout)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue