getIDByEmail gets user AAD object ID by email using Microsoft Graph API. https://learn.microsoft.com/en-us/graph/api/user-get
(ctx context.Context, emails []string)
| 203 | // getIDByEmail gets user AAD object ID by email using Microsoft Graph API. |
| 204 | // https://learn.microsoft.com/en-us/graph/api/user-get |
| 205 | func (p *provider) getIDByEmail(ctx context.Context, emails []string) (map[string]string, error) { |
| 206 | userID := make(map[string]string) |
| 207 | var emailsToGet []string |
| 208 | |
| 209 | for _, email := range emails { |
| 210 | id, ok := userIDCache.Get(email) |
| 211 | if ok { |
| 212 | if id != "" { |
| 213 | userID[email] = id |
| 214 | } |
| 215 | } else { |
| 216 | emailsToGet = append(emailsToGet, email) |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | if len(emailsToGet) == 0 { |
| 221 | return userID, nil |
| 222 | } |
| 223 | |
| 224 | if p.graphToken == "" { |
| 225 | if err := p.refreshGraphToken(ctx); err != nil { |
| 226 | return nil, errors.Wrapf(err, "failed to refresh graph token") |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | for _, email := range emailsToGet { |
| 231 | id, err := p.getUserIDByEmail(ctx, email) |
| 232 | if err != nil { |
| 233 | // Cache empty string for not found users to avoid repeated lookups. |
| 234 | userIDCache.Add(email, "") |
| 235 | continue |
| 236 | } |
| 237 | userID[email] = id |
| 238 | userIDCache.Add(email, id) |
| 239 | } |
| 240 | |
| 241 | return userID, nil |
| 242 | } |
| 243 | |
| 244 | // usersResponse is the response from Microsoft Graph API for user list queries. |
| 245 | type usersResponse struct { |
no test coverage detected