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

[PB-3257] Feat/access logs #266

Merged
merged 7 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@internxt/sdk",
"author": "Internxt <[email protected]>",
"version": "1.8.0",
"version": "1.8.1",
"description": "An sdk for interacting with Internxt's services",
"repository": {
"type": "git",
Expand Down
25 changes: 16 additions & 9 deletions src/auth/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ export class Auth {
user: UserSettings;
userTeam: TeamsSettings | null;
}>(
'/access',
'/auth/login/access',
{
email: details.email,
password: encryptedPasswordHash,
Expand Down Expand Up @@ -188,7 +188,7 @@ export class Auth {
sKey: string;
tfa: boolean | null;
}>(
'/login',
'/auth/login',
{
email: email,
},
Expand All @@ -202,15 +202,22 @@ export class Auth {
});
}

/**
* Logout
*/
public logout(token?: Token): Promise<void> {
return this.client.get('/auth/logout', this.headersWithToken(token ?? <string>this.apiSecurity?.token));
}

/**
* Generates a new TwoFactorAuth code
*/
public generateTwoFactorAuthQR(): Promise<TwoFactorAuthQR> {
public generateTwoFactorAuthQR(token?: Token): Promise<TwoFactorAuthQR> {
return this.client
.get<{
qr: string;
code: string;
}>('/tfa', this.headersWithToken(<string>this.apiSecurity?.token))
}>('/auth/tfa', this.headersWithToken(token ?? <string>this.apiSecurity?.token))
.then((data) => {
return {
qr: data.qr,
Expand All @@ -224,8 +231,8 @@ export class Auth {
* @param pass
* @param code
*/
public disableTwoFactorAuth(pass: string, code: string): Promise<void> {
return this.client.delete('/tfa', this.headersWithToken(<string>this.apiSecurity?.token), {
public disableTwoFactorAuth(pass: string, code: string, token?: Token): Promise<void> {
return this.client.delete('/auth/tfa', this.headersWithToken(token ?? <string>this.apiSecurity?.token), {
pass: pass,
code: code,
});
Expand All @@ -236,14 +243,14 @@ export class Auth {
* @param backupKey
* @param code
*/
public storeTwoFactorAuthKey(backupKey: string, code: string): Promise<void> {
public storeTwoFactorAuthKey(backupKey: string, code: string, token?: Token): Promise<void> {
return this.client.put(
'/tfa',
'/auth/tfa',
{
key: backupKey,
code: code,
},
this.headersWithToken(<string>this.apiSecurity?.token),
this.headersWithToken(token ?? <string>this.apiSecurity?.token),
);
}

Expand Down
45 changes: 43 additions & 2 deletions src/workspaces/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ import {
UsersAndTeamsAnItemIsShareWidthResponse,
WorkspaceUser,
WorkspaceUsage,
ItemType,
WorkspaceLogOrderBy,
WorkspaceLogType,
WorkspaceLogResponse,
} from './types';

export class Workspaces {
Expand Down Expand Up @@ -203,7 +207,7 @@ export class Workspaces {

public getPersonalTrash(
workspaceId: string,
type: 'file' | 'folder',
type: ItemType,
offset = 0,
limit = 50,
): Promise<FetchTrashContentResponse> {
Expand Down Expand Up @@ -647,7 +651,7 @@ export class Workspaces {
itemId,
}: {
workspaceId: string;
itemType: 'folder' | 'file';
itemType: ItemType;
itemId: string;
}): [Promise<UsersAndTeamsAnItemIsShareWidthResponse>, RequestCanceler] {
const { promise, requestCanceler } = this.client.getCancellable<UsersAndTeamsAnItemIsShareWidthResponse>(
Expand All @@ -661,6 +665,43 @@ export class Workspaces {
public getWorkspace(workspaceId: string): Promise<Workspace> {
return this.client.get<Workspace>(`workspaces/${workspaceId}`, this.headers());
}

public getWorkspaceLogs(
workspaceId: string,
limit = 50,
offset = 0,
member?: string,
activity?: WorkspaceLogType[],
lastDays?: number,
summary = true,
orderBy?: WorkspaceLogOrderBy,
): Promise<WorkspaceLogResponse[]> {
const params = new URLSearchParams();

params.append('limit', String(limit));
params.append('offset', String(offset));
params.append('summary', String(summary));

if (member) {
params.append('member', member);
}
if (activity && activity.length > 0) {
activity.forEach((act, index) => {
params.append(`activity[${index}]`, act);
});
}
if (lastDays) {
params.append('lastDays', String(lastDays));
}
if (orderBy) {
params.append('orderBy', String(orderBy));
}

return this.client.get<WorkspaceLogResponse[]>(
`workspaces/${workspaceId}/access/logs?${params.toString()}`,
this.headers(),
);
}
}

export * from './types';
82 changes: 82 additions & 0 deletions src/workspaces/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,3 +317,85 @@ export type UsersAndTeamsAnItemIsShareWidthResponse = {
export type OrderByOptions = 'views:ASC' | 'views:DESC' | 'createdAt:ASC' | 'createdAt:DESC';

export type GetMemberUsageResponse = { backupsUsage: number; driveUsage: number; spaceLimit: number };

export enum WorkspaceLogType {
Login = 'login',
ChangedPassword = 'changed-password',
Logout = 'logout',
ShareFile = 'share-file',
ShareFolder = 'share-folder',
DeleteFile = 'delete-file',
DeleteFolder = 'delete-folder',
}

export enum WorkspaceLogActionType {
Share = 'share',
Delete = 'delete',
DeleteAll = 'delete-all',
}

export enum WorkspaceLogPlatform {
Web = 'web',
Mobile = 'mobile',
Desktop = 'desktop',
}

export interface WorkspaceLogUser {
id: number;
name: string;
lastname: string;
email: string;
username: string;
bridgeUser?: string;
rootFolderId?: number;
uuid: string;
sharedWorkspace?: boolean;
avatar?: string;
lastPasswordChangedAt?: Date;
}

export interface WorkspaceLog {
id: string;
workspaceId: string;
creator: string;
type: WorkspaceLogType;
platform: WorkspaceLogPlatform;
entityId?: string;
user: WorkspaceLogUser;
workspace: Workspace;
createdAt: Date;
updatedAt: Date;
}

export interface WorkspaceLogResponse {
id: string;
workspaceId: string;
creator: string;
type: WorkspaceLogType;
platform: WorkspaceLogPlatform;
entityId?: string;
createdAt: Date;
updatedAt: Date;
user: WorkspaceLogUser;
workspace: Workspace;
file?: {
uuid: string;
plainName: string;
folderUuid: string;
type: string;
};
folder?: {
uuid: string;
plainName: string;
parentId: string;
parentUuid?: string;
};
}

export type WorkspaceLogOrderBy =
| 'createdAt:ASC'
| 'createdAt:DESC'
| 'platform:ASC'
| 'platform:DESC'
| 'type:ASC'
| 'type:DESC';
14 changes: 7 additions & 7 deletions test/auth/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Auth, CryptoProvider, Keys, LoginDetails, Password, RegisterDetails, Token } from '../../src';
import sinon from 'sinon';
import { emptyRegisterDetails } from './registerDetails.mother';
import { basicHeaders, headersWithToken } from '../../src/shared/headers';
import { ApiSecurity, AppDetails } from '../../src/shared';
import { HttpClient } from '../../src/shared/http/client';
import { Auth, CryptoProvider, Keys, LoginDetails, Password, RegisterDetails, Token } from '../../src';

const httpClient = HttpClient.create('');

Expand Down Expand Up @@ -238,14 +238,14 @@ describe('# auth service tests', () => {

// Assert
expect(postStub.firstCall.args).toEqual([
'/login',
'/auth/login',
{
email: loginDetails.email,
},
headers,
]);
expect(postStub.secondCall.args).toEqual([
'/access',
'/auth/login/access',
{
email: loginDetails.email,
password: 'password-encrypted_salt',
Expand Down Expand Up @@ -309,7 +309,7 @@ describe('# auth service tests', () => {

// Assert
expect(postStub.firstCall.args).toEqual([
'/login',
'/auth/login',
{
email: email,
},
Expand Down Expand Up @@ -355,7 +355,7 @@ describe('# auth service tests', () => {
const body = await client.generateTwoFactorAuthQR();

// Assert
await expect(callStub.firstCall.args).toEqual(['/tfa', headers]);
await expect(callStub.firstCall.args).toEqual(['/auth/tfa', headers]);
expect(body).toEqual({
qr: 'qr',
backupKey: 'code',
Expand All @@ -376,7 +376,7 @@ describe('# auth service tests', () => {

// Assert
await expect(callStub.firstCall.args).toEqual([
'/tfa',
'/auth/tfa',
headers,
{
pass: pass,
Expand All @@ -400,7 +400,7 @@ describe('# auth service tests', () => {

// Assert
await expect(callStub.firstCall.args).toEqual([
'/tfa',
'/auth/tfa',
{
key: backupKey,
code: code,
Expand Down
Loading