mirror of
https://github.com/gravitational/teleport.git
synced 2026-11-01 05:27:24 +08:00
* Add API types for CircleCI joining * Add validation for CircleCI configuration * Add JoinMethodCircleCI across codebase * Add token validator and token source for circleci * Update join methods RFD * Add CircleCI token source to register.go * Add serverside support for circleci joining * Add test for TokenSource * Add success case for token validation test * Add expired test case * Add test case for token from another org * Test RegisterWithToken for CircleCI * Refactor GitHub RegisterUsingToken tests * Refactor CircleCI RegisterUsingToken tests * Add tests for ProvisionTokenSpecV2 * Appease linters * Go Imports files * Fix failing test for msising IDtoken * Update lib/auth/join_circleci.go Co-authored-by: Zac Bergquist <zac.bergquist@goteleport.com> * Move test server cleanup closer to initialisation * Fix weird import reordering * Include unexpected type in error message * Simplify boolean algebra :) Co-authored-by: Zac Bergquist <zac.bergquist@goteleport.com>
62 lines
1.5 KiB
Go
62 lines
1.5 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 circleci
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/coreos/go-oidc"
|
|
"github.com/gravitational/trace"
|
|
"github.com/jonboulle/clockwork"
|
|
)
|
|
|
|
func ValidateToken(
|
|
ctx context.Context,
|
|
clock clockwork.Clock,
|
|
issuerURLTemplate string,
|
|
organizationID string,
|
|
token string,
|
|
) (*IDTokenClaims, error) {
|
|
issuer := issuerURL(issuerURLTemplate, organizationID)
|
|
// We can't cache the provider as the issuer changes for every circleci
|
|
// organization - we should build a provider that can support a global
|
|
// cache.
|
|
p, err := oidc.NewProvider(
|
|
ctx,
|
|
issuer,
|
|
)
|
|
if err != nil {
|
|
return nil, trace.Wrap(err)
|
|
}
|
|
|
|
verifier := p.Verifier(&oidc.Config{
|
|
ClientID: organizationID,
|
|
Now: clock.Now,
|
|
})
|
|
|
|
idToken, err := verifier.Verify(ctx, token)
|
|
if err != nil {
|
|
return nil, trace.Wrap(err)
|
|
}
|
|
|
|
claims := IDTokenClaims{}
|
|
if err := idToken.Claims(&claims); err != nil {
|
|
return nil, trace.Wrap(err)
|
|
}
|
|
return &claims, nil
|
|
}
|