-
Notifications
You must be signed in to change notification settings - Fork 0
/
request.go
210 lines (185 loc) · 6.12 KB
/
request.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package azure
import (
"fmt"
"reflect"
"time"
"github.com/giantswarm/dex-operator/pkg/idp/provider"
abstractions "github.com/microsoft/kiota-abstractions-go"
"github.com/microsoftgraph/msgraph-sdk-go/applications"
"github.com/microsoftgraph/msgraph-sdk-go/models"
)
const (
ProviderName = "ad"
ProviderDisplayName = "Azure AD"
ProviderConnectorType = "microsoft"
TenantIDKey = "tenant-id"
ClientIDKey = "client-id"
ClientSecretKey = "client-secret"
PermissionType = "Scope"
DefaultName = "giantswarm-dex"
Claim = "groups"
Audience = "AzureADMyOrg"
DexOperatorName = "dex-operator"
)
func ProviderScope() []string {
return []string{"https://graph.microsoft.com/.default"}
}
// We compare the permissions set for the app to the permissions set on the parent app and ensure they are exactly the same
func computePermissionsUpdatePatch(app models.Applicationable, parentApp models.Applicationable) (bool, []models.RequiredResourceAccessable) {
var original, patch []models.RequiredResourceAccessable
{
original = app.GetRequiredResourceAccess()
patch = getPermissionCreateRequestBody(parentApp)
}
if reflect.DeepEqual(original, patch) {
return false, nil
}
return true, patch
}
func computeRedirectURIUpdatePatch(app models.Applicationable, config provider.AppConfig) (bool, models.WebApplicationable) {
var original, patch models.WebApplicationable
{
original = app.GetWeb()
patch = getRedirectURIsRequestBody([]string{config.RedirectURI})
}
if reflect.DeepEqual(original, patch) {
return false, nil
}
if original == nil {
return true, patch
}
uris := original.GetRedirectUris()
for _, uri := range uris {
if uri == config.RedirectURI {
return false, nil
}
}
patch = getRedirectURIsRequestBody(append(uris, config.RedirectURI))
return true, patch
}
func computeClaimsUpdatePatch(app models.Applicationable) (bool, models.OptionalClaimsable) {
var original, patch models.OptionalClaimsable
{
original = app.GetOptionalClaims()
patch = getClaimsRequestBody()
}
if reflect.DeepEqual(original, patch) {
return false, nil
}
if original == nil {
return true, patch
}
update := false
patch = models.NewOptionalClaims()
{
needsUpdate, patchClaim := computeClaimUpdatePatch(original.GetAccessToken())
patch.SetAccessToken(patchClaim)
update = update || needsUpdate
}
{
needsUpdate, patchClaim := computeClaimUpdatePatch(original.GetIdToken())
patch.SetIdToken(patchClaim)
update = update || needsUpdate
}
{
needsUpdate, patchClaim := computeClaimUpdatePatch(original.GetSaml2Token())
patch.SetSaml2Token(patchClaim)
update = update || needsUpdate
}
if !update {
return false, nil
}
return true, patch
}
func computeClaimUpdatePatch(claims []models.OptionalClaimable) (bool, []models.OptionalClaimable) {
for _, claim := range claims {
if n := claim.GetName(); n != nil {
if *n == Claim {
return false, claims
}
}
}
return true, append(claims, getClaim())
}
func GetAppGetRequestConfig(name string) *applications.ApplicationsRequestBuilderGetRequestConfiguration {
headers := abstractions.NewRequestHeaders()
headers.Add("ConsistencyLevel", "eventual")
requestFilter := fmt.Sprintf("displayName eq '%s'", name)
requestCount := true
requestTop := int32(1)
requestParameters := &applications.ApplicationsRequestBuilderGetQueryParameters{
Filter: &requestFilter,
Count: &requestCount,
Top: &requestTop,
Orderby: []string{"displayName"},
}
return &applications.ApplicationsRequestBuilderGetRequestConfiguration{
Headers: headers,
QueryParameters: requestParameters,
}
}
func GetAllAppsContainingRequestConfig(name string) *applications.ApplicationsRequestBuilderGetRequestConfiguration {
headers := abstractions.NewRequestHeaders()
headers.Add("ConsistencyLevel", "eventual")
requestSearch := fmt.Sprintf("\"displayName:%v\"", name)
requestCount := true
requestParameters := &applications.ApplicationsRequestBuilderGetQueryParameters{
Search: &requestSearch,
Count: &requestCount,
Orderby: []string{"displayName"},
}
return &applications.ApplicationsRequestBuilderGetRequestConfiguration{
Headers: headers,
QueryParameters: requestParameters,
}
}
func getAppCreateRequestBody(config provider.AppConfig) models.Applicationable {
// Assemble request body
app := models.NewApplication()
app.SetDisplayName(&config.Name)
app.SetWeb(getRedirectURIsRequestBody([]string{config.RedirectURI}))
app.SetOptionalClaims(getClaimsRequestBody())
app.SetIdentifierUris([]string{config.IdentifierURI})
audience := Audience
app.SetSignInAudience(&audience)
return app
}
func getPermissionCreateRequestBody(parentApp models.Applicationable) []models.RequiredResourceAccessable {
return parentApp.GetRequiredResourceAccess()
}
func getRedirectURIsRequestBody(redirectURIs []string) models.WebApplicationable {
web := models.NewWebApplication()
web.SetRedirectUris(redirectURIs)
return web
}
func getClaimsRequestBody() models.OptionalClaimsable {
claim := getClaim()
claims := models.NewOptionalClaims()
claims.SetAccessToken([]models.OptionalClaimable{claim})
claims.SetIdToken([]models.OptionalClaimable{claim})
claims.SetSaml2Token([]models.OptionalClaimable{claim})
return claims
}
func getClaim() *models.OptionalClaim {
claimName := Claim
claim := models.NewOptionalClaim()
claim.SetName(&claimName)
return claim
}
func getClaimFromName(claimName string) *models.OptionalClaim {
claim := models.NewOptionalClaim()
claim.SetName(&claimName)
return claim
}
func GetSecretCreateRequestBody(config provider.AppConfig) *applications.ItemAddPasswordPostRequestBody {
keyCredential := models.NewPasswordCredential()
keyCredential.SetDisplayName(&config.Name)
validUntil := time.Now().AddDate(0, config.SecretValidityMonths, 0)
keyCredential.SetEndDateTime(&validUntil)
secret := applications.NewItemAddPasswordPostRequestBody()
secret.SetPasswordCredential(keyCredential)
return secret
}
func getAdminConsentUrl(organization string, clientID string) string {
return fmt.Sprintf("https://login.microsoftonline.com/%s/adminconsent?client_id=%s", organization, clientID)
}