Skip to content

Commit

Permalink
feat(authentication): Add/RemoveTeamMembers gRPC rpcs (#885)
Browse files Browse the repository at this point in the history
* feat(authentication): Add/RemoveTeamMembers gRPC rpcs

* fix(authentication): Add/RemoveTeamMembers rpcs missing teamId validation
  • Loading branch information
kon14 authored Jan 10, 2024
1 parent 5e1690b commit 9bbd18d
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 2 deletions.
14 changes: 13 additions & 1 deletion libraries/grpc-sdk/src/modules/authentication/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ import {
} from '../../protoUtils';

export class Authentication extends ConduitModule<typeof AuthenticationDefinition> {
constructor(private readonly moduleName: string, url: string, grpcToken?: string) {
constructor(
private readonly moduleName: string,
url: string,
grpcToken?: string,
) {
super(moduleName, 'authentication', url, grpcToken);
this.initializeClient(AuthenticationDefinition);
}
Expand Down Expand Up @@ -47,6 +51,14 @@ export class Authentication extends ConduitModule<typeof AuthenticationDefinitio
return this.client!.teamDelete({ teamId });
}

addTeamMembers(teamId: string, memberIds: string[]) {
return this.client!.addTeamMembers({ teamId, memberIds });
}

removeTeamMembers(teamId: string, memberIds: string[]) {
return this.client!.removeTeamMembers({ teamId, memberIds });
}

validateAccessToken(
accessToken: string,
path?: string,
Expand Down
56 changes: 55 additions & 1 deletion modules/authentication/src/Authentication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import ConduitGrpcSdk, {
HealthCheckStatus,
} from '@conduitplatform/grpc-sdk';
import path from 'path';
import { isNil } from 'lodash';
import { isEmpty, isNil } from 'lodash';
import { status } from '@grpc/grpc-js';
import AppConfigSchema, { Config } from './config';
import { AdminHandlers } from './admin';
Expand All @@ -27,10 +27,12 @@ import {
GetTeamRequest,
CreateTeamRequest,
Team as GrpcTeam,
ModifyTeamMembersRequest,
ValidateAccessTokenRequest,
ValidateAccessTokenResponse,
ValidateAccessTokenResponse_Status,
} from './protoTypes/authentication';
import { Empty } from './protoTypes/google/protobuf/empty';
import { runMigrations } from './migrations';
import metricsSchema from './metrics';
import { TokenProvider } from './handlers/tokenProvider';
Expand Down Expand Up @@ -59,6 +61,8 @@ export default class Authentication extends ManagedModule<Config> {
createTeam: this.createTeam.bind(this),
teamDelete: this.teamDelete.bind(this),
validateAccessToken: this.validateAccessToken.bind(this),
addTeamMembers: this.addTeamMembers.bind(this),
removeTeamMembers: this.removeTeamMembers.bind(this),
},
};
protected metricsSchema = metricsSchema;
Expand Down Expand Up @@ -383,6 +387,56 @@ export default class Authentication extends ManagedModule<Config> {
return callback(null, { message: result as string });
}

async addTeamMembers(
call: GrpcRequest<ModifyTeamMembersRequest>,
callback: GrpcCallback<Empty>,
) {
const teamId = call.request.teamId;
if (isEmpty(teamId)) {
return callback({
code: status.INVALID_ARGUMENT,
message: 'teamId must be a valid Team ID!',
});
}
const urlParams = { team: teamId };
const bodyParams = { members: call.request.memberIds };
const request = createParsedRouterRequest(
{ ...urlParams, ...bodyParams },
urlParams,
undefined,
bodyParams,
);
await new TeamsAdmin(this.grpcSdk).addTeamMembers(request).catch(e => {
return callback({ code: status.INTERNAL, message: (e as Error).message });
});
return callback(null, {});
}

async removeTeamMembers(
call: GrpcRequest<ModifyTeamMembersRequest>,
callback: GrpcCallback<Empty>,
) {
const teamId = call.request.teamId;
if (isEmpty(teamId)) {
return callback({
code: status.INVALID_ARGUMENT,
message: 'teamId must be a valid Team ID!',
});
}
const urlParams = { team: teamId };
const bodyParams = { members: call.request.memberIds };
const request = createParsedRouterRequest(
{ ...urlParams, ...bodyParams },
urlParams,
undefined,
bodyParams,
);
await new TeamsAdmin(this.grpcSdk).removeTeamMembers(request).catch(e => {
return callback({ code: status.INTERNAL, message: (e as Error).message });
});
return callback(null, {});
}

async validateAccessToken(
call: GrpcRequest<ValidateAccessTokenRequest>,
callback: GrpcCallback<ValidateAccessTokenResponse>,
Expand Down
8 changes: 8 additions & 0 deletions modules/authentication/src/authentication.proto
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
syntax = "proto3";
import "google/protobuf/empty.proto";
package authentication;

message UserLoginRequest {
Expand Down Expand Up @@ -59,6 +60,11 @@ message TeamDeleteResponse {
string message = 1;
}

message ModifyTeamMembersRequest {
string teamId = 1;
repeated string memberIds = 2;
}

message ValidateAccessTokenRequest {
string accessToken = 1;
optional string path = 2;
Expand All @@ -83,5 +89,7 @@ service Authentication {
rpc GetTeam(GetTeamRequest) returns (Team);
rpc CreateTeam(CreateTeamRequest) returns (Team);
rpc TeamDelete(TeamDeleteRequest) returns (TeamDeleteResponse);
rpc AddTeamMembers(ModifyTeamMembersRequest) returns (google.protobuf.Empty);
rpc RemoveTeamMembers(ModifyTeamMembersRequest) returns (google.protobuf.Empty);
rpc ValidateAccessToken(ValidateAccessTokenRequest) returns (ValidateAccessTokenResponse);
}

0 comments on commit 9bbd18d

Please sign in to comment.