Refactor scopes to use Intl for labels and descriptions (#1570)
* Refactor scope labels for applications and pats * move scopes to composables * Refactor pages to use intl * Fix merge error * Extract messages
This commit is contained in:
parent
1cbe99a0d8
commit
0adb7685f6
675
composables/auth/scopes.ts
Normal file
675
composables/auth/scopes.ts
Normal file
@ -0,0 +1,675 @@
|
||||
export const scopeMessages = defineMessages({
|
||||
userReadEmailLabel: {
|
||||
id: 'scopes.userReadEmail.label',
|
||||
defaultMessage: 'Read user email',
|
||||
},
|
||||
userReadEmailDescription: {
|
||||
id: 'scopes.userReadEmail.description',
|
||||
defaultMessage: 'Read your email',
|
||||
},
|
||||
userReadLabel: {
|
||||
id: 'scopes.userRead.label',
|
||||
defaultMessage: 'Read user data',
|
||||
},
|
||||
userReadDescription: {
|
||||
id: 'scopes.userRead.description',
|
||||
defaultMessage: 'Access your public profile information',
|
||||
},
|
||||
userWriteLabel: {
|
||||
id: 'scopes.userWrite.label',
|
||||
defaultMessage: 'Write user data',
|
||||
},
|
||||
userWriteDescription: {
|
||||
id: 'scopes.userWrite.description',
|
||||
defaultMessage: 'Write to your profile',
|
||||
},
|
||||
userDeleteLabel: {
|
||||
id: 'scopes.userDelete.label',
|
||||
defaultMessage: 'Delete your account',
|
||||
},
|
||||
userDeleteDescription: {
|
||||
id: 'scopes.userDelete.description',
|
||||
defaultMessage: 'Delete your account',
|
||||
},
|
||||
userAuthWriteLabel: {
|
||||
id: 'scopes.userAuthWrite.label',
|
||||
defaultMessage: 'Write auth data',
|
||||
},
|
||||
userAuthWriteDescription: {
|
||||
id: 'scopes.userAuthWrite.description',
|
||||
defaultMessage: 'Modify your authentication data',
|
||||
},
|
||||
notificationReadLabel: {
|
||||
id: 'scopes.notificationRead.label',
|
||||
defaultMessage: 'Read notifications',
|
||||
},
|
||||
notificationReadDescription: {
|
||||
id: 'scopes.notificationRead.description',
|
||||
defaultMessage: 'Read your notifications',
|
||||
},
|
||||
notificationWriteLabel: {
|
||||
id: 'scopes.notificationWrite.label',
|
||||
defaultMessage: 'Write notifications',
|
||||
},
|
||||
notificationWriteDescription: {
|
||||
id: 'scopes.notificationWrite.description',
|
||||
defaultMessage: 'Delete/View your notifications',
|
||||
},
|
||||
payoutsReadLabel: {
|
||||
id: 'scopes.payoutsRead.label',
|
||||
defaultMessage: 'Read payouts',
|
||||
},
|
||||
payoutsReadDescription: {
|
||||
id: 'scopes.payoutsRead.description',
|
||||
defaultMessage: 'Read your payouts data',
|
||||
},
|
||||
payoutsWriteLabel: {
|
||||
id: 'scopes.payoutsWrite.label',
|
||||
defaultMessage: 'Write payouts',
|
||||
},
|
||||
payoutsWriteDescription: {
|
||||
id: 'scopes.payoutsWrite.description',
|
||||
defaultMessage: 'Withdraw money',
|
||||
},
|
||||
analyticsLabel: {
|
||||
id: 'scopes.analytics.label',
|
||||
defaultMessage: 'Read analytics',
|
||||
},
|
||||
analyticsDescription: {
|
||||
id: 'scopes.analytics.description',
|
||||
defaultMessage: 'Access your analytics data',
|
||||
},
|
||||
projectCreateLabel: {
|
||||
id: 'scopes.projectCreate.label',
|
||||
defaultMessage: 'Create projects',
|
||||
},
|
||||
projectCreateDescription: {
|
||||
id: 'scopes.projectCreate.description',
|
||||
defaultMessage: 'Create new projects',
|
||||
},
|
||||
projectReadLabel: {
|
||||
id: 'scopes.projectRead.label',
|
||||
defaultMessage: 'Read projects',
|
||||
},
|
||||
projectReadDescription: {
|
||||
id: 'scopes.projectRead.description',
|
||||
defaultMessage: 'Read all your projects',
|
||||
},
|
||||
projectWriteLabel: {
|
||||
id: 'scopes.projectWrite.label',
|
||||
defaultMessage: 'Write projects',
|
||||
},
|
||||
projectWriteDescription: {
|
||||
id: 'scopes.projectWrite.description',
|
||||
defaultMessage: 'Write to project data',
|
||||
},
|
||||
projectDeleteLabel: {
|
||||
id: 'scopes.projectDelete.label',
|
||||
defaultMessage: 'Delete projects',
|
||||
},
|
||||
projectDeleteDescription: {
|
||||
id: 'scopes.projectDelete.description',
|
||||
defaultMessage: 'Delete your projects',
|
||||
},
|
||||
versionCreateLabel: {
|
||||
id: 'scopes.versionCreate.label',
|
||||
defaultMessage: 'Create versions',
|
||||
},
|
||||
versionCreateDescription: {
|
||||
id: 'scopes.versionCreate.description',
|
||||
defaultMessage: 'Create new versions',
|
||||
},
|
||||
versionReadLabel: {
|
||||
id: 'scopes.versionRead.label',
|
||||
defaultMessage: 'Read versions',
|
||||
},
|
||||
versionReadDescription: {
|
||||
id: 'scopes.versionRead.description',
|
||||
defaultMessage: 'Read all versions',
|
||||
},
|
||||
versionWriteLabel: {
|
||||
id: 'scopes.versionWrite.label',
|
||||
defaultMessage: 'Write versions',
|
||||
},
|
||||
versionWriteDescription: {
|
||||
id: 'scopes.versionWrite.description',
|
||||
defaultMessage: 'Write to version data',
|
||||
},
|
||||
versionDeleteLabel: {
|
||||
id: 'scopes.versionDelete.label',
|
||||
defaultMessage: 'Delete versions',
|
||||
},
|
||||
versionDeleteDescription: {
|
||||
id: 'scopes.versionDelete.description',
|
||||
defaultMessage: 'Delete a version',
|
||||
},
|
||||
reportCreateLabel: {
|
||||
id: 'scopes.reportCreate.label',
|
||||
defaultMessage: 'Create reports',
|
||||
},
|
||||
reportCreateDescription: {
|
||||
id: 'scopes.reportCreate.description',
|
||||
defaultMessage: 'Create reports',
|
||||
},
|
||||
reportReadLabel: {
|
||||
id: 'scopes.reportRead.label',
|
||||
defaultMessage: 'Read reports',
|
||||
},
|
||||
reportReadDescription: {
|
||||
id: 'scopes.reportRead.description',
|
||||
defaultMessage: 'Read reports',
|
||||
},
|
||||
reportWriteLabel: {
|
||||
id: 'scopes.reportWrite.label',
|
||||
defaultMessage: 'Write reports',
|
||||
},
|
||||
reportWriteDescription: {
|
||||
id: 'scopes.reportWrite.description',
|
||||
defaultMessage: 'Edit reports',
|
||||
},
|
||||
reportDeleteLabel: {
|
||||
id: 'scopes.reportDelete.label',
|
||||
defaultMessage: 'Delete reports',
|
||||
},
|
||||
reportDeleteDescription: {
|
||||
id: 'scopes.reportDelete.description',
|
||||
defaultMessage: 'Delete reports',
|
||||
},
|
||||
threadReadLabel: {
|
||||
id: 'scopes.threadRead.label',
|
||||
defaultMessage: 'Read threads',
|
||||
},
|
||||
threadReadDescription: {
|
||||
id: 'scopes.threadRead.description',
|
||||
defaultMessage: 'Read threads',
|
||||
},
|
||||
threadWriteLabel: {
|
||||
id: 'scopes.threadWrite.label',
|
||||
defaultMessage: 'Write threads',
|
||||
},
|
||||
threadWriteDescription: {
|
||||
id: 'scopes.threadWrite.description',
|
||||
defaultMessage: 'Write to threads',
|
||||
},
|
||||
patCreateLabel: {
|
||||
id: 'scopes.patCreate.label',
|
||||
defaultMessage: 'Create PATs',
|
||||
},
|
||||
patCreateDescription: {
|
||||
id: 'scopes.patCreate.description',
|
||||
defaultMessage: 'Create personal API tokens',
|
||||
},
|
||||
patReadLabel: {
|
||||
id: 'scopes.patRead.label',
|
||||
defaultMessage: 'Read PATs',
|
||||
},
|
||||
patReadDescription: {
|
||||
id: 'scopes.patRead.description',
|
||||
defaultMessage: 'View created API tokens',
|
||||
},
|
||||
patWriteLabel: {
|
||||
id: 'scopes.patWrite.label',
|
||||
defaultMessage: 'Write PATs',
|
||||
},
|
||||
patWriteDescription: {
|
||||
id: 'scopes.patWrite.description',
|
||||
defaultMessage: 'Edit personal API tokens',
|
||||
},
|
||||
patDeleteLabel: {
|
||||
id: 'scopes.patDelete.label',
|
||||
defaultMessage: 'Delete PATs',
|
||||
},
|
||||
patDeleteDescription: {
|
||||
id: 'scopes.patDelete.description',
|
||||
defaultMessage: 'Delete your personal API tokens',
|
||||
},
|
||||
sessionReadLabel: {
|
||||
id: 'scopes.sessionRead.label',
|
||||
defaultMessage: 'Read sessions',
|
||||
},
|
||||
sessionReadDescription: {
|
||||
id: 'scopes.sessionRead.description',
|
||||
defaultMessage: 'Read active sessions',
|
||||
},
|
||||
sessionDeleteLabel: {
|
||||
id: 'scopes.sessionDelete.label',
|
||||
defaultMessage: 'Delete sessions',
|
||||
},
|
||||
sessionDeleteDescription: {
|
||||
id: 'scopes.sessionDelete.description',
|
||||
defaultMessage: 'Delete sessions',
|
||||
},
|
||||
performAnalyticsLabel: {
|
||||
id: 'scopes.performAnalytics.label',
|
||||
defaultMessage: 'Perform analytics',
|
||||
},
|
||||
performAnalyticsDescription: {
|
||||
id: 'scopes.performAnalytics.description',
|
||||
defaultMessage: 'Perform analytics actions',
|
||||
},
|
||||
collectionCreateLabel: {
|
||||
id: 'scopes.collectionCreate.label',
|
||||
defaultMessage: 'Create collections',
|
||||
},
|
||||
collectionCreateDescription: {
|
||||
id: 'scopes.collectionCreate.description',
|
||||
defaultMessage: 'Create collections',
|
||||
},
|
||||
collectionReadLabel: {
|
||||
id: 'scopes.collectionRead.label',
|
||||
defaultMessage: 'Read collections',
|
||||
},
|
||||
collectionReadDescription: {
|
||||
id: 'scopes.collectionRead.description',
|
||||
defaultMessage: 'Read collections',
|
||||
},
|
||||
collectionWriteLabel: {
|
||||
id: 'scopes.collectionWrite.label',
|
||||
defaultMessage: 'Write collections',
|
||||
},
|
||||
collectionWriteDescription: {
|
||||
id: 'scopes.collectionWrite.description',
|
||||
defaultMessage: 'Write to collections',
|
||||
},
|
||||
collectionDeleteLabel: {
|
||||
id: 'scopes.collectionDelete.label',
|
||||
defaultMessage: 'Delete collections',
|
||||
},
|
||||
collectionDeleteDescription: {
|
||||
id: 'scopes.collectionDelete.description',
|
||||
defaultMessage: 'Delete collections',
|
||||
},
|
||||
organizationCreateLabel: {
|
||||
id: 'scopes.organizationCreate.label',
|
||||
defaultMessage: 'Create organizations',
|
||||
},
|
||||
organizationCreateDescription: {
|
||||
id: 'scopes.organizationCreate.description',
|
||||
defaultMessage: 'Create organizations',
|
||||
},
|
||||
organizationReadLabel: {
|
||||
id: 'scopes.organizationRead.label',
|
||||
defaultMessage: 'Read organizations',
|
||||
},
|
||||
organizationReadDescription: {
|
||||
id: 'scopes.organizationRead.description',
|
||||
defaultMessage: 'Read organizations',
|
||||
},
|
||||
organizationWriteLabel: {
|
||||
id: 'scopes.organizationWrite.label',
|
||||
defaultMessage: 'Write organizations',
|
||||
},
|
||||
organizationWriteDescription: {
|
||||
id: 'scopes.organizationWrite.description',
|
||||
defaultMessage: 'Write to organizations',
|
||||
},
|
||||
organizationDeleteLabel: {
|
||||
id: 'scopes.organizationDelete.label',
|
||||
defaultMessage: 'Delete organizations',
|
||||
},
|
||||
organizationDeleteDescription: {
|
||||
id: 'scopes.organizationDelete.description',
|
||||
defaultMessage: 'Delete organizations',
|
||||
},
|
||||
sessionAccessLabel: {
|
||||
id: 'scopes.sessionAccess.label',
|
||||
defaultMessage: 'Access sessions',
|
||||
},
|
||||
sessionAccessDescription: {
|
||||
id: 'scopes.sessionAccess.description',
|
||||
defaultMessage: 'Access modrinth-issued sessions',
|
||||
},
|
||||
})
|
||||
|
||||
const scopeDefinitions = [
|
||||
{
|
||||
id: 'USER_READ_EMAIL',
|
||||
value: BigInt(1) << BigInt(0),
|
||||
label: scopeMessages.userReadEmailLabel,
|
||||
desc: scopeMessages.userReadEmailDescription,
|
||||
},
|
||||
{
|
||||
id: 'USER_READ',
|
||||
value: BigInt(1) << BigInt(1),
|
||||
label: scopeMessages.userReadLabel,
|
||||
desc: scopeMessages.userReadDescription,
|
||||
},
|
||||
{
|
||||
id: 'USER_WRITE',
|
||||
value: BigInt(1) << BigInt(2),
|
||||
label: scopeMessages.userWriteLabel,
|
||||
desc: scopeMessages.userWriteDescription,
|
||||
},
|
||||
{
|
||||
id: 'USER_DELETE',
|
||||
value: BigInt(1) << BigInt(3),
|
||||
label: scopeMessages.userDeleteLabel,
|
||||
desc: scopeMessages.userDeleteDescription,
|
||||
},
|
||||
{
|
||||
id: 'USER_AUTH_WRITE',
|
||||
value: BigInt(1) << BigInt(4),
|
||||
label: scopeMessages.userAuthWriteLabel,
|
||||
desc: scopeMessages.userAuthWriteDescription,
|
||||
},
|
||||
{
|
||||
id: 'NOTIFICATION_READ',
|
||||
value: BigInt(1) << BigInt(5),
|
||||
label: scopeMessages.notificationReadLabel,
|
||||
desc: scopeMessages.notificationReadDescription,
|
||||
},
|
||||
{
|
||||
id: 'NOTIFICATION_WRITE',
|
||||
value: BigInt(1) << BigInt(6),
|
||||
label: scopeMessages.notificationWriteLabel,
|
||||
desc: scopeMessages.notificationWriteDescription,
|
||||
},
|
||||
{
|
||||
id: 'PAYOUTS_READ',
|
||||
value: BigInt(1) << BigInt(7),
|
||||
label: scopeMessages.payoutsReadLabel,
|
||||
desc: scopeMessages.payoutsReadDescription,
|
||||
},
|
||||
{
|
||||
id: 'PAYOUTS_WRITE',
|
||||
value: BigInt(1) << BigInt(8),
|
||||
label: scopeMessages.payoutsWriteLabel,
|
||||
desc: scopeMessages.payoutsWriteDescription,
|
||||
},
|
||||
{
|
||||
id: 'ANALYTICS',
|
||||
value: BigInt(1) << BigInt(9),
|
||||
label: scopeMessages.analyticsLabel,
|
||||
desc: scopeMessages.analyticsDescription,
|
||||
},
|
||||
{
|
||||
id: 'PROJECT_CREATE',
|
||||
value: BigInt(1) << BigInt(10),
|
||||
label: scopeMessages.projectCreateLabel,
|
||||
desc: scopeMessages.projectCreateDescription,
|
||||
},
|
||||
{
|
||||
id: 'PROJECT_READ',
|
||||
value: BigInt(1) << BigInt(11),
|
||||
label: scopeMessages.projectReadLabel,
|
||||
desc: scopeMessages.projectReadDescription,
|
||||
},
|
||||
{
|
||||
id: 'PROJECT_WRITE',
|
||||
value: BigInt(1) << BigInt(12),
|
||||
label: scopeMessages.projectWriteLabel,
|
||||
desc: scopeMessages.projectWriteDescription,
|
||||
},
|
||||
{
|
||||
id: 'PROJECT_DELETE',
|
||||
value: BigInt(1) << BigInt(13),
|
||||
label: scopeMessages.projectDeleteLabel,
|
||||
desc: scopeMessages.projectDeleteDescription,
|
||||
},
|
||||
{
|
||||
id: 'VERSION_CREATE',
|
||||
value: BigInt(1) << BigInt(14),
|
||||
label: scopeMessages.versionCreateLabel,
|
||||
desc: scopeMessages.versionCreateDescription,
|
||||
},
|
||||
{
|
||||
id: 'VERSION_READ',
|
||||
value: BigInt(1) << BigInt(15),
|
||||
label: scopeMessages.versionReadLabel,
|
||||
desc: scopeMessages.versionReadDescription,
|
||||
},
|
||||
{
|
||||
id: 'VERSION_WRITE',
|
||||
value: BigInt(1) << BigInt(16),
|
||||
label: scopeMessages.versionWriteLabel,
|
||||
desc: scopeMessages.versionWriteDescription,
|
||||
},
|
||||
{
|
||||
id: 'VERSION_DELETE',
|
||||
value: BigInt(1) << BigInt(17),
|
||||
label: scopeMessages.versionDeleteLabel,
|
||||
desc: scopeMessages.versionDeleteDescription,
|
||||
},
|
||||
{
|
||||
id: 'REPORT_CREATE',
|
||||
value: BigInt(1) << BigInt(18),
|
||||
label: scopeMessages.reportCreateLabel,
|
||||
desc: scopeMessages.reportCreateDescription,
|
||||
},
|
||||
{
|
||||
id: 'REPORT_READ',
|
||||
value: BigInt(1) << BigInt(19),
|
||||
label: scopeMessages.reportReadLabel,
|
||||
desc: scopeMessages.reportReadDescription,
|
||||
},
|
||||
{
|
||||
id: 'REPORT_WRITE',
|
||||
value: BigInt(1) << BigInt(20),
|
||||
label: scopeMessages.reportWriteLabel,
|
||||
desc: scopeMessages.reportWriteDescription,
|
||||
},
|
||||
{
|
||||
id: 'REPORT_DELETE',
|
||||
value: BigInt(1) << BigInt(21),
|
||||
label: scopeMessages.reportDeleteLabel,
|
||||
desc: scopeMessages.reportDeleteDescription,
|
||||
},
|
||||
{
|
||||
id: 'THREAD_READ',
|
||||
value: BigInt(1) << BigInt(22),
|
||||
label: scopeMessages.threadReadLabel,
|
||||
desc: scopeMessages.threadReadDescription,
|
||||
},
|
||||
{
|
||||
id: 'THREAD_WRITE',
|
||||
value: BigInt(1) << BigInt(23),
|
||||
label: scopeMessages.threadWriteLabel,
|
||||
desc: scopeMessages.threadWriteDescription,
|
||||
},
|
||||
{
|
||||
id: 'PAT_CREATE',
|
||||
value: BigInt(1) << BigInt(24),
|
||||
label: scopeMessages.patCreateLabel,
|
||||
desc: scopeMessages.patCreateDescription,
|
||||
},
|
||||
{
|
||||
id: 'PAT_READ',
|
||||
value: BigInt(1) << BigInt(25),
|
||||
label: scopeMessages.patReadLabel,
|
||||
desc: scopeMessages.patReadDescription,
|
||||
},
|
||||
{
|
||||
id: 'PAT_WRITE',
|
||||
value: BigInt(1) << BigInt(26),
|
||||
label: scopeMessages.patWriteLabel,
|
||||
desc: scopeMessages.patWriteDescription,
|
||||
},
|
||||
{
|
||||
id: 'PAT_DELETE',
|
||||
value: BigInt(1) << BigInt(27),
|
||||
label: scopeMessages.patDeleteLabel,
|
||||
desc: scopeMessages.patDeleteDescription,
|
||||
},
|
||||
{
|
||||
id: 'SESSION_READ',
|
||||
value: BigInt(1) << BigInt(28),
|
||||
label: scopeMessages.sessionReadLabel,
|
||||
desc: scopeMessages.sessionReadDescription,
|
||||
},
|
||||
{
|
||||
id: 'SESSION_DELETE',
|
||||
value: BigInt(1) << BigInt(29),
|
||||
label: scopeMessages.sessionDeleteLabel,
|
||||
desc: scopeMessages.sessionDeleteDescription,
|
||||
},
|
||||
{
|
||||
id: 'PERFORM_ANALYTICS',
|
||||
value: BigInt(1) << BigInt(30),
|
||||
label: scopeMessages.performAnalyticsLabel,
|
||||
desc: scopeMessages.performAnalyticsDescription,
|
||||
},
|
||||
{
|
||||
id: 'COLLECTION_CREATE',
|
||||
value: BigInt(1) << BigInt(31),
|
||||
label: scopeMessages.collectionCreateLabel,
|
||||
desc: scopeMessages.collectionCreateDescription,
|
||||
},
|
||||
{
|
||||
id: 'COLLECTION_READ',
|
||||
value: BigInt(1) << BigInt(32),
|
||||
label: scopeMessages.collectionReadLabel,
|
||||
desc: scopeMessages.collectionReadDescription,
|
||||
},
|
||||
{
|
||||
id: 'COLLECTION_WRITE',
|
||||
value: BigInt(1) << BigInt(33),
|
||||
label: scopeMessages.collectionWriteLabel,
|
||||
desc: scopeMessages.collectionWriteDescription,
|
||||
},
|
||||
{
|
||||
id: 'COLLECTION_DELETE',
|
||||
value: BigInt(1) << BigInt(34),
|
||||
label: scopeMessages.collectionDeleteLabel,
|
||||
desc: scopeMessages.collectionDeleteDescription,
|
||||
},
|
||||
{
|
||||
id: 'ORGANIZATION_CREATE',
|
||||
value: BigInt(1) << BigInt(35),
|
||||
label: scopeMessages.organizationCreateLabel,
|
||||
desc: scopeMessages.organizationCreateDescription,
|
||||
},
|
||||
{
|
||||
id: 'ORGANIZATION_READ',
|
||||
value: BigInt(1) << BigInt(36),
|
||||
label: scopeMessages.organizationReadLabel,
|
||||
desc: scopeMessages.organizationReadDescription,
|
||||
},
|
||||
{
|
||||
id: 'ORGANIZATION_WRITE',
|
||||
value: BigInt(1) << BigInt(37),
|
||||
label: scopeMessages.organizationWriteLabel,
|
||||
desc: scopeMessages.organizationWriteDescription,
|
||||
},
|
||||
{
|
||||
id: 'ORGANIZATION_DELETE',
|
||||
value: BigInt(1) << BigInt(38),
|
||||
label: scopeMessages.organizationDeleteLabel,
|
||||
desc: scopeMessages.organizationDeleteDescription,
|
||||
},
|
||||
{
|
||||
id: 'SESSION_ACCESS',
|
||||
value: BigInt(1) << BigInt(39),
|
||||
label: scopeMessages.sessionAccessLabel,
|
||||
desc: scopeMessages.sessionAccessDescription,
|
||||
},
|
||||
]
|
||||
|
||||
const Scopes = scopeDefinitions.reduce((acc, scope) => {
|
||||
acc[scope.id] = scope.value
|
||||
return acc
|
||||
}, {} as Record<string, bigint>)
|
||||
|
||||
export const restrictedScopes = [
|
||||
Scopes.PAT_READ,
|
||||
Scopes.PAT_CREATE,
|
||||
Scopes.PAT_WRITE,
|
||||
Scopes.PAT_DELETE,
|
||||
Scopes.SESSION_READ,
|
||||
Scopes.SESSION_DELETE,
|
||||
Scopes.SESSION_ACCESS,
|
||||
Scopes.USER_AUTH_WRITE,
|
||||
Scopes.USER_DELETE,
|
||||
Scopes.PERFORM_ANALYTICS,
|
||||
]
|
||||
|
||||
export const scopeList = Object.entries(Scopes)
|
||||
.filter(([_, value]) => !restrictedScopes.includes(value))
|
||||
.map(([key, _]) => key)
|
||||
|
||||
export const getScopeValue = (scope: string) => {
|
||||
return Scopes[scope]
|
||||
}
|
||||
|
||||
export const encodeScopes = (scopes: string[]) => {
|
||||
let scopeFlag = BigInt(0)
|
||||
|
||||
// We iterate over the provided scopes
|
||||
for (const scope of scopes) {
|
||||
// We iterate over the entries of the Scopes object
|
||||
for (const [scopeName, scopeFlagValue] of Object.entries(Scopes)) {
|
||||
// If the scope name is the same as the provided scope, add the scope flag to the scopeFlag variable
|
||||
if (scopeName === scope) {
|
||||
scopeFlag = scopeFlag | scopeFlagValue
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return scopeFlag
|
||||
}
|
||||
|
||||
export const decodeScopes = (scopes: bigint | number) => {
|
||||
if (typeof scopes === 'number') {
|
||||
scopes = BigInt(scopes)
|
||||
}
|
||||
|
||||
const authorizedScopes = []
|
||||
|
||||
// We iterate over the entries of the Scopes object
|
||||
for (const [scopeName, scopeFlag] of Object.entries(Scopes)) {
|
||||
// If the scope flag is present in the provided number, add the scope name to the list
|
||||
if ((scopes & scopeFlag) === scopeFlag) {
|
||||
authorizedScopes.push(scopeName)
|
||||
}
|
||||
}
|
||||
|
||||
return authorizedScopes
|
||||
}
|
||||
|
||||
export const hasScope = (scopes: bigint, scope: string) => {
|
||||
const authorizedScopes = decodeScopes(scopes)
|
||||
return authorizedScopes.includes(scope)
|
||||
}
|
||||
|
||||
export const toggleScope = (scopes: bigint, scope: string) => {
|
||||
const authorizedScopes = decodeScopes(scopes)
|
||||
if (authorizedScopes.includes(scope)) {
|
||||
return encodeScopes(authorizedScopes.filter((authorizedScope) => authorizedScope !== scope))
|
||||
} else {
|
||||
return encodeScopes([...authorizedScopes, scope])
|
||||
}
|
||||
}
|
||||
|
||||
export const useScopes = () => {
|
||||
const { formatMessage } = useVIntl()
|
||||
|
||||
const scopesToDefinitions = (scopes: bigint) => {
|
||||
const authorizedScopes = decodeScopes(scopes)
|
||||
return authorizedScopes.map((scope) => {
|
||||
const scopeDefinition = scopeDefinitions.find(
|
||||
(scopeDefinition) => scopeDefinition.id === scope
|
||||
)
|
||||
if (!scopeDefinition) {
|
||||
throw new Error(`Scope ${scope} not found`)
|
||||
}
|
||||
return formatMessage(scopeDefinition.desc)
|
||||
})
|
||||
}
|
||||
|
||||
const scopesToLabels = (scopes: bigint) => {
|
||||
const authorizedScopes = decodeScopes(scopes)
|
||||
return authorizedScopes.map((scope) => {
|
||||
const scopeDefinition = scopeDefinitions.find(
|
||||
(scopeDefinition) => scopeDefinition.id === scope
|
||||
)
|
||||
if (!scopeDefinition) {
|
||||
throw new Error(`Scope ${scope} not found`)
|
||||
}
|
||||
return formatMessage(scopeDefinition.label)
|
||||
})
|
||||
}
|
||||
|
||||
return {
|
||||
scopesToDefinitions,
|
||||
scopesToLabels,
|
||||
}
|
||||
}
|
||||
@ -68,6 +68,9 @@
|
||||
"profile.label.no-projects-auth": {
|
||||
"message": "You don't have any projects.\nWould you like to <create-link>create one</create-link>?"
|
||||
},
|
||||
"profile.label.organizations": {
|
||||
"message": "Organizations"
|
||||
},
|
||||
"profile.meta.description": {
|
||||
"message": "Download {username}'s projects on Modrinth"
|
||||
},
|
||||
@ -134,6 +137,258 @@
|
||||
"project-type.shader.singular": {
|
||||
"message": "Shader"
|
||||
},
|
||||
"revenue.transfers.total": {
|
||||
"message": "You have withdrawn {amount} in total."
|
||||
},
|
||||
"revenue.transfers.total.method": {
|
||||
"message": "You have withdrawn {amount} through {method}."
|
||||
},
|
||||
"revenue.transfers.total.year": {
|
||||
"message": "You have withdrawn {amount} in {year}."
|
||||
},
|
||||
"revenue.transfers.total.year_method": {
|
||||
"message": "You have withdrawn {amount} in {year} through {method}."
|
||||
},
|
||||
"scopes.analytics.description": {
|
||||
"message": "Access your analytics data"
|
||||
},
|
||||
"scopes.analytics.label": {
|
||||
"message": "Read analytics"
|
||||
},
|
||||
"scopes.collectionCreate.description": {
|
||||
"message": "Create collections"
|
||||
},
|
||||
"scopes.collectionCreate.label": {
|
||||
"message": "Create collections"
|
||||
},
|
||||
"scopes.collectionDelete.description": {
|
||||
"message": "Delete collections"
|
||||
},
|
||||
"scopes.collectionDelete.label": {
|
||||
"message": "Delete collections"
|
||||
},
|
||||
"scopes.collectionRead.description": {
|
||||
"message": "Read collections"
|
||||
},
|
||||
"scopes.collectionRead.label": {
|
||||
"message": "Read collections"
|
||||
},
|
||||
"scopes.collectionWrite.description": {
|
||||
"message": "Write to collections"
|
||||
},
|
||||
"scopes.collectionWrite.label": {
|
||||
"message": "Write collections"
|
||||
},
|
||||
"scopes.notificationRead.description": {
|
||||
"message": "Read your notifications"
|
||||
},
|
||||
"scopes.notificationRead.label": {
|
||||
"message": "Read notifications"
|
||||
},
|
||||
"scopes.notificationWrite.description": {
|
||||
"message": "Delete/View your notifications"
|
||||
},
|
||||
"scopes.notificationWrite.label": {
|
||||
"message": "Write notifications"
|
||||
},
|
||||
"scopes.organizationCreate.description": {
|
||||
"message": "Create organizations"
|
||||
},
|
||||
"scopes.organizationCreate.label": {
|
||||
"message": "Create organizations"
|
||||
},
|
||||
"scopes.organizationDelete.description": {
|
||||
"message": "Delete organizations"
|
||||
},
|
||||
"scopes.organizationDelete.label": {
|
||||
"message": "Delete organizations"
|
||||
},
|
||||
"scopes.organizationRead.description": {
|
||||
"message": "Read organizations"
|
||||
},
|
||||
"scopes.organizationRead.label": {
|
||||
"message": "Read organizations"
|
||||
},
|
||||
"scopes.organizationWrite.description": {
|
||||
"message": "Write to organizations"
|
||||
},
|
||||
"scopes.organizationWrite.label": {
|
||||
"message": "Write organizations"
|
||||
},
|
||||
"scopes.patCreate.description": {
|
||||
"message": "Create personal API tokens"
|
||||
},
|
||||
"scopes.patCreate.label": {
|
||||
"message": "Create PATs"
|
||||
},
|
||||
"scopes.patDelete.description": {
|
||||
"message": "Delete your personal API tokens"
|
||||
},
|
||||
"scopes.patDelete.label": {
|
||||
"message": "Delete PATs"
|
||||
},
|
||||
"scopes.patRead.description": {
|
||||
"message": "View created API tokens"
|
||||
},
|
||||
"scopes.patRead.label": {
|
||||
"message": "Read PATs"
|
||||
},
|
||||
"scopes.patWrite.description": {
|
||||
"message": "Edit personal API tokens"
|
||||
},
|
||||
"scopes.patWrite.label": {
|
||||
"message": "Write PATs"
|
||||
},
|
||||
"scopes.payoutsRead.description": {
|
||||
"message": "Read your payouts data"
|
||||
},
|
||||
"scopes.payoutsRead.label": {
|
||||
"message": "Read payouts"
|
||||
},
|
||||
"scopes.payoutsWrite.description": {
|
||||
"message": "Withdraw money"
|
||||
},
|
||||
"scopes.payoutsWrite.label": {
|
||||
"message": "Write payouts"
|
||||
},
|
||||
"scopes.performAnalytics.description": {
|
||||
"message": "Perform analytics actions"
|
||||
},
|
||||
"scopes.performAnalytics.label": {
|
||||
"message": "Perform analytics"
|
||||
},
|
||||
"scopes.projectCreate.description": {
|
||||
"message": "Create new projects"
|
||||
},
|
||||
"scopes.projectCreate.label": {
|
||||
"message": "Create projects"
|
||||
},
|
||||
"scopes.projectDelete.description": {
|
||||
"message": "Delete your projects"
|
||||
},
|
||||
"scopes.projectDelete.label": {
|
||||
"message": "Delete projects"
|
||||
},
|
||||
"scopes.projectRead.description": {
|
||||
"message": "Read all your projects"
|
||||
},
|
||||
"scopes.projectRead.label": {
|
||||
"message": "Read projects"
|
||||
},
|
||||
"scopes.projectWrite.description": {
|
||||
"message": "Write to project data"
|
||||
},
|
||||
"scopes.projectWrite.label": {
|
||||
"message": "Write projects"
|
||||
},
|
||||
"scopes.reportCreate.description": {
|
||||
"message": "Create reports"
|
||||
},
|
||||
"scopes.reportCreate.label": {
|
||||
"message": "Create reports"
|
||||
},
|
||||
"scopes.reportDelete.description": {
|
||||
"message": "Delete reports"
|
||||
},
|
||||
"scopes.reportDelete.label": {
|
||||
"message": "Delete reports"
|
||||
},
|
||||
"scopes.reportRead.description": {
|
||||
"message": "Read reports"
|
||||
},
|
||||
"scopes.reportRead.label": {
|
||||
"message": "Read reports"
|
||||
},
|
||||
"scopes.reportWrite.description": {
|
||||
"message": "Edit reports"
|
||||
},
|
||||
"scopes.reportWrite.label": {
|
||||
"message": "Write reports"
|
||||
},
|
||||
"scopes.sessionAccess.description": {
|
||||
"message": "Access modrinth-issued sessions"
|
||||
},
|
||||
"scopes.sessionAccess.label": {
|
||||
"message": "Access sessions"
|
||||
},
|
||||
"scopes.sessionDelete.description": {
|
||||
"message": "Delete sessions"
|
||||
},
|
||||
"scopes.sessionDelete.label": {
|
||||
"message": "Delete sessions"
|
||||
},
|
||||
"scopes.sessionRead.description": {
|
||||
"message": "Read active sessions"
|
||||
},
|
||||
"scopes.sessionRead.label": {
|
||||
"message": "Read sessions"
|
||||
},
|
||||
"scopes.threadRead.description": {
|
||||
"message": "Read threads"
|
||||
},
|
||||
"scopes.threadRead.label": {
|
||||
"message": "Read threads"
|
||||
},
|
||||
"scopes.threadWrite.description": {
|
||||
"message": "Write to threads"
|
||||
},
|
||||
"scopes.threadWrite.label": {
|
||||
"message": "Write threads"
|
||||
},
|
||||
"scopes.userAuthWrite.description": {
|
||||
"message": "Modify your authentication data"
|
||||
},
|
||||
"scopes.userAuthWrite.label": {
|
||||
"message": "Write auth data"
|
||||
},
|
||||
"scopes.userDelete.description": {
|
||||
"message": "Delete your account"
|
||||
},
|
||||
"scopes.userDelete.label": {
|
||||
"message": "Delete your account"
|
||||
},
|
||||
"scopes.userRead.description": {
|
||||
"message": "Access your public profile information"
|
||||
},
|
||||
"scopes.userRead.label": {
|
||||
"message": "Read user data"
|
||||
},
|
||||
"scopes.userReadEmail.description": {
|
||||
"message": "Read your email"
|
||||
},
|
||||
"scopes.userReadEmail.label": {
|
||||
"message": "Read user email"
|
||||
},
|
||||
"scopes.userWrite.description": {
|
||||
"message": "Write to your profile"
|
||||
},
|
||||
"scopes.userWrite.label": {
|
||||
"message": "Write user data"
|
||||
},
|
||||
"scopes.versionCreate.description": {
|
||||
"message": "Create new versions"
|
||||
},
|
||||
"scopes.versionCreate.label": {
|
||||
"message": "Create versions"
|
||||
},
|
||||
"scopes.versionDelete.description": {
|
||||
"message": "Delete a version"
|
||||
},
|
||||
"scopes.versionDelete.label": {
|
||||
"message": "Delete versions"
|
||||
},
|
||||
"scopes.versionRead.description": {
|
||||
"message": "Read all versions"
|
||||
},
|
||||
"scopes.versionRead.label": {
|
||||
"message": "Read versions"
|
||||
},
|
||||
"scopes.versionWrite.description": {
|
||||
"message": "Write to version data"
|
||||
},
|
||||
"scopes.versionWrite.label": {
|
||||
"message": "Write versions"
|
||||
},
|
||||
"settings.language.categories.auto": {
|
||||
"message": "Automatic"
|
||||
},
|
||||
|
||||
@ -65,12 +65,14 @@
|
||||
import { Button, XIcon, CheckIcon, Avatar } from 'omorphia'
|
||||
import { useBaseFetch } from '@/composables/fetch.js'
|
||||
import { useAuth } from '@/composables/auth.js'
|
||||
import { getScopeDefinitions } from '@/utils/auth/scopes.ts'
|
||||
|
||||
import { useScopes } from '@/composables/auth/scopes.ts'
|
||||
|
||||
const data = useNuxtApp()
|
||||
|
||||
const router = useRoute()
|
||||
const auth = await useAuth()
|
||||
const { scopesToDefinitions } = useScopes()
|
||||
|
||||
const clientId = router.query?.client_id || false
|
||||
const redirectUri = router.query?.redirect_uri || false
|
||||
@ -115,7 +117,7 @@ const { data: app } = await useAsyncData('oauth/app/' + clientId, () =>
|
||||
})
|
||||
)
|
||||
|
||||
const scopeDefinitions = getScopeDefinitions(BigInt(authorizationData.value?.requested_scopes || 0))
|
||||
const scopeDefinitions = scopesToDefinitions(BigInt(authorizationData.value?.requested_scopes || 0))
|
||||
|
||||
const { data: createdBy } = await useAsyncData('user/' + app.value.created_by, () =>
|
||||
useBaseFetch('user/' + app.value.created_by, {
|
||||
|
||||
@ -61,7 +61,7 @@
|
||||
<Checkbox
|
||||
v-for="scope in scopeList"
|
||||
:key="scope"
|
||||
:label="getScopeLabel(scope)"
|
||||
:label="scopesToLabels(getScopeValue(scope)).join(', ')"
|
||||
:model-value="hasScope(scopesVal, scope)"
|
||||
@update:model-value="() => (scopesVal = toggleScope(scopesVal, scope))"
|
||||
/>
|
||||
@ -230,7 +230,14 @@ import {
|
||||
ConfirmModal,
|
||||
} from 'omorphia'
|
||||
import Modal from '~/components/ui/Modal.vue'
|
||||
import { scopeList, hasScope, toggleScope, getScopeLabel } from '~/utils/auth/scopes.ts'
|
||||
|
||||
import {
|
||||
scopeList,
|
||||
hasScope,
|
||||
toggleScope,
|
||||
useScopes,
|
||||
getScopeValue,
|
||||
} from '~/composables/auth/scopes.ts'
|
||||
|
||||
definePageMeta({
|
||||
middleware: 'auth',
|
||||
@ -241,6 +248,7 @@ useHead({
|
||||
})
|
||||
|
||||
const data = useNuxtApp()
|
||||
const { scopesToLabels } = useScopes()
|
||||
|
||||
const appModal = ref()
|
||||
|
||||
|
||||
@ -56,14 +56,14 @@
|
||||
</label>
|
||||
<div class="scope-list">
|
||||
<div
|
||||
v-for="scope in getScopeDefinitions(authorization.scopes)"
|
||||
v-for="scope in scopesToDefinitions(authorization.scopes)"
|
||||
:key="scope"
|
||||
class="scope-list-item"
|
||||
>
|
||||
<div class="scope-list-item-icon">
|
||||
<CheckIcon />
|
||||
</div>
|
||||
{{ constCaseToTitleCase(scope) }}
|
||||
{{ scope }}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@ -89,7 +89,10 @@
|
||||
</template>
|
||||
<script setup>
|
||||
import { Button, TrashIcon, CheckIcon, ConfirmModal, Avatar } from 'omorphia'
|
||||
import { getScopeDefinitions } from '~/utils/auth/scopes.ts'
|
||||
|
||||
import { useScopes } from '~/composables/auth/scopes.ts'
|
||||
|
||||
const { scopesToDefinitions } = useScopes()
|
||||
|
||||
const revokingId = ref(null)
|
||||
|
||||
@ -166,12 +169,6 @@ async function revokeApp(id) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
const constCaseToTitleCase = (str) =>
|
||||
str
|
||||
.split('_')
|
||||
.map((x) => x[0].toUpperCase() + x.slice(1).toLowerCase())
|
||||
.join(' ')
|
||||
</script>
|
||||
|
||||
<style lang="scss" scoped>
|
||||
|
||||
@ -25,7 +25,7 @@
|
||||
<Checkbox
|
||||
v-for="scope in scopeList"
|
||||
:key="scope"
|
||||
:label="getScopeLabel(scope)"
|
||||
:label="scopesToLabels(getScopeValue(scope)).join(', ')"
|
||||
:model-value="hasScope(scopesVal, scope)"
|
||||
@update:model-value="scopesVal = toggleScope(scopesVal, scope)"
|
||||
/>
|
||||
@ -151,7 +151,13 @@
|
||||
<script setup>
|
||||
import { PlusIcon, XIcon, Checkbox, TrashIcon, EditIcon, SaveIcon, ConfirmModal } from 'omorphia'
|
||||
|
||||
import { hasScope, scopeList, toggleScope, getScopeLabel } from '~/utils/auth/scopes.ts'
|
||||
import {
|
||||
hasScope,
|
||||
scopeList,
|
||||
toggleScope,
|
||||
useScopes,
|
||||
getScopeValue,
|
||||
} from '~/composables/auth/scopes.ts'
|
||||
|
||||
import CopyCode from '~/components/ui/CopyCode.vue'
|
||||
import Modal from '~/components/ui/Modal.vue'
|
||||
@ -165,6 +171,7 @@ useHead({
|
||||
})
|
||||
|
||||
const data = useNuxtApp()
|
||||
const { scopesToLabels } = useScopes()
|
||||
const patModal = ref()
|
||||
|
||||
const editPatIndex = ref(null)
|
||||
|
||||
@ -1,346 +0,0 @@
|
||||
const scopeDefinitions = [
|
||||
{
|
||||
name: 'USER_READ_EMAIL',
|
||||
description: 'Read your email',
|
||||
label: 'Read user email',
|
||||
value: BigInt(1) << BigInt(0),
|
||||
},
|
||||
{
|
||||
name: 'USER_READ',
|
||||
description: 'Access your public profile information',
|
||||
label: 'Read user data',
|
||||
value: BigInt(1) << BigInt(1),
|
||||
},
|
||||
{
|
||||
name: 'USER_WRITE',
|
||||
description: 'Write to your profile',
|
||||
label: 'Write user data',
|
||||
value: BigInt(1) << BigInt(2),
|
||||
},
|
||||
{
|
||||
name: 'USER_DELETE',
|
||||
description: 'Delete your account',
|
||||
label: 'Delete your account',
|
||||
value: BigInt(1) << BigInt(3),
|
||||
},
|
||||
{
|
||||
name: 'USER_AUTH_WRITE',
|
||||
description: 'Modify your authentication data',
|
||||
label: 'Write auth data',
|
||||
value: BigInt(1) << BigInt(4),
|
||||
},
|
||||
{
|
||||
name: 'NOTIFICATION_READ',
|
||||
description: 'Read your notifications',
|
||||
label: 'Read notifications',
|
||||
value: BigInt(1) << BigInt(5),
|
||||
},
|
||||
{
|
||||
name: 'NOTIFICATION_WRITE',
|
||||
description: 'Delete/View your notifications',
|
||||
label: 'Write notifications',
|
||||
value: BigInt(1) << BigInt(6),
|
||||
},
|
||||
{
|
||||
name: 'PAYOUTS_READ',
|
||||
description: 'Read your payouts data',
|
||||
label: 'Read payouts',
|
||||
value: BigInt(1) << BigInt(7),
|
||||
},
|
||||
{
|
||||
name: 'PAYOUTS_WRITE',
|
||||
description: 'Withdraw money',
|
||||
label: 'Write payouts',
|
||||
value: BigInt(1) << BigInt(8),
|
||||
},
|
||||
{
|
||||
name: 'ANALYTICS',
|
||||
description: 'Access your analytics data',
|
||||
label: 'Read analytics',
|
||||
value: BigInt(1) << BigInt(9),
|
||||
},
|
||||
{
|
||||
name: 'PROJECT_CREATE',
|
||||
description: 'Create new projects',
|
||||
label: 'Create projects',
|
||||
value: BigInt(1) << BigInt(10),
|
||||
},
|
||||
{
|
||||
name: 'PROJECT_READ',
|
||||
description: 'Read all your projects',
|
||||
label: 'Read projects',
|
||||
value: BigInt(1) << BigInt(11),
|
||||
},
|
||||
{
|
||||
name: 'PROJECT_WRITE',
|
||||
description: 'Write to project data',
|
||||
label: 'Write projects',
|
||||
value: BigInt(1) << BigInt(12),
|
||||
},
|
||||
{
|
||||
name: 'PROJECT_DELETE',
|
||||
description: 'Delete your projects',
|
||||
label: 'Delete projects',
|
||||
value: BigInt(1) << BigInt(13),
|
||||
},
|
||||
{
|
||||
name: 'VERSION_CREATE',
|
||||
description: 'Create new versions',
|
||||
label: 'Create versions',
|
||||
value: BigInt(1) << BigInt(14),
|
||||
},
|
||||
{
|
||||
name: 'VERSION_READ',
|
||||
description: 'Read all versions',
|
||||
label: 'Read versions',
|
||||
value: BigInt(1) << BigInt(15),
|
||||
},
|
||||
{
|
||||
name: 'VERSION_WRITE',
|
||||
description: 'Write to version data',
|
||||
label: 'Write versions',
|
||||
value: BigInt(1) << BigInt(16),
|
||||
},
|
||||
{
|
||||
name: 'VERSION_DELETE',
|
||||
description: 'Delete a version',
|
||||
label: 'Delete versions',
|
||||
value: BigInt(1) << BigInt(17),
|
||||
},
|
||||
{
|
||||
name: 'REPORT_CREATE',
|
||||
description: 'Create reports',
|
||||
label: 'Create reports',
|
||||
value: BigInt(1) << BigInt(18),
|
||||
},
|
||||
{
|
||||
name: 'REPORT_READ',
|
||||
description: 'Read reports',
|
||||
label: 'Read reports',
|
||||
value: BigInt(1) << BigInt(19),
|
||||
},
|
||||
{
|
||||
name: 'REPORT_WRITE',
|
||||
description: 'Edit reports',
|
||||
label: 'Write reports',
|
||||
value: BigInt(1) << BigInt(20),
|
||||
},
|
||||
{
|
||||
name: 'REPORT_DELETE',
|
||||
description: 'Delete reports',
|
||||
label: 'Delete reports',
|
||||
value: BigInt(1) << BigInt(21),
|
||||
},
|
||||
{
|
||||
name: 'THREAD_READ',
|
||||
description: 'Read threads',
|
||||
label: 'Read threads',
|
||||
value: BigInt(1) << BigInt(22),
|
||||
},
|
||||
{
|
||||
name: 'THREAD_WRITE',
|
||||
description: 'Write to threads',
|
||||
label: 'Write threads',
|
||||
value: BigInt(1) << BigInt(23),
|
||||
},
|
||||
{
|
||||
name: 'PAT_CREATE',
|
||||
description: 'Create personal API tokens',
|
||||
label: 'Create PATs',
|
||||
value: BigInt(1) << BigInt(24),
|
||||
},
|
||||
{
|
||||
name: 'PAT_READ',
|
||||
description: 'View created API tokens',
|
||||
label: 'Read PATs',
|
||||
value: BigInt(1) << BigInt(25),
|
||||
},
|
||||
{
|
||||
name: 'PAT_WRITE',
|
||||
description: 'Edit personal API tokens',
|
||||
label: 'Write PATs',
|
||||
value: BigInt(1) << BigInt(26),
|
||||
},
|
||||
{
|
||||
name: 'PAT_DELETE',
|
||||
description: 'Delete your personal API tokens',
|
||||
label: 'Delete PATs',
|
||||
value: BigInt(1) << BigInt(27),
|
||||
},
|
||||
{
|
||||
name: 'SESSION_READ',
|
||||
description: 'Read active sessions',
|
||||
label: 'Read sessions',
|
||||
value: BigInt(1) << BigInt(28),
|
||||
},
|
||||
{
|
||||
name: 'SESSION_DELETE',
|
||||
description: 'Delete sessions',
|
||||
label: 'Delete sessions',
|
||||
value: BigInt(1) << BigInt(29),
|
||||
},
|
||||
{
|
||||
name: 'PERFORM_ANALYTICS',
|
||||
description: 'Perform analytics actions',
|
||||
label: 'Perform analytics',
|
||||
value: BigInt(1) << BigInt(30),
|
||||
},
|
||||
{
|
||||
name: 'COLLECTION_CREATE',
|
||||
description: 'Create collections',
|
||||
label: 'Create collections',
|
||||
value: BigInt(1) << BigInt(31),
|
||||
},
|
||||
{
|
||||
name: 'COLLECTION_READ',
|
||||
description: 'Read collections',
|
||||
label: 'Read collections',
|
||||
value: BigInt(1) << BigInt(32),
|
||||
},
|
||||
{
|
||||
name: 'COLLECTION_WRITE',
|
||||
description: 'Write to collections',
|
||||
label: 'Write collections',
|
||||
value: BigInt(1) << BigInt(33),
|
||||
},
|
||||
{
|
||||
name: 'COLLECTION_DELETE',
|
||||
description: 'Delete collections',
|
||||
label: 'Delete collections',
|
||||
value: BigInt(1) << BigInt(34),
|
||||
},
|
||||
{
|
||||
name: 'ORGANIZATION_CREATE',
|
||||
description: 'Create organizations',
|
||||
label: 'Create organizations',
|
||||
value: BigInt(1) << BigInt(35),
|
||||
},
|
||||
{
|
||||
name: 'ORGANIZATION_READ',
|
||||
description: 'Read organizations',
|
||||
label: 'Read organizations',
|
||||
value: BigInt(1) << BigInt(36),
|
||||
},
|
||||
{
|
||||
name: 'ORGANIZATION_WRITE',
|
||||
description: 'Write to organizations',
|
||||
label: 'Write organizations',
|
||||
value: BigInt(1) << BigInt(37),
|
||||
},
|
||||
{
|
||||
name: 'ORGANIZATION_DELETE',
|
||||
description: 'Delete organizations',
|
||||
label: 'Delete organizations',
|
||||
value: BigInt(1) << BigInt(38),
|
||||
},
|
||||
{
|
||||
name: 'SESSION_ACCESS',
|
||||
description: 'Access modrinth-issued sessions',
|
||||
label: 'Access sessions',
|
||||
value: BigInt(1) << BigInt(39),
|
||||
},
|
||||
]
|
||||
|
||||
const Scopes = scopeDefinitions.reduce((acc, scope) => {
|
||||
acc[scope.name] = scope.value
|
||||
return acc
|
||||
}, {} as Record<string, bigint>)
|
||||
|
||||
const ScopeLabels = scopeDefinitions.reduce((acc, scope) => {
|
||||
acc[scope.name] = scope.label
|
||||
return acc
|
||||
}, {} as Record<string, string>)
|
||||
|
||||
const ScopeDescriptions = scopeDefinitions.reduce((acc, scope) => {
|
||||
acc[scope.name] = scope.description
|
||||
return acc
|
||||
}, {} as Record<string, string>)
|
||||
|
||||
export const getScopeLabel = (scope: string | bigint) => {
|
||||
if (typeof scope === 'bigint') {
|
||||
for (const [scopeName, scopeFlag] of Object.entries(Scopes)) {
|
||||
if (scopeFlag === scope) {
|
||||
scope = scopeName
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if (typeof scope === 'bigint') {
|
||||
return 'Unknown scope'
|
||||
}
|
||||
}
|
||||
|
||||
return ScopeLabels?.[scope] ?? 'Unknown scope'
|
||||
}
|
||||
|
||||
export const restrictedScopes = [
|
||||
Scopes.PAT_READ,
|
||||
Scopes.PAT_CREATE,
|
||||
Scopes.PAT_WRITE,
|
||||
Scopes.PAT_DELETE,
|
||||
Scopes.SESSION_READ,
|
||||
Scopes.SESSION_DELETE,
|
||||
Scopes.SESSION_ACCESS,
|
||||
Scopes.USER_AUTH_WRITE,
|
||||
Scopes.USER_DELETE,
|
||||
Scopes.PERFORM_ANALYTICS,
|
||||
]
|
||||
|
||||
export const scopeList = Object.entries(Scopes)
|
||||
.filter(([_, value]) => !restrictedScopes.includes(value))
|
||||
.map(([key, _]) => key)
|
||||
|
||||
export const encodeScopes = (scopes: string[]) => {
|
||||
let scopeFlag = BigInt(0)
|
||||
|
||||
// We iterate over the provided scopes
|
||||
for (const scope of scopes) {
|
||||
// We iterate over the entries of the Scopes object
|
||||
for (const [scopeName, scopeFlagValue] of Object.entries(Scopes)) {
|
||||
// If the scope name is the same as the provided scope, add the scope flag to the scopeFlag variable
|
||||
if (scopeName === scope) {
|
||||
scopeFlag = scopeFlag | scopeFlagValue
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return scopeFlag
|
||||
}
|
||||
|
||||
export const decodeScopes = (scopes: bigint | number) => {
|
||||
if (typeof scopes === 'number') {
|
||||
scopes = BigInt(scopes)
|
||||
}
|
||||
|
||||
const authorizedScopes = []
|
||||
|
||||
// We iterate over the entries of the Scopes object
|
||||
for (const [scopeName, scopeFlag] of Object.entries(Scopes)) {
|
||||
// If the scope flag is present in the provided number, add the scope name to the list
|
||||
if ((scopes & scopeFlag) === scopeFlag) {
|
||||
authorizedScopes.push(scopeName)
|
||||
}
|
||||
}
|
||||
|
||||
return authorizedScopes
|
||||
}
|
||||
|
||||
export const hasScope = (scopes: bigint, scope: string) => {
|
||||
const authorizedScopes = decodeScopes(scopes)
|
||||
return authorizedScopes.includes(scope)
|
||||
}
|
||||
|
||||
export const toggleScope = (scopes: bigint, scope: string) => {
|
||||
const authorizedScopes = decodeScopes(scopes)
|
||||
if (authorizedScopes.includes(scope)) {
|
||||
return encodeScopes(authorizedScopes.filter((authorizedScope) => authorizedScope !== scope))
|
||||
} else {
|
||||
return encodeScopes([...authorizedScopes, scope])
|
||||
}
|
||||
}
|
||||
|
||||
export const getScopeDefinitions = (scopes: bigint) => {
|
||||
return decodeScopes(scopes)
|
||||
.filter((scope) => Object.keys(ScopeDescriptions).includes(scope))
|
||||
.map((scope) => (ScopeDescriptions as Record<string, string>)[scope])
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user