Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing the pk identities claim command to augment DIs #278

Merged
merged 1 commit into from
Dec 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/PolykeyAgent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,7 @@ class PolykeyAgent {
notificationsManager: this.notificationsManager,
sessionManager: this.sessionManager,
vaultManager: this.vaultManager,
sigchain: this.sigchain,
grpcServerClient: this.grpcServerClient,
grpcServerAgent: this.grpcServerAgent,
fwdProxy: this.fwdProxy,
Expand Down
2 changes: 1 addition & 1 deletion src/bin/identities/CommandAllow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class CommandAllow extends CommandPolykey {
// Setting By Identity
const providerMessage = new identitiesPB.Provider();
providerMessage.setProviderId(gestaltId.providerId);
providerMessage.setMessage(gestaltId.identityId);
providerMessage.setIdentityId(gestaltId.identityId);
setActionMessage.setIdentity(providerMessage);
await binUtils.retryAuthentication(
(auth) =>
Expand Down
75 changes: 49 additions & 26 deletions src/bin/identities/CommandAuthenticate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import CommandPolykey from '../CommandPolykey';
import * as binUtils from '../utils';
import * as binOptions from '../utils/options';
import * as binProcessors from '../utils/processors';
import * as identitiesUtils from '../../identities/utils';

class CommandAuthenticate extends CommandPolykey {
constructor(...args: ConstructorParameters<typeof CommandPolykey>) {
Expand Down Expand Up @@ -32,7 +33,11 @@ class CommandAuthenticate extends CommandPolykey {
this.fs,
);
let pkClient: PolykeyClient;
let genReadable: ReturnType<
typeof pkClient.grpcClient.identitiesAuthenticate
>;
this.exitHandlers.handlers.push(async () => {
if (genReadable != null) genReadable.stream.cancel();
if (pkClient != null) await pkClient.stop();
});
try {
Expand All @@ -45,32 +50,50 @@ class CommandAuthenticate extends CommandPolykey {
});
const providerMessage = new identitiesPB.Provider();
providerMessage.setProviderId(providerId);
providerMessage.setMessage(identityId);
const successMessage = await binUtils.retryAuthentication(
async (auth) => {
const stream = pkClient.grpcClient.identitiesAuthenticate(
providerMessage,
auth,
);
const codeMessage = (await stream.next()).value;
process.stdout.write(
binUtils.outputFormatter({
type: options.format === 'json' ? 'json' : 'list',
data: [`Your device code is: ${codeMessage!.getMessage()}`],
}),
);
return (await stream.next()).value;
},
meta,
);
process.stdout.write(
binUtils.outputFormatter({
type: options.format === 'json' ? 'json' : 'list',
data: [
`Successfully authenticated user: ${successMessage!.getMessage()}`,
],
}),
);
providerMessage.setIdentityId(identityId);
await binUtils.retryAuthentication(async (auth) => {
genReadable = pkClient.grpcClient.identitiesAuthenticate(
providerMessage,
auth,
);
for await (const message of genReadable) {
switch (message.getStepCase()) {
case identitiesPB.AuthenticationProcess.StepCase.REQUEST: {
const authRequest = message.getRequest()!;
this.logger.info(
`Navigate to the URL in order to authenticate`,
);
this.logger.info(
'Use any additional additional properties to complete authentication',
);
identitiesUtils.browser(authRequest.getUrl());
process.stdout.write(
binUtils.outputFormatter({
type: options.format === 'json' ? 'json' : 'dict',
data: {
url: authRequest.getUrl(),
...Object.fromEntries(authRequest.getDataMap().entries()),
},
}),
);
break;
}
case identitiesPB.AuthenticationProcess.StepCase.RESPONSE: {
const authResponse = message.getResponse()!;
this.logger.info(
`Authenticated digital identity provider ${providerId} with identity ${identityId}`,
);
process.stdout.write(
binUtils.outputFormatter({
type: options.format === 'json' ? 'json' : 'list',
data: [authResponse.getIdentityId()],
}),
);
break;
}
}
}
}, meta);
} finally {
if (pkClient! != null) await pkClient.stop();
}
Expand Down
2 changes: 1 addition & 1 deletion src/bin/identities/CommandClaim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class CommandClaim extends CommandPolykey {
});
const providerMessage = new identitiesPB.Provider();
providerMessage.setProviderId(providerId);
providerMessage.setMessage(identityId);
providerMessage.setIdentityId(identityId);
await binUtils.retryAuthentication(
(auth) => pkClient.grpcClient.identitiesClaim(providerMessage, auth),
meta,
Expand Down
2 changes: 1 addition & 1 deletion src/bin/identities/CommandDisallow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class CommandDisallow extends CommandPolykey {
// Setting by Identity
const providerMessage = new identitiesPB.Provider();
providerMessage.setProviderId(gestaltId.providerId);
providerMessage.setMessage(gestaltId.identityId);
providerMessage.setIdentityId(gestaltId.identityId);
setActionMessage.setIdentity(providerMessage);
// Trusting.
await binUtils.retryAuthentication(
Expand Down
2 changes: 1 addition & 1 deletion src/bin/identities/CommandDiscover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class CommandDiscover extends CommandPolykey {
// Discovery by Identity
const providerMessage = new identitiesPB.Provider();
providerMessage.setProviderId(gestaltId.providerId);
providerMessage.setMessage(gestaltId.identityId);
providerMessage.setIdentityId(gestaltId.identityId);
await binUtils.retryAuthentication(
(auth) =>
pkClient.grpcClient.gestaltsDiscoveryByIdentity(
Expand Down
9 changes: 2 additions & 7 deletions src/bin/identities/CommandGet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class CommandGet extends CommandPolykey {
// Getting from identity.
const providerMessage = new identitiesPB.Provider();
providerMessage.setProviderId(gestaltId.providerId);
providerMessage.setMessage(gestaltId.identityId);
providerMessage.setIdentityId(gestaltId.identityId);
res = await binUtils.retryAuthentication(
(auth) =>
pkClient.grpcClient.gestaltsGestaltGetByIdentity(
Expand All @@ -88,12 +88,7 @@ class CommandGet extends CommandPolykey {
// Listing identities
for (const identityKey of Object.keys(gestalt.identities)) {
const identity = gestalt.identities[identityKey];
output.push(
parsers.formatIdentityString(
identity.providerId,
identity.identityId,
),
);
output.push(`${identity.providerId}:${identity.identityId}`);
}
}
process.stdout.write(
Expand Down
10 changes: 1 addition & 9 deletions src/bin/identities/CommandList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import type PolykeyClient from '../../PolykeyClient';
import CommandPolykey from '../CommandPolykey';
import * as binOptions from '../utils/options';
import * as binUtils from '../utils';
import * as parsers from '../utils/parsers';
import * as binProcessors from '../utils/processors';

class CommandList extends CommandPolykey {
Expand Down Expand Up @@ -90,25 +89,18 @@ class CommandList extends CommandPolykey {
for (const gestalt of gestalts) {
output.push(`gestalt ${count}`);
output.push(`permissions: ${gestalt.permissions ?? 'None'}`);

// Listing nodes
for (const node of gestalt.nodes) {
output.push(`${node.id}`);
}
// Listing identities
for (const identity of gestalt.identities) {
output.push(
parsers.formatIdentityString(
identity.providerId,
identity.identityId,
),
);
output.push(`${identity.providerId}:${identity.identityId}`);
}
output.push('');
emmacasolin marked this conversation as resolved.
Show resolved Hide resolved
count++;
}
}

process.stdout.write(
binUtils.outputFormatter({
type: options.format === 'json' ? 'json' : 'list',
Expand Down
4 changes: 2 additions & 2 deletions src/bin/identities/CommandPermissions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class CommandPermissions extends CommandPolykey {
port: clientOptions.clientPort,
logger: this.logger.getChild(PolykeyClient.name),
});
let actions;
let actions: string[] = [];
if (gestaltId.nodeId) {
// Getting by Node.
const nodeMessage = new nodesPB.Node();
Expand All @@ -63,7 +63,7 @@ class CommandPermissions extends CommandPolykey {
// Getting by Identity
const providerMessage = new identitiesPB.Provider();
providerMessage.setProviderId(gestaltId.providerId);
providerMessage.setMessage(gestaltId.identityId);
providerMessage.setIdentityId(gestaltId.identityId);
const res = await binUtils.retryAuthentication(
(auth) =>
pkClient.grpcClient.gestaltsActionsGetByIdentity(
Expand Down
15 changes: 7 additions & 8 deletions src/bin/identities/CommandSearch.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import type { ProviderId, IdentityId } from '../../identities/types';
import type PolykeyClient from '../../PolykeyClient';
import CommandPolykey from '../CommandPolykey';
import * as binOptions from '../utils/options';
import * as binUtils from '../utils';
import * as parsers from '../utils/parsers';
import * as binProcessors from '../utils/processors';

class CommandSearch extends CommandPolykey {
Expand Down Expand Up @@ -54,15 +52,16 @@ class CommandSearch extends CommandPolykey {
pkClient.grpcClient.identitiesInfoGet(providerMessage, auth),
meta,
);
let output = '';
if (res.getIdentityId() && res.getProviderId()) {
output = `${res.getProviderId()}:${res.getIdentityId()}`;
} else {
this.logger.info('No Connected Identities found for Provider');
}
process.stdout.write(
binUtils.outputFormatter({
type: options.format === 'json' ? 'json' : 'list',
data: [
parsers.formatIdentityString(
res.getProviderId() as ProviderId,
res.getMessage() as IdentityId,
),
],
data: [output],
}),
);
} finally {
Expand Down
2 changes: 1 addition & 1 deletion src/bin/identities/CommandTrust.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class CommandTrust extends CommandPolykey {
// Setting by Identity
const providerMessage = new identitiesPB.Provider();
providerMessage.setProviderId(gestaltId.providerId!);
providerMessage.setMessage(gestaltId.identityId!);
providerMessage.setIdentityId(gestaltId.identityId!);
setActionMessage.setIdentity(providerMessage);
await binUtils.retryAuthentication(
(auth) =>
Expand Down
2 changes: 1 addition & 1 deletion src/bin/identities/CommandUntrust.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class CommandUntrust extends CommandPolykey {
// Setting by Identity
const providerMessage = new identitiesPB.Provider();
providerMessage.setProviderId(gestaltId.providerId!);
providerMessage.setMessage(gestaltId.identityId!);
providerMessage.setIdentityId(gestaltId.identityId!);
setActionMessage.setIdentity(providerMessage);
await binUtils.retryAuthentication(
(auth) =>
Expand Down
8 changes: 1 addition & 7 deletions src/bin/utils/parsers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,4 @@ function parseIdentityString(identityString: string): {
return { providerId, identityId };
}

function formatIdentityString(
providerId: ProviderId,
identityId: IdentityId,
): string {
return `${providerId}:${identityId}`;
}
export { parseNumber, parseSecretPath, parseGestaltId, formatIdentityString };
export { parseNumber, parseSecretPath, parseGestaltId };
2 changes: 1 addition & 1 deletion src/client/GRPCClientClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ class GRPCClientClient extends GRPCClient<ClientServiceClient> {

@ready(new clientErrors.ErrorClientClientDestroyed())
public identitiesAuthenticate(...args) {
return grpcUtils.promisifyReadableStreamCall<identitiesPB.Provider>(
return grpcUtils.promisifyReadableStreamCall<identitiesPB.AuthenticationProcess>(
this.client,
this.client.identitiesAuthenticate,
)(...args);
Expand Down
5 changes: 4 additions & 1 deletion src/client/clientService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import type { GestaltGraph } from '../gestalts';
import type { SessionManager } from '../sessions';
import type { NotificationsManager } from '../notifications';
import type { Discovery } from '../discovery';
import type { Sigchain } from '../sigchain';
import type { GRPCServer } from '../grpc';
import type { ForwardProxy, ReverseProxy } from '../network';
import type { FileSystem } from '../types';
Expand Down Expand Up @@ -43,6 +44,7 @@ function createClientService({
sessionManager,
notificationsManager,
discovery,
sigchain,
grpcServerClient,
grpcServerAgent,
fwdProxy,
Expand All @@ -58,6 +60,7 @@ function createClientService({
sessionManager: SessionManager;
notificationsManager: NotificationsManager;
discovery: Discovery;
sigchain: Sigchain;
grpcServerClient: GRPCServer;
grpcServerAgent: GRPCServer;
fwdProxy: ForwardProxy;
Expand Down Expand Up @@ -93,7 +96,7 @@ function createClientService({
}),
...createIdentitiesRPC({
identitiesManager,
gestaltGraph,
sigchain,
CMCDragonkai marked this conversation as resolved.
Show resolved Hide resolved
nodeManager,
authenticate,
}),
Expand Down
7 changes: 4 additions & 3 deletions src/client/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ class ErrorClientAuthDenied extends ErrorClient {
exitCode = 77;
}

class ErrorClientInvalidNode extends ErrorClient {
exitCode: number = 70;
class ErrorClientInvalidProvider extends ErrorClient {
CMCDragonkai marked this conversation as resolved.
Show resolved Hide resolved
description = 'Provider Id is invalid or does not exist';
exitCode = 70;
}

export {
Expand All @@ -32,5 +33,5 @@ export {
ErrorClientAuthMissing,
ErrorClientAuthFormat,
ErrorClientAuthDenied,
ErrorClientInvalidNode,
ErrorClientInvalidProvider,
};
Loading