-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from kilterset/extract-apis
Extract API implementations from Post Login API
- Loading branch information
Showing
27 changed files
with
1,333 additions
and
318 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
interface AccessTokenMock { | ||
build: <T>(api: T) => { | ||
addScope: (name: string) => T; | ||
removeScope: (name: string) => T; | ||
setCustomClaim: (name: string, value: unknown) => T; | ||
}; | ||
state: { | ||
scopes: string[]; | ||
claims: Record<string, unknown>; | ||
}; | ||
} | ||
|
||
interface CredentialsExchangeAccessTokenMock { | ||
build: <T>(api: T) => { | ||
setCustomClaim: (name: string, value: unknown) => T; | ||
}; | ||
state: { | ||
claims: Record<string, unknown>; | ||
}; | ||
} | ||
|
||
export function accessTokenMock(flow: "CredentialsExchange"): CredentialsExchangeAccessTokenMock; | ||
export function accessTokenMock(flow: string): AccessTokenMock; | ||
export function accessTokenMock(flow: string) { | ||
switch(flow) { | ||
case "CredentialsExchange": { | ||
const state = { | ||
claims: {} as Record<string, unknown>, | ||
}; | ||
|
||
const build = <T>(api: T) => ({ | ||
setCustomClaim: (name: string, value: unknown) => { | ||
state.claims[name] = value; | ||
return api; | ||
}, | ||
}) | ||
|
||
return { build, state }; | ||
} | ||
|
||
default: { | ||
const state = { | ||
scopes: [] as string[], | ||
claims: {} as Record<string, unknown>, | ||
}; | ||
|
||
const build = <T>(api: T) => ({ | ||
addScope: (name: string) => { | ||
state.scopes = [ | ||
...new Set(state.scopes).add(name), | ||
]; | ||
|
||
return api; | ||
}, | ||
|
||
removeScope: (name: string) => { | ||
state.scopes = state.scopes.filter( | ||
(value) => value !== name | ||
); | ||
|
||
return api; | ||
}, | ||
|
||
setCustomClaim: (name: string, value: unknown) => { | ||
state.claims[name] = value; | ||
return api; | ||
}, | ||
}) | ||
|
||
return { build, state }; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
interface AccessMock { | ||
build: <API>(api: API) => { | ||
deny: (reason: string) => API; | ||
}; | ||
state: { | ||
denied: false | { reason: string }; | ||
}; | ||
}; | ||
|
||
interface CredentialsExchangeAccessMock { | ||
build: <API>(api: API) => { | ||
deny: (code: string, reason: string) => API; | ||
}; | ||
state: { | ||
denied: false | { code: string; reason: string }; | ||
}; | ||
}; | ||
|
||
export function accessMock(flow: "CredentialsExchange"): CredentialsExchangeAccessMock; | ||
export function accessMock(flow: string): AccessMock; | ||
export function accessMock(flow: string) { | ||
switch(flow) { | ||
case "CredentialsExchange": { | ||
const state = { | ||
denied: false as false | { code: string; reason: string }, | ||
}; | ||
|
||
const build = <API>(api: API) => ({ | ||
deny: (code: string, reason: string) => { | ||
state.denied = { code, reason }; | ||
return api; | ||
}, | ||
}); | ||
|
||
return { build, state }; | ||
} | ||
|
||
default: { | ||
const state = { | ||
denied: false as false | { reason: string }, | ||
}; | ||
|
||
const build = <API>(api: API) => ({ | ||
deny: (reason: string) => { | ||
state.denied = { reason }; | ||
return api; | ||
}, | ||
}); | ||
|
||
return { build, state }; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { Factor } from "../../types"; | ||
|
||
export interface FactorList { | ||
allOptions: Factor[]; | ||
default: Factor | undefined; | ||
} | ||
|
||
export function authenticationMock(_flow: string, { userId }: { userId: string }) { | ||
let numCallsToSetPrimaryUser = 0; | ||
|
||
const state = { | ||
primaryUserId: userId, | ||
challenge: false as false | FactorList, | ||
enrollment: false as false | FactorList, | ||
newlyRecordedMethods: [] as string[], | ||
}; | ||
|
||
const build = <T>(api: T) => ({ | ||
challengeWith: (factor: Factor, options?: { additionalFactors?: Factor[] }) => { | ||
const additionalFactors = options?.additionalFactors ?? []; | ||
|
||
state.challenge = { | ||
allOptions: [factor, ...additionalFactors], | ||
default: factor, | ||
}; | ||
}, | ||
challengeWithAny(factors: Factor[]) { | ||
state.challenge = { | ||
allOptions: factors, | ||
default: undefined, | ||
}; | ||
}, | ||
enrollWith: (factor: Factor, options?: { additionalFactors?: Factor[] }) => { | ||
const additionalFactors = options?.additionalFactors ?? []; | ||
|
||
state.enrollment = { | ||
allOptions: [factor, ...additionalFactors], | ||
default: factor, | ||
}; | ||
}, | ||
enrollWithAny(factors: Factor[]) { | ||
state.enrollment = { | ||
allOptions: factors, | ||
default: undefined, | ||
}; | ||
}, | ||
setPrimaryUser: (primaryUserId: string) => { | ||
numCallsToSetPrimaryUser++; | ||
|
||
if (numCallsToSetPrimaryUser > 1) { | ||
throw new Error( | ||
"`authentication.setPrimaryUser` can only be set once per transaction" | ||
); | ||
} | ||
|
||
state.primaryUserId = primaryUserId; | ||
}, | ||
recordMethod: (providerUrl: string) => { | ||
state.newlyRecordedMethods.push(providerUrl); | ||
return api; | ||
}, | ||
}); | ||
|
||
return { state, build }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
export function idTokenMock(flow: string) { | ||
const state = { | ||
claims: {} as Record<string, unknown>, | ||
}; | ||
|
||
const build = <T>(api: T) => ({ | ||
setCustomClaim: (name: string, value: unknown) => { | ||
state.claims[name] = value; | ||
return api; | ||
}, | ||
}); | ||
|
||
return { state, build }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { MultifactorEnableOptions } from "../../types"; | ||
|
||
export function multifactorMock(flow: string) { | ||
const state = { | ||
enabled: false as false | { provider: string; options?: MultifactorEnableOptions }, | ||
}; | ||
|
||
const build = <T>(api: T) => ({ | ||
enable: (provider: string, options?: MultifactorEnableOptions) => { | ||
state.enabled = { provider, options }; | ||
return api; | ||
}, | ||
}); | ||
|
||
return { state, build }; | ||
} |
Oops, something went wrong.