Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(api): add Security Logs API support #222

Merged
merged 2 commits into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/Crowdin.Api/Core/InternalExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ internal static void AddParamIfPresent(this IDictionary<string, string> queryPar
queryParams.AddParamIfPresent(key, value.ToString());
}
}

internal static void AddParamIfPresent(this IDictionary<string, string> queryParams, string key, long? value)
{
if (value.HasValue)
{
queryParams.AddParamIfPresent(key, value.ToString());
}
}

internal static void AddParamIfPresent(this IDictionary<string, string> queryParams, string key, object? value)
{
Expand Down
4 changes: 4 additions & 0 deletions src/Crowdin.Api/CrowdinApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
using Crowdin.Api.ProjectsGroups;
using Crowdin.Api.Reports;
using Crowdin.Api.Screenshots;
using Crowdin.Api.SecurityLogs;
using Crowdin.Api.SourceFiles;
using Crowdin.Api.SourceStrings;
using Crowdin.Api.Storage;
Expand Down Expand Up @@ -73,6 +74,8 @@ public class CrowdinApiClient : ICrowdinApiClient

public ScreenshotsApiExecutor Screenshots { get; }

public SecurityLogsApiExecutor SecurityLogs { get; }

public SourceFilesApiExecutor SourceFiles { get; }

public SourceStringsApiExecutor SourceStrings { get; }
Expand Down Expand Up @@ -174,6 +177,7 @@ public CrowdinApiClient(
ProjectsGroups = new ProjectsGroupsApiExecutor(this);
Reports = new ReportsApiExecutor(this);
Screenshots = new ScreenshotsApiExecutor(this);
SecurityLogs = new SecurityLogsApiExecutor(this);
SourceFiles = new SourceFilesApiExecutor(this);
SourceStrings = new SourceStringsApiExecutor(this);
Storage = new StorageApiExecutor(this);
Expand Down
3 changes: 3 additions & 0 deletions src/Crowdin.Api/ICrowdinApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using Crowdin.Api.ProjectsGroups;
using Crowdin.Api.Reports;
using Crowdin.Api.Screenshots;
using Crowdin.Api.SecurityLogs;
using Crowdin.Api.SourceFiles;
using Crowdin.Api.SourceStrings;
using Crowdin.Api.Storage;
Expand Down Expand Up @@ -66,6 +67,8 @@ public interface ICrowdinApiClient

ScreenshotsApiExecutor Screenshots { get; }

SecurityLogsApiExecutor SecurityLogs { get; }

SourceFilesApiExecutor SourceFiles { get; }

SourceStringsApiExecutor SourceStrings { get; }
Expand Down
35 changes: 35 additions & 0 deletions src/Crowdin.Api/SecurityLogs/SecurityLog.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@

using System;
using JetBrains.Annotations;
using Newtonsoft.Json;

namespace Crowdin.Api.SecurityLogs
{
[PublicAPI]
public class SecurityLog
{
[JsonProperty("id")]
public long Id { get; set; }

[JsonProperty("event")]
public SecurityLogEventType Event { get; set; }

[JsonProperty("info")]
public string Info { get; set; }

[JsonProperty("userId")]
public long UserId { get; set; }

[JsonProperty("location")]
public string Location { get; set; }

[JsonProperty("ipAddress")]
public string IpAddress { get; set; }

[JsonProperty("deviceName")]
public string DeviceName { get; set; }

[JsonProperty("createdAt")]
public DateTimeOffset CreatedAt { get; set; }
}
}
76 changes: 76 additions & 0 deletions src/Crowdin.Api/SecurityLogs/SecurityLogEventType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@

using System.ComponentModel;
using JetBrains.Annotations;

namespace Crowdin.Api.SecurityLogs
{
[PublicAPI]
public enum SecurityLogEventType
{
[Description("login")]
Login,

[Description("password.set")]
PasswordSet,

[Description("password.change")]
PasswordChange,

[Description("email.change")]
EmailChange,

[Description("login.change")]
LoginChange,

[Description("personal_token.issued")]
PersonalTokenIssued,

[Description("personal_token.revoked")]
PersonalTokenRevoked,

[Description("mfa.enabled")]
MfaEnabled,

[Description("mfa.disabled")]
MfaDisabled,

[Description("session.revoke")]
SessionRevoke,

[Description("session.revoke_all")]
SessionRevokeAll,

[Description("sso.connect")]
SsoConnect,

[Description("sso.disconnect")]
SsoDisconnect,

[Description("user.remove")]
UserRemove,

[Description("application.connected")]
ApplicationConnected,

[Description("application.disconnected")]
ApplicationDisconnected,

[Description("webauthn.created")]
WebAuthNCreated,

[Description("webauthn.deleted")]
WebAuthNDeleted,

[Description("trusted_device.remove")]
TrustedDeviceRemove,

[Description("trusted_device.remove_all")]
TrustedDeviceRemoveAll,

[Description("device_verification.enabled")]
DeviceVerificationEnabled,

[Description("device_verification.disabled")]
DeviceVerificationDisabled
}
}
116 changes: 116 additions & 0 deletions src/Crowdin.Api/SecurityLogs/SecurityLogsApiExecutor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@

using System.Collections.Generic;
using System.Threading.Tasks;

using Crowdin.Api.Core;
using JetBrains.Annotations;

#nullable enable

namespace Crowdin.Api.SecurityLogs
{
public class SecurityLogsApiExecutor
{
private readonly ICrowdinApiClient _apiClient;
private readonly IJsonParser _jsonParser;

public SecurityLogsApiExecutor(ICrowdinApiClient apiClient)
{
_apiClient = apiClient;
_jsonParser = apiClient.DefaultJsonParser;
}

public SecurityLogsApiExecutor(ICrowdinApiClient apiClient, IJsonParser jsonParser)
{
_apiClient = apiClient;
_jsonParser = jsonParser;
}

/// <summary>
/// List User Security Logs. Documentation:
/// <a href="https://developer.crowdin.com/api/v2/#operation/api.users.security-logs.getMany">Crowdin API</a>
/// <a href="https://developer.crowdin.com/enterprise/api/v2/#operation/api.users.security-logs.getMany">Crowdin Enterprise API</a>
/// </summary>
[PublicAPI]
public async Task<ResponseList<SecurityLog>> ListUserSecurityLogs(
long userId,
int limit = 25,
int offset = 0,
SecurityLogEventType? eventType = null,
string? ipAddress = null)
{
string url = FormUrl_Logs(userId);

IDictionary<string, string> queryParams = Utils.CreateQueryParamsFromPaging(limit, offset);
queryParams.AddDescriptionEnumValueIfPresent("event", eventType);
queryParams.AddParamIfPresent("ipAddress", ipAddress);

CrowdinApiResult result = await _apiClient.SendGetRequest(url, queryParams);
return _jsonParser.ParseResponseList<SecurityLog>(result.JsonObject);
}

/// <summary>
/// Get User Security Log. Documentation:
/// <a href="https://developer.crowdin.com/api/v2/#operation/api.users.security-logs.get">Crowdin API</a>
/// <a href="https://developer.crowdin.com/enterprise/api/v2/#operation/api.users.security-logs.get">Crowdin Enterprise API</a>
/// </summary>
[PublicAPI]
public async Task<SecurityLog> GetUserSecurityLog(long userId, long securityLogId)
{
string url = FormUrl_LogId(userId, securityLogId);
CrowdinApiResult result = await _apiClient.SendGetRequest(url);
return _jsonParser.ParseResponseObject<SecurityLog>(result.JsonObject);
}

#region Helper methods

private static string FormUrl_Logs(long userId)
{
return $"/users/{userId}/security-logs";
}

private static string FormUrl_LogId(long userId, long securityLogId)
{
return $"/users/{userId}/security-logs/{securityLogId}";
}

#endregion

#region Enterprise API

/// <summary>
/// List Organization Security Logs. Documentation:
/// <a href="https://developer.crowdin.com/enterprise/api/v2/#operation/api.security-logs.getMany">Crowdin Enterprise API</a>
/// </summary>
[PublicAPI]
public async Task<ResponseList<SecurityLog>> ListOrganizationSecurityLogs(
int limit = 25,
int offset = 0,
SecurityLogEventType? eventType = null,
string? ipAddress = null,
long? userId = null)
{
IDictionary<string, string> queryParams = Utils.CreateQueryParamsFromPaging(limit, offset);
queryParams.AddDescriptionEnumValueIfPresent("event", eventType);
queryParams.AddParamIfPresent("ipAddress", ipAddress);
queryParams.AddParamIfPresent("userId", userId);

CrowdinApiResult result = await _apiClient.SendGetRequest("/security-logs", queryParams);
return _jsonParser.ParseResponseList<SecurityLog>(result.JsonObject);
}

/// <summary>
/// Get Organization Security Log. Documentation:
/// <a href="https://developer.crowdin.com/enterprise/api/v2/#operation/api.security-logs.get">Crowdin Enterprise API</a>
/// </summary>
[PublicAPI]
public async Task<SecurityLog> GetOrganizationSecurityLog(long securityLogId)
{
var url = $"/security-logs/{securityLogId}";
CrowdinApiResult result = await _apiClient.SendGetRequest(url);
return _jsonParser.ParseResponseObject<SecurityLog>(result.JsonObject);
}

#endregion
}
}
60 changes: 60 additions & 0 deletions tests/Crowdin.Api.Tests/Core/Resources/SecurityLogs.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading