From 78cefe19bd1fcbb0487fbd253c4b7553064f6fb5 Mon Sep 17 00:00:00 2001 From: kv Date: Sat, 13 Jan 2024 13:40:14 -0800 Subject: [PATCH] update ts client --- client/ts/src/lib.ts | 98 +++++++++++--------------------------- client/ts/src/responses.ts | 8 ++-- client/ts/src/types.ts | 92 +++++++++++++++++++++-------------- 3 files changed, 86 insertions(+), 112 deletions(-) diff --git a/client/ts/src/lib.ts b/client/ts/src/lib.ts index c3755c38c..0875fb36d 100644 --- a/client/ts/src/lib.ts +++ b/client/ts/src/lib.ts @@ -14,83 +14,39 @@ import { export * as Types from "./types"; -export type LoginOptions = { - jwt?: string; - username?: string; - password?: string; - secret?: string; -}; +type InitOptions = + | { type: "jwt"; params: { jwt: string } } + | { type: "api-key"; params: { api_key: string; secret: string } }; -export function MonitorClient(base_url: string, token?: string) { - const state = { jwt: token ?? "" }; - - const auth = async ( - request: R - ): Promise => { - return await axios({ - method: "post", - url: base_url + "/auth", - data: request, - }).then(({ data }) => data); +export function MonitorClient(url: string, options: InitOptions) { + const state = { + jwt: options.type === "jwt" ? options.params.jwt : undefined, + api_key: options.type === "api-key" ? options.params.api_key : undefined, + secret: options.type === "api-key" ? options.params.secret : undefined, }; - const login = async (options: LoginOptions) => { - if (options.username) { - if (options.password) { - const res = await auth({ - type: "LoginLocalUser", - params: { username: options.username, password: options.password }, - }); - state.jwt = res.jwt; - return res.jwt; - } else if (options.secret) { - const { jwt } = await auth({ - type: "LoginWithSecret", - params: { username: options.username, secret: options.secret }, - }); - state.jwt = jwt; - return jwt; - } - } - }; + const request = async (path: string, request: Req) => + await axios + .post(url + path, request, { + headers: { + Authorization: state.jwt, + "X-API-KEY": state.api_key, + "X-API-SECRET": state.secret, + }, + }) + .then(({ data }) => data); - const request = async ( - path: string, - request: Req - ): Promise => { - return await axios({ - method: "post", - url: base_url + path, - headers: { - Authorization: `Bearer ${state.jwt}`, - }, - data: request, - }).then(({ data }) => data); - }; + const auth = async (req: Req) => + await request("/auth", req); - const read = async ( - req: R - ): Promise => { - return await request("/read", req); - }; + const read = async (req: Req) => + await request("/read", req); - const write = async ( - req: R - ): Promise => { - return await request("/write", req); - }; + const write = async (req: Req) => + await request("/write", req); - const execute = async ( - req: R - ): Promise => { - return await request("/execute", req); - }; + const execute = async (req: Req) => + await request("/execute", req); - return { - auth, - login, - read, - write, - execute, - }; + return { request, auth, read, write, execute }; } diff --git a/client/ts/src/responses.ts b/client/ts/src/responses.ts index 98159596f..0ae4e32f5 100644 --- a/client/ts/src/responses.ts +++ b/client/ts/src/responses.ts @@ -5,7 +5,6 @@ export type AuthResponses = { CreateLocalUser: Types.CreateLocalUserResponse; LoginLocalUser: Types.LoginLocalUserResponse; ExchangeForJwt: Types.ExchangeForJwtResponse; - LoginWithSecret: Types.LoginWithSecretResponse; }; export type ReadResponses = { @@ -14,6 +13,7 @@ export type ReadResponses = { GetUsers: Types.GetUsersResponse; GetUsername: Types.GetUsernameResponse; GetCoreInfo: Types.GetCoreInfoResponse; + ListApiKeys: Types.ListApiKeysResponse; // ==== SEARCH ==== FindResources: Types.FindResourcesResponse; @@ -97,9 +97,9 @@ export type ReadResponses = { }; export type WriteResponses = { - // ==== SECRET ==== - CreateLoginSecret: Types.CreateLoginSecretResponse; - DeleteLoginSecret: Types.DeleteLoginSecretResponse; + // ==== API KEY ==== + CreateApiKey: Types.CreateApiKeyResponse; + DeleteApiKey: Types.DeleteApiKeyResponse; // ==== USER ==== PushRecentlyViewed: Types.PushRecentlyViewedResponse; diff --git a/client/ts/src/types.ts b/client/ts/src/types.ts index 0e71f0eb7..29f3b93e0 100644 --- a/client/ts/src/types.ts +++ b/client/ts/src/types.ts @@ -1,5 +1,5 @@ /* - Generated by typeshare 1.6.0 + Generated by typeshare 1.7.0 */ export interface MongoIdObj { @@ -257,13 +257,6 @@ export interface DeploymentActionState { export type GetDeploymentActionStateResponse = DeploymentActionState; -export interface ApiSecret { - name: string; - hash?: string; - created_at: I64; - expires?: I64; -} - export type ResourceTarget = | { type: "System", id: string } | { type: "Build", id: string } @@ -282,7 +275,6 @@ export interface User { create_server_permissions?: boolean; create_build_permissions?: boolean; avatar?: string; - secrets?: ApiSecret[]; password?: string; github_id?: string; google_id?: string; @@ -293,6 +285,23 @@ export interface User { export type GetUserResponse = User; +export interface ApiKey { + /** UNIQUE KEY ASSOCIATED WITH SECRET */ + key: string; + /** HASH OF THE SECRET */ + secret: string; + /** USER ASSOCIATED WITH THE API KEY */ + user_id: string; + /** NAME ASSOCIATED WITH THE API KEY FOR MANAGEMENT */ + name: string; + /** TIMESTAMP OF KEY CREATION */ + created_at: I64; + /** EXPIRY OF KEY, OR 0 IF NEVER EXPIRES */ + expires: I64; +} + +export type ListApiKeysResponse = ApiKey[]; + export type GetUsersResponse = User[]; export type ProcedureConfig = @@ -771,15 +780,6 @@ export interface ExchangeForJwtResponse { jwt: string; } -export interface LoginWithSecret { - username: string; - secret: string; -} - -export interface LoginWithSecretResponse { - jwt: string; -} - export interface RunBuild { build_id: string; } @@ -1060,6 +1060,9 @@ export interface GetVersionResponse { export interface GetUser { } +export interface ListApiKeys { +} + export interface GetUsers { } @@ -1308,6 +1311,28 @@ export interface UpdateAlerter { config: PartialAlerterConfig; } +export interface CreateApiKey { + name: string; + expires?: I64; +} + +export interface CreateApiKeyResponse { + /** X-API-KEY */ + key: string; + /** + * X-API-SECRET + * There is no way to get the secret again after it is distributed in this message + */ + secret: string; +} + +export interface DeleteApiKey { + key: string; +} + +export interface DeleteApiKeyResponse { +} + export interface CreateBuild { name: string; config: _PartialBuildConfig; @@ -1460,22 +1485,6 @@ export interface UpdateRepo { config: _PartialRepoConfig; } -export interface CreateLoginSecret { - name: string; - expires?: I64; -} - -export interface CreateLoginSecretResponse { - secret: string; -} - -export interface DeleteLoginSecret { - name: string; -} - -export interface DeleteLoginSecretResponse { -} - export interface CreateServer { name: string; config: _PartialServerConfig; @@ -1589,7 +1598,6 @@ export type AuthRequest = | { type: "GetLoginOptions", params: GetLoginOptions } | { type: "CreateLocalUser", params: CreateLocalUser } | { type: "LoginLocalUser", params: LoginLocalUser } - | { type: "LoginWithSecret", params: LoginWithSecret } | { type: "ExchangeForJwt", params: ExchangeForJwt }; export type ExecuteRequest = @@ -1612,6 +1620,7 @@ export type ReadRequest = | { type: "GetUsers", params: GetUsers } | { type: "GetUsername", params: GetUsername } | { type: "GetCoreInfo", params: GetCoreInfo } + | { type: "ListApiKeys", params: ListApiKeys } | { type: "FindResources", params: FindResources } | { type: "GetProceduresSummary", params: GetProceduresSummary } | { type: "GetProcedure", params: GetProcedure } @@ -1670,8 +1679,8 @@ export type ReadRequest = | { type: "GetSystemComponents", params: GetSystemComponents }; export type WriteRequest = - | { type: "CreateLoginSecret", params: CreateLoginSecret } - | { type: "DeleteLoginSecret", params: DeleteLoginSecret } + | { type: "CreateApiKey", params: CreateApiKey } + | { type: "DeleteApiKey", params: DeleteApiKey } | { type: "PushRecentlyViewed", params: PushRecentlyViewed } | { type: "SetLastSeenUpdate", params: SetLastSeenUpdate } | { type: "UpdateUserPerimissions", params: UpdateUserPermissions } @@ -1740,3 +1749,12 @@ export type Tag = tag_id: string; }}; +export type WsLoginMessage = + | { type: "Jwt", params: { + jwt: string; +}} + | { type: "ApiKeys", params: { + key: string; + secret: string; +}}; +