Skip to content

Commit

Permalink
creating new branch for #267
Browse files Browse the repository at this point in the history
changed message to identity_id in provider message

updating all commands that use provider message

updated everything on the agent side for augment command

started tests and debugging augment command

identities claim working in client service test

wrote test for command in identities
  • Loading branch information
emmacasolin committed Nov 2, 2021
1 parent 2732d79 commit 50df7f3
Show file tree
Hide file tree
Showing 17 changed files with 145 additions and 75 deletions.
1 change: 1 addition & 0 deletions src/PolykeyAgent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,7 @@ class Polykey {
nodeManager: this.nodes,
notificationsManager: this.notifications,
sessionManager: this.sessions,
sigchain: this.sigchain,
vaultManager: this.vaults,
fwdProxy: this.fwdProxy,
revProxy: this.revProxy,
Expand Down
2 changes: 1 addition & 1 deletion src/bin/identities/allow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ allow.action(async (id, permissions, options) => {
// Setting by Identity
const providerMessage = new identitiesPB.Provider();
providerMessage.setProviderId(providerId!);
providerMessage.setMessage(identityId!);
providerMessage.setIdentityId(identityId!);
setActionMessage.setIdentity(providerMessage);
name = `${id}`;
//Trusting.
Expand Down
6 changes: 3 additions & 3 deletions src/bin/identities/authenticate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ commandAugmentKeynode.action(async (providerId, identitiyId, options) => {
//Constructing message.
const providerMessage = new identitiesPB.Provider();
providerMessage.setProviderId(providerId);
providerMessage.setMessage(identitiyId);
providerMessage.setIdentityId(identitiyId);

//Sending message.
const gen = grpcClient.identitiesAuthenticate(providerMessage);
Expand All @@ -57,7 +57,7 @@ commandAugmentKeynode.action(async (providerId, identitiyId, options) => {
process.stdout.write(
outputFormatter({
type: options.format === 'json' ? 'json' : 'list',
data: [`Your device code is: ${codeMessage!.getMessage()}`],
data: [`Your device code is: ${codeMessage!.getIdentityId()}`],
}),
);

Expand All @@ -67,7 +67,7 @@ commandAugmentKeynode.action(async (providerId, identitiyId, options) => {
outputFormatter({
type: options.format === 'json' ? 'json' : 'list',
data: [
`Successfully authenticated user: ${successMessage!.getMessage()}`,
`Successfully authenticated user: ${successMessage!.getIdentityId()}`,
],
}),
);
Expand Down
19 changes: 17 additions & 2 deletions src/bin/identities/claim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ claim.action(async (providerId, identitiyId, options) => {
//Constructing message.
const providerMessage = new identitiesPB.Provider();
providerMessage.setProviderId(providerId);
providerMessage.setMessage(identitiyId);
providerMessage.setIdentityId(identitiyId);

//Sending message.
const pCall = grpcClient.identitiesClaim(providerMessage);
Expand All @@ -51,8 +51,23 @@ claim.action(async (providerId, identitiyId, options) => {
await clientUtils.refreshSession(meta, client.session);
resolveP(null);
});
await pCall;
const response = await pCall;
await p;

const output = [`Successfully published identity claim with id: ${response.getClaimId()}
on provider: ${providerId}`];

if (response.getUrl()) {
output.push(`See claim at: ${response.getUrl()}`);
}

process.stdout.write(
outputFormatter({
type: options.format === 'json' ? 'json' : 'list',
data: output,
}),
);

} catch (err) {
if (err instanceof errors.ErrorGRPCClientTimeout) {
process.stderr.write(`${err.message}\n`);
Expand Down
2 changes: 1 addition & 1 deletion src/bin/identities/disallow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ commandAllowGestalts.action(async (id, permissions, options) => {
// Setting by Identity
const providerMessage = new identitiesPB.Provider();
providerMessage.setProviderId(providerId!);
providerMessage.setMessage(identityId!);
providerMessage.setIdentityId(identityId!);
setActionMessage.setIdentity(providerMessage);
name = `${id}`;
//Trusting.
Expand Down
2 changes: 1 addition & 1 deletion src/bin/identities/discover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ commandTrustGestalts.action(async (id, options) => {
// Discovery by Identity
const providerMessage = new identitiesPB.Provider();
providerMessage.setProviderId(providerId!);
providerMessage.setMessage(identityId!);
providerMessage.setIdentityId(identityId!);
const pCall = grpcClient.gestaltsDiscoveryByIdentity(providerMessage);
const { p, resolveP } = utils.promise();
pCall.call.on('metadata', async (meta) => {
Expand Down
2 changes: 1 addition & 1 deletion src/bin/identities/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ get.action(async (id, options) => {
//Getting from identity.
const providerMessage = new identitiesPB.Provider();
providerMessage.setProviderId(providerId!);
providerMessage.setMessage(identityId!);
providerMessage.setIdentityId(identityId!);
const pCall = grpcClient.gestaltsGestaltGetByIdentity(providerMessage);
const { p, resolveP } = utils.promise();
pCall.call.on('metadata', async (meta) => {
Expand Down
2 changes: 1 addition & 1 deletion src/bin/identities/permissions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ commandTrustGestalts.action(async (id, options) => {
//Getting by Identity
const providerMessage = new identitiesPB.Provider();
providerMessage.setProviderId(providerId!);
providerMessage.setMessage(identityId!);
providerMessage.setIdentityId(identityId!);
const pCall = grpcClient.gestaltsActionsGetByIdentity(providerMessage);
const { p, resolveP } = utils.promise();
pCall.call.on('metadata', async (meta) => {
Expand Down
2 changes: 1 addition & 1 deletion src/bin/identities/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ commandTrustGestalts.action(async (providerId, options) => {
process.stdout.write(
outputFormatter({
type: options.format === 'json' ? 'json' : 'list',
data: [`Found identity: ${res.getProviderId()}:${res.getMessage()}`],
data: [`Found identity: ${res.getProviderId()}:${res.getIdentityId()}`],
}),
);
} catch (err) {
Expand Down
2 changes: 1 addition & 1 deletion src/bin/identities/trust.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ trust.action(async (id, options) => {
// Setting by Identity
const providerMessage = new identitiesPB.Provider();
providerMessage.setProviderId(providerId!);
providerMessage.setMessage(identityId!);
providerMessage.setIdentityId(identityId!);
setActionMessage.setIdentity(providerMessage);
const pCall = grpcClient.gestaltsActionsSetByIdentity(setActionMessage);
const { p, resolveP } = utils.promise();
Expand Down
2 changes: 1 addition & 1 deletion src/bin/identities/untrust.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ commandTrustGestalts.action(async (id, options) => {
// Setting by Identity
const providerMessage = new identitiesPB.Provider();
providerMessage.setProviderId(providerId!);
providerMessage.setMessage(identityId!);
providerMessage.setIdentityId(identityId!);
setActionMessage.setIdentity(providerMessage);
const pCall = grpcClient.gestaltsActionsUnsetByIdentity(setActionMessage);
const { p, resolveP } = utils.promise();
Expand Down
4 changes: 4 additions & 0 deletions src/client/clientService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type { NotificationsManager } from '../notifications';
import type { Discovery } from '../discovery';
import type { ForwardProxy, ReverseProxy } from '../network';
import type { GRPCServer } from '../grpc';
import { Sigchain } from '../sigchain';

import { promisify } from 'util';
import * as grpc from '@grpc/grpc-js';
Expand Down Expand Up @@ -49,6 +50,7 @@ function createClientService({
identitiesManager,
gestaltGraph,
sessionManager,
sigchain,
notificationsManager,
discovery,
fwdProxy,
Expand All @@ -62,6 +64,7 @@ function createClientService({
identitiesManager: IdentitiesManager;
gestaltGraph: GestaltGraph;
sessionManager: SessionManager;
sigchain: Sigchain;
notificationsManager: NotificationsManager;
discovery: Discovery;
fwdProxy: ForwardProxy;
Expand Down Expand Up @@ -90,6 +93,7 @@ function createClientService({
}),
...createIdentitiesRPC({
identitiesManager,
sigchain,
gestaltGraph,
nodeManager,
sessionManager,
Expand Down
10 changes: 5 additions & 5 deletions src/client/rpcGestalts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ const createGestaltsRPC = ({
call.sendMetadata(responseMeta);
const gestalt = await gestaltGraph.getGestaltByIdentity(
call.request.getProviderId() as ProviderId,
call.request.getMessage() as IdentityId,
call.request.getIdentityId() as IdentityId,
);
if (gestalt != null) {
response.setGestaltGraph(JSON.stringify(gestalt));
Expand Down Expand Up @@ -134,7 +134,7 @@ const createGestaltsRPC = ({
//Constructing identity info.
const gen = discovery.discoverGestaltByIdentity(
info.getProviderId() as ProviderId,
info.getMessage() as IdentityId,
info.getIdentityId() as IdentityId,
);
for await (const _ of gen) {
// Empty
Expand Down Expand Up @@ -185,7 +185,7 @@ const createGestaltsRPC = ({
);
call.sendMetadata(responseMeta);
const providerId = info.getProviderId() as ProviderId;
const identityId = info.getMessage() as IdentityId;
const identityId = info.getIdentityId() as IdentityId;
const result = await gestaltGraph.getGestaltActionsByIdentity(
providerId,
identityId,
Expand Down Expand Up @@ -265,7 +265,7 @@ const createGestaltsRPC = ({
//Setting the action.
const action = makeGestaltAction(info.getAction());
const providerId = info.getIdentity()?.getProviderId() as ProviderId;
const identityId = info.getIdentity()?.getMessage() as IdentityId;
const identityId = info.getIdentity()?.getIdentityId() as IdentityId;
await gestaltGraph.setGestaltActionByIdentity(
providerId,
identityId,
Expand Down Expand Up @@ -338,7 +338,7 @@ const createGestaltsRPC = ({
//Setting the action.
const action = makeGestaltAction(info.getAction());
const providerId = info.getIdentity()?.getProviderId() as ProviderId;
const identityId = info.getIdentity()?.getMessage() as IdentityId;
const identityId = info.getIdentity()?.getIdentityId() as IdentityId;
await gestaltGraph.unsetGestaltActionByIdentity(
providerId,
identityId,
Expand Down
65 changes: 39 additions & 26 deletions src/client/rpcIdentities.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,32 @@
import type { NodeManager } from '../nodes';
import type { NodeInfo } from '../nodes/types';
import type { GestaltGraph } from '../gestalts';
import type { SessionManager } from '../sessions';
import type { Sigchain } from '../sigchain';
import type { IdentitiesManager } from '../identities';
import type { ClaimLinkIdentity, ClaimType } from '../claims/types';
import type {
IdentityId,
ProviderId,
TokenData,
IdentityInfo,
} from '../identities/types';

import * as utils from './utils';
import * as errors from '../errors';
import * as grpc from '@grpc/grpc-js';
import * as claimsUtils from '../claims/utils';
import * as grpcUtils from '../grpc/utils';
import * as utilsPB from '../proto/js/polykey/v1/utils/utils_pb';
import * as identitiesPB from '../proto/js/polykey/v1/identities/identities_pb';

const createIdentitiesRPC = ({
identitiesManager,
sigchain,
nodeManager,
gestaltGraph,
sessionManager,
}: {
identitiesManager: IdentitiesManager;
sigchain: Sigchain;
nodeManager: NodeManager;
gestaltGraph: GestaltGraph;
sessionManager: SessionManager;
Expand Down Expand Up @@ -53,7 +56,7 @@ const createIdentitiesRPC = ({
'userCode was not a string',
);
}
response.setMessage(userCode);
response.setIdentityId(userCode);
await genWritable.next(response);

//Wait to finish.
Expand All @@ -62,7 +65,7 @@ const createIdentitiesRPC = ({
throw new errors.ErrorProviderAuthentication(
'Failed to authenticate.',
);
response.setMessage(userName);
response.setIdentityId(userName);
await genWritable.next(response);
await genWritable.next(null);
} catch (err) {
Expand All @@ -86,7 +89,7 @@ const createIdentitiesRPC = ({
const provider = call.request.getProvider();
await identitiesManager.putToken(
provider?.getProviderId() as ProviderId,
provider?.getMessage() as IdentityId,
provider?.getIdentityId() as IdentityId,
{ accessToken: call.request.getToken() } as TokenData,
);
} catch (err) {
Expand All @@ -107,7 +110,7 @@ const createIdentitiesRPC = ({
call.sendMetadata(responseMeta);
const tokens = await identitiesManager.getToken(
call.request.getProviderId() as ProviderId,
call.request.getMessage() as IdentityId,
call.request.getIdentityId() as IdentityId,
);
response.setToken(JSON.stringify(tokens));
} catch (err) {
Expand All @@ -128,7 +131,7 @@ const createIdentitiesRPC = ({
call.sendMetadata(responseMeta);
await identitiesManager.delToken(
call.request.getProviderId() as ProviderId,
call.request.getMessage() as IdentityId,
call.request.getIdentityId() as IdentityId,
);
} catch (err) {
callback(grpcUtils.fromError(err), response);
Expand Down Expand Up @@ -171,7 +174,7 @@ const createIdentitiesRPC = ({
?.getProviderId() as ProviderId;
const identityId = call.request
.getProvider()
?.getMessage() as IdentityId;
?.getIdentityId() as IdentityId;
const provider = identitiesManager.getProvider(providerId);
if (provider == null)
throw Error(
Expand All @@ -187,7 +190,7 @@ const createIdentitiesRPC = ({
const identityInfoMessage = new identitiesPB.Info();
const providerMessage = new identitiesPB.Provider();
providerMessage.setProviderId(identity.providerId);
providerMessage.setMessage(identity.identityId);
providerMessage.setIdentityId(identity.identityId);
identityInfoMessage.setProvider(providerMessage);
identityInfoMessage.setName(identity.name ?? '');
identityInfoMessage.setEmail(identity.email ?? '');
Expand Down Expand Up @@ -220,7 +223,7 @@ const createIdentitiesRPC = ({
const identities = await provider.getAuthIdentityIds();
if (identities.length !== 0) {
providerMessage.setProviderId(providerId);
providerMessage.setMessage(identities[0]);
providerMessage.setIdentityId(identities[0]);
} else throw Error(`No identities found for provider: ${providerId}`);
callback(null, providerMessage);
} catch (err) {
Expand All @@ -234,29 +237,39 @@ const createIdentitiesRPC = ({
call: grpc.ServerUnaryCall<identitiesPB.Provider, utilsPB.EmptyMessage>,
callback: grpc.sendUnaryData<utilsPB.EmptyMessage>,
): Promise<void> => {
// To augment a keynode we need a provider, generate an oauthkey and then
const info = call.request;
const response = new clientPB.IdentityClaimMessage();
try {
await sessionManager.verifyToken(utils.getToken(call.metadata));
const responseMeta = utils.createMetaTokenResponse(
await sessionManager.generateToken(),
);
call.sendMetadata(responseMeta);
const nodeId = nodeManager.getNodeId(); //Getting the local node ID.

//Do the deed...
const nodeInfo: NodeInfo = {
id: nodeId,
chain: {},
};
const identityInfo: IdentityInfo = {
providerId: info.getProviderId() as ProviderId,
identityId: info.getMessage() as IdentityId,
claims: {},
};
await gestaltGraph.linkNodeAndIdentity(nodeInfo, identityInfo); //Need to call this
// it takes NodeInfo and IdentityInfo.
// Getting and creating NodeInfo is blocked by
// Check provider is authenticated
const providerId = call.request.getProviderId() as ProviderId;
const provider = identitiesManager.getProvider(providerId);
if (provider == null) throw Error(`Invalid provider: ${providerId}`);

const identityId = call.request.getIdentityId() as IdentityId;
const token = await identitiesManager.getToken(providerId, identityId);
if (token == null) {
throw Error(`${identityId} has not been authenticated`);
}

await provider.checkToken(token, identityId);

// Create identity claim on our node
const claim = await nodeManager.claimIdentity(providerId, identityId);

// Publish claim on identity
const claimDecoded = claimsUtils.decodeClaim(claim);
const publishedClaimData = await provider.publishClaim(identityId, claimDecoded);

response.setClaimId(publishedClaimData.id);
if (publishedClaimData.url !== undefined) {
response.setUrl(publishedClaimData.url);
}

} catch (err) {
callback(grpcUtils.fromError(err), null);
}
Expand Down
Loading

0 comments on commit 50df7f3

Please sign in to comment.