(ctx context.Context, email string)
| 247 | } |
| 248 | |
| 249 | func (p *provider) getUserIDByEmail(ctx context.Context, email string) (string, error) { |
| 250 | // First, try to get user directly by userPrincipalName. |
| 251 | apiURL := fmt.Sprintf("https://graph.microsoft.com/v1.0/users/%s", url.PathEscape(email)) |
| 252 | |
| 253 | b, err := p.doGraphRequest(ctx, http.MethodGet, apiURL, nil) |
| 254 | if err == nil { |
| 255 | var user userResponse |
| 256 | if err := json.Unmarshal(b, &user); err == nil && user.ID != "" { |
| 257 | return user.ID, nil |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | // If direct lookup fails, search by mail field (needed for guest users). |
| 262 | // Guest users have UPNs like "user_domain.com#EXT#@tenant.onmicrosoft.com" |
| 263 | // but their mail field contains the original email. |
| 264 | filterURL := fmt.Sprintf("https://graph.microsoft.com/v1.0/users?$filter=mail%%20eq%%20'%s'", url.QueryEscape(email)) |
| 265 | b, err = p.doGraphRequest(ctx, http.MethodGet, filterURL, nil) |
| 266 | if err != nil { |
| 267 | return "", errors.Wrapf(err, "failed to search user by mail") |
| 268 | } |
| 269 | |
| 270 | var users usersResponse |
| 271 | if err := json.Unmarshal(b, &users); err != nil { |
| 272 | return "", errors.Wrapf(err, "failed to unmarshal users response") |
| 273 | } |
| 274 | |
| 275 | if len(users.Value) == 0 { |
| 276 | return "", errors.Errorf("user with email %s not found", email) |
| 277 | } |
| 278 | |
| 279 | return users.Value[0].ID, nil |
| 280 | } |
| 281 | |
| 282 | // getTeamsAppID retrieves the Teams app ID from the app catalog. |
| 283 | // The app must be published to the organization's app catalog. |
no test coverage detected