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

Human readable sign-in #274

Merged
merged 2 commits into from
Jul 1, 2024
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
5 changes: 3 additions & 2 deletions packages/client-node/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ services:
- IPFS_HOST=/dns4/ipfs1/tcp/5001
- IPFS_MIN_REPLICA=1
- IPFS_MAX_REPLICA=2
- ENC_PASSWORD=test
- ENC_PASSWORD_5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY=test1
- ENC_PASSWORD_5FLSigC9HGRKVhB9FiEo4Y3koPsNmBmLJbpXg2mp1hXcS59Y=test2
depends_on:
- private-database1
- ipfs-cluster1
Expand Down Expand Up @@ -79,7 +80,7 @@ services:
- IPFS_HOST=/dns4/ipfs2/tcp/5001
- IPFS_MIN_REPLICA=1
- IPFS_MAX_REPLICA=2
- ENC_PASSWORD=test
- ENC_PASSWORD_5FHneW46xGXgs5mUiveU4sbTyGBzmstUspZC92UhjJM694ty=test
depends_on:
- private-database2
- ipfs-cluster2
Expand Down
2 changes: 1 addition & 1 deletion packages/client/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@logion/client",
"version": "0.46.1-1",
"version": "0.46.1-2",
"description": "logion SDK for client applications",
"main": "dist/index.js",
"packageManager": "yarn@3.2.0",
Expand Down
7 changes: 2 additions & 5 deletions packages/client/src/AuthenticationClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,14 @@ export class AuthenticationClient {
const addresses = validAccountIds.map(validAccountId => validAccountId.toKey());
const signInResponse = await axios.post("/api/auth/sign-in", { addresses });
const sessionId = signInResponse.data.sessionId;
const attributes = [ sessionId ];

const signatures: Record<string, AuthenticationSignature> = {};
for(const validAccountId of validAccountIds) {
const signedOn = DateTime.now();
const signature = await signer.signRaw({
signerId: validAccountId,
resource: 'authentication',
operation: 'login',
signedOn,
attributes,
sessionId,
});
signatures[validAccountId.toKey()] = {
signature: signature.signature,
Expand All @@ -66,7 +63,7 @@ export class AuthenticationClient {
};
}

const authenticateResponse = await axios.post(`/api/auth/${sessionId}/authenticate`, {
const authenticateResponse = await axios.post(`/api/auth/${sessionId}/authenticate/v2`, {
signatures
});
const authenticatedAddresses: AuthenticationResponse = authenticateResponse.data.tokens;
Expand Down
13 changes: 2 additions & 11 deletions packages/client/src/Signer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,15 @@ import { ISubmittableResult } from '@polkadot/types/types';
import { ExtrinsicStatus } from '@polkadot/types/interfaces/author';
import { Registry } from '@polkadot/types-codec/types';
import { base64Encode } from '@polkadot/util-crypto';
import { stringToHex } from '@polkadot/util';
import { Hash } from 'fast-sha256';
import { DateTime } from "luxon";
import { toIsoString } from "./DateTimeUtil.js";
import { requireDefined } from "./assertions.js";

export interface SignRawParameters {
signerId: ValidAccountId;
resource: string;
operation: string;
signedOn: DateTime;
attributes: string[];
sessionId: string;
}

export type SignatureType = "POLKADOT" | "ETHEREUM" | "CROSSMINT_ETHEREUM" | "MULTIVERSX";
Expand Down Expand Up @@ -107,13 +104,7 @@ export abstract class BaseSigner implements FullSigner {
abstract signToHex(signerId: ValidAccountId, message: string): Promise<TypedSignature>;

buildMessage(parameters: SignRawParameters): string {
return stringToHex(hashAttributes(this.buildAttributes(parameters)));
}

buildAttributes(parameters: SignRawParameters): string[] {
const signedOn = toIsoString(parameters.signedOn);
const attributes = [parameters.resource, parameters.operation, signedOn];
return attributes.concat(parameters.attributes);
return `logion-auth: ${ parameters.sessionId } on ${ toIsoString(parameters.signedOn) }`
}

async signAndSend(parameters: SignParameters): Promise<SuccessfulSubmission> {
Expand Down
8 changes: 3 additions & 5 deletions packages/client/test/AuthenticationClient.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,8 @@ function setupSignIn(axiosInstance: Mock<AxiosInstance>, validAccountIds: ValidA
function setupSignatures(signer: Mock<RawSigner>, addresses: ValidAccountId[], sessionId: string, signatures: string[]) {
for(let i = 0; i < addresses.length; ++i) {
signer.setup(instance => instance.signRaw(It.Is<SignRawParameters>(params =>
params.resource === "authentication"
&& params.signerId === addresses[i]
&& params.operation === "login"
&& params.attributes[0] === sessionId
params.signerId === addresses[i]
&& params.sessionId === sessionId
))).returns(Promise.resolve({ signature: signatures[i], type: "POLKADOT"}));
}
}
Expand All @@ -131,7 +129,7 @@ function setupAuthenticate(axiosInstance: Mock<AxiosInstance>, accounts: ValidAc
authenticateResponse.setup(instance => instance.data).returns({
tokens
});
axiosInstance.setup(instance => instance.post(`/api/auth/${sessionId}/authenticate`, It.Is<any>(body =>
axiosInstance.setup(instance => instance.post(`/api/auth/${sessionId}/authenticate/v2`, It.Is<any>(body =>
bodyIncludesSignatures(body, accounts, signatures)
))).returns(Promise.resolve(authenticateResponse.object()));
}
Expand Down
Loading