Web:OAuth2: add CRUD

This commit is contained in:
Timofey Boyko 2023-09-22 17:28:37 +03:00
parent ebdffb55ea
commit 8e751923d9
5 changed files with 183 additions and 4 deletions

View File

@ -1,5 +1,14 @@
import { makeAutoObservable, runInAction } from "mobx"; import { makeAutoObservable, runInAction } from "mobx";
import {
addClient,
deleteClient,
getClient,
getClientList,
regenerateSecret,
updateClient,
} from "@docspace/common/api/oauth";
const clients = [...Array(5)].map((value, index) => { const clients = [...Array(5)].map((value, index) => {
return { return {
enabled: true, enabled: true,
@ -41,8 +50,30 @@ class OAuthStore {
makeAutoObservable(this); makeAutoObservable(this);
} }
getClients = () => { fetchClient = async (clientId) => {
await getClient(clientId);
};
fetchClients = async (page, limit) => {
this.clients = clients; this.clients = clients;
await getClientList(0, 20);
};
saveClient = async (client) => {
await addClient(client);
};
updateClient = async (clientId, client) => {
await updateClient(clientId, client);
};
regenerateSecret = async (clientId) => {
await regenerateSecret(clientId);
};
deleteClient = async (clientId) => {
await deleteClient(clientId);
}; };
setCurrentClient = (client) => { setCurrentClient = (client) => {
@ -59,9 +90,9 @@ class OAuthStore {
this.clients[index].enabled = !this.clients[index].enabled; this.clients[index].enabled = !this.clients[index].enabled;
}; };
deleteClient = (id) => { // deleteClient = (id) => {
this.clients = this.clients.filter((proj) => proj.id !== id); // this.clients = this.clients.filter((proj) => proj.id !== id);
}; // };
editClient = (client) => { editClient = (client) => {
const index = this.clients.findIndex((proj) => proj.id === client.id); const index = this.clients.findIndex((proj) => proj.id === client.id);

View File

@ -38,6 +38,10 @@ import PublicRoomStore from "./PublicRoomStore";
import WebhooksStore from "./WebhooksStore"; import WebhooksStore from "./WebhooksStore";
import ClientLoadingStore from "./ClientLoadingStore"; import ClientLoadingStore from "./ClientLoadingStore";
import OAuthStore from "./OAuthStore";
const oauthStore = new OAuthStore();
const oformsStore = new OformsStore(authStore); const oformsStore = new OformsStore(authStore);
const selectedFolderStore = new SelectedFolderStore(authStore.settingsStore); const selectedFolderStore = new SelectedFolderStore(authStore.settingsStore);
@ -223,6 +227,7 @@ const store = {
webhooksStore, webhooksStore,
clientLoadingStore, clientLoadingStore,
publicRoomStore, publicRoomStore,
oauthStore,
}; };
export default store; export default store;

View File

@ -0,0 +1,88 @@
import axios, { AxiosRequestConfig } from "axios";
import { ClientDTO, ClientListDTO } from "../../utils/oauth/dto";
const axiosConfig: AxiosRequestConfig = {
baseURL: "/api",
responseType: "json",
timeout: 0,
withCredentials: true,
headers: { "X-API-Version": "1", "X-Tenant": "1" },
};
const client = axios.create(axiosConfig);
const request = (options: any): Promise<any> => {
const onSuccess = (response: any) => {
return response.data;
};
const onError = (error: any) => {
return error;
};
return client(options).then(onSuccess).catch(onError);
};
export const getClient = async (clientId: string): Promise<ClientDTO> => {
const client: ClientDTO = await request({
method: "get",
url: `/clients/${clientId}`,
});
return client;
};
export const getClientList = async (
page: number,
limit: number
): Promise<ClientListDTO> => {
const clients: ClientListDTO = (
await request({
method: "get",
url: `/clients?page=${page}&limit=${limit}`,
})
).data;
return clients;
};
export const addClient = async (data: ClientDTO): Promise<ClientDTO> => {
const client: ClientDTO = await request({
method: "post",
url: `/clients`,
data,
});
return client;
};
export const updateClient = async (
clientId: string,
data: ClientDTO
): Promise<ClientDTO> => {
const client: ClientDTO = await request({
method: "put",
url: `/clients/${clientId}`,
data,
});
return client;
};
export const regenerateSecret = async (clientId: string): Promise<string> => {
const clientSecret: string = (
await request({
method: "patch",
url: `/clients/${clientId}`,
})
).client_secret;
return clientSecret;
};
export const deleteClient = async (clientId: string): Promise<void> => {
await request({
method: "delete",
url: `/clients/${clientId}`,
});
};

View File

@ -0,0 +1,45 @@
import { OauthScopes } from "./enums";
export type ClientDTO = {
client_id: string;
client_secret: string;
description: string;
terms_url: string;
policy_url: string;
logo_url: string;
authenticationMethod: string;
redirect_uri: string;
logout_redirect_uri: string;
scopes: OauthScopes[];
tenant: number;
invalidated?: boolean;
};
export type ClientListDTO = {
content: ClientDTO[];
empty: boolean;
first: boolean;
last: true;
number: number;
numberOfElements: number;
pageable: {
offset: number;
pageNumber: number;
pageSize: number;
paged: boolean;
sort: {
empty: boolean;
sorted: boolean;
unsorted: boolean;
};
unpaged: boolean;
};
size: number;
sort: {
empty: boolean;
sorted: boolean;
unsorted: boolean;
};
totalElements: number;
totalPages: number;
};

View File

@ -0,0 +1,10 @@
export const enum OauthScopes {
ReadFiles = "files:read",
WriteFiles = "files:write",
ReadRooms = "rooms:read",
WriteRooms = "rooms:write",
ReadAccount = "account.self:read",
WriteAccount = "account.self:write",
ReadAccounts = "accounts:read",
WriteAccounts = "accounts:write",
}