Skip to content

Commit

Permalink
Added error handling for adding AAD users to database
Browse files Browse the repository at this point in the history
Adjusted the logic for the Teams Enumeration method
  • Loading branch information
FlangvikOld committed Aug 3, 2023
1 parent 595f1b4 commit 7f93c57
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 39 deletions.
79 changes: 41 additions & 38 deletions TeamFiltration/TeamFiltration/Handlers/TeamsHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,70 +165,68 @@ public async Task<WorkingWithResp> GetWorkingWithList(string tenantId)

failedResp:
//TODO:Add logic to select FireProx endpoint based on current location

var enumUserReq = await _teamsClient.GetAsync(enumUserUrl + $"{TeamsRegion}/beta/users/{username}/externalsearchv3");


if (enumUserReq.IsSuccessStatusCode)
{

//We got an 200 OK response
var userResp = await enumUserReq.Content.ReadAsStringAsync();


//Indication of valid JSOn response
if (userResp.Contains("tenantId"))
{
//get the object
List<TeamsExtSearchRep> responeObject = JsonConvert.DeserializeObject<List<TeamsExtSearchRep>>(userResp);
List<TeamsExtSearchRep> usersFoundObject = JsonConvert.DeserializeObject<List<TeamsExtSearchRep>>(userResp);

//Any size
if (responeObject.Count() > 0)
if (usersFoundObject.Count() > 0)
{
foreach (var responeObject in usersFoundObject)
{

if (
//Check that the TenantID is not null
responeObject.FirstOrDefault().tenantId != null

//Check that the coExistenceMode is not Unknown
&& !responeObject.FirstOrDefault().featureSettings.coExistenceMode.Equals("Unknown")

//Check that the Display != Equals email.
&& !responeObject.FirstOrDefault().displayName.Equals(username)
if (
//Check that the TenantID is not null
responeObject.tenantId != null

//Check that the UPN matches the email your are looking for
&& responeObject.FirstOrDefault().userPrincipalName.ToLower().Equals(username.ToLower())
)
{
//Check that the coExistenceMode is not Unknown
&& !responeObject.featureSettings.coExistenceMode.Equals("Unknown")

try
//Check that the Display != Equals email OR that the UPN = userPrincipalName
&& (!responeObject.displayName.Equals(username) || responeObject.userPrincipalName.ToLower().Equals(username.ToLower())))
{

//Check the user presence
HttpResponseMessage getUserPresence = await _teamsClient.PollyPostAsync(
$"https://presence.teams.microsoft.com/v1/presence/getpresence/",
try
{

new StringContent(
"[{ \"mri\":\"" + responeObject.FirstOrDefault().mri + "\"}]"
, Encoding.UTF8
, "application/json"
)
);
//Check the user presence
HttpResponseMessage getUserPresence = await _teamsClient.PollyPostAsync(
$"https://presence.teams.microsoft.com/v1/presence/getpresence/",

new StringContent(
"[{ \"mri\":\"" + responeObject.mri + "\"}]"
, Encoding.UTF8
, "application/json"
)
);

var getPresenceObject = JsonConvert.DeserializeObject<List<GetPresenceResp>>(await getUserPresence.Content.ReadAsStringAsync());

if (getPresenceObject.FirstOrDefault()?.presence?.calendarData?.isOutOfOffice != null)
{
Outofofficenote = getPresenceObject.FirstOrDefault()?.presence?.calendarData.outOfOfficeNote;
var getPresenceObject = JsonConvert.DeserializeObject<List<GetPresenceResp>>(await getUserPresence.Content.ReadAsStringAsync());

if (getPresenceObject.FirstOrDefault()?.presence?.calendarData?.isOutOfOffice != null)
{
Outofofficenote = getPresenceObject.FirstOrDefault()?.presence?.calendarData.outOfOfficeNote;
}
}
}
catch (Exception ex)
{
catch (Exception ex)
{


}
}

return (true, responeObject.FirstOrDefault().objectId, responeObject.FirstOrDefault(), Outofofficenote);
return (true, responeObject.objectId, responeObject, Outofofficenote);
}
}
}
}
Expand All @@ -237,8 +235,13 @@ public async Task<WorkingWithResp> GetWorkingWithList(string tenantId)
}
else if (enumUserReq.StatusCode.Equals(HttpStatusCode.Forbidden))
{
//As of 24.04.2023 - Seems like MS have patched this.
return (false, "", null, null);
//We got an 200 OK response
var userResp = await enumUserReq.Content.ReadAsStringAsync();

if (userResp.Equals("{\"errorCode\":\"Forbidden\"}"))
//As of 24.04.2023 - Seems like MS have patched this.
//return (false, "", null, null);
return (true, "", null, null);
}
else if (enumUserReq.StatusCode.Equals(HttpStatusCode.InternalServerError))
{
Expand Down
15 changes: 14 additions & 1 deletion TeamFiltration/TeamFiltration/Modules/Exfiltrate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1684,7 +1684,20 @@ await filteredUserPrincipalNames.ParallelForEachAsync(
async upn =>
{
if (logDb)
_databaseHandler.WriteValidAcc(new ValidAccount() { Username = upn.userPrincipalName.Trim().ToLower(), Id = Helpers.Generic.StringToGUID(upn.userPrincipalName.Trim().ToLower()).ToString() });
try
{
_databaseHandler.WriteValidAcc(new ValidAccount()
{
Username = upn.userPrincipalName.Trim().ToLower(),
Id = Helpers.Generic.StringToGUID(upn.userPrincipalName.Trim().ToLower()).ToString()
});
}
catch (Exception ex)
{


}

},
maxDegreeOfParallelism: 700);
}
Expand Down

0 comments on commit 7f93c57

Please sign in to comment.