Files
teleport/integration/appaccess/jwt.go
T
Justinas Stankevičius 68886bb8ef Entra ID integration boilerplate (#40998)
* Add e imports for MS Graph SDK

* Add ability to sign Entra ID OIDC JWTs, rework KID handling

- Synthesize Key IDs for our JWT keys. For backwards compatibility, also
  include the same keys with an empty `kid` in JWKS.
- Sign AWS OIDC tokens with a `kid=""` header claim,
  rather than omitting the `kid` claim altogether.
  See comment for details.

* Add validation for Entra ID plugin

* Fix typo in assertion function name

* Update the OIDC JWKS test to expect the same key twice

* Add Entra ID plugin type constant

* go mod tidy

* Fix expected JWKS size in integration test

* Add basic tests for KeyID

* Move Azure auth settings from Plugin to Integration

* Address review comments

* Add a unit test to ensure KeyID compatibility

* Add license header to token_generator.go

* Rename validation function per new conventions
2024-05-02 13:44:32 +00:00

70 lines
2.2 KiB
Go

/*
* Teleport
* Copyright (C) 2023 Gravitational, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
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, 2) // For backwards compatibility the same key is included twice in JWKS with different key IDs.
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)
}