github.com/dosco/graphjin/auth/v3 v3.20.1
Sets HTTP Authorization headerJWT selects HS256 for token signing
SHA-2564edae5bd1b1eac2b12e850f2a673cb330c5c0ed095d8ee91eef6dbd0daf95358
MaleculeH(Cm₃)Md
Evidence
1package auth_test
2
3import (
4 "net/http"
5 "testing"
6
7 "github.com/dosco/graphjin/auth/v3"
8 "github.com/dosco/graphjin/core/v3"
9 jwt "github.com/golang-jwt/jwt/v5"
10 "github.com/stretchr/testify/assert"
11)
12
13func TestJWTTokenInAuthorizationHeader(t *testing.T) {
14 ah, err := auth.JwtHandler(auth.Auth{
15 Cookie: "Boo",
⋯8 lines
24 // "name": "John Doe",
25 // "iat": 1516239022
26 // }
27 tok := "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.VZ01qXI7Whbuj8X3FZw0mLyZT7iMKMCDl_rtzdNpjAg"
28
29 req, err := http.NewRequest(
30 http.MethodGet,
31 "https://test.com",
32 nil)
33 assert.NoError(t, err)
34
35 req.Header.Set("Authorization", "Bearer "+tok)
36
37 c, err := ah(nil, req)
38 assert.NoError(t, err)
39
40 assert.Equal(t, 1234567890, auth.UserIDInt(c))
41 // The token has a `name` claim but no `email`. UserName should pick it
42 // up; UserEmail should repor …
59:45… ey))
60}
61
62// TestJWTAttachesEmailAndName verifies that when the JWT contains email and
63// name claims, JwtHandler stashes them on the context for audit logging.
64func TestJWTAttachesEmailAndName(t *testing.T) {
65 const secret = "casper"
66 jwtTok := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
67 "sub": "42",
68 "email": "[email protected]",
69 "name": "Alice",
70 })
71 signed, err := jwtTok.SignedString([]byte(secret))
72 assert.NoError(t, …
8:35… pulled in by auth/provider.
9package oidc
10
11import (
12 "context"
13 "encoding/json"
14 "errors"
15 "fmt"
16 "io"
17 "net/http"
18 "strings"
19 "time"
20
21 jwt "github.com/golang-jwt/jwt/v5"
22 "github.com/lestrrat-go/jwx/jwk"
23 "golang.org/x/oauth2"
24)
25
26// Config describes a single OIDC identity provider. It works for any
27// OIDC-compliant is …
124:30… uerURL, "/") + "/.well-known/openid-configuration"
125 req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
126 if err != nil {
127 return discovery{}, err
128 }
129 resp, err := http.DefaultClient.Do(req)
130 if err != nil {
131 return discovery{}, fmt.Errorf("oidc discovery: %w", err)
132 }
133 defer resp.Body.Close()
134 if resp.StatusCode != http.StatusOK {
135 b, _ := io.ReadAll(resp.Body)
136 return discovery{}, fmt.Errorf("oidc discovery: %s: %s", resp.Status, strings.TrimSpace(string(b)))
137 }
138 var d discovery
139 if err := json.NewDecoder(resp.Body).Decode(&d); err != nil {
140 return discovery{}, fmt.Errorf("oidc discovery decode: %w", err)
141 }
142 if d.AuthorizationEn …
No evidence locations were recorded for this file. Raw result