Skip to content

Commit

Permalink
Add scope flag to idp commands (#19727)
Browse files Browse the repository at this point in the history
* Add `scope` flag to idp commands

* fix flag inherit
  • Loading branch information
mustard-mh committed May 15, 2024
1 parent 2aaa732 commit 5ff4535
Show file tree
Hide file tree
Showing 11 changed files with 124 additions and 23 deletions.
2 changes: 1 addition & 1 deletion components/gitpod-cli/cmd/idp-gcloud-token.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ var idpGCloudTokenCmd = &cobra.Command{
}
}()

tkn, err := idpToken(ctx, idpGCloudTokenOpts.Audience)
tkn, err := idpToken(ctx, idpGCloudTokenOpts.Audience, "")
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion components/gitpod-cli/cmd/idp-login-aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ var idpLoginAwsCmd = &cobra.Command{
ctx, cancel := context.WithTimeout(cmd.Context(), 5*time.Second)
defer cancel()

tkn, err := idpToken(ctx, idpLoginAwsOpts.Audience)
tkn, err := idpToken(ctx, idpLoginAwsOpts.Audience, idpLoginOpts.Scope)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion components/gitpod-cli/cmd/idp-login-vault.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ var idpLoginVaultCmd = &cobra.Command{
ctx, cancel := context.WithTimeout(cmd.Context(), 5*time.Second)
defer cancel()

tkn, err := idpToken(ctx, idpLoginVaultOpts.Audience)
tkn, err := idpToken(ctx, idpLoginVaultOpts.Audience, idpLoginOpts.Scope)
if err != nil {
return err
}
Expand Down
6 changes: 6 additions & 0 deletions components/gitpod-cli/cmd/idp-login.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,17 @@ import (
"github.com/spf13/cobra"
)

var idpLoginOpts struct {
Scope string
}

var idpLoginCmd = &cobra.Command{
Use: "login",
Short: "Login to a service for which trust has been established",
}

func init() {
idpCmd.AddCommand(idpLoginCmd)

idpLoginCmd.PersistentFlags().StringVar(&idpLoginOpts.Scope, "scope", "", "scopes string of the ID token")
}
7 changes: 5 additions & 2 deletions components/gitpod-cli/cmd/idp-token.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
var idpTokenOpts struct {
Audience []string
Decode bool
Scope string
}

var idpTokenCmd = &cobra.Command{
Expand All @@ -37,7 +38,7 @@ var idpTokenCmd = &cobra.Command{
ctx, cancel := context.WithTimeout(cmd.Context(), 5*time.Second)
defer cancel()

tkn, err := idpToken(ctx, idpTokenOpts.Audience)
tkn, err := idpToken(ctx, idpTokenOpts.Audience, idpTokenOpts.Scope)

token, _, err := jwt.NewParser().ParseUnverified(tkn, jwt.MapClaims{})
if err != nil {
Expand Down Expand Up @@ -66,7 +67,7 @@ var idpTokenCmd = &cobra.Command{
},
}

func idpToken(ctx context.Context, audience []string) (idToken string, err error) {
func idpToken(ctx context.Context, audience []string, scope string) (idToken string, err error) {
wsInfo, err := gitpod.GetWSInfo(ctx)
if err != nil {
return "", err
Expand Down Expand Up @@ -95,6 +96,7 @@ func idpToken(ctx context.Context, audience []string) (idToken string, err error
Msg: &v1.GetIDTokenRequest{
Audience: audience,
WorkspaceId: wsInfo.WorkspaceId,
Scope: scope,
},
})
if err != nil {
Expand All @@ -110,4 +112,5 @@ func init() {
_ = idpTokenCmd.MarkFlagRequired("audience")

idpTokenCmd.Flags().BoolVar(&idpTokenOpts.Decode, "decode", false, "decode token to JSON")
idpTokenCmd.Flags().StringVar(&idpTokenOpts.Scope, "scope", "", "scopes string of the ID token")
}
4 changes: 4 additions & 0 deletions components/public-api-server/pkg/apiv1/identityprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ func (srv *IdentityProviderService) GetIDToken(ctx context.Context, req *connect
userInfo.AppendClaims("context", workspace.Workspace.ContextURL)
userInfo.AppendClaims("workspace_id", workspaceID)

if req.Msg.GetScope() != "" {
userInfo.AppendClaims("scope", req.Msg.GetScope())
}

if workspace.Workspace.Context != nil && workspace.Workspace.Context.Repository != nil && workspace.Workspace.Context.Repository.CloneURL != "" {
userInfo.AppendClaims("repository", workspace.Workspace.Context.Repository.CloneURL)
}
Expand Down
50 changes: 50 additions & 0 deletions components/public-api-server/pkg/apiv1/identityprovider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,56 @@ func TestGetIDToken(t *testing.T) {
Error: connect.NewError(connect.CodeInvalidArgument, fmt.Errorf("Must have at least one audience entry")).Error(),
},
},
{
Name: "include scope",
TokenSource: func(t *testing.T) IDTokenSource {
return functionIDTokenSource(func(ctx context.Context, org string, audience []string, userInfo oidc.UserInfo) (string, error) {
require.Equal(t, "correct@gitpod.io", userInfo.GetEmail())
require.True(t, userInfo.IsEmailVerified())
require.Equal(t, "foo", userInfo.GetClaim("scope"))

return "foobar", nil
})
},
ServerSetup: func(ma *protocol.MockAPIInterface) {
ma.EXPECT().GetIDToken(gomock.Any()).MinTimes(1).Return(nil)
ma.EXPECT().GetWorkspace(gomock.Any(), workspaceID).MinTimes(1).Return(
&protocol.WorkspaceInfo{
Workspace: &protocol.Workspace{
ContextURL: "https://github.com/gitpod-io/gitpod",
Context: &protocol.WorkspaceContext{
Repository: &protocol.Repository{
CloneURL: "https://github.com/gitpod-io/gitpod.git",
},
},
},
},
nil,
)
ma.EXPECT().GetLoggedInUser(gomock.Any()).Return(
&protocol.User{
Name: "foobar",
Identities: []*protocol.Identity{
nil,
{Deleted: true, PrimaryEmail: "nonsense@gitpod.io"},
{Deleted: false, PrimaryEmail: "correct@gitpod.io"},
},
OrganizationId: "test",
},
nil,
)
},
Request: &v1.GetIDTokenRequest{
WorkspaceId: workspaceID,
Audience: []string{"some.audience.com"},
Scope: "foo",
},
Expectation: Expectation{
Response: &v1.GetIDTokenResponse{
Token: "foobar",
},
},
},
{
Name: "token source error",
TokenSource: func(t *testing.T) IDTokenSource {
Expand Down
22 changes: 22 additions & 0 deletions components/public-api-server/pkg/identityprovider/idp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,28 @@ func TestIDToken(t *testing.T) {
},
},
},
{
Name: "with custom claims",
Audience: []string{"some.audience.com"},
UserInfo: func() oidc.UserInfo {
userInfo := oidc.NewUserInfo()
userInfo.AppendClaims("scope", "foobar")
return userInfo
}(),
Expectation: Expectation{
Token: &jwt.Token{
Method: &jwt.SigningMethodRSA{Name: "RS256", Hash: crypto.SHA256},
Header: map[string]interface{}{"alg": string(jose.RS256)},
Claims: jwt.MapClaims{
"aud": []any{string("some.audience.com")},
"azp": string("some.audience.com"),
"iss": string("https://api.gitpod.io/idp"),
"scope": "foobar",
},
Valid: true,
},
},
},
}

for _, test := range tests {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ service IdentityProviderService {
message GetIDTokenRequest {
string workspace_id = 1;
repeated string audience = 2;
string scope = 3;
}

message GetIDTokenResponse {
Expand Down
45 changes: 27 additions & 18 deletions components/public-api/go/experimental/v1/identityprovider.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5ff4535

Please sign in to comment.