Skip to content

Commit 071d65b

Browse files
committed
update
1 parent 0e522b5 commit 071d65b

7 files changed

Lines changed: 757 additions & 117 deletions

File tree

cmd/sandbox/auth.go

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
// Copyright 2022-2026 Salesforce, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package sandbox
16+
17+
import (
18+
"context"
19+
"fmt"
20+
"slices"
21+
"strings"
22+
23+
"github.com/slackapi/slack-cli/internal/iostreams"
24+
"github.com/slackapi/slack-cli/internal/shared"
25+
"github.com/slackapi/slack-cli/internal/shared/types"
26+
"github.com/slackapi/slack-cli/internal/slackerror"
27+
"github.com/slackapi/slack-cli/internal/style"
28+
)
29+
30+
// getSandboxAuth returns the auth used for sandbox management.
31+
// The --token flag will be used if present, otherwise we prompt the user to select a team to use for authentication.
32+
func getSandboxAuth(ctx context.Context, clients *shared.ClientFactory, tokenFlag string) (string, *types.SlackAuth, error) {
33+
var auth *types.SlackAuth
34+
if tokenFlag != "" {
35+
a, err := clients.Auth().AuthWithToken(ctx, tokenFlag)
36+
if err != nil {
37+
return "", nil, err
38+
}
39+
auth = &a
40+
} else {
41+
a, err := resolveAuthForSandbox(ctx, clients)
42+
if err != nil {
43+
return "", nil, err
44+
}
45+
auth = a
46+
}
47+
48+
clients.Config.APIHostResolved = clients.Auth().ResolveAPIHost(ctx, clients.Config.APIHostFlag, auth)
49+
clients.Config.LogstashHostResolved = clients.Auth().ResolveLogstashHost(ctx, clients.Config.APIHostResolved, clients.CLIVersion)
50+
51+
return auth.Token, auth, nil
52+
}
53+
54+
// resolveAuthForSandbox determines what auth to use for sandbox operations.
55+
// If the global --token flag is set, we will use that.
56+
// Else if the global --team flag is set, we will use the associated token for that team.
57+
// Else if the user is only logged in to one team, we will default to that team's auth.
58+
// Else we will prompt the user to select a team to use for authentication.
59+
func resolveAuthForSandbox(ctx context.Context, clients *shared.ClientFactory) (*types.SlackAuth, error) {
60+
// Check for the global --token flag
61+
if clients.Config.TokenFlag != "" {
62+
auth, err := clients.Auth().AuthWithToken(ctx, clients.Config.TokenFlag)
63+
if err != nil {
64+
return nil, err
65+
}
66+
return &auth, nil
67+
}
68+
69+
auths, err := clients.Auth().Auths(ctx)
70+
if err != nil {
71+
return nil, err
72+
}
73+
74+
if len(auths) == 0 {
75+
return nil, slackerror.New(slackerror.ErrCredentialsNotFound).
76+
WithMessage("You must be logged in to manage sandboxes").
77+
WithRemediation("Run 'slack login' to authenticate, or use --token for CI/CD")
78+
}
79+
80+
// Check for the global --team flag
81+
if clients.Config.TeamFlag != "" {
82+
for _, auth := range auths {
83+
if auth.TeamID == clients.Config.TeamFlag || auth.TeamDomain == clients.Config.TeamFlag {
84+
return &auth, nil
85+
}
86+
}
87+
return nil, slackerror.New(slackerror.ErrTeamNotFound).
88+
WithMessage("No auth found for team: %s", clients.Config.TeamFlag).
89+
WithRemediation("Run 'slack auth list' to see your authorized workspaces")
90+
}
91+
92+
// Prompt the user to select a team to use for authentication (if there are multiple auths), or default to the user's only auth
93+
if len(auths) == 1 {
94+
return &auths[0], nil
95+
}
96+
type authOption struct {
97+
auth types.SlackAuth
98+
label string
99+
}
100+
options := make([]authOption, 0, len(auths))
101+
for _, a := range auths {
102+
options = append(options, authOption{
103+
auth: a,
104+
label: fmt.Sprintf("%s %s", a.TeamDomain, style.Secondary(a.TeamID)),
105+
})
106+
}
107+
slices.SortFunc(options, func(a, b authOption) int {
108+
if c := strings.Compare(a.auth.TeamDomain, b.auth.TeamDomain); c != 0 {
109+
return c
110+
}
111+
return strings.Compare(a.auth.TeamID, b.auth.TeamID)
112+
})
113+
labels := make([]string, 0, len(options))
114+
for _, opt := range options {
115+
labels = append(labels, opt.label)
116+
}
117+
selection, err := clients.IO.SelectPrompt(ctx, "Select a team for authentication", labels, iostreams.SelectPromptConfig{
118+
Flag: clients.Config.Flags.Lookup("team"),
119+
Required: true,
120+
})
121+
if err != nil {
122+
return nil, err
123+
}
124+
switch {
125+
case selection.Flag:
126+
for _, opt := range options {
127+
if opt.auth.TeamID == selection.Option || opt.auth.TeamDomain == selection.Option {
128+
return &opt.auth, nil
129+
}
130+
}
131+
return nil, slackerror.New(slackerror.ErrTeamNotFound).
132+
WithMessage("No auth found for team: %s", selection.Option).
133+
WithRemediation("Run 'slack auth list' to see your authorized workspaces")
134+
case selection.Prompt:
135+
return &options[selection.Index].auth, nil
136+
default:
137+
return nil, slackerror.New(slackerror.ErrInvalidAuth)
138+
}
139+
}

0 commit comments

Comments
 (0)