-
Notifications
You must be signed in to change notification settings - Fork 0
/
groups.go
189 lines (154 loc) · 5.98 KB
/
groups.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
package oauthsdk
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"strconv"
"strings"
"time"
)
var authUrl string
var client_id string
var client_secret string
var graphBaseUrl string = "https://graph.microsoft.com"
type GraphResponse struct {
TokenType string `json:"token_type"`
ExpiresIn string `json:"expires_in"`
ExtExpiresIn string `json:"ext_expires_in"`
ExpiresOn string `json:"expires_on"`
NotBefore string `json:"not_before"`
Resource string `json:"resource"`
AccessToken string `json:"access_token"`
}
func NewGraph(id string, secret string, url string) error {
if id == "" || secret == "" || url == "" {
return errors.New("Parameters cannot be nil")
}
if len(Certs) == 0 {
return errors.New("Certs have not been populated, Please run the New method first")
}
authUrl = url
client_id = id
client_secret = secret
err := getGraphToken(client_id, client_secret)
if err != nil {
return errors.New(fmt.Sprint(". Error => ", err))
}
return nil
}
func getGraphToken(client_id string, client_secret string) error {
data := url.Values{}
data.Set("grant_type", "client_credentials")
data.Set("resource", "https://graph.microsoft.com")
data.Set("client_id", client_id)
data.Set("client_secret", client_secret)
data.Set("scope", "Calendar.ReadWrite.All")
client := &http.Client{}
r, _ := http.NewRequest("POST", authUrl, strings.NewReader(data.Encode()))
resp, err := client.Do(r)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return errors.New("Unable to read token response body")
}
var response GraphResponse
json.Unmarshal(body, &response)
oauth, err := New("https://login.microsoftonline.com/common/discovery/keys")
t, err := oauth.Parse(response.AccessToken, false)
if err != nil {
return err
}
GraphAccessToken = *t
return nil
}
func (t *Token) validateTokenCurrent() bool {
var exp string
for _, val := range t.Claims {
if val.Claim == "exp" {
exp = val.Value
}
}
f, err := strconv.ParseFloat(exp, 64)
expiry := time.Unix(int64(f), 0)
if err != nil {
fmt.Println(err)
return false
}
if expiry.After(time.Now()) {
return true
}
return false
}
func (t *Token) userInGroup(upn string, groupObjectId string) (bool, error) {
if !GraphAccessToken.validateTokenCurrent() {
getGraphToken(client_id, client_secret)
}
client := &http.Client{}
r, _ := http.NewRequest("GET", graphBaseUrl+"users/"+upn+"/memberOf", nil)
r.Header.Set("Authorization", GraphAccessToken.Token.Raw)
r.Header.Set("Content-Type", "application/json")
resp, err := client.Do(r)
if err != nil {
fmt.Print(err)
return false, errors.New(fmt.Sprint("Error making HTTP call. Error => ", err))
}
if resp.StatusCode != 200 {
fmt.Print("Unable to validate JWT User is a valid member of the security group")
return false, errors.New("Unable to validate JWT User is a valid member of the security group")
}
var groups UsersGroupMembership
body, _ := ioutil.ReadAll(resp.Body)
json.Unmarshal(body, &groups)
for _, group := range groups.Value {
if group.ID == groupObjectId {
return true, nil
}
}
return false, nil
}
type UsersGroupMembership struct {
OdataContext string `json:"@odata.context"`
Value []struct {
OdataType string `json:"@odata.type"`
ID string `json:"id"`
DeletedDateTime interface{} `json:"deletedDateTime"`
Description string `json:"description"`
DisplayName string `json:"displayName"`
RoleTemplateID string `json:"roleTemplateId,omitempty"`
Classification interface{} `json:"classification,omitempty"`
CreatedDateTime time.Time `json:"createdDateTime,omitempty"`
CreationOptions []interface{} `json:"creationOptions,omitempty"`
ExpirationDateTime interface{} `json:"expirationDateTime,omitempty"`
GroupTypes []interface{} `json:"groupTypes,omitempty"`
IsAssignableToRole interface{} `json:"isAssignableToRole,omitempty"`
Mail string `json:"mail,omitempty"`
MailEnabled bool `json:"mailEnabled,omitempty"`
MailNickname string `json:"mailNickname,omitempty"`
MembershipRule interface{} `json:"membershipRule,omitempty"`
MembershipRuleProcessingState interface{} `json:"membershipRuleProcessingState,omitempty"`
OnPremisesDomainName string `json:"onPremisesDomainName,omitempty"`
OnPremisesLastSyncDateTime time.Time `json:"onPremisesLastSyncDateTime,omitempty"`
OnPremisesNetBiosName string `json:"onPremisesNetBiosName,omitempty"`
OnPremisesSamAccountName string `json:"onPremisesSamAccountName,omitempty"`
OnPremisesSecurityIdentifier string `json:"onPremisesSecurityIdentifier,omitempty"`
OnPremisesSyncEnabled bool `json:"onPremisesSyncEnabled,omitempty"`
PreferredDataLocation interface{} `json:"preferredDataLocation,omitempty"`
PreferredLanguage interface{} `json:"preferredLanguage,omitempty"`
ProxyAddresses []string `json:"proxyAddresses,omitempty"`
RenewedDateTime time.Time `json:"renewedDateTime,omitempty"`
ResourceBehaviorOptions []interface{} `json:"resourceBehaviorOptions,omitempty"`
ResourceProvisioningOptions []interface{} `json:"resourceProvisioningOptions,omitempty"`
SecurityEnabled bool `json:"securityEnabled,omitempty"`
SecurityIdentifier string `json:"securityIdentifier,omitempty"`
Theme interface{} `json:"theme,omitempty"`
Visibility interface{} `json:"visibility,omitempty"`
OnPremisesProvisioningErrors []interface{} `json:"onPremisesProvisioningErrors,omitempty"`
} `json:"value"`
}