forked from kyma-project/kyma
-
Notifications
You must be signed in to change notification settings - Fork 0
/
oauthstrategy.go
42 lines (34 loc) · 1.02 KB
/
oauthstrategy.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
package authorization
import (
"fmt"
"net/http"
"github.com/kyma-project/kyma/components/application-gateway/internal/apperrors"
"github.com/kyma-project/kyma/components/application-gateway/internal/httpconsts"
log "github.com/sirupsen/logrus"
)
type oauthStrategy struct {
oauthClient OAuthClient
clientId string
clientSecret string
url string
}
func newOAuthStrategy(oauthClient OAuthClient, clientId, clientSecret, url string) oauthStrategy {
return oauthStrategy{
oauthClient: oauthClient,
clientId: clientId,
clientSecret: clientSecret,
url: url,
}
}
func (o oauthStrategy) AddAuthorization(r *http.Request, _ TransportSetter) apperrors.AppError {
token, err := o.oauthClient.GetToken(o.clientId, o.clientSecret, o.url)
if err != nil {
log.Errorf("failed to get token : '%s'", err)
return err
}
r.Header.Set(httpconsts.HeaderAuthorization, fmt.Sprintf("Bearer %s", token))
return nil
}
func (o oauthStrategy) Invalidate() {
o.oauthClient.InvalidateTokenCache(o.clientId)
}