-
Notifications
You must be signed in to change notification settings - Fork 11
/
api_key_authentication_provider.go
94 lines (79 loc) · 3 KB
/
api_key_authentication_provider.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
package authentication
import (
"context"
"errors"
"strings"
abs "github.com/microsoft/kiota-abstractions-go"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
)
// ApiKeyAuthenticationProvider implements the AuthenticationProvider interface and adds an API key to the request.
type ApiKeyAuthenticationProvider struct {
apiKey string
parameterName string
keyLocation KeyLocation
validator *AllowedHostsValidator
}
type KeyLocation int
const (
// QUERYPARAMETER_KEYLOCATION is the value for the key location to be used as a query parameter.
QUERYPARAMETER_KEYLOCATION KeyLocation = iota
// HEADER_KEYLOCATION is the value for the key location to be used as a header.
HEADER_KEYLOCATION
)
// NewApiKeyAuthenticationProvider creates a new ApiKeyAuthenticationProvider instance
func NewApiKeyAuthenticationProvider(apiKey string, parameterName string, keyLocation KeyLocation) (*ApiKeyAuthenticationProvider, error) {
return NewApiKeyAuthenticationProviderWithValidHosts(apiKey, parameterName, keyLocation, nil)
}
// NewApiKeyAuthenticationProviderWithValidHosts creates a new ApiKeyAuthenticationProvider instance while specifying a list of valid hosts
func NewApiKeyAuthenticationProviderWithValidHosts(apiKey string, parameterName string, keyLocation KeyLocation, validHosts []string) (*ApiKeyAuthenticationProvider, error) {
if len(apiKey) == 0 {
return nil, errors.New("apiKey cannot be empty")
}
if len(parameterName) == 0 {
return nil, errors.New("parameterName cannot be empty")
}
validator, err := NewAllowedHostsValidatorErrorCheck(validHosts)
if err != nil {
return nil, err
}
return &ApiKeyAuthenticationProvider{
apiKey: apiKey,
parameterName: parameterName,
keyLocation: keyLocation,
validator: validator,
}, nil
}
// AuthenticateRequest adds the API key to the request.
func (p *ApiKeyAuthenticationProvider) AuthenticateRequest(ctx context.Context, request *abs.RequestInformation, additionalAuthenticationContext map[string]interface{}) error {
ctx, span := otel.GetTracerProvider().Tracer("github.com/microsoft/kiota-abstractions-go").Start(ctx, "GetAuthorizationToken")
defer span.End()
if request == nil {
return errors.New("request cannot be nil")
}
url, err := request.GetUri()
if err != nil {
return err
}
if !(*(p.validator)).IsUrlHostValid(url) {
span.SetAttributes(attribute.Bool("com.microsoft.kiota.authentication.is_url_valid", false))
return nil
}
if !strings.EqualFold(url.Scheme, "https") {
span.SetAttributes(attribute.Bool("com.microsoft.kiota.authentication.is_url_valid", false))
err := errors.New("url scheme must be https")
span.RecordError(err)
return err
}
span.SetAttributes(attribute.Bool("com.microsoft.kiota.authentication.is_url_valid", true))
switch p.keyLocation {
case QUERYPARAMETER_KEYLOCATION:
query := url.Query()
query.Set(p.parameterName, p.apiKey)
url.RawQuery = query.Encode()
request.SetUri(*url)
case HEADER_KEYLOCATION:
request.Headers.Add(p.parameterName, p.apiKey)
}
return nil
}