-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create Site Preview JWT in the API (#2554)
Allows having the Site Preview unauthenticated ## Description Before: the JWT with the preview scope and setting was generated in the site. To check if the current user is allowed to preview the given scope the site had to make a request to the API and therefore needed the access token of the current user. With this setup the site has to be behind an authproxy. Now: the JWT is generated by the API and submitted to the site. This way the site can be public. ## BREAKING The API now requires the SITE_PREVIEW_SECRET environment variable. To make it explicit to add it this env now is also mandatory for local development.
- Loading branch information
Showing
16 changed files
with
135 additions
and
93 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
--- | ||
"@comet/cms-admin": minor | ||
"@comet/cms-site": minor | ||
"@comet/cms-api": minor | ||
--- | ||
|
||
Create site preview JWT in the API | ||
|
||
With this change the site preview can be deployed unprotected. Authentication is made via a JWT created in the API and validated in the site. A separate domain for the site preview is still necessary. | ||
|
||
BREAKING: this update of Comet v7 requires to have set sitePreviewSecret (which has to be the same value like possibly already set for site). Please refer to https://github.com/vivid-planet/comet-starter/pull/371 for more information on how to upgrade. |
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
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
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
36 changes: 36 additions & 0 deletions
36
packages/api/cms-api/src/page-tree/site-preview.resolver.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,36 @@ | ||
import { Inject } from "@nestjs/common"; | ||
import { Args, Query, Resolver } from "@nestjs/graphql"; | ||
import { GraphQLJSONObject } from "graphql-scalars"; | ||
import { SignJWT } from "jose"; | ||
|
||
import { RequiredPermission } from "../user-permissions/decorators/required-permission.decorator"; | ||
import { ContentScope } from "../user-permissions/interfaces/content-scope.interface"; | ||
import { SITE_PREVIEW_CONFIG } from "./page-tree.constants"; | ||
|
||
export type SitePreviewConfig = { | ||
secret: string; | ||
}; | ||
|
||
@Resolver() | ||
export class SitePreviewResolver { | ||
constructor(@Inject(SITE_PREVIEW_CONFIG) private readonly config: SitePreviewConfig) {} | ||
|
||
@Query(() => String) | ||
@RequiredPermission("pageTree") | ||
async sitePreviewJwt( | ||
@Args("scope", { type: () => GraphQLJSONObject }) scope: ContentScope, | ||
@Args("path") path: string, | ||
@Args("includeInvisible") includeInvisible: boolean, | ||
): Promise<string> { | ||
return new SignJWT({ | ||
scope, | ||
path, | ||
previewData: { | ||
includeInvisible, | ||
}, | ||
}) | ||
.setProtectedHeader({ alg: "HS256" }) | ||
.setExpirationTime("1 day") | ||
.sign(new TextEncoder().encode(this.config.secret)); | ||
} | ||
} |
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.