-
Notifications
You must be signed in to change notification settings - Fork 373
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
145b74e
commit d67cf27
Showing
2 changed files
with
93 additions
and
0 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,71 @@ | ||
package api | ||
|
||
import ( | ||
"crypto/rand" | ||
"crypto/rsa" | ||
"encoding/json" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/lestrrat-go/jwx/v2/jwk" | ||
"github.com/stretchr/testify/require" | ||
"github.com/supabase/auth/internal/conf" | ||
) | ||
|
||
func TestJwks(t *testing.T) { | ||
// generate RSA key pair for testing | ||
rsaPrivateKey, err := rsa.GenerateKey(rand.Reader, 2048) | ||
require.NoError(t, err) | ||
rsaJwkPrivate, err := jwk.FromRaw(rsaPrivateKey) | ||
require.NoError(t, err) | ||
rsaJwkPublic, err := rsaJwkPrivate.PublicKey() | ||
require.NoError(t, err) | ||
kid := rsaJwkPublic.KeyID() | ||
|
||
cases := []struct { | ||
desc string | ||
config conf.JWTConfiguration | ||
expectedLen int | ||
}{ | ||
{ | ||
desc: "only hmac key in config", | ||
config: conf.JWTConfiguration{ | ||
Aud: "authenticated", | ||
Secret: "test-secret", | ||
}, | ||
expectedLen: 0, | ||
}, | ||
{ | ||
desc: "rsa key in config", | ||
config: conf.JWTConfiguration{ | ||
Aud: "authenticated", | ||
Secret: "test-secret", | ||
Keys: conf.JwtKeysDecoder{ | ||
kid: conf.JwkInfo{ | ||
PublicKey: rsaJwkPublic, | ||
PrivateKey: rsaJwkPrivate, | ||
}, | ||
}, | ||
}, | ||
expectedLen: 1, | ||
}, | ||
} | ||
|
||
for _, c := range cases { | ||
t.Run(c.desc, func(t *testing.T) { | ||
mockAPI, _, err := setupAPIForTest() | ||
require.NoError(t, err) | ||
mockAPI.config.JWT = c.config | ||
|
||
req := httptest.NewRequest(http.MethodGet, "/.well-known/jwks.json", nil) | ||
w := httptest.NewRecorder() | ||
mockAPI.handler.ServeHTTP(w, req) | ||
require.Equal(t, http.StatusOK, w.Code) | ||
|
||
var data map[string]interface{} | ||
require.NoError(t, json.NewDecoder(w.Body).Decode(&data)) | ||
require.Len(t, data["keys"], c.expectedLen) | ||
}) | ||
} | ||
} |
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,22 @@ | ||
package conf | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestDecode(t *testing.T) { | ||
// array of JWKs containing 4 keys | ||
gotrueJwtKeys := `[{"kty":"oct","k":"lTPZqLlq8unu2q3-sx_9fZMphA5j5qzLbd4lZgp7u3k","kid":"998ec4f0-d7b2-46da-919d-fe8c67117674","use":"sig","alg":"HS256"},{"kty":"RSA","n":"0hOs9TihsN3kyTpAzvKH8Hy_E94wouGCP9dakxnPQc0Ap7G56d79m4v5OpqStbJ6QC_p2tHho-jQsx1dFMVUDofETiNqANda2b51uU41PHBxYkEn0qo7XuhvsD1jMSW_URhkugZM0jaiV6DM-1GoDI-lwlpq-MqG7buQc9GdhD6RY9wsJU4fd6Z3ehDt-RCOyCYjfNhRfDPNOI7cNXPl8TDapWBzORiu-1xt-ImsjlDn4HNb_rdU3nqKihsgGDiRtv7wizYUvN8DrLB1wWBCRPBPzjrSQ0CBOiNvo1DM11PfkFkojJiKBsMWqoCWfzSe_BmxxqnibnHXzTwGUGyJwQ","e":"AQAB","d":"BKkeVmiRsMIe02aNExz0bl3MIkQU4DuLIite-sIRCVi47copUbKVEzR7UeUllwLycqEFfzRBEpElloBdemqdez7gmGsGzOWPjM1h4KZcfhPmC2aekYOd59SrDACepjOxeg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q6PhZZUdhEtd2zU","kid":"462eb2fa-e31d-42e9-bb27-70d2ea832b89","use":"enc","alg":"RS256"},{"kty":"EC","x":"AglJaeHJfeea2ypQpIfKz-YVM0SDmxyosdX3hm3r_OY","y":"deWJWjUn9cW8qX3cR0SpiEserdgfarAFNifBEQqtGu8","crv":"P-256","d":"isP8oPMtBWSMAQPVJ_nKZaOwruDe7PJJbRxGtp8AEFg","kid":"c41f3f01-1b31-4795-9df6-8c4d78ff9908","use":"sig","alg":"ES256"},{"crv":"Ed25519","d":"ixHsrx2zZ44wavVzENw9TjcdlIx61ANlwFKeuWFLjms","x":"3v1LnbbCmuZtZEHm8r9gZlvUw-PQimYP2Oc587hMok8","kty":"OKP","kid":"6804dfd3-d3e0-4c1d-a8dc-dc83632001ce","use":"sig","alg":"EdDSA"}]` | ||
var decoder JwtKeysDecoder | ||
require.NoError(t, decoder.Decode(gotrueJwtKeys)) | ||
require.Len(t, decoder, 4) | ||
|
||
for kid, key := range decoder { | ||
require.NotEmpty(t, kid) | ||
require.NotNil(t, key.PrivateKey) | ||
require.NotNil(t, key.PublicKey) | ||
require.NotEmpty(t, key.PublicKey.KeyUsage(), "missing use claim") | ||
} | ||
} |