Skip to content

Commit

Permalink
feat: allow using simulator card
Browse files Browse the repository at this point in the history
Signed-off-by: Timo Glastra <[email protected]>
  • Loading branch information
TimoGlastra committed Sep 13, 2024
1 parent ae09b5f commit 0c1a8f6
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 4 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@ export default function App() {
setFlow(
new AusweisAuthFlow({
debug: true,
// Can set to true to allow simulator cards. In this case `onEnterPin` and `onAttachCard` won't be called
allowSimulatorCard: false,
onEnterPin: ({ attemptsRemaining }) => {
// Mock incorrect pin entry
return attemptsRemaining === 1 ? '123456' : '123123'
Expand Down
2 changes: 2 additions & 0 deletions ausweis-example/app/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ export default function App() {
setFlow(
new AusweisAuthFlow({
debug: true,
// Can set to true to allow simulator cards
allowSimulatorCard: false,
onEnterPin: ({ attemptsRemaining }) => {
// Mock incorrect pin entry
return attemptsRemaining === 1 ? '123456' : '123123'
Expand Down
2 changes: 1 addition & 1 deletion ios/AusweisSdk.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ Pod::Spec.new do |s|

s.source_files = "**/*.{h,m,swift}"

s.dependency 'AusweisApp2'
s.dependency 'AusweisApp2', '2.2.1'
end
35 changes: 33 additions & 2 deletions src/AusweisAuthFlow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export interface AusweisAuthFlowOptions {
/**
* callback that will be called when a card is attached/detached from the NFC scanner.
*/
onCardAttachedChanged?: (options: { isCardAttached: boolean }) => void
onCardAttachedChanged?: (options: { isCardAttached: boolean; isSimulator: boolean }) => void

/**
* callback that will be called with status updates on the auth flow progress.
Expand Down Expand Up @@ -110,6 +110,11 @@ export interface AusweisAuthFlowOptions {
*/
debug?: boolean

/**
* Will enable the use of a simulator card if available. Note that this can only be used in test environments
*/
allowSimulatorCard?: boolean

iosNfcModalMessages?: AusweisSdkRunAuthCommand['messages']
}

Expand All @@ -125,6 +130,7 @@ export class AusweisAuthFlow {
private messageListener?: Subscription
private sentCommands: Array<AusweisSdkCommand> = []
private isSdkInitialized = false
private isSimulatorCardAttached = false

public constructor(private options: AusweisAuthFlowOptions) {}

Expand Down Expand Up @@ -239,10 +245,21 @@ export class AusweisAuthFlow {

if (message.msg === 'READER') {
// If card is empty object the card is unknown, we see that as no card attached for this flow
const isCardAttached = message.card !== null && message.card !== undefined && Object.keys(message.card).length > 0
const isSimulator = message.name === 'Simulator'
const isCardAttached =
message.attached &&
(isSimulator || (message.card !== null && message.card !== undefined && Object.keys(message.card).length > 0))

if (isSimulator) this.isSimulatorCardAttached = isCardAttached
console.log({
isCardAttached,
isSimulator,
isSimulatorCardAttached: this.isSimulatorCardAttached,
})

this.options.onCardAttachedChanged?.({
isCardAttached,
isSimulator,
})
}

Expand All @@ -253,6 +270,13 @@ export class AusweisAuthFlow {
}

if (message.msg === 'INSERT_CARD') {
if (this.options.allowSimulatorCard && this.isSimulatorCardAttached) {
this.sendCommand({
cmd: 'SET_CARD',
name: 'Simulator',
})
return
}
this.options.onAttachCard?.()
}

Expand Down Expand Up @@ -316,6 +340,13 @@ export class AusweisAuthFlow {
const retryCounter = message.reader.card?.retryCounter ?? 3

try {
if (this.options.allowSimulatorCard && this.isSimulatorCardAttached) {
this.sendCommand({
cmd: 'SET_PIN',
})
return
}

// The attempts remaining is weird. The retryCounter when 1 will require the CAN. If it's 0 it requires
// the PUK. We don't support setting CAN / PUK in this flow so we substract 1 from the retry counter.
// There is the case however if the user unlocked the card using CAN in e.g. the Ausweis App (not SDK)
Expand Down
4 changes: 3 additions & 1 deletion src/AusweisSdkCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,12 @@ export interface AusweisSdkInterruptCommand extends BaseAusweisSdkCommandPayload

/**
* SET_PIN command payload to set the PIN of inserted card.
*
* `value` can be omitted in case of simulator card.
*/
export interface AusweisSdkSetPinCommand extends BaseAusweisSdkCommandPayload {
cmd: 'SET_PIN'
value: string
value?: string
}

/**
Expand Down

0 comments on commit 0c1a8f6

Please sign in to comment.