Skip to content

Commit

Permalink
Merge branch 'jul/fix-oidc-case' into 'master'
Browse files Browse the repository at this point in the history
fix(oidc): fix type case

See merge request TankerHQ/sdk-js!1015
  • Loading branch information
JMounier committed Mar 6, 2024
2 parents 37f0ea4 + 1d8837e commit b392086
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 31 deletions.
14 changes: 7 additions & 7 deletions packages/core/src/LocalUser/requests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type EmailRequest = {
hashed_email: Uint8Array;
v2_encrypted_email: Uint8Array;
};
type OidcRequest = {
type OidcIdTokenRequest = {
oidc_id_token: string;
oidc_challenge: b64string;
oidc_challenge_signature: b64string;
Expand All @@ -32,16 +32,16 @@ type PhoneNumberRequest = {
type E2ePassphraseRequest = {
hashed_e2e_passphrase: Uint8Array;
};
type OIDCRequest = {
type OidcRequest = {
oidc_subject: string,
oidc_provider_id: string,
};

export type PreverifiedVerificationRequest = Preverified<EmailRequest> | Preverified<PhoneNumberRequest> | Preverified<OIDCRequest>;
export type PreverifiedVerificationRequest = Preverified<EmailRequest> | Preverified<PhoneNumberRequest> | Preverified<OidcRequest>;

export type VerificationRequestWithToken = WithToken<PassphraseRequest>
| WithVerificationCode<EmailRequest>
| WithToken<OidcRequest>
| WithToken<OidcIdTokenRequest>
| WithVerificationCode<PhoneNumberRequest>
| WithToken<E2ePassphraseRequest>;
export type VerificationRequest = VerificationRequestWithToken | PreverifiedVerificationRequest;
Expand Down Expand Up @@ -113,10 +113,10 @@ export const formatVerificationRequest = async (
};
}

if ('preverifiedOIDCSubject' in verification) {
if ('preverifiedOidcSubject' in verification) {
return {
oidc_provider_id: verification.oidcProviderID,
oidc_subject: verification.preverifiedOIDCSubject,
oidc_provider_id: verification.oidcProviderId,
oidc_subject: verification.preverifiedOidcSubject,
is_preverified: true,
};
}
Expand Down
28 changes: 14 additions & 14 deletions packages/core/src/LocalUser/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ export type OidcVerification = { oidcIdToken: string; };
export type PhoneNumberVerification = { phoneNumber: string; verificationCode: string; };
export type PreverifiedEmailVerification = { preverifiedEmail: string; };
export type PreverifiedPhoneNumberVerification = { preverifiedPhoneNumber: string; };
export type PreverifiedOIDCVerification = { preverifiedOIDCSubject: string; oidcProviderID: string };
export type PreverifiedVerification = PreverifiedEmailVerification | PreverifiedPhoneNumberVerification | PreverifiedOIDCVerification;
export type PreverifiedOidcVerification = { preverifiedOidcSubject: string; oidcProviderId: string };
export type PreverifiedVerification = PreverifiedEmailVerification | PreverifiedPhoneNumberVerification | PreverifiedOidcVerification;

export type ProvisionalVerification = EmailVerification | PhoneNumberVerification;
export type E2eRemoteVerification = E2ePassphraseVerification;
Expand All @@ -34,7 +34,7 @@ export type RemoteVerification = E2eRemoteVerification
| PhoneNumberVerification
| PreverifiedEmailVerification
| PreverifiedPhoneNumberVerification
| PreverifiedOIDCVerification;
| PreverifiedOidcVerification;
export type Verification = RemoteVerification | KeyVerification;

export type WithTokenOptions = { withToken?: { nonce: string; }; };
Expand All @@ -44,17 +44,17 @@ export type RemoteVerificationWithToken = RemoteVerification & WithTokenOptions;
export type VerificationOptions = { withSessionToken?: boolean; allowE2eMethodSwitch?: boolean; };

const validE2eMethods = ['e2ePassphrase'];
const validNonE2eMethods = ['email', 'passphrase', 'verificationKey', 'oidcIdToken', 'phoneNumber', 'preverifiedEmail', 'preverifiedPhoneNumber', 'preverifiedOIDCSubject'];
const validNonE2eMethods = ['email', 'passphrase', 'verificationKey', 'oidcIdToken', 'phoneNumber', 'preverifiedEmail', 'preverifiedPhoneNumber', 'preverifiedOidcSubject'];
const validMethods = [...validE2eMethods, ...validNonE2eMethods];
const validKeys = [...validMethods, 'verificationCode', 'oidcProviderID'];
const validKeys = [...validMethods, 'verificationCode', 'oidcProviderId'];

const validVerifOptionsKeys = ['withSessionToken', 'allowE2eMethodSwitch'];

export const isE2eVerification = (verification: VerificationWithToken): verification is E2eRemoteVerification => Object.keys(verification).some(k => validE2eMethods.includes(k));

export const isNonE2eVerification = (verification: VerificationWithToken) => Object.keys(verification).some(k => validNonE2eMethods.includes(k));

export const isPreverifiedVerification = (verification: VerificationWithToken): verification is PreverifiedVerification => 'preverifiedEmail' in verification || 'preverifiedPhoneNumber' in verification || 'preverifiedOIDCSubject' in verification;
export const isPreverifiedVerification = (verification: VerificationWithToken): verification is PreverifiedVerification => 'preverifiedEmail' in verification || 'preverifiedPhoneNumber' in verification || 'preverifiedOidcSubject' in verification;

export const isPreverifiedVerificationMethod = (verificationMethod: VerificationMethod): verificationMethod is (PreverifiedEmailVerificationMethod | PreverifiedPhoneNumberVerificationMethod) => verificationMethod.type === 'preverifiedEmail' || verificationMethod.type === 'preverifiedPhoneNumber';

Expand Down Expand Up @@ -97,12 +97,12 @@ export const assertVerification = (verification: Verification) => {
assertNotEmptyString(verification.preverifiedEmail, 'verification.preverifiedEmail');
} else if ('preverifiedPhoneNumber' in verification) {
assertNotEmptyString(verification.preverifiedPhoneNumber, 'verification.preverifiedPhoneNumber');
} else if ('preverifiedOIDCSubject' in verification) {
assertNotEmptyString(verification.preverifiedOIDCSubject, 'verification.preverifiedOIDCSubject');
if (!('oidcProviderID' in verification)) {
throw new InvalidArgument('verification', 'oidc pre-verification should also have a oidcProviderID', verification);
} else if ('preverifiedOidcSubject' in verification) {
assertNotEmptyString(verification.preverifiedOidcSubject, 'verification.preverifiedOidcSubject');
if (!('oidcProviderId' in verification)) {
throw new InvalidArgument('verification', 'oidc pre-verification should also have a oidcProviderId', verification);
}
assertNotEmptyString(verification.oidcProviderID, 'verification.oidcProviderID');
assertNotEmptyString(verification.oidcProviderId, 'verification.oidcProviderId');
}
};

Expand Down Expand Up @@ -143,15 +143,15 @@ export const countPreverifiedVerifications = (verifications: Array<PreverifiedVe
const counts = {
preverifiedEmail: 0,
preverifiedPhoneNumber: 0,
preverifiedOIDCSubject: 0,
preverifiedOidcSubject: 0,
};
verifications.forEach((verification) => {
if ('preverifiedEmail' in verification) {
counts.preverifiedEmail += 1;
} else if ('preverifiedPhoneNumber' in verification) {
counts.preverifiedPhoneNumber += 1;
} else if ('preverifiedOIDCSubject' in verification) {
counts.preverifiedOIDCSubject += 1;
} else if ('preverifiedOidcSubject' in verification) {
counts.preverifiedOidcSubject += 1;
} else {
assertNever(verification, 'verification');
}
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/Tanker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ export class Tanker extends EventEmitter {
}

const counts = countPreverifiedVerifications(verifications);
if (counts.preverifiedEmail > 1 || counts.preverifiedPhoneNumber > 1 || counts.preverifiedOIDCSubject > 1) {
if (counts.preverifiedEmail > 1 || counts.preverifiedPhoneNumber > 1 || counts.preverifiedOidcSubject > 1) {
throw new InvalidArgument('verications', 'contains at most one of each preverified verification method', counts);
}

Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export type {
KeyVerification,
PreverifiedEmailVerification,
PreverifiedPhoneNumberVerification,
PreverifiedOIDCVerification,
PreverifiedOidcVerification,
PreverifiedVerification,
Verification,
VerificationMethod,
Expand Down
10 changes: 5 additions & 5 deletions packages/functional-tests/src/enroll.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Tanker, b64string, PreverifiedPhoneNumberVerification, PreverifiedEmailVerification, PreverifiedOIDCVerification } from '@tanker/core';
import type { Tanker, b64string, PreverifiedPhoneNumberVerification, PreverifiedEmailVerification, PreverifiedOidcVerification } from '@tanker/core';
import { expect } from '@tanker/test-utils';
import { getPublicIdentity } from '@tanker/identity';
import { statuses, errors } from '@tanker/core';
Expand All @@ -17,7 +17,7 @@ export const generateEnrollTests = (args: TestArgs) => {
let bobIdentity: b64string;
let emailVerification: PreverifiedEmailVerification;
let phoneNumberVerification: PreverifiedPhoneNumberVerification;
let oidcVerification: PreverifiedOIDCVerification;
let oidcVerification: PreverifiedOidcVerification;
let providerID: string;

before(async () => {
Expand All @@ -34,8 +34,8 @@ export const generateEnrollTests = (args: TestArgs) => {
preverifiedPhoneNumber: phoneNumber,
};
oidcVerification = {
oidcProviderID: providerID,
preverifiedOIDCSubject: 'a subject',
oidcProviderId: providerID,
preverifiedOidcSubject: 'a subject',
};
});

Expand Down Expand Up @@ -115,7 +115,7 @@ export const generateEnrollTests = (args: TestArgs) => {
await appHelper.setEnrollUsersEnabled();
// Let's say Martine is bob's middle name
bobIdToken = await getGoogleIdToken(oidcSettings.googleAuth.users.martine.refreshToken);
oidcVerification.preverifiedOIDCSubject = extractSubject(bobIdToken);
oidcVerification.preverifiedOidcSubject = extractSubject(bobIdToken);
});

after(async () => {
Expand Down
6 changes: 3 additions & 3 deletions packages/functional-tests/src/verification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -724,23 +724,23 @@ export const generateVerificationTests = (args: TestArgs) => {
});

it('fails when registering with a preverified oidc', async () => {
await expect(bobLaptop.registerIdentity({ preverifiedOIDCSubject: subject, oidcProviderID: provider.id })).to.be.rejectedWith(errors.InvalidArgument, 'cannot register identity with preverified methods');
await expect(bobLaptop.registerIdentity({ preverifiedOidcSubject: subject, oidcProviderId: provider.id })).to.be.rejectedWith(errors.InvalidArgument, 'cannot register identity with preverified methods');
});

it('fails when verifying identity with preverified oidc', async () => {
await bobLaptop.setOidcTestNonce(await bobLaptop.createOidcNonce());
await bobLaptop.registerIdentity({ oidcIdToken: martineIdToken });

await bobPhone.start(bobIdentity);
await expect(bobPhone.verifyIdentity({ preverifiedOIDCSubject: subject, oidcProviderID: provider.id })).to.be.rejectedWith(errors.InvalidArgument, 'cannot verify identity with preverified methods');
await expect(bobPhone.verifyIdentity({ preverifiedOidcSubject: subject, oidcProviderId: provider.id })).to.be.rejectedWith(errors.InvalidArgument, 'cannot verify identity with preverified methods');
});

it('adds preverified oidc as a new verification method', async () => {
const email = await appHelper.generateRandomEmail();
let verificationCode = await appHelper.getEmailVerificationCode(email);
await bobLaptop.registerIdentity({ email, verificationCode });

await bobLaptop.setVerificationMethod({ preverifiedOIDCSubject: subject, oidcProviderID: provider.id });
await bobLaptop.setVerificationMethod({ preverifiedOidcSubject: subject, oidcProviderId: provider.id });

expect(await bobLaptop.getVerificationMethods()).to.have.deep.members([
{ type: 'email', email }, { type: 'oidcIdToken', providerId: provider.id, providerDisplayName: provider.display_name }]);
Expand Down

0 comments on commit b392086

Please sign in to comment.