Skip to content

Commit 339d2a8

Browse files
Merge pull request #219 from brevdev/get-exeternal-auth-id
kas flow if you have been migrated
2 parents d1b6c68 + 236a594 commit 339d2a8

4 files changed

Lines changed: 72 additions & 10 deletions

File tree

pkg/cmd/cmderrors/cmderrors.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package cmderrors
22

33
import (
44
"fmt"
5+
"os"
6+
"os/exec"
57
"strings"
68

79
"github.com/getsentry/sentry-go"
@@ -34,6 +36,26 @@ func DisplayAndHandleError(err error) {
3436
case breverrors.WorkspaceNotRunning: // report error to track when this occurs, but don't print stacktrace to user unless in dev mode
3537
er.ReportError(err)
3638
prettyErr = (t.Yellow(errors.Cause(err).Error()))
39+
case *breverrors.NvidiaMigrationError:
40+
// Handle nvidia migration error
41+
if nvErr, ok := errors.Cause(err).(*breverrors.NvidiaMigrationError); ok {
42+
prettyErr = t.Yellow(nvErr.Error() + "\n" + nvErr.Directive())
43+
44+
// Attempt automatic KAS login
45+
fmt.Println("\n This account has been migrated to NVIDIA Auth. Attempting to log in with NVIDIA account...")
46+
brevBin, err1 := os.Executable()
47+
if err1 == nil {
48+
cmd := exec.Command(brevBin, "login", "--auth", "nvidia") // #nosec G204
49+
cmd.Stdout = os.Stdout
50+
cmd.Stderr = os.Stderr
51+
cmd.Stdin = os.Stdin
52+
_ = cmd.Run() // Ignore error as it will be handled by the login command
53+
}
54+
} else {
55+
// Fallback in case type assertion fails (shouldn't happen but better safe than sorry)
56+
prettyErr = t.Red(errors.Cause(err).Error())
57+
er.ReportError(err)
58+
}
3759
default:
3860
if isSneakyValidationErr(err) {
3961
prettyErr = (t.Yellow(errors.Cause(err).Error()))

pkg/entity/entity.go

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -167,17 +167,24 @@ const (
167167
Standard GlobalUserType = "Standard"
168168
)
169169

170+
type ExternalIdentity struct {
171+
IdentityID string `json:"identity_id,omitempty"`
172+
Provider string `json:"provider,omitempty"`
173+
ExternalID string `json:"external_id,omitempty"`
174+
}
175+
170176
type User struct {
171-
ID string `json:"id"`
172-
PublicKey string `json:"publicKey,omitempty"`
173-
Username string `json:"username"`
174-
Name string `json:"name"`
175-
Email string `json:"email"`
176-
WorkspacePassword string `json:"workspacePassword"`
177-
BaseWorkspaceRepo string `json:"baseWorkspaceRepo"`
178-
GlobalUserType GlobalUserType `json:"globalUserType"`
179-
IdeConfig IDEConfig `json:"ideConfig,omitempty"`
180-
OnboardingData map[string]interface{} `json:"onboardingData"`
177+
ID string `json:"id"`
178+
PublicKey string `json:"publicKey,omitempty"`
179+
Username string `json:"username"`
180+
Name string `json:"name"`
181+
Email string `json:"email"`
182+
WorkspacePassword string `json:"workspacePassword"`
183+
BaseWorkspaceRepo string `json:"baseWorkspaceRepo"`
184+
GlobalUserType GlobalUserType `json:"globalUserType"`
185+
IdeConfig IDEConfig `json:"ideConfig,omitempty"`
186+
OnboardingData map[string]interface{} `json:"onboardingData"`
187+
ExternalIdentities []*ExternalIdentity `json:"externalIdentities,omitempty"`
181188
}
182189

183190
type UserKeys struct {

pkg/errors/errors.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,3 +277,19 @@ func makeErrorMessage(message string, skip int) string {
277277
}
278278

279279
// logger.L().Error("", zap.Error(err))
280+
281+
type NvidiaMigrationError struct {
282+
Message string
283+
}
284+
285+
func (e NvidiaMigrationError) Error() string {
286+
return e.Message
287+
}
288+
289+
func (e NvidiaMigrationError) Directive() string {
290+
return "Please run 'brev login --auth nvidia' to log in with your NVIDIA account"
291+
}
292+
293+
func NewNvidiaMigrationError(msg string) *NvidiaMigrationError {
294+
return &NvidiaMigrationError{Message: msg}
295+
}

pkg/store/user.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package store
33
import (
44
"fmt"
55

6+
"github.com/brevdev/brev-cli/pkg/auth"
67
"github.com/brevdev/brev-cli/pkg/entity"
78
breverrors "github.com/brevdev/brev-cli/pkg/errors"
89
)
@@ -22,6 +23,22 @@ func (s AuthHTTPStore) GetCurrentUser() (*entity.User, error) {
2223
return nil, NewHTTPResponseError(res)
2324
}
2425

26+
// Check if user has multiple identities and is using Auth0
27+
if len(result.ExternalIdentities) > 1 {
28+
// Get the current token to check if it's from Auth0
29+
currentToken, err := s.authHTTPClient.auth.GetAccessToken()
30+
if err != nil {
31+
return nil, breverrors.WrapAndTrace(err)
32+
}
33+
34+
// Check if the current token is from Auth0
35+
isAuth0Token := auth.IssuerCheck(currentToken, "https://brevdev.us.auth0.com/")
36+
if isAuth0Token {
37+
// User has multiple identities and is using Auth0, suggest NVIDIA login
38+
return nil, breverrors.NewNvidiaMigrationError("This account has an NVIDIA login available")
39+
}
40+
}
41+
2542
breverrors.GetDefaultErrorReporter().SetUser(breverrors.ErrorUser{
2643
ID: result.ID,
2744
Username: result.Username,

0 commit comments

Comments
 (0)