-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcredential_authorization_provider_adapter.go
55 lines (46 loc) · 1.74 KB
/
credential_authorization_provider_adapter.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
package servicenowsdkgo
import (
"context"
"errors"
"github.com/michaeldcanady/servicenow-sdk-go/core"
"github.com/michaeldcanady/servicenow-sdk-go/internal"
newInt "github.com/michaeldcanady/servicenow-sdk-go/internal/new"
abstractions "github.com/microsoft/kiota-abstractions-go"
"github.com/microsoft/kiota-abstractions-go/authentication"
)
var _ authentication.AuthenticationProvider = (*credentialAuthenticationProviderAdapter)(nil)
// credentialAuthenticationProviderAdapter adapter for core.Credential to kiota AuthenticationProvider
type credentialAuthenticationProviderAdapter struct {
cred core.Credential //nolint: staticcheck
}
// newCredentialAuthenticationProviderAdapter adapts provided credential to an AuthenticationProvider
func newCredentialAuthenticationProviderAdapter(credential core.Credential) (*credentialAuthenticationProviderAdapter, error) {
if internal.IsNil(credential) {
return nil, errors.New("credential is nil")
}
return &credentialAuthenticationProviderAdapter{
cred: credential,
}, nil
}
// AuthenticateRequest authenticates the provided RequestInformation.
func (provider *credentialAuthenticationProviderAdapter) AuthenticateRequest(_ context.Context, request *abstractions.RequestInformation, _ map[string]interface{}) error {
if newInt.IsNil(provider) {
return nil
}
if newInt.IsNil(request) {
return errors.New("request is nil")
}
if request.Headers == nil {
request.Headers = abstractions.NewRequestHeaders()
}
if !request.Headers.ContainsKey(newInt.RequestHeaderAuthorization.String()) {
authString, err := provider.cred.GetAuthentication()
if err != nil {
return err
}
if authString != "" {
request.Headers.Add(newInt.RequestHeaderAuthorization.String(), authString)
}
}
return nil
}