-
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.
Refactor to support permission handling
- Loading branch information
Showing
7 changed files
with
165 additions
and
11 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import type { Permissions } from 'webextension-polyfill'; | ||
import { permissions } from 'webextension-polyfill'; | ||
import { arrayDifference, CloudProviders } from './shared'; | ||
|
||
function domainToMatchPattern(domain: string): string { | ||
return `*://*.${domain}/*`; | ||
} | ||
|
||
const RequiredOriginPatterns = [ | ||
// Without this permission, the extension cannot login | ||
'gitkraken.dev' | ||
].map(domainToMatchPattern); | ||
const CloudProviderOriginPatterns = CloudProviders.map(domainToMatchPattern); | ||
|
||
export type OriginTypes = 'required' | 'cloud'; | ||
|
||
export interface PermissionsRequest { | ||
request: Permissions.Permissions; | ||
hasRequired: boolean; | ||
hasCloud: boolean; | ||
} | ||
|
||
export async function refreshPermissions(): Promise<PermissionsRequest | undefined> { | ||
const exitingPermissions = await permissions.getAll(); | ||
|
||
const newRequiredOrigins = arrayDifference(RequiredOriginPatterns, exitingPermissions.origins); | ||
const newCloudOrigins = arrayDifference(CloudProviderOriginPatterns, exitingPermissions.origins); | ||
const newOrigins = [...newRequiredOrigins, ...newCloudOrigins]; | ||
const unusedOrigins = arrayDifference(exitingPermissions.origins, [...RequiredOriginPatterns, ...CloudProviderOriginPatterns]); | ||
|
||
if (!unusedOrigins.length) { | ||
const unusedPermissions: Permissions.Permissions = { | ||
origins: unusedOrigins | ||
}; | ||
const result = await permissions.remove(unusedPermissions); | ||
if (!result) { | ||
console.warn('Failed to remove unnecessary permissions'); | ||
} | ||
} | ||
return newOrigins.length | ||
? { | ||
request: { | ||
origins: newOrigins, | ||
}, | ||
hasRequired: Boolean(newRequiredOrigins.length), | ||
hasCloud: Boolean(newCloudOrigins.length) | ||
} | ||
: undefined; | ||
} |
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