From 3b9c56c5f4674641867faef472712313517c9625 Mon Sep 17 00:00:00 2001 From: Timofey Boyko Date: Mon, 25 Sep 2023 17:02:28 +0300 Subject: [PATCH] Web:Common:OAuth: add new utils, new interfaces --- packages/common/api/oauth/index.ts | 70 +++++++++++++++++++------- packages/common/utils/oauth/dto.ts | 53 +++++++++++++++++++- packages/common/utils/oauth/index.ts | 73 ++++++++++++++++++++++++++++ 3 files changed, 177 insertions(+), 19 deletions(-) create mode 100644 packages/common/utils/oauth/index.ts diff --git a/packages/common/api/oauth/index.ts b/packages/common/api/oauth/index.ts index ff9263bdae..9998ce3277 100644 --- a/packages/common/api/oauth/index.ts +++ b/packages/common/api/oauth/index.ts @@ -1,6 +1,17 @@ import axios, { AxiosRequestConfig } from "axios"; -import { ClientDTO, ClientListDTO } from "../../utils/oauth/dto"; +import { + transformToClientProps, + transformToClientDTO, +} from "./../../utils/oauth/index"; + +import { + ClientDTO, + ClientListDTO, + ClientListProps, + ClientProps, + ScopeDTO, +} from "../../utils/oauth/dto"; const axiosConfig: AxiosRequestConfig = { baseURL: "/api", @@ -24,49 +35,56 @@ const request = (options: any): Promise => { return client(options).then(onSuccess).catch(onError); }; -export const getClient = async (clientId: string): Promise => { +export const getClient = async (clientId: string): Promise => { const client: ClientDTO = await request({ method: "get", url: `/clients/${clientId}`, }); - return client; + + return transformToClientProps(client); }; export const getClientList = async ( page: number, limit: number -): Promise => { - const clients: ClientListDTO = ( - await request({ - method: "get", - url: `/clients?page=${page}&limit=${limit}`, - }) - ).data; +): Promise => { + const { data }: { data: ClientListDTO } = await request({ + method: "get", + url: `/clients?page=${page}&limit=${limit}`, + }); + + const clients = { ...data, content: [] as ClientProps[] }; + + data.content.forEach((item) => { + const client = transformToClientProps(item); + + clients.content.push({ ...client }); + }); return clients; }; -export const addClient = async (data: ClientDTO): Promise => { +export const addClient = async (data: ClientProps): Promise => { const client: ClientDTO = await request({ method: "post", url: `/clients`, - data, + data: transformToClientDTO(data), }); - return client; + return transformToClientProps(client); }; export const updateClient = async ( clientId: string, - data: ClientDTO -): Promise => { + data: ClientProps +): Promise => { const client: ClientDTO = await request({ method: "put", url: `/clients/${clientId}`, - data, + data: transformToClientDTO(data), }); - return client; + return transformToClientProps(client); }; export const regenerateSecret = async (clientId: string): Promise => { @@ -86,3 +104,21 @@ export const deleteClient = async (clientId: string): Promise => { url: `/clients/${clientId}`, }); }; + +export const getScope = async (name: string): Promise => { + const scope: ScopeDTO = await request({ + method: "get", + url: `/scopes/${name}`, + }); + + return scope; +}; + +export const getScopeList = async (): Promise => { + const scopeList: ScopeDTO[] = await request({ + method: "get", + url: `/scopes`, + }); + + return scopeList; +}; diff --git a/packages/common/utils/oauth/dto.ts b/packages/common/utils/oauth/dto.ts index aba9d4422b..19418130a3 100644 --- a/packages/common/utils/oauth/dto.ts +++ b/packages/common/utils/oauth/dto.ts @@ -1,4 +1,23 @@ -import { OauthScopes } from "./enums"; +export type ScopeDTO = { + name: string; + description: string; +}; + +export interface ClientProps { + clientId: string; + secret: string; + description: string; + termsUrl: string; + policyUrl: string; + logoUrl: string; + authenticationMethod: string; + redirectUri: string; + logoutRedirectUri: string; + scopes: string[]; + tenant: number; + invalidated?: boolean; + name: string; +} export type ClientDTO = { client_id: string; @@ -10,11 +29,41 @@ export type ClientDTO = { authenticationMethod: string; redirect_uri: string; logout_redirect_uri: string; - scopes: OauthScopes[]; + scopes: string[]; tenant: number; invalidated?: boolean; + name: string; }; +export interface ClientListProps { + content: ClientProps[]; + 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; +} + export type ClientListDTO = { content: ClientDTO[]; empty: boolean; diff --git a/packages/common/utils/oauth/index.ts b/packages/common/utils/oauth/index.ts new file mode 100644 index 0000000000..2671e09316 --- /dev/null +++ b/packages/common/utils/oauth/index.ts @@ -0,0 +1,73 @@ +import { ClientDTO, ClientProps } from "./dto"; + +export const transformToClientProps = (clientDto: ClientDTO): ClientProps => { + const { + client_id, + client_secret, + description, + terms_url, + policy_url, + logo_url, + authenticationMethod, + redirect_uri, + logout_redirect_uri, + scopes, + tenant, + invalidated, + name, + } = clientDto; + + const client: ClientProps = { + clientId: client_id, + secret: client_secret, + description, + termsUrl: terms_url, + policyUrl: policy_url, + logoUrl: logo_url, + authenticationMethod, + redirectUri: redirect_uri, + logoutRedirectUri: logout_redirect_uri, + scopes, + tenant, + invalidated, + name, + }; + + return client; +}; + +export const transformToClientDTO = (clientProps: ClientProps): ClientDTO => { + const { + clientId: client_id, + secret: client_secret, + description, + termsUrl: terms_url, + policyUrl: policy_url, + logoUrl: logo_url, + authenticationMethod, + redirectUri: redirect_uri, + logoutRedirectUri: logout_redirect_uri, + scopes, + tenant, + invalidated, + name, + } = clientProps; + + const client: ClientDTO = { + client_id, + client_secret, + description, + terms_url, + policy_url, + logo_url, + authenticationMethod, + redirect_uri, + logout_redirect_uri, + scopes, + tenant, + invalidated, + name, + }; + + return client; +};