This repository has been archived by the owner on Dec 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
authUtils.go
96 lines (80 loc) · 2.87 KB
/
authUtils.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package api
import (
"context"
"net/http"
"github.com/keptn/go-utils/pkg/api/models"
v2 "github.com/keptn/go-utils/pkg/api/utils/v2"
"github.com/keptn/go-utils/pkg/common/httputils"
)
type AuthV1Interface interface {
// Authenticate authenticates the client request against the server.
Authenticate() (*models.EventContext, *models.Error)
}
// AuthHandler handles projects
type AuthHandler struct {
authHandler *v2.AuthHandler
BaseURL string
AuthToken string
AuthHeader string
HTTPClient *http.Client
Scheme string
}
// NewAuthHandler returns a new AuthHandler
func NewAuthHandler(baseURL string) *AuthHandler {
return NewAuthHandlerWithHTTPClient(baseURL, &http.Client{Transport: wrapOtelTransport(getClientTransport(nil))})
}
// NewAuthHandlerWithHTTPClient returns a new AuthHandler that uses the specified http.Client
func NewAuthHandlerWithHTTPClient(baseURL string, httpClient *http.Client) *AuthHandler {
return &AuthHandler{
BaseURL: httputils.TrimHTTPScheme(baseURL),
HTTPClient: httpClient,
Scheme: "http",
authHandler: v2.NewAuthHandlerWithHTTPClient(baseURL, httpClient),
}
}
// NewAuthenticatedAuthHandler returns a new AuthHandler that authenticates at the endpoint via the provided token
// Deprecated: use APISet instead
func NewAuthenticatedAuthHandler(baseURL string, authToken string, authHeader string, httpClient *http.Client, scheme string) *AuthHandler {
if httpClient == nil {
httpClient = &http.Client{}
}
httpClient.Transport = wrapOtelTransport(getClientTransport(httpClient.Transport))
return createAuthenticatedAuthHandler(baseURL, authToken, authHeader, httpClient, scheme)
}
func createAuthenticatedAuthHandler(baseURL string, authToken string, authHeader string, httpClient *http.Client, scheme string) *AuthHandler {
return &AuthHandler{
BaseURL: httputils.TrimHTTPScheme(baseURL),
AuthHeader: authHeader,
AuthToken: authToken,
HTTPClient: httpClient,
Scheme: scheme,
authHandler: v2.NewAuthenticatedAuthHandler(baseURL, authToken, authHeader, httpClient, scheme),
}
}
func (a *AuthHandler) getBaseURL() string {
return a.BaseURL
}
func (a *AuthHandler) getAuthToken() string {
return a.AuthToken
}
func (a *AuthHandler) getAuthHeader() string {
return a.AuthHeader
}
func (a *AuthHandler) getHTTPClient() *http.Client {
return a.HTTPClient
}
// Authenticate authenticates the client request against the server.
func (a *AuthHandler) Authenticate() (*models.EventContext, *models.Error) {
a.ensureHandlerIsSet()
return a.authHandler.Authenticate(context.TODO(), v2.AuthAuthenticateOptions{})
}
func (a *AuthHandler) ensureHandlerIsSet() {
if a.authHandler != nil {
return
}
if a.AuthToken != "" {
a.authHandler = v2.NewAuthenticatedAuthHandler(a.BaseURL, a.AuthToken, a.AuthHeader, a.HTTPClient, a.Scheme)
} else {
a.authHandler = v2.NewAuthHandlerWithHTTPClient(a.BaseURL, a.HTTPClient)
}
}