Adapter creation moved to graph package (#432)

CreateAdapter function exported to the `graph` package. This will be the same area that authentication and token refreshment will eventually go.
This commit is contained in:
Danny 2022-07-28 12:06:33 -04:00 committed by GitHub
parent 987980510c
commit a4d99a74ed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 18 deletions

View File

@ -0,0 +1,23 @@
package graph
import (
az "github.com/Azure/azure-sdk-for-go/sdk/azidentity"
ka "github.com/microsoft/kiota-authentication-azure-go"
msgraphsdk "github.com/microsoftgraph/msgraph-sdk-go"
)
// CreateAdapter uses provided credentials to log into M365 using Kiota Azure Library
// with Azure identity package.
func CreateAdapter(tenant, client, secret string) (*msgraphsdk.GraphRequestAdapter, error) {
// Client Provider: Uses Secret for access to tenant-level data
cred, err := az.NewClientSecretCredential(tenant, client, secret, nil)
if err != nil {
return nil, err
}
auth, err := ka.NewAzureIdentityAuthenticationProviderWithScopes(cred, []string{"https://graph.microsoft.com/.default"})
if err != nil {
return nil, err
}
adapter, err := msgraphsdk.NewGraphRequestAdapter(auth)
return adapter, err
}

View File

@ -8,8 +8,6 @@ import (
"strings"
"sync/atomic"
az "github.com/Azure/azure-sdk-for-go/sdk/azidentity"
ka "github.com/microsoft/kiota-authentication-azure-go"
msgraphsdk "github.com/microsoftgraph/msgraph-sdk-go"
msgraphgocore "github.com/microsoftgraph/msgraph-sdk-go-core"
"github.com/microsoftgraph/msgraph-sdk-go/models"
@ -17,6 +15,7 @@ import (
"github.com/pkg/errors"
"github.com/alcionai/corso/internal/connector/exchange"
"github.com/alcionai/corso/internal/connector/graph"
"github.com/alcionai/corso/internal/connector/support"
"github.com/alcionai/corso/internal/data"
"github.com/alcionai/corso/pkg/account"
@ -83,23 +82,13 @@ func NewGraphConnector(acct account.Account) (*GraphConnector, error) {
return &gc, nil
}
func createAdapter(tenant, client, secret string) (*msgraphsdk.GraphRequestAdapter, error) {
// Client Provider: Uses Secret for access to tenant-level data
cred, err := az.NewClientSecretCredential(tenant, client, secret, nil)
if err != nil {
return nil, err
}
auth, err := ka.NewAzureIdentityAuthenticationProviderWithScopes(cred, []string{"https://graph.microsoft.com/.default"})
if err != nil {
return nil, err
}
adapter, err := msgraphsdk.NewGraphRequestAdapter(auth)
return adapter, err
}
// createSubConnector private constructor method for subConnector
// createService constructor for graphService component
func (gc *GraphConnector) createService(shouldFailFast bool) (*graphService, error) {
adapter, err := createAdapter(gc.credentials.TenantID, gc.credentials.ClientID, gc.credentials.ClientSecret)
adapter, err := graph.CreateAdapter(
gc.credentials.TenantID,
gc.credentials.ClientID,
gc.credentials.ClientSecret,
)
if err != nil {
return nil, err
}
@ -110,6 +99,7 @@ func (gc *GraphConnector) createService(shouldFailFast bool) (*graphService, err
}
return &connector, err
}
func (gs *graphService) EnableFailFast() {
gs.failFast = true
}