Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
poolsar42 committed Jun 12, 2023
1 parent e06a787 commit ae20939
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 76 deletions.
58 changes: 24 additions & 34 deletions apps/passport/app/routes/authorize.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,6 @@ import { getRGBColor, getTextColor } from '@proofzero/design-system/src/helpers'
import { AddressURNSpace } from '@proofzero/urns/address'
import type { DropdownSelectListItem } from '@proofzero/design-system/src/atoms/dropdown/DropdownSelectList'

import { loader as scopesLoader } from './settings/applications/$clientId/scopes'
import type { GetAuthorizedAppScopesMethodResult } from '@proofzero/platform.access/src/jsonrpc/methods/getAuthorizedAppScopes'

export type UserProfile = {
displayName: string
pfp: {
Expand Down Expand Up @@ -263,32 +260,17 @@ export const loader: LoaderFunction = getRollupReqFunctionErrorWrapper(
} //else we present the authz screen below
}
const accountClient = getAccountClient(jwt, context.env, context.traceSpan)
const profile = await accountClient.getProfile.query({
account: accountUrn,
})

const preauthorizedScopes: GetAuthorizedAppScopesMethodResult =
await scopesLoader({
request,
params: { clientId },
context,
})

const personaData = await accessClient.getPersonaData.query({
accountUrn,
clientId,
})

const dataForScopes = await getDataForScopes(
scope,
accountUrn,
jwt,
context.env,
context.traceSpan,
Object.keys(preauthorizedScopes?.claimValues).length
? preauthorizedScopes.claimValues
: undefined
)
const [profile, personaData, dataForScopes] = await Promise.all([
accountClient.getProfile.query({
account: accountUrn,
}),
accessClient.getPersonaData.query({
accountUrn,
clientId,
}),
getDataForScopes(scope, accountUrn, jwt, context.env, context.traceSpan),
])

if (personaData) {
dataForScopes.personaData = personaData
Expand Down Expand Up @@ -445,29 +427,37 @@ export default function Authorize() {
DropdownSelectListItem | undefined
>(() => {
let selected
if (connectedEmails && connectedEmails.length)
selected = connectedEmails.find((email) => email.selected)
if (connectedEmails && connectedEmails.length && persona?.email) {
selected = connectedEmails.find((email) => email.value === persona.email)
} else {
// sorted in edges in by date descending order
selected = connectedEmails?.[connectedEmails.length - 1]
}
return selected
})
const [selectedConnectedAccounts, setSelectedConnectedAccounts] = useState<
Array<DropdownSelectListItem> | Array<AuthorizationControlSelection>
>(() => {
if (personaData?.connected_accounts === AuthorizationControlSelection.ALL) {
if (persona.connected_accounts === AuthorizationControlSelection.ALL) {
return [AuthorizationControlSelection.ALL]
} else {
return connectedAccounts?.length
? connectedAccounts.filter((acc) => acc.selected)
? connectedAccounts.filter((acc) =>
persona.connected_accounts?.includes(acc.value)
)
: []
}
})
const [selectedSCWallets, setSelectedSCWallets] = useState<
Array<DropdownSelectListItem> | Array<AuthorizationControlSelection>
>(() => {
if (personaData?.erc_4337 === AuthorizationControlSelection.ALL) {
if (persona.erc_4337 === AuthorizationControlSelection.ALL) {
return [AuthorizationControlSelection.ALL]
} else {
return connectedSmartContractWallets?.length
? connectedSmartContractWallets.filter((acc) => acc.selected)
? connectedSmartContractWallets.filter((acc) =>
persona.erc_4337?.includes(acc.value)
)
: []
}
})
Expand Down
35 changes: 4 additions & 31 deletions apps/passport/app/utils/authorize.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import type { PersonaData } from '@proofzero/types/application'
import { redirect } from '@remix-run/cloudflare'
import { CryptoAddressType, NodeType } from '@proofzero/types/address'
import type { DropdownSelectListItem } from '@proofzero/design-system/src/atoms/dropdown/DropdownSelectList'
import type { GetAuthorizedAppScopesMethodResult } from '@proofzero/platform.access/src/jsonrpc/methods/getAuthorizedAppScopes'

export type DataForScopes = {
connectedEmails: DropdownSelectListItem[]
Expand Down Expand Up @@ -55,8 +54,7 @@ export const getDataForScopes = async (
accountURN: AccountURN,
jwt?: string,
env?: any,
traceSpan?: any,
preauthorizedScopes?: GetAuthorizedAppScopesMethodResult['claimValues']
traceSpan?: any
): Promise<DataForScopes> => {
if (!accountURN)
throw new UnauthorizedError({ message: 'Account URN is required' })
Expand All @@ -73,14 +71,7 @@ export const getDataForScopes = async (

if (connectedAccounts && connectedAccounts.length) {
if (requestedScope.includes(Symbol.keyFor(SCOPE_EMAIL)!)) {
connectedEmails = getEmailDropdownItems(
connectedAccounts,
true,
preauthorizedScopes &&
Object.keys(preauthorizedScopes).includes(Symbol.keyFor(SCOPE_EMAIL)!)
? preauthorizedScopes[Symbol.keyFor(SCOPE_EMAIL)!].meta.urns[0]
: undefined
)
connectedEmails = getEmailDropdownItems(connectedAccounts)
}
if (requestedScope.includes(Symbol.keyFor(SCOPE_CONNECTED_ACCOUNTS)!)) {
const addresses = await Promise.all(
Expand All @@ -98,16 +89,7 @@ export const getDataForScopes = async (
return addressClient.getAddressProfile.query()
})
)
connectedAddresses = getAddressDropdownItems(
addresses,
preauthorizedScopes &&
Object.keys(preauthorizedScopes).includes(
Symbol.keyFor(SCOPE_CONNECTED_ACCOUNTS)!
)
? preauthorizedScopes[Symbol.keyFor(SCOPE_CONNECTED_ACCOUNTS)!].meta
.urns
: undefined
)
connectedAddresses = getAddressDropdownItems(addresses)
}
if (requestedScope.includes(Symbol.keyFor(SCOPE_SMART_CONTRACT_WALLETS)!)) {
const addresses = await Promise.all(
Expand All @@ -120,16 +102,7 @@ export const getDataForScopes = async (
return addressClient.getAddressProfile.query()
})
)
connectedSmartContractWallets = getAddressDropdownItems(
addresses,
preauthorizedScopes &&
Object.keys(preauthorizedScopes).includes(
Symbol.keyFor(SCOPE_SMART_CONTRACT_WALLETS)!
)
? preauthorizedScopes[Symbol.keyFor(SCOPE_SMART_CONTRACT_WALLETS)!]
.meta.urns
: undefined
)
connectedSmartContractWallets = getAddressDropdownItems(addresses)
}
}

Expand Down
13 changes: 2 additions & 11 deletions packages/utils/getNormalisedConnectedAccounts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,7 @@ export const adjustAddressTypeToDisplay = (
}

export const getEmailDropdownItems = (
connectedAddresses?: Addresses | null,
preselected = false,
preauthorizedEmail?: string
connectedAddresses?: Addresses | null
): Array<DropdownSelectListItem> => {
if (!connectedAddresses) return []

Expand Down Expand Up @@ -89,19 +87,13 @@ export const getEmailDropdownItems = (
| CryptoAddressType,
title: address.qc.alias,
value: address.baseUrn as AddressURN,
selected: preauthorizedEmail
? address.baseUrn === preauthorizedEmail
: preselected
? i === filteredEmailsFromConnectedAddresses.length - 1
: undefined,
}
})
}

//addressDropdownItems
export const getAddressDropdownItems = (
addressProfiles?: Array<GetAddressProfileResult> | null,
preselectedItems: Array<string> = []
addressProfiles?: Array<GetAddressProfileResult> | null
): Array<DropdownSelectListItem> => {
if (!addressProfiles) return []
return addressProfiles.map((address) => {
Expand All @@ -111,7 +103,6 @@ export const getAddressDropdownItems = (
subtitle: `${adjustAddressTypeToDisplay(address.type)} - ${
address.address
}`,
selected: preselectedItems.includes(address.id),
}
})
}

0 comments on commit ae20939

Please sign in to comment.