Files
teleport/integration/appaccess/jwt.go
T
Trent Clarke 948417257f Split out appaccess and proxy integration tests (#16232)
* Proxy tests running

* rollback

* whitespace  fix

* Rollback port fix

* Linter appeasement

* License fix

* Update signals.go
2022-09-08 08:27:51 -06:00

56 lines
1.7 KiB
Go

// 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/gravitational/teleport/lib/defaults"
"github.com/gravitational/teleport/lib/jwt"
"github.com/gravitational/teleport/lib/web"
"github.com/stretchr/testify/require"
)
func verifyJWT(t *testing.T, pack *Pack, token, appURI string) {
// Get and unmarshal JWKs
status, body, err := pack.MakeRequest("", 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)
}