-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b88c7f4
commit fe970e9
Showing
7 changed files
with
309 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
--- | ||
title: Using JSON Web Tokens | ||
description: This document explains how to handle JSON Web Tokens via both HTTP and gRPC. | ||
--- | ||
|
||
## HTTP | ||
|
||
JSON Web Tokens can only be presented via HTTP requests in the form of an `Authorization` header. | ||
|
||
### `Authorization` Header | ||
|
||
For applications that communicate with Flipt over HTTP, the `Authorization` header is required. | ||
|
||
It must be provided in the form `Authorization: JWT <jwt>`. | ||
|
||
The following examples illustrate this in the context of various programming languages: | ||
|
||
<CodeGroup> | ||
|
||
{/* prettier-ignore */} | ||
```go client.go | ||
import ( | ||
"context" | ||
"net/http" | ||
) | ||
|
||
func main() { | ||
req := http.NewRequest("GET", "https://flipt.your.instance/api/v1/flags", nil) | ||
req.Header.Set("Authorization", "JWT eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c") | ||
|
||
resp, err := http.Do(req) | ||
// ... | ||
} | ||
``` | ||
|
||
{/* prettier-ignore */} | ||
```typescript client.ts | ||
import fetch from 'node-fetch'; | ||
|
||
const headers = { 'Authorization': 'JWT eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c' } | ||
const response = await fetch('https://flipt.your.instance/api/v1/flags', { headers: headers }) | ||
``` | ||
|
||
{/* prettier-ignore */} | ||
```python client.py | ||
import requests | ||
|
||
def doRequest(): | ||
|
||
headers ={"Authorization": "JWT eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c"} | ||
requests.get("https://flipt.your.instance/api/v1/flags", headers=headers) | ||
|
||
return | ||
``` | ||
|
||
</CodeGroup> | ||
|
||
## GRPC | ||
|
||
For gRPC we use the [Metadata](https://grpc.io/docs/what-is-grpc/core-concepts/#metadata) functionality similar to HTTP Headers. | ||
The lower-case `authorization` metadata key should be supplied with a single string `JWT <jwt>` to any RPC calls. | ||
|
||
### Example | ||
|
||
The following example authenticates a single gRPC client request: | ||
|
||
```go rpc.go | ||
func DoRequest(ctx context.Context, flagKey string) { | ||
ctx := metadata.AppendToOutgoingContext(ctx, "authorization", "JWT eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c") | ||
|
||
flag, err := flipt.GetFlags(ctx, &flipt.GetFlagRequest{ | ||
Key: flagKey, | ||
}) | ||
|
||
//... | ||
} | ||
``` | ||
|
||
This subsequent example demonstrates using a client unary interceptor, which authenticates all outgoing requests: | ||
|
||
```go interceptor.go | ||
func AuthUnaryClientInterceptor(optFuncs ...CallOption) grpc.UnaryClientInterceptor { | ||
return func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error { | ||
ctx = metadata.AppendToOutgoingContext(ctx, "authorization", "JWT eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c") | ||
return invoker(ctx, method, req, reply, cc, opts...) | ||
} | ||
} | ||
``` |
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
Oops, something went wrong.