ContainerImage.SpamasaurusRex/pkg/graphhelper/graphhelper.go

78 lines
1.9 KiB
Go

package graphhelper
import (
"context"
"fmt"
"os"
"strings"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
auth "github.com/microsoft/kiota-authentication-azure-go"
msgraphsdk "github.com/microsoftgraph/msgraph-sdk-go"
// "github.com/microsoftgraph/msgraph-sdk-go/models"
// "github.com/microsoftgraph/msgraph-sdk-go/users"
)
type GraphHelper struct {
deviceCodeCredential *azidentity.DeviceCodeCredential
userClient *msgraphsdk.GraphServiceClient
graphUserScopes []string
}
func NewGraphHelper() *GraphHelper {
g := &GraphHelper{}
return g
}
func (g *GraphHelper) InitializeGraphForUserAuth() error {
clientId := os.Getenv("CLIENT_ID")
tenantId := os.Getenv("TENANT_ID")
scopes := os.Getenv("GRAPH_USER_SCOPES")
g.graphUserScopes = strings.Split(scopes, ",")
// Create the device code credential
credential, err := azidentity.NewDeviceCodeCredential(&azidentity.DeviceCodeCredentialOptions{
ClientID: clientId,
TenantID: tenantId,
UserPrompt: func(ctx context.Context, message azidentity.DeviceCodeMessage) error {
fmt.Println(message.Message)
return nil
},
})
if err != nil {
return err
}
g.deviceCodeCredential = credential
// Create an auth provider using the credential
authProvider, err := auth.NewAzureIdentityAuthenticationProviderWithScopes(credential, g.graphUserScopes)
if err != nil {
return err
}
// Create a request adapter using the auth provider
adapter, err := msgraphsdk.NewGraphRequestAdapter(authProvider)
if err != nil {
return err
}
// Create a Graph client using request adapter
client := msgraphsdk.NewGraphServiceClient(adapter)
g.userClient = client
return nil
}
func (g *GraphHelper) GetUserToken() (*string, error) {
token, err := g.deviceCodeCredential.GetToken(context.Background(), policy.TokenRequestOptions{
Scopes: g.graphUserScopes,
})
if err != nil {
return nil, err
}
return &token.Token, nil
}