Skip to content

Commit da962a2

Browse files
committed
Use Authorization + token instead of X-User-ID
1 parent 827be5d commit da962a2

25 files changed

+79
-51
lines changed

.dockerignore

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
quickpizza.db
2+
Makefile

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ Example of header usage:
274274
```
275275
curl -X POST http://localhost:3333/api/pizza \
276276
-H "Content-Type: application/json" \
277-
-H "X-User-ID: 23423" \
277+
-H "Authorization: abcdef0123456789" \
278278
-H "x-error-record-recommendation: internal-error" \
279279
-H "x-error-record-recommendation-percentage: 20" \
280280
-d '{}'

k6/browser/02.cookies.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export default async function () {
2020
const pizzaContext = await browser.newContext();
2121
await pizzaContext.addCookies([
2222
{
23-
name: "X-User-ID",
23+
name: "FooBar",
2424
value: 123456,
2525
domain: BASE_URL,
2626
path: '/',
@@ -33,7 +33,7 @@ export default async function () {
3333

3434
check(cookies, {
3535
"cookie length of QuickPizza page": cookies => cookies.length === 1,
36-
"cookie name": cookies => cookies[0].name === "X-User-ID",
36+
"cookie name": cookies => cookies[0].name === "FooBar",
3737
"cookie value": cookies => cookies[0].value === "123456"
3838
});
3939

@@ -44,10 +44,10 @@ export default async function () {
4444
const anotherPage = await anotherContext.newPage();
4545
const anotherCookies = await anotherContext.cookies();
4646

47-
await anotherPage.goto('https://test.k6.io/');
47+
await anotherPage.goto('https://example.org/');
4848

4949
check(anotherCookies, {
50-
"cookie length of k6 test page": anotherCookies => anotherCookies.length === 0,
50+
"cookie length of example test page": anotherCookies => anotherCookies.length === 0,
5151
});
5252

5353
await anotherPage.close();

k6/browser/07.hybrid.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ export function getPizza() {
7272
let res = http.post(`${BASE_URL}/api/pizza`, JSON.stringify(restrictions), {
7373
headers: {
7474
'Content-Type': 'application/json',
75-
'X-User-ID': 123456,
75+
'Authorization': 'token abcdef0123456789',
7676
},
7777
});
7878
check(res, { "status is 200": (res) => res.status === 200 });
@@ -123,4 +123,4 @@ export async function pizzaRecommendations() {
123123
} finally {
124124
page.close();
125125
}
126-
}
126+
}

k6/disruptor/01.basic.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export default function () {
4949
let res = http.post(`${BASE_URL}/api/pizza`, JSON.stringify(restrictions), {
5050
headers: {
5151
"Content-Type": "application/json",
52-
"X-User-ID": 23423,
52+
"Authorization": "token abcdef0123456789",
5353
},
5454
});
5555
check(res, {"status is 200": (res) => res.status === 200});

k6/disruptor/02.error.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export default function () {
4949
let res = http.post(`${BASE_URL}/api/pizza`, JSON.stringify(restrictions), {
5050
headers: {
5151
"Content-Type": "application/json",
52-
"X-User-ID": 23423,
52+
"Authorization": "token abcdef0123456789",
5353
},
5454
});
5555
check(res, {"status is 200": (res) => res.status === 200});

k6/extensions/01.basic-internal.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export default function() {
2121
let res = http.post(`${BASE_URL}/api/pizza`, JSON.stringify(restrictions), {
2222
headers: {
2323
'Content-Type': 'application/json',
24-
'X-User-ID': 23423,
24+
'Authorization': 'token abcdef0123456789',
2525
},
2626
});
2727
check(res, { "status is 200": (res) => res.status === 200 });
@@ -35,4 +35,4 @@ export default function() {
3535
return followRestrictions;
3636
}});
3737
sleep(1);
38-
}
38+
}

k6/foundations/01.basic.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ export default function () {
2020
let res = http.post(`${BASE_URL}/api/pizza`, JSON.stringify(restrictions), {
2121
headers: {
2222
'Content-Type': 'application/json',
23-
'X-User-ID': 23423,
23+
'Authorization': 'token abcdef0123456789',
2424
},
2525
});
2626
check(res, { "status is 200": (res) => res.status === 200 });
2727
console.log(`${res.json().pizza.name} (${res.json().pizza.ingredients.length} ingredients)`);
2828
sleep(1);
29-
}
29+
}

k6/foundations/02.stages.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ export default function () {
2323
let res = http.post(`${BASE_URL}/api/pizza`, JSON.stringify(restrictions), {
2424
headers: {
2525
'Content-Type': 'application/json',
26-
'X-User-ID': 23423,
26+
'Authorization': 'token abcdef0123456789',
2727
},
2828
});
2929
check(res, { "status is 200": (res) => res.status === 200 });
3030
console.log(`${res.json().pizza.name} (${res.json().pizza.ingredients.length} ingredients)`);
3131
sleep(1);
32-
}
32+
}

k6/foundations/03.lifecycle.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export default function () {
3030
let res = http.post(`${BASE_URL}/api/pizza`, JSON.stringify(restrictions), {
3131
headers: {
3232
'Content-Type': 'application/json',
33-
'X-User-ID': 23423,
33+
'Authorization': 'token abcdef0123456789',
3434
},
3535
});
3636
check(res, { "status is 200": (res) => res.status === 200 });
@@ -41,4 +41,4 @@ export default function () {
4141
export function teardown(){
4242
// TODO: Send notification to Slack
4343
console.log("That's all folks!")
44-
}
44+
}

k6/foundations/04.metrics.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export default function () {
3434
let res = http.post(`${BASE_URL}/api/pizza`, JSON.stringify(restrictions), {
3535
headers: {
3636
'Content-Type': 'application/json',
37-
'X-User-ID': 23423,
37+
'Authorization': 'token abcdef0123456789',
3838
},
3939
});
4040
check(res, { "status is 200": (res) => res.status === 200 });
@@ -47,4 +47,4 @@ export default function () {
4747
export function teardown(){
4848
// TODO: Send notification to Slack
4949
console.log("That's all folks!")
50-
}
50+
}

k6/foundations/05.thresholds.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export default function () {
3939
let res = http.post(`${BASE_URL}/api/pizza`, JSON.stringify(restrictions), {
4040
headers: {
4141
'Content-Type': 'application/json',
42-
'X-User-ID': 23423,
42+
'Authorization': 'token abcdef0123456789',
4343
},
4444
});
4545
check(res, { "status is 200": (res) => res.status === 200 });
@@ -52,4 +52,4 @@ export default function () {
5252
export function teardown(){
5353
// TODO: Send notification to Slack
5454
console.log("That's all folks!")
55-
}
55+
}

k6/foundations/06.checks-with-thresholds.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export default function () {
4040
let res = http.post(`${BASE_URL}/api/pizza`, JSON.stringify(restrictions), {
4141
headers: {
4242
'Content-Type': 'application/json',
43-
'X-User-ID': 23423,
43+
'Authorization': 'token abcdef0123456789',
4444
},
4545
});
4646
check(res, { "status is 200": (res) => res.status === 200 });
@@ -53,4 +53,4 @@ export default function () {
5353
export function teardown() {
5454
// TODO: Send notification to Slack
5555
console.log("That's all folks!")
56-
}
56+
}

k6/foundations/07.scenarios.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export function getPizza() {
5454
let res = http.post(`${BASE_URL}/api/pizza`, JSON.stringify(restrictions), {
5555
headers: {
5656
'Content-Type': 'application/json',
57-
'X-User-ID': 23423,
57+
'Authorization': 'token abcdef0123456789',
5858
},
5959
});
6060
check(res, { "status is 200": (res) => res.status === 200 });
@@ -74,4 +74,4 @@ export function handleSummary(data) {
7474
'summary.json': JSON.stringify(data, null, 2),
7575
stdout: textSummary(data, { indent: " ", enableColors: true }),
7676
}
77-
}
77+
}

k6/foundations/08.arrival-rate.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export function getPizza() {
5151
let res = http.post(`${BASE_URL}/api/pizza`, JSON.stringify(restrictions), {
5252
headers: {
5353
'Content-Type': 'application/json',
54-
'X-User-ID': 23423,
54+
'Authorization': 'token abcdef0123456789',
5555
},
5656
});
5757
check(res, { "status is 200": (res) => res.status === 200 });
@@ -84,4 +84,4 @@ export function handleSummary(data) {
8484
'summary.json': JSON.stringify(data, null, 2),
8585
stdout: textSummary(data, { indent: " ", enableColors: true }),
8686
}
87-
}
87+
}

k6/foundations/09.data.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export function getPizza() {
5959
let res = http.post(`${BASE_URL}/api/pizza`, JSON.stringify(restrictions), {
6060
headers: {
6161
'Content-Type': 'application/json',
62-
'X-User-ID': customers[Math.floor(Math.random() * customers.length)],
62+
'Authorization': 'token abcdef0123456789',
6363
},
6464
});
6565
check(res, { "status is 200": (res) => res.status === 200 });
@@ -79,4 +79,4 @@ export function handleSummary(data) {
7979
'summary.json': JSON.stringify(data, null, 2),
8080
stdout: textSummary(data, { indent: " ", enableColors: true }),
8181
}
82-
}
82+
}

k6/foundations/10.summary.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export default function () {
4141
let res = http.post(`${BASE_URL}/api/pizza`, JSON.stringify(restrictions), {
4242
headers: {
4343
'Content-Type': 'application/json',
44-
'X-User-ID': 23423,
44+
'Authorization': 'token abcdef0123456789',
4545
},
4646
});
4747
check(res, { "status is 200": (res) => res.status === 200 });
@@ -61,4 +61,4 @@ export function handleSummary(data) {
6161
'summary.json': JSON.stringify(data, null, 2),
6262
stdout: textSummary(data, { indent: " ", enableColors: true }),
6363
}
64-
}
64+
}

k6/foundations/11.composability.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export function getPizza() {
7373
let res = http.post(`${BASE_URL}/api/pizza`, JSON.stringify(restrictions), {
7474
headers: {
7575
"Content-Type": "application/json",
76-
"X-User-ID": customers[Math.floor(Math.random() * customers.length)],
76+
'Authorization': 'token abcdef0123456789',
7777
},
7878
});
7979
check(res, { "status is 200": (res) => res.status === 200 });

k6/foundations/12.modularization.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ export function getPizza() {
6868
let res = http.post(`${BASE_URL}/api/pizza`, JSON.stringify(restrictions), {
6969
headers: {
7070
'Content-Type': 'application/json',
71-
'X-User-ID': customers[Math.floor(Math.random() * customers.length)],
71+
'Authorization': 'token abcdef0123456789',
7272
},
7373
});
7474
check(res, { "status is 200": (res) => res.status === 200 });
@@ -92,4 +92,4 @@ export function handleSummary(data) {
9292
'summary.json': JSON.stringify(data, null, 2),
9393
stdout: textSummary(data, { indent: " ", enableColors: true }),
9494
}
95-
}
95+
}

k6/foundations/14.basic.tracing.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export default function () {
2525
let res = http.post(`${BASE_URL}/api/pizza`, JSON.stringify(restrictions), {
2626
headers: {
2727
'Content-Type': 'application/json',
28-
'X-User-ID': 23423,
28+
'Authorization': 'token abcdef0123456789',
2929
},
3030
});
3131
check(res, { "status is 200": (res) => res.status === 200 });

k6/foundations/15.basic.profiling.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export default function () {
2323
let res = http.post(`${BASE_URL}/api/pizza`, JSON.stringify(restrictions), {
2424
headers: {
2525
"Content-Type": "application/json",
26-
"X-User-ID": 23423,
26+
'Authorization': 'token abcdef0123456789',
2727
},
2828
});
2929
check(res, { "status is 200": (res) => res.status === 200 });

pkg/http/client.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,9 @@ func (hc httpClient) do(request *http.Request) (*http.Response, error) {
9696
// Authenticate request with the super-secret internal token.
9797
request.Header.Add("X-Is-Internal", "1")
9898

99-
// Propagate X-User-ID if present in request context.
100-
if user, ok := request.Context().Value("user").(string); ok {
101-
request.Header.Add("X-User-ID", user)
99+
// Propagate Authorization if present in request context.
100+
if auth, ok := request.Context().Value("authorization").(string); ok {
101+
request.Header.Add("Authorization", auth)
102102
}
103103

104104
return hc.client.Do(request)

pkg/http/http.go

+12-4
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ import (
3838
"github.com/grafana/quickpizza/pkg/web"
3939
)
4040

41+
const tokenLength = 16
42+
4143
// Variables storing prometheus metrics.
4244
var (
4345
pizzaRecommendations = promauto.NewCounterVec(prometheus.CounterOpts{
@@ -117,7 +119,7 @@ func NewServer() (*Server, error) {
117119
router.Use(cors.New(cors.Options{
118120
AllowedOrigins: []string{"*"},
119121
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
120-
AllowedHeaders: []string{"Accept", "Authorization", "Content-Type", "X-CSRF-Token", "X-User-ID"},
122+
AllowedHeaders: []string{"Accept", "Authorization", "Content-Type", "X-CSRF-Token"},
121123
ExposedHeaders: []string{"Link"},
122124
AllowCredentials: true,
123125
MaxAge: 300, // Maximum value not ignored by any of major browsers
@@ -741,13 +743,19 @@ func SvelteKitHandler(path string) http.Handler {
741743

742744
func ValidateUserMiddleware(next http.Handler) http.Handler {
743745
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
744-
userID := r.Header.Get("X-User-ID")
745-
if userID == "" {
746+
auth := r.Header.Get("Authorization")
747+
prefix, token, found := strings.Cut(auth, " ")
748+
prefix = strings.ToLower(prefix)
749+
750+
// Here, we would actually check the token against the DB, or
751+
// verify it using a private key (e.g. for JWT), but for this
752+
// testing service we just check its length.
753+
if !found || prefix != "token" || len(token) != tokenLength {
746754
w.WriteHeader(http.StatusUnauthorized)
747755
return
748756
}
749757

750-
ctx := context.WithValue(r.Context(), "user", userID)
758+
ctx := context.WithValue(r.Context(), "authorization", auth)
751759
next.ServeHTTP(w, r.WithContext(ctx))
752760
})
753761
}

pkg/web/src/lib/stores.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
import { writable } from "svelte/store";
22

3-
export const userID = writable(0);
3+
export const userIDStore = writable(0);
4+
export const userTokenStore = writable('');

0 commit comments

Comments
 (0)