Skip to content

Commit

Permalink
refactor: rename closures to use 'create' prefix for consistency
Browse files Browse the repository at this point in the history
  • Loading branch information
psanders committed Dec 28, 2024
1 parent a7acc57 commit 442cc94
Show file tree
Hide file tree
Showing 98 changed files with 602 additions and 531 deletions.
7 changes: 5 additions & 2 deletions mods/apiserver/src/applications/createApplication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { GrpcErrorMessage, withErrorHandling } from "@fonoster/common";
import { getAccessKeyIdFromCall } from "@fonoster/identity";
import {
getAccessKeyIdFromCall,
GrpcErrorMessage,
withErrorHandling
} from "@fonoster/common";
import { getLogger } from "@fonoster/logger";
import { BaseApiObject, CreateApplicationRequest } from "@fonoster/types";
import { ServerInterceptingCall } from "@grpc/grpc-js";
Expand Down
4 changes: 2 additions & 2 deletions mods/apiserver/src/applications/listApplications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
import {
GrpcErrorMessage,
Validators as V,
withErrorHandlingAndValidation
withErrorHandlingAndValidation,
getAccessKeyIdFromCall
} from "@fonoster/common";
import { getAccessKeyIdFromCall } from "@fonoster/identity";
import { getLogger } from "@fonoster/logger";
import {
ListApplicationsRequest,
Expand Down
4 changes: 2 additions & 2 deletions mods/apiserver/src/applications/updateApplication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { withErrorHandling } from "@fonoster/common";
import { getAccessKeyIdFromCall, withAccess } from "@fonoster/identity";
import { getAccessKeyIdFromCall, withErrorHandling } from "@fonoster/common";
import { getLogger } from "@fonoster/logger";
import { UpdateApplicationRequest } from "@fonoster/types";
import { ServerInterceptingCall } from "@grpc/grpc-js";
import { createGetFnUtil } from "./createGetFnUtil";
import { convertToApplicationData } from "./utils/convertToApplicationData";
import { validOrThrow } from "./utils/validOrThrow";
import { Prisma } from "../core/db";
import { withAccess } from "@fonoster/identity";

const logger = getLogger({ service: "apiserver", filePath: __filename });

Expand Down
4 changes: 2 additions & 2 deletions mods/apiserver/src/calls/createCall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
import {
GrpcErrorMessage,
Validators as V,
withErrorHandlingAndValidation
withErrorHandlingAndValidation,
getAccessKeyIdFromCall
} from "@fonoster/common";
import { getAccessKeyIdFromCall } from "@fonoster/identity";
import { getLogger } from "@fonoster/logger";
import { BaseApiObject, CreateCallRequest } from "@fonoster/types";
import { ServerInterceptingCall } from "@grpc/grpc-js";
Expand Down
4 changes: 2 additions & 2 deletions mods/apiserver/src/calls/getCall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ import {
GrpcErrorMessage,
Validators as V,
withErrorHandlingAndValidation,
InfluxDBClient
InfluxDBClient,
getAccessKeyIdFromCall
} from "@fonoster/common";
import { getAccessKeyIdFromCall } from "@fonoster/identity";
import { getLogger } from "@fonoster/logger";
import { ServerInterceptingCall } from "@grpc/grpc-js";
import { createFetchSingleCall } from "./createFetchSingleCall";
Expand Down
4 changes: 2 additions & 2 deletions mods/apiserver/src/calls/listCalls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ import {
GrpcErrorMessage,
Validators as V,
withErrorHandlingAndValidation,
InfluxDBClient
InfluxDBClient,
getAccessKeyIdFromCall
} from "@fonoster/common";
import { getAccessKeyIdFromCall } from "@fonoster/identity";
import { getLogger } from "@fonoster/logger";
import { ServerInterceptingCall } from "@grpc/grpc-js";
import { createFetchCalls } from "./createFetchCalls";
Expand Down
6 changes: 4 additions & 2 deletions mods/apiserver/src/core/httpBridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* limitations under the License.
*/
import { Readable } from "stream";
import { updateMembershipStatus } from "@fonoster/identity";
import { createUpdateMembershipStatus } from "@fonoster/identity";
import { getLogger } from "@fonoster/logger";
import express, { Request, Response } from "express";
import { identityConfig } from "./identityConfig";
Expand Down Expand Up @@ -63,7 +63,9 @@ function httpBridge(params: { port: number }) {
"/api/identity/accept-invite",
async (req: Request, res: Response) => {
try {
await updateMembershipStatus(identityConfig)(req.query.token as string);
await createUpdateMembershipStatus(identityConfig)(
req.query.token as string
);
res.redirect(APP_URL);
} catch (error) {
logger.verbose("error updating membership status", error);
Expand Down
4 changes: 2 additions & 2 deletions mods/apiserver/src/core/runServices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
import {
GRPC_SERVING_STATUS,
getServerCredentials,
statusMap
statusMap,
createAuthInterceptor
} from "@fonoster/common";
import { createAuthInterceptor } from "@fonoster/identity";
import { getLogger } from "@fonoster/logger";
import * as grpc from "@grpc/grpc-js";
import { HealthImplementation } from "grpc-health-check";
Expand Down
2 changes: 1 addition & 1 deletion mods/apiserver/src/secrets/createGetFnUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { Prisma } from "../core/db";
import { notFoundError } from "../core/notFoundError";

function createGetFnUtil(prisma: Prisma) {
return async (ref: string) => {
return async function getFnUtil(ref: string) {
const response = await prisma.secret.findUnique({
where: { ref }
});
Expand Down
4 changes: 2 additions & 2 deletions mods/apiserver/src/secrets/createSecret.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
import {
GrpcErrorMessage,
Validators as V,
withErrorHandlingAndValidation
withErrorHandlingAndValidation,
getAccessKeyIdFromCall
} from "@fonoster/common";
import { getAccessKeyIdFromCall } from "@fonoster/identity";
import { getLogger } from "@fonoster/logger";
import { BaseApiObject, CreateSecretRequest } from "@fonoster/types";
import { ServerInterceptingCall } from "@grpc/grpc-js";
Expand Down
5 changes: 3 additions & 2 deletions mods/apiserver/src/secrets/listSecrets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ import {
GrpcErrorMessage,
Validators as V,
datesMapper,
withErrorHandlingAndValidation
withErrorHandlingAndValidation,
getAccessKeyIdFromCall
} from "@fonoster/common";
import { getAccessKeyIdFromCall } from "@fonoster/identity";
import {} from "@fonoster/identity";
import { getLogger } from "@fonoster/logger";
import { ListSecretsRequest, ListSecretsResponse } from "@fonoster/types";
import { ServerInterceptingCall } from "@grpc/grpc-js";
Expand Down
3 changes: 1 addition & 2 deletions mods/apiserver/src/secrets/updateSecret.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { Validators as V } from "@fonoster/common";
import { getAccessKeyIdFromCall } from "@fonoster/identity";
import { Validators as V, getAccessKeyIdFromCall } from "@fonoster/common";
import { getLogger } from "@fonoster/logger";
import { UpdateSecretRequest } from "@fonoster/types";
import { ServerInterceptingCall } from "@grpc/grpc-js";
Expand Down
6 changes: 3 additions & 3 deletions mods/apiserver/src/voice/createCreateVoiceClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { createCallAccessToken } from "@fonoster/identity";
import { createGenerateCallAccessToken } from "@fonoster/identity";
import { getLogger } from "@fonoster/logger";
import { Channel, Client, StasisStart } from "ari-client";
import { CreateContainer } from "./integrations/types";
Expand All @@ -28,7 +28,7 @@ import { mapCallDirectionToEnum } from "../events/mapCallDirectionToEnum";

const logger = getLogger({ service: "apiserver", filePath: __filename });

const createToken = createCallAccessToken(identityConfig);
const generateCallAccessToken = createGenerateCallAccessToken(identityConfig);

// Note: By the time the call arrives here the owner of the app MUST be authenticated
function createCreateVoiceClient(
Expand All @@ -55,7 +55,7 @@ function createCreateVoiceClient(

const { accessKeyId, endpoint, tts, stt } = await createContainer(appRef);

const sessionToken = await createToken({ accessKeyId, appRef });
const sessionToken = await generateCallAccessToken({ accessKeyId, appRef });

const metadataStr = (await getChannelVar(ChannelVar.METADATA))?.value;

Expand Down
6 changes: 4 additions & 2 deletions mods/authz/src/createCheckMethodAuthorized.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { createInterceptingCall } from "@fonoster/common";
import {
getAccessKeyIdFromCall,
createInterceptingCall
} from "@fonoster/common";
import { getLogger } from "@fonoster/logger";
import { ServerInterceptingCall, status } from "@grpc/grpc-js";
import { AuthzClient } from "./client/AuthzClient";
import { CheckMethodAuthorizedRequest } from "./types";
import { getAccessKeyIdFromCall } from "@fonoster/identity";

const logger = getLogger({ service: "authz", filePath: __filename });

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,15 @@
*/
import { getLogger } from "@fonoster/logger";
import { ServerInterceptingCall } from "@grpc/grpc-js";
import { permissionDeniedError, unauthenticatedError } from "./errors";
import { Access, TokenUseEnum } from "./exchanges";
import { workspaceAccess } from "./roles";
import {
decodeToken,
getAccessKeyIdFromCall,
getTokenFromCall,
hasAccess,
isValidToken,
tokenHasAccessKeyId
} from "./utils";
import { getAccessKeyIdFromCall } from "./getAccessKeyIdFromCall";
import { getTokenFromCall } from "./getTokenFromCall";
import { isValidToken } from "./isValidToken";
import { decodeToken } from "./decodeToken";
import { hasAccess } from "./hasAccess";
import { Access, TokenUseEnum } from "./types";
import { tokenHasAccessKeyId } from "./tokenHasAccessKeyId";
import { permissionDeniedError, unauthenticatedError } from "./errors";

const logger = getLogger({ service: "identity", filePath: __filename });

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
* limitations under the License.
*/
import { jwtDecode } from "jwt-decode";
import { TokenUseEnum } from "../exchanges/TokenUseEnum";
import { DecodedToken } from "../exchanges/types";
import { TokenUseEnum, DecodedToken } from "./types";

function decodeToken<T extends TokenUseEnum>(token: string): DecodedToken<T> {
return jwtDecode(token);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { createInterceptingCall } from "@fonoster/common";
import { ServerInterceptingCall, status } from "@grpc/grpc-js";
import { createInterceptingCall } from "../utils";

const unauthenticatedError = (call: ServerInterceptingCall) =>
createInterceptingCall({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,25 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { createServiceDefinition } from "@fonoster/common";
import * as grpc from "@grpc/grpc-js";
import { serviceDefinitionParams } from "./service";
import { createServiceDefinition } from "../utils";

type GetPublicKeyResponse = {
publicKey: string;
};

const IdentityServiceClient = grpc.makeGenericClientConstructor(
createServiceDefinition(serviceDefinitionParams),
createServiceDefinition({
serviceName: "Identity",
pckg: "identity",
proto: "identity.proto",
version: "v1beta2"
}),
"",
{}
);

function getPublicKeyClient(endpoint: string) {
function getPublicKey(endpoint: string) {
return new Promise<GetPublicKeyResponse>((resolve, reject) => {
const client = new IdentityServiceClient(
endpoint,
Expand All @@ -50,4 +54,4 @@ function getPublicKeyClient(endpoint: string) {
});
}

export { getPublicKeyClient };
export { getPublicKey };
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,15 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { Access, Role } from "../exchanges";
import { roles } from "../roles";
import { Access, Role } from "./types";
import { roles } from "./roles";

// This method only checks if the role has access to the path
function hasAccess(access: Access[], grpcPath: string) {
// This function only checks if the role has access to the grpc method
function hasAccess(access: Access[], method: string) {
const roleList = access.map((a: Access) => a.role);

return roleList.some((r: string) =>
roles.find(
(role: Role) => role.name === r && role.access.includes(grpcPath)
)
roles.find((role: Role) => role.name === r && role.access.includes(method))
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
enum JsonWebErrorEnum {
JsonWebTokenError = "JsonWebTokenError",
TokenExpiredError = "TokenExpiredError"
}

export { JsonWebErrorEnum };
export * from "./createAuthInterceptor";
export * from "./decodeToken";
export * from "./getPublicKey";
export * from "./getAccessKeyIdFromCall";
export * from "./getTokenFromCall";
export * from "./hasAccess";
export * from "./isValidToken";
export * from "./types";
export * from "./roles";
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
import { getLogger } from "@fonoster/logger";
import jwt from "jsonwebtoken";
import { JsonWebErrorEnum } from "../JsonWebErrorEnum";
import { JsonWebErrorEnum } from "./types";

const logger = getLogger({ service: "identity", filePath: __filename });

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* limitations under the License.
*/
import { ApiRoleEnum, WorkspaceRoleEnum } from "@fonoster/types";
import { Role } from "./exchanges/types";
import { Role } from "./types";

const VOICE_SERVICE_ROLE = "VOICE_SERVICE";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { decodeToken } from "./decodeToken";
import { Access, TokenUseEnum } from "../exchanges";
import { Access, decodeToken, TokenUseEnum } from ".";

function tokenHasAccessKeyId(token: string, accessKeyId: string) {
const decodedToken = decodeToken<TokenUseEnum.ACCESS>(token);
Expand Down
Loading

0 comments on commit 442cc94

Please sign in to comment.