Skip to content

Commit 7c01abc

Browse files
authenticationProvider cannot be nil
1 parent a58f310 commit 7c01abc

File tree

3 files changed

+72
-1
lines changed

3 files changed

+72
-1
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package servicenowsdkgo
2+
3+
import (
4+
"context"
5+
"errors"
6+
7+
"github.com/michaeldcanady/servicenow-sdk-go/core"
8+
"github.com/michaeldcanady/servicenow-sdk-go/internal"
9+
abstractions "github.com/microsoft/kiota-abstractions-go"
10+
"github.com/microsoft/kiota-abstractions-go/authentication"
11+
)
12+
13+
const (
14+
authorizationHeader = "Authorization"
15+
)
16+
17+
var _ authentication.AuthenticationProvider = (*credentialAuthenticationProviderAdapter)(nil)
18+
19+
// credentialAuthenticationProviderAdapter adapter for core.Credential to kiota AuthenticationProvider
20+
type credentialAuthenticationProviderAdapter struct {
21+
cred core.Credential //nolint: staticcheck
22+
}
23+
24+
// newCredentialAuthenticationProviderAdapter adapts provided credential to an AuthenticationProvider
25+
func newCredentialAuthenticationProviderAdapter(credential core.Credential) (*credentialAuthenticationProviderAdapter, error) {
26+
if internal.IsNil(credential) {
27+
return nil, errors.New("credential is nil")
28+
}
29+
30+
return &credentialAuthenticationProviderAdapter{
31+
cred: credential,
32+
}, nil
33+
}
34+
35+
// AuthenticateRequest authenticates the provided RequestInformation.
36+
func (provider *credentialAuthenticationProviderAdapter) AuthenticateRequest(ctx context.Context, request *abstractions.RequestInformation, _ map[string]interface{}) error {
37+
if request == nil {
38+
return errors.New("request is nil")
39+
}
40+
if request.Headers == nil {
41+
request.Headers = abstractions.NewRequestHeaders()
42+
}
43+
if !request.Headers.ContainsKey(authorizationHeader) {
44+
authString, err := provider.cred.GetAuthentication()
45+
if err != nil {
46+
return err
47+
}
48+
if authString != "" {
49+
request.Headers.Add(authorizationHeader, authString)
50+
}
51+
}
52+
53+
return nil
54+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package servicenowsdkgo
2+
3+
import "testing"
4+
5+
func TestNewCredentialAuthenticationProviderAdapter(t *testing.T) {
6+
// TODO: add tests
7+
}
8+
9+
func TestCredentialAuthenticationProviderAdapter_AuthenticateRequest(t *testing.T) {
10+
// TODO: add tests
11+
}

servicenow_client.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,13 @@ func NewServiceNowClient2(credential core.Credential, instance string) (*Service
7171
return nil, err
7272
}
7373

74-
client, err := newServiceNowServiceClientWithOptions(nil, withURL(strings.Replace(instance, "/api", "", -1)))
74+
authenticationProvider, err := newCredentialAuthenticationProviderAdapter(credential)
75+
if err != nil {
76+
// can't test since if credential is nil, it will be picked up earlier
77+
return nil, err
78+
}
79+
80+
client, err := newServiceNowServiceClientWithOptions(authenticationProvider, withURL(strings.Replace(instance, "/api", "", -1)))
7581
if err != nil {
7682
return nil, err
7783
}

0 commit comments

Comments
 (0)