// Copyright 2022 Gravitational, Inc // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package appaccess import ( "encoding/json" "net/http" "testing" "github.com/stretchr/testify/require" "github.com/gravitational/teleport/api/types/wrappers" "github.com/gravitational/teleport/lib/defaults" "github.com/gravitational/teleport/lib/jwt" "github.com/gravitational/teleport/lib/web" ) func verifyJWT(t *testing.T, pack *Pack, token, appURI string) { // Get and unmarshal JWKs status, body, err := pack.MakeRequest(nil, http.MethodGet, "/.well-known/jwks.json") require.NoError(t, err) require.Equal(t, http.StatusOK, status) var jwks web.JWKSResponse err = json.Unmarshal([]byte(body), &jwks) require.NoError(t, err) require.Len(t, jwks.Keys, 1) publicKey, err := jwt.UnmarshalJWK(jwks.Keys[0]) require.NoError(t, err) // Verify JWT. key, err := jwt.New(&jwt.Config{ PublicKey: publicKey, Algorithm: defaults.ApplicationTokenAlgorithm, ClusterName: pack.jwtAppClusterName, }) require.NoError(t, err) claims, err := key.Verify(jwt.VerifyParams{ Username: pack.username, RawToken: token, URI: appURI, }) require.NoError(t, err) require.Equal(t, pack.username, claims.Username) require.Equal(t, pack.user.GetRoles(), claims.Roles) filteredTraits := wrappers.Traits{} for trait, values := range pack.user.GetTraits() { if len(values) > 0 { filteredTraits[trait] = values } } require.Equal(t, filteredTraits, claims.Traits) }