Fix Permission / AppPermission (#220)
This commit is contained in:
parent
7c790e44a2
commit
96ffb925bd
7 changed files with 23 additions and 14 deletions
5
.changeset/fast-tigers-mate.md
Normal file
5
.changeset/fast-tigers-mate.md
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
"@saleor/app-sdk": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Restores MANAGE_APPS to Permissions, but remove it from AppPermissions
|
|
@ -2,7 +2,7 @@ import { NextApiHandler, NextApiRequest, NextApiResponse } from "next";
|
||||||
|
|
||||||
import { APL } from "../../APL";
|
import { APL } from "../../APL";
|
||||||
import { createDebug } from "../../debug";
|
import { createDebug } from "../../debug";
|
||||||
import { AppPermission } from "../../types";
|
import { Permission } from "../../types";
|
||||||
import {
|
import {
|
||||||
processSaleorProtectedHandler,
|
processSaleorProtectedHandler,
|
||||||
ProtectedHandlerError,
|
ProtectedHandlerError,
|
||||||
|
@ -37,7 +37,7 @@ export const createProtectedHandler =
|
||||||
(
|
(
|
||||||
handlerFn: NextProtectedApiHandler,
|
handlerFn: NextProtectedApiHandler,
|
||||||
apl: APL,
|
apl: APL,
|
||||||
requiredPermissions?: AppPermission[]
|
requiredPermissions?: Permission[]
|
||||||
): NextApiHandler =>
|
): NextApiHandler =>
|
||||||
(req, res) => {
|
(req, res) => {
|
||||||
debug("Protected handler called");
|
debug("Protected handler called");
|
||||||
|
|
|
@ -4,7 +4,7 @@ import { APL } from "../../APL";
|
||||||
import { AuthData } from "../../APL/apl";
|
import { AuthData } from "../../APL/apl";
|
||||||
import { createDebug } from "../../debug";
|
import { createDebug } from "../../debug";
|
||||||
import { getBaseUrl, getSaleorHeaders } from "../../headers";
|
import { getBaseUrl, getSaleorHeaders } from "../../headers";
|
||||||
import { AppPermission } from "../../types";
|
import { Permission } from "../../types";
|
||||||
import { verifyJWT } from "../../verify-jwt";
|
import { verifyJWT } from "../../verify-jwt";
|
||||||
|
|
||||||
const debug = createDebug("processProtectedHandler");
|
const debug = createDebug("processProtectedHandler");
|
||||||
|
@ -39,7 +39,7 @@ export type ProtectedHandlerContext = {
|
||||||
interface ProcessSaleorProtectedHandlerArgs {
|
interface ProcessSaleorProtectedHandlerArgs {
|
||||||
req: NextApiRequest;
|
req: NextApiRequest;
|
||||||
apl: APL;
|
apl: APL;
|
||||||
requiredPermissions?: AppPermission[];
|
requiredPermissions?: Permission[];
|
||||||
}
|
}
|
||||||
|
|
||||||
type ProcessAsyncSaleorProtectedHandler = (
|
type ProcessAsyncSaleorProtectedHandler = (
|
||||||
|
|
|
@ -1,12 +1,12 @@
|
||||||
import { createDebug } from "./debug";
|
import { createDebug } from "./debug";
|
||||||
import { AppPermission } from "./types";
|
import { Permission } from "./types";
|
||||||
import { DashboardTokenPayload } from "./verify-jwt";
|
import { DashboardTokenPayload } from "./verify-jwt";
|
||||||
|
|
||||||
const debug = createDebug("checkJwtPermissions");
|
const debug = createDebug("checkJwtPermissions");
|
||||||
|
|
||||||
export const hasPermissionsInJwtToken = (
|
export const hasPermissionsInJwtToken = (
|
||||||
tokenData?: Pick<DashboardTokenPayload, "user_permissions">,
|
tokenData?: Pick<DashboardTokenPayload, "user_permissions">,
|
||||||
permissionsToCheckAgainst?: AppPermission[]
|
permissionsToCheckAgainst?: Permission[]
|
||||||
) => {
|
) => {
|
||||||
debug(`Permissions required ${permissionsToCheckAgainst}`);
|
debug(`Permissions required ${permissionsToCheckAgainst}`);
|
||||||
|
|
||||||
|
|
10
src/types.ts
10
src/types.ts
|
@ -15,7 +15,7 @@ export type AppExtensionMount =
|
||||||
| "ORDER_OVERVIEW_MORE_ACTIONS";
|
| "ORDER_OVERVIEW_MORE_ACTIONS";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* TODO: Extract from Saleor graphQL schema
|
* All permissions that users can have
|
||||||
* Reference https://docs.saleor.io/docs/3.x/api-reference/enums/permission-enum
|
* Reference https://docs.saleor.io/docs/3.x/api-reference/enums/permission-enum
|
||||||
*/
|
*/
|
||||||
export type Permission =
|
export type Permission =
|
||||||
|
@ -40,9 +40,13 @@ export type Permission =
|
||||||
| "MANAGE_PRODUCT_TYPES_AND_ATTRIBUTES"
|
| "MANAGE_PRODUCT_TYPES_AND_ATTRIBUTES"
|
||||||
| "MANAGE_SHIPPING"
|
| "MANAGE_SHIPPING"
|
||||||
| "MANAGE_SETTINGS"
|
| "MANAGE_SETTINGS"
|
||||||
| "MANAGE_TRANSLATIONS";
|
| "MANAGE_TRANSLATIONS"
|
||||||
|
| "MANAGE_APPS";
|
||||||
|
|
||||||
export type AppPermission = Permission;
|
/**
|
||||||
|
* All permissions that App can have.
|
||||||
|
*/
|
||||||
|
export type AppPermission = Exclude<Permission, "MANAGE_APPS">;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @see https://github.com/saleor/saleor/blob/main/saleor/graphql/schema.graphql#L1505
|
* @see https://github.com/saleor/saleor/blob/main/saleor/graphql/schema.graphql#L1505
|
||||||
|
|
|
@ -1,10 +1,10 @@
|
||||||
import * as jose from "jose";
|
import * as jose from "jose";
|
||||||
|
|
||||||
import { AppPermission } from "../types";
|
import { Permission } from "../types";
|
||||||
|
|
||||||
type TokenUserPayload = {
|
type TokenUserPayload = {
|
||||||
email: string;
|
email: string;
|
||||||
userPermissions: AppPermission[];
|
userPermissions: Permission[];
|
||||||
};
|
};
|
||||||
|
|
||||||
export const extractUserFromJwt = (jwtToken: string): TokenUserPayload => {
|
export const extractUserFromJwt = (jwtToken: string): TokenUserPayload => {
|
||||||
|
|
|
@ -2,21 +2,21 @@ import * as jose from "jose";
|
||||||
|
|
||||||
import { createDebug } from "./debug";
|
import { createDebug } from "./debug";
|
||||||
import { hasPermissionsInJwtToken } from "./has-permissions-in-jwt-token";
|
import { hasPermissionsInJwtToken } from "./has-permissions-in-jwt-token";
|
||||||
import { AppPermission } from "./types";
|
import { Permission } from "./types";
|
||||||
import { getJwksUrlFromSaleorApiUrl } from "./urls";
|
import { getJwksUrlFromSaleorApiUrl } from "./urls";
|
||||||
|
|
||||||
const debug = createDebug("verify-jwt");
|
const debug = createDebug("verify-jwt");
|
||||||
|
|
||||||
export interface DashboardTokenPayload extends jose.JWTPayload {
|
export interface DashboardTokenPayload extends jose.JWTPayload {
|
||||||
app: string;
|
app: string;
|
||||||
user_permissions: AppPermission[];
|
user_permissions: Permission[];
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface verifyJWTArguments {
|
export interface verifyJWTArguments {
|
||||||
appId: string;
|
appId: string;
|
||||||
saleorApiUrl: string;
|
saleorApiUrl: string;
|
||||||
token: string;
|
token: string;
|
||||||
requiredPermissions?: AppPermission[];
|
requiredPermissions?: Permission[];
|
||||||
}
|
}
|
||||||
|
|
||||||
export const verifyJWT = async ({
|
export const verifyJWT = async ({
|
||||||
|
|
Loading…
Reference in a new issue