Skip to content

Commit 267db3e

Browse files
mjfwebblidiazuin
andauthored
docs: add section about passing in JWTs (#71) (#72)
Co-authored-by: Lidia Zuin <[email protected]>
1 parent 8458cff commit 267db3e

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

modules/ROOT/pages/authentication-and-authorization/configuration.adoc

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,48 @@ type JWT @jwt {
127127
====
128128
The seemingly excessive escaping is required to doubly escape: once for GraphQL and once for `dot-prop`, which is used under the hood to resolve the path.
129129
====
130+
131+
== Passing in JWTs
132+
133+
To pass in an encoded JWT, you must use the token field of the context.
134+
When using Apollo Server, extract the authorization header into the token property of the context as follows:
135+
136+
[source, javascript, indent=0]
137+
----
138+
const server = new ApolloServer({
139+
schema,
140+
});
141+
142+
await startStandaloneServer(server, {
143+
context: async ({ req }) => ({ token: req.headers.authorization }),
144+
});
145+
----
146+
147+
For example, a HTTP request with the following `authorization` header should look like this:
148+
149+
[source]
150+
----
151+
POST / HTTP/1.1
152+
authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyLCJyb2xlcyI6WyJ1c2VyX2FkbWluIiwicG9zdF9hZG1pbiIsImdyb3VwX2FkbWluIl19.IY0LWqgHcjEtOsOw60mqKazhuRFKroSXFQkpCtWpgQI
153+
content-type: application/json
154+
----
155+
156+
Alternatively, you can pass a key `jwt` of type `JwtPayload` into the context, which has the following definition:
157+
158+
[source, typescript, indent=0]
159+
----
160+
// standard claims https://datatracker.ietf.org/doc/html/rfc7519#section-4.1
161+
interface JwtPayload {
162+
[key: string]: any;
163+
iss?: string | undefined;
164+
sub?: string | undefined;
165+
aud?: string | string[] | undefined;
166+
exp?: number | undefined;
167+
nbf?: number | undefined;
168+
iat?: number | undefined;
169+
jti?: string | undefined;
170+
}
171+
----
172+
173+
[WARNING]
174+
_Do not_ pass in the header or the signature.

0 commit comments

Comments
 (0)