This repository has been archived by the owner on May 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
azure.go
121 lines (100 loc) · 2.87 KB
/
azure.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package azure
import (
"context"
"fmt"
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
msgraph "github.com/microsoftgraph/msgraph-sdk-go"
msgraphgocore "github.com/microsoftgraph/msgraph-sdk-go-core"
"github.com/microsoftgraph/msgraph-sdk-go/groups"
"github.com/microsoftgraph/msgraph-sdk-go/models"
"log/slog"
"strings"
)
type Config struct {
AzureTenantId string
AzureClientId string
AzureClientSecret string
AzureGroup string
}
type Azure struct {
Config Config
azclient *msgraph.GraphServiceClient
users AzureUsers
}
type AzureUser struct {
Email string
DisplayName string
}
type AzureUsers []AzureUser
func New(ctx context.Context, config Config) (*Azure, error) {
az := Azure{
Config: config,
}
cred, err := azidentity.NewClientSecretCredential(
config.AzureTenantId,
config.AzureClientId,
config.AzureClientSecret,
&azidentity.ClientSecretCredentialOptions{})
if err != nil {
return nil, err
}
az.azclient, err = msgraph.NewGraphServiceClientWithCredentials(cred, []string{})
if err != nil {
return nil, err
}
// try to connect to the group
group, err := az.azclient.Groups().ByGroupId(config.AzureGroup).Get(ctx, nil)
if err != nil {
return nil, err
}
slog.Info("Connected to group", "group", *group.GetDisplayName())
return &az, nil
}
func (az *Azure) Users(ctx context.Context) ([]AzureUser, error) {
if az.users == nil {
users := []AzureUser{}
top := int32(999)
query := groups.ItemMembersGraphUserRequestBuilderGetQueryParameters{
Select: []string{"id", "displayName", "mail"},
Top: &top,
}
options := &groups.ItemMembersGraphUserRequestBuilderGetRequestConfiguration{
QueryParameters: &query,
}
result, err := az.azclient.Groups().ByGroupId(az.Config.AzureGroup).Members().GraphUser().Get(ctx, options)
if err != nil {
return nil, fmt.Errorf("error getting group members: %w", err)
}
pageIterator, err := msgraphgocore.NewPageIterator[*models.User](result, az.azclient.GetAdapter(), models.CreateUserCollectionResponseFromDiscriminatorValue)
if err != nil {
return nil, fmt.Errorf("error creating page iterator: %w", err)
}
err = pageIterator.Iterate(ctx, func(user *models.User) bool {
if user != nil {
slog.Debug("Azure group member",
"email", *user.GetMail(),
"displayName", *user.GetDisplayName())
users = append(users, AzureUser{
Email: *user.GetMail(),
DisplayName: *user.GetDisplayName(),
})
}
return true
})
az.users = users
}
return az.users, nil
}
func (az *Azure) IsUserInGroup(ctx context.Context, email string) (isInGroup bool, displayName *string, err error) {
users, err := az.Users(ctx)
if err != nil {
return false, nil, err
}
emailLC := strings.ToLower(email)
for _, user := range users {
if strings.ToLower(user.Email) == emailLC {
return true, &user.DisplayName, nil
}
}
return false, nil, nil
}