-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* WIP integrating breaking changes; * Integrated breaking changes * Add extend permission dialog; * Version bump;
- Loading branch information
1 parent
f111165
commit d47b88f
Showing
17 changed files
with
493 additions
and
128 deletions.
There are no files selected for viewing
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
107 changes: 107 additions & 0 deletions
107
packages/common/src/hooks/permissions/useMissingPermissions.ts
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,107 @@ | ||
import { | ||
stringifyToQueryParams, | ||
getUniqueDrivesWithHighestPermission, | ||
stringGuidsEqual, | ||
} from '@youfoundation/js-lib/helpers'; | ||
import { AppPermissionType } from '@youfoundation/js-lib/network'; | ||
import { getExtendAppRegistrationParams } from '@youfoundation/js-lib/auth'; | ||
import { useDotYouClientContext } from '../auth/useDotYouClientContext'; | ||
import { useSecurityContext } from './useSecurityContext'; | ||
|
||
const getExtendAppRegistrationUrl = ( | ||
host: string, | ||
appId: string, | ||
drives: { a: string; t: string; n: string; d: string; p: number }[], | ||
circleDrives: { a: string; t: string; n: string; d: string; p: number }[] | undefined, | ||
permissionKeys: number[], | ||
needsAllConnected: boolean, | ||
returnUrl: string | ||
) => { | ||
const params = getExtendAppRegistrationParams( | ||
appId, | ||
drives, | ||
circleDrives, | ||
permissionKeys, | ||
needsAllConnected, | ||
returnUrl | ||
); | ||
|
||
return `${host}/owner/appupdate?${stringifyToQueryParams(params)}`; | ||
}; | ||
|
||
export const useMissingPermissions = ({ | ||
appId, | ||
drives, | ||
circleDrives, | ||
permissions, | ||
needsAllConnected, | ||
}: { | ||
appId: string; | ||
drives: { | ||
a: string; | ||
t: string; | ||
n: string; | ||
d: string; | ||
p: number; | ||
}[]; | ||
circleDrives?: | ||
| { | ||
a: string; | ||
t: string; | ||
n: string; | ||
d: string; | ||
p: number; | ||
}[] | ||
| undefined; | ||
permissions: AppPermissionType[]; | ||
needsAllConnected?: boolean; | ||
}) => { | ||
const { data: context } = useSecurityContext().fetch; | ||
const host = useDotYouClientContext().getRoot(); | ||
|
||
if (!context || !host) return; | ||
|
||
const driveGrants = context?.permissionContext.permissionGroups.flatMap( | ||
(group) => group.driveGrants | ||
); | ||
const uniqueDriveGrants = driveGrants ? getUniqueDrivesWithHighestPermission(driveGrants) : []; | ||
|
||
const permissionKeys = context?.permissionContext.permissionGroups.flatMap( | ||
(group) => group.permissionSet.keys | ||
); | ||
|
||
const missingDrives = drives.filter((drive) => { | ||
const matchingGrants = uniqueDriveGrants.filter( | ||
(grant) => | ||
stringGuidsEqual(grant.permissionedDrive.drive.alias, drive.a) && | ||
stringGuidsEqual(grant.permissionedDrive.drive.type, drive.t) | ||
); | ||
|
||
const hasAccess = matchingGrants.some((grant) => { | ||
const allPermissions = grant.permissionedDrive.permission.reduce((a, b) => a + b, 0); | ||
return allPermissions >= drive.p; | ||
}); | ||
|
||
return !hasAccess; | ||
}); | ||
|
||
const missingPermissions = permissions?.filter((key) => permissionKeys?.indexOf(key) === -1); | ||
|
||
const hasAllConnectedCircle = context?.caller.isGrantedConnectedIdentitiesSystemCircle; | ||
const missingAllConnectedCircle = (needsAllConnected && !hasAllConnectedCircle) || false; | ||
|
||
if (missingDrives.length === 0 && missingPermissions.length === 0 && !missingAllConnectedCircle) | ||
return; | ||
|
||
const extendPermissionUrl = getExtendAppRegistrationUrl( | ||
host, | ||
appId, | ||
missingDrives, | ||
circleDrives, | ||
missingPermissions, | ||
missingAllConnectedCircle, | ||
'homebase-fchat://' | ||
); | ||
|
||
return extendPermissionUrl; | ||
}; |
32 changes: 32 additions & 0 deletions
32
packages/common/src/hooks/permissions/useSecurityContext.ts
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,32 @@ | ||
import { useQuery } from '@tanstack/react-query'; | ||
import { | ||
ApiType, | ||
getSecurityContext, | ||
getSecurityContextOverPeer, | ||
} from '@youfoundation/js-lib/core'; | ||
import { useDotYouClientContext } from '../auth/useDotYouClientContext'; | ||
|
||
export const useSecurityContext = (odinId?: string, isEnabled?: boolean) => { | ||
const dotYouClient = useDotYouClientContext(); | ||
|
||
const fetch = async (odinId?: string) => { | ||
if ( | ||
!odinId || | ||
odinId === window.location.hostname || | ||
(dotYouClient.getType() === ApiType.App && odinId === dotYouClient.getIdentity()) | ||
) { | ||
return await getSecurityContext(dotYouClient); | ||
} else return await getSecurityContextOverPeer(dotYouClient, odinId); | ||
}; | ||
|
||
return { | ||
fetch: useQuery({ | ||
queryKey: ['security-context', odinId], | ||
queryFn: () => fetch(odinId), | ||
refetchOnMount: false, | ||
refetchOnWindowFocus: false, | ||
staleTime: 24 * 60 * 60 * 1000, // 24 hours | ||
enabled: isEnabled === undefined ? true : isEnabled, | ||
}), | ||
}; | ||
}; |
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
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
Oops, something went wrong.