Skip to content

Commit

Permalink
Accept invite flow (#20)
Browse files Browse the repository at this point in the history
* add accepting nested routes

* email verification flow

* update connector

* Refactored credential helpers

* add enums for zkp, zkp api refactored structure

* rename auth store methods and auth api types

* update auth helpers
  • Loading branch information
lukachi authored Jan 22, 2024
1 parent 98bfbbe commit b1deb26
Show file tree
Hide file tree
Showing 31 changed files with 775 additions and 492 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"@mui/icons-material": "^5.14.19",
"@mui/material": "^5.14.20",
"@mui/x-date-pickers": "^6.18.7",
"@rarimo/rarime-connector": "^2.0.0-rc.3",
"@rarimo/rarime-connector": "^2.0.0-rc.5",
"@walletconnect/modal": "^2.6.2",
"i18next": "^22.4.3",
"lodash": "^4.17.21",
Expand Down
6 changes: 3 additions & 3 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ const App: FC<HTMLAttributes<HTMLDivElement>> = () => {

const { provider, isValidChain } = useWeb3Context()
const { checkSnapStatus } = useMetamaskZkpSnapContext()
const { authorize } = useAuth()
const { paletteMode } = useUiState()
const { connectProviders } = useAuth()

useViewportSizes()

Expand All @@ -26,14 +26,14 @@ const App: FC<HTMLAttributes<HTMLDivElement>> = () => {
const { isMetamaskInstalled, isSnapInstalled } = await checkSnapStatus()

if (isMetamaskInstalled && isSnapInstalled) {
await authorize()
await connectProviders()
}
} catch (error) {
ErrorHandler.processWithoutFeedback(error)
}

setIsAppInitialized(true)
}, [provider?.address, checkSnapStatus, authorize])
}, [provider?.address, checkSnapStatus, connectProviders])

const theme = useMemo(() => createTheme(paletteMode), [paletteMode])

Expand Down
44 changes: 44 additions & 0 deletions src/api/modules/auth/helpers/authorize.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import type { ZKProof } from '@rarimo/rarime-connector'

import { api, OrgUserRoles } from '@/api'
import { AuthTokensGroup } from '@/api/modules/auth'
import { ApiServicePaths } from '@/enums/api'

export const authorizeUser = async ({
role,
userDid,
orgDid,
groupId,
zkProof,
}: {
role: OrgUserRoles
userDid: string
orgDid: string
groupId: string
zkProof: ZKProof
}) => {
const { data } = await api.post<AuthTokensGroup>(`${ApiServicePaths.Auth}/v1/authorize`, {
body: {
data: {
id: userDid,
type: 'authorize',
attributes: {
proof: {
role: role,
group: groupId,
issuer: orgDid,
proof: zkProof,
},
},
},
},
})

return data
}

export const refreshJwt = async () => {
const { data } = await api.get<AuthTokensGroup>(`${ApiServicePaths.Auth}/v1/refresh`)

return data
}
1 change: 1 addition & 0 deletions src/api/modules/auth/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './authorize'
2 changes: 2 additions & 0 deletions src/api/modules/auth/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './helpers'
export * from './types'
12 changes: 12 additions & 0 deletions src/api/modules/auth/types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export type AuthTokensGroup = {
id: string
type: 'token'
accessToken: {
token: string
tokenType: 'access'
}
refreshToken: {
token: string
tokenType: 'access'
}
}
30 changes: 1 addition & 29 deletions src/api/modules/orgs/helpers/org-groups-requests.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import { fetcher } from '@distributedlab/fetcher'
import omit from 'lodash/omit'

import {
api,
CredentialRequest,
CredentialSubject,
OrgGroupCreatedRequest,
OrgGroupCreateRequest,
OrgGroupRequest,
Expand All @@ -15,7 +11,6 @@ import {
OrgGroupVCsMetadata,
OrgsStatuses,
OrgUserRoles,
VCSchema,
} from '@/api'
import { ApiServicePaths } from '@/enums/api'

Expand Down Expand Up @@ -409,6 +404,7 @@ const DUMMY_CREATED_REQUEST: OrgGroupCreatedRequest = {
req_id: '9d6a5063-684e-4ab4-b49a-82cdceadf63f',
created_at: '2021-08-12T14:00:00Z',
request: DUMMY_ORG_GROUP_REQUESTS[0],
claim_id: '',
}

const fakeLoadRequestsAll = async (query?: OrgGroupRequestQueryParams) => {
Expand Down Expand Up @@ -457,13 +453,11 @@ export const createInvitation = async ({ orgId, groupId, email, rules }: OrgGrou
export const acceptInvitation = async ({
orgId,
groupId,
reqId,
otp,
userDid,
}: {
orgId: string
groupId: string
reqId: string
otp: string
userDid: string
}) => {
Expand All @@ -472,7 +466,6 @@ export const acceptInvitation = async ({
{
body: {
data: {
id: reqId,
type: 'invitations-accept-email',
attributes: {
otp: otp,
Expand Down Expand Up @@ -615,27 +608,6 @@ export const getOrgGroupPublishingRequests = async ({
return data
}

export const loadAndParseCredentialSchema = async (
schemaUrl: string,
credentialSubject?: CredentialSubject,
): Promise<{
key: string
type: string
value: string
}> => {
const { data } = await fetcher.get<VCSchema>(schemaUrl)

const [key, { type }] = Object.entries(
omit(data?.properties.credentialSubject.properties, 'id'),
)[0]

return {
key,
type,
value: credentialSubject?.[key] ?? '',
}
}

export const loadOrgGroupReqMetadataById = async (
metadataId: string,
): Promise<OrgGroupVCsMetadata> => {
Expand Down
99 changes: 3 additions & 96 deletions src/api/modules/orgs/types/requests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ import {
OrgGroupRequestPublishingStatuses,
OrgGroupRequestStatuses,
} from '@/api'

export type CredentialSubject = Record<string, string> & {
metadata_id: string
}
import { CredentialSubject } from '@/api/modules/zkp'

export type CredentialRequest = {
credential_schema: string
Expand Down Expand Up @@ -57,7 +54,8 @@ export type OrgGroupCreatedRequest = {
group_id: string
email: string
created_at: string
request: OrgGroupRequest
claim_id: string
request?: OrgGroupRequest
}

export type OrgGroupRequestFiltersMap = {
Expand All @@ -70,97 +68,6 @@ export type OrgGroupRequestQueryParams = {
include?: OrgGroupRequestIncludes[]
}

// TODO: move to rarime-connector
export type VCSchema = {
$schema: string
type: string
$metadata: {
uris: {
jsonLdContext: string
jsonSchema: string
}
}
required: Array<string>
properties: {
'@context': {
type: Array<string>
}
id: {
type: string
}
type: {
type: Array<string>
items: {
type: string
}
}
issuer: {
type: Array<string>
format: string
required: Array<string>
properties: {
id: {
type: string
format: string
}
}
}
issuanceDate: {
type: string
format: string
}
expirationDate: {
type: string
format: string
}
credentialSchema: {
type: string
required: Array<string>
properties: {
id: {
type: string
format: string
}
type: {
type: string
}
}
}
subjectPosition: {
type: string
enum: Array<string>
}
merklizationRootPosition: {
type: string
enum: Array<string>
}
revNonce: {
type: string
}
version: {
type: string
}
updatable: {
type: string
}
credentialSubject: {
type: string
required: Array<string>
properties: {
id: {
title: string
type: string
format: string
}
} & {
[key: string]: {
type: string
}
}
}
}
}

export type OrgGroupRequestPublishing = {
id: string
type: 'claims'
Expand Down
19 changes: 19 additions & 0 deletions src/api/modules/zkp/enums/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export enum Operators {
NOOP = 0,
EQ = 1,
LT = 2,
GT = 3,
IN = 4,
NIN = 5,
NE = 6,
}

export enum QueryOperators {
$noop = Operators.NOOP,
$eq = Operators.EQ,
$lt = Operators.LT,
$gt = Operators.GT,
$in = Operators.IN,
$nin = Operators.NIN,
$ne = Operators.NE,
}
29 changes: 29 additions & 0 deletions src/api/modules/zkp/helpers/builders.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { CreateProofRequestParams } from '@rarimo/rarime-connector/dist/types'

import { OrgUserRoles } from '@/api'
import { QueryOperators } from '@/api/modules/zkp'

export const buildAuthorizeRequest = ({
providerAddress,
isAdmin,
}: {
providerAddress: string
isAdmin: boolean
}): CreateProofRequestParams => {
return {
circuitId: 'credentialAtomicQueryMTPV2',
accountAddress: providerAddress,
issuerDid: 'config.issuerDid', // TODO: implement

query: {
allowedIssuers: ['*'],
credentialSubject: {
role: {
// FIXME: how to other roles will work
[QueryOperators.$eq]: isAdmin ? OrgUserRoles.Admin : OrgUserRoles.Undefined,
},
},
type: ['VerifiableCredentials', 'Role'],
},
}
}
35 changes: 35 additions & 0 deletions src/api/modules/zkp/helpers/credentials.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { fetcher } from '@distributedlab/fetcher'
import type { SaveCredentialsRequestParams } from '@rarimo/rarime-connector'
import omit from 'lodash/omit'

import { api } from '@/api'
import { CredentialSubject, VCSchema } from '@/api/modules/zkp'

export const getClaimOffer = async (userDid: string, claimTypeUrn: string) => {
const { data } = await api.get<SaveCredentialsRequestParams>(
`/v1/credentials/${userDid}/${claimTypeUrn}`,
)

return data
}

export const loadAndParseCredentialSchema = async (
schemaUrl: string,
credentialSubject?: CredentialSubject,
): Promise<{
key: string
type: string
value: string
}> => {
const { data } = await fetcher.get<VCSchema>(schemaUrl)

const [key, { type }] = Object.entries(
omit(data?.properties.credentialSubject.properties, 'id'),
)[0]

return {
key,
type,
value: credentialSubject?.[key] ?? '',
}
}
2 changes: 2 additions & 0 deletions src/api/modules/zkp/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './builders'
export * from './credentials'
3 changes: 3 additions & 0 deletions src/api/modules/zkp/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './enums'
export * from './helpers'
export * from './types'
Loading

0 comments on commit b1deb26

Please sign in to comment.