Skip to content

Commit

Permalink
[TECH-2970] Add ballot reference param to request access code (#288)
Browse files Browse the repository at this point in the history
* Add ballot reference param

* Update lib/av_client/connectors/voter_authorization_coordinator.ts

Co-authored-by: Martin Laursen <[email protected]>

---------

Co-authored-by: Martin Laursen <[email protected]>
  • Loading branch information
av-mads and av-martin authored Jan 31, 2024
1 parent 8d1db47 commit 4dc7374
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
6 changes: 4 additions & 2 deletions lib/av_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,17 +146,19 @@ export class AVClient implements IAVClient {
*
* @param opaqueVoterId Voter ID that preserves voter anonymity.
* @param email where the voter expects to receive otp code.
* @param ballotReference The ballot which which voter voter intends to vote on
* @returns Returns undefined or throws an error.
* @throws VoterRecordNotFound if no voter was found
* @throws {@link NetworkError | NetworkError } if any request failed to get a response
*/
public async requestAccessCode(opaqueVoterId: string, email: string): Promise<void> {
public async requestAccessCode(opaqueVoterId: string, email: string, ballotReference?: string): Promise<void> {
const coordinatorURL = this.getLatestConfig().items.voterAuthorizerConfig.content.voterAuthorizer.url;
const voterAuthorizerContextUuid = this.getLatestConfig().items.voterAuthorizerConfig.content.voterAuthorizer.contextUuid;
const coordinator = new VoterAuthorizationCoordinator(coordinatorURL, voterAuthorizerContextUuid);

return coordinator.createSession(opaqueVoterId, email)
return coordinator.createSession(opaqueVoterId, email, ballotReference)
.then(({ data: { sessionId } }) => {
// In the US voters are allowed to chose their ballot
return sessionId as string
})
.then(async sessionId => {
Expand Down
9 changes: 6 additions & 3 deletions lib/av_client/connectors/voter_authorization_coordinator.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import axios, { AxiosInstance, AxiosResponse } from 'axios'
import { IdentityConfirmationToken } from "./otp_provider";
import { EmailDoesNotMatchVoterRecordError, NetworkError, UnsupportedServerReplyError, VoterRecordNotFoundError, DBBError } from "../errors";
import { EmailDoesNotMatchVoterRecordError, NetworkError, UnsupportedServerReplyError, VoterRecordNotFoundError, DBBError, BallotReferenceNotOnVoterRecord } from "../errors";
import { ProofOfElectionCodes } from "../crypto/proof_of_election_codes";
import { BallotBoxReceipt } from '../types';

Expand All @@ -18,11 +18,12 @@ export default class VoterAuthorizationCoordinator {
* @param opaqueVoterId Gets
* @returns
*/
createSession(opaqueVoterId: string, email: string): Promise<AxiosResponse> {
createSession(opaqueVoterId: string, email: string, ballotReference?: string): Promise<AxiosResponse> {
return this.backend.post('create_session', {
electionContextUuid: this.electionContextUuid,
opaqueVoterId: opaqueVoterId,
email
ballotReference: ballotReference,
email,
}).catch(error => {
const response = error.response;

Expand All @@ -34,6 +35,8 @@ export default class VoterAuthorizationCoordinator {
const errorCode = response.data.error_code;
const errorMessage = response.data.error_message;
switch(errorCode) {
case 'BALLOT_REFERERENCE_NOT_ON_VOTER_RECORD':
throw new BallotReferenceNotOnVoterRecord(errorMessage);
case 'EMAIL_DOES_NOT_MATCH_VOTER_RECORD':
throw new EmailDoesNotMatchVoterRecordError(errorMessage);
case 'COULD_NOT_CONNECT_TO_OTP_PROVIDER':
Expand Down
9 changes: 9 additions & 0 deletions lib/av_client/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,15 @@ export class EmailDoesNotMatchVoterRecordError extends AvClientError {
}
}

export class BallotReferenceNotOnVoterRecord extends AvClientError {
readonly name = "BallotReferenceNotOnVoterRecord";

constructor(message: string) {
super(message);
Object.setPrototypeOf(this, BallotReferenceNotOnVoterRecord.prototype);
}
}

export class DBBError extends AvClientError {
readonly name = "DBB_ERROR";

Expand Down

0 comments on commit 4dc7374

Please sign in to comment.