Skip to content

Commit

Permalink
feat: splits createTransaction into two functions for register and au…
Browse files Browse the repository at this point in the history
…thenticate
  • Loading branch information
ctran88 committed Apr 4, 2024
1 parent 33c930b commit 76f47f2
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 15 deletions.
23 changes: 20 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ const passageApp = await passage.getApp();
console.log(passageApp.authOrigin);
```

## Create a transaction
## Create a registration transaction

To create a transaction to kick off a user passkey registration or authentication, you should use the `passage.createTransaction()` function.
To create a transaction to kick off a user passkey registration, you should use the `passage.createRegisterTransaction()` function.

```javascript
import { PassageFlex } from '@passageidentity/passage-flex-node';
Expand All @@ -59,12 +59,29 @@ const passage = new PassageFlex({
apiKey: process.env.PASSAGE_API_KEY,
});

const transaction = await passage.createTransaction({
const transaction = await passage.createRegisterTransaction({
externalId: 'a unique immutable string that represents your user',
passkeyDisplayName: "the label for the user's passkey that they will see when logging in",
});
```

## Create an authentication transaction

To create a transaction to kick off a user passkey authentication, you should use the `passage.createAuthenticateTransaction()` function.

```javascript
import { PassageFlex } from '@passageidentity/passage-flex-node';

const passage = new PassageFlex({
appId: process.env.PASSAGE_APP_ID,
apiKey: process.env.PASSAGE_API_KEY,
});

const transaction = await passage.createAuthenticateTransaction({
externalId: 'a unique immutable string that represents your user',
});
```

## Verify a nonce

To verify a nonce that you received from the end of of passkey registration or authentication ceremony, you should use the `passage.verifyNonce()` function.
Expand Down
37 changes: 30 additions & 7 deletions src/classes/PassageFlex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ import {
} from '../generated';
import apiConfiguration from '../utils/apiConfiguration';
import { AppInfo } from '../models/AppInfo';
import { TransactionArgs } from '../types/TransactionArgs';
import { AuthenticateTransactionArgs } from '../types/AuthenticateTransactionArgs';
import { UserInfo } from '../models/UserInfo';
import { RegisterTransactionArgs } from '../types/RegisterTransactionArgs';

/**
* PassageFlex class used to get app info, create transactions, and verify nonces
Expand Down Expand Up @@ -77,25 +78,47 @@ export class PassageFlex {
}

/**
* Create a transaction to start a user's registration or authentication process
* Create a transaction to start a user's registration process
*
* @param {TransactionArgs} args The required values to create a transaction
* @param {RegisterTransactionArgs} args The required values to create a transaction
* @return {Promise<string>} The transaction ID
*/
public async createTransaction(args: TransactionArgs): Promise<string> {
public async createRegisterTransaction(args: RegisterTransactionArgs): Promise<string> {
try {
const { externalId, passkeyDisplayName } = args;
const response = await this.transactionClient.createTransaction({
const response = await this.transactionClient.createRegisterTransaction({
appId: this.appId,
createTransactionRequest: {
createTransactionRegisterRequest: {
externalId,
passkeyDisplayName,
},
});

return response.transactionId;
} catch (err) {
throw new PassageError('Could not create transaction', err as ResponseError);
throw new PassageError('Could not create register transaction', err as ResponseError);
}
}

/**
* Create a transaction to start a user's authentication process
*
* @param {AuthenticateTransactionArgs} args The required values to create a transaction
* @return {Promise<string>} The transaction ID
*/
public async createAuthenticateTransaction(args: AuthenticateTransactionArgs): Promise<string> {
try {
const { externalId } = args;
const response = await this.transactionClient.createAuthenticateTransaction({
appId: this.appId,
createTransactionAuthenticateRequest: {
externalId,
},
});

return response.transactionId;
} catch (err) {
throw new PassageError('Could not create authenticate transaction', err as ResponseError);
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ export { PassageError } from './classes/PassageError';
export { AppInfo } from './models/AppInfo';
export { UserInfo } from './models/UserInfo';
export type { PassageConfig } from './types/PassageConfig';
export type { TransactionArgs } from './types/TransactionArgs';
export type { RegisterTransactionArgs } from './types/RegisterTransactionArgs';
export type { AuthenticateTransactionArgs } from './types/AuthenticateTransactionArgs';
export { UserStatus, WebAuthnDevices, WebAuthnIcons, WebAuthnType } from './generated';
6 changes: 6 additions & 0 deletions src/types/AuthenticateTransactionArgs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export type AuthenticateTransactionArgs = {
/**
* the user's unique identifier that will be associated with this transaction
*/
externalId: string;
};
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export type TransactionArgs = {
export type RegisterTransactionArgs = {
/**
* the user's unique identifier that will be associated with this transaction
*/
Expand Down
24 changes: 21 additions & 3 deletions tests/Passage.test.ts → tests/PassageFlex.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { WebAuthnDevices, WebAuthnType } from '../src/generated';

require('dotenv').config();

describe('Passage', () => {
describe('PassageFlex', () => {
const expectedAppId = process.env.TEST_APP_ID;
const expectedApiKey = process.env.TEST_API_KEY;
const userId = process.env.TEST_USER_ID;
Expand Down Expand Up @@ -68,7 +68,7 @@ describe('Passage', () => {
});
});

describe('createTransaction', () => {
describe('createRegisterTransaction', () => {
let passage: PassageFlex;

beforeEach(() => {
Expand All @@ -79,14 +79,32 @@ describe('Passage', () => {
});

it('should return the transaction ID', async () => {
const transactionId = await passage.createTransaction({
const transactionId = await passage.createRegisterTransaction({
externalId: 'test',
passkeyDisplayName: 'test',
});
expect(transactionId).toEqual(expect.any(String));
});
});

describe('createAuthenticateTransaction', () => {
let passage: PassageFlex;

beforeEach(() => {
passage = new PassageFlex({
appId: expectedAppId,
apiKey: expectedApiKey,
});
});

it('should return the transaction ID', async () => {
const transactionId = await passage.createAuthenticateTransaction({
externalId: 'test',
});
expect(transactionId).toEqual(expect.any(String));
});
});

describe('verifyNonce', () => {
let passage: PassageFlex;

Expand Down

0 comments on commit 76f47f2

Please sign in to comment.