|
9 | 9 |
|
10 | 10 | "github.com/spf13/cobra" |
11 | 11 | "go.datum.net/datumctl/internal/authutil" |
12 | | - "go.datum.net/datumctl/internal/keyring" |
13 | | - "golang.org/x/oauth2" |
14 | 12 | metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
15 | 13 | clientauthv1 "k8s.io/client-go/pkg/apis/clientauthentication/v1" |
16 | 14 | ) |
@@ -49,63 +47,19 @@ func runGetToken(cmd *cobra.Command, args []string) error { |
49 | 47 | return fmt.Errorf("invalid --output format %q. Must be %s or %s", outputFormat, outputFormatToken, outputFormatK8sV1Creds) |
50 | 48 | } |
51 | 49 |
|
52 | | - // Get Active User Credential |
53 | | - activeUserKey, err := keyring.Get(authutil.ServiceName, authutil.ActiveUserKey) |
| 50 | + // Get the token source (which handles refresh and persistence automatically) |
| 51 | + tokenSource, err := authutil.GetTokenSource(ctx) |
54 | 52 | if err != nil { |
55 | | - if errors.Is(err, keyring.ErrNotFound) { |
| 53 | + if errors.Is(err, authutil.ErrNoActiveUser) { |
56 | 54 | return errors.New("no active user found in keyring. Please login first using 'datumctl auth login'") |
57 | 55 | } |
58 | | - return fmt.Errorf("failed to get active user key from keyring: %w", err) |
| 56 | + return fmt.Errorf("failed to get token source: %w", err) |
59 | 57 | } |
60 | 58 |
|
61 | | - credsJSON, err := keyring.Get(authutil.ServiceName, activeUserKey) |
62 | | - if err != nil { |
63 | | - return fmt.Errorf("failed to get credentials for active user '%s' from keyring", activeUserKey) |
64 | | - } |
65 | | - |
66 | | - var foundCreds authutil.StoredCredentials |
67 | | - if err := json.Unmarshal([]byte(credsJSON), &foundCreds); err != nil { |
68 | | - return fmt.Errorf("failed to parse stored credential JSON for active user '%s'", activeUserKey) |
69 | | - } |
70 | | - foundUserKey := activeUserKey |
71 | | - |
72 | | - // Check if Token pointer is nil |
73 | | - if foundCreds.Token == nil { |
74 | | - return fmt.Errorf("internal error: stored token for active user '%s' is nil", foundUserKey) |
75 | | - } |
76 | | - |
77 | | - // Create oauth2.Config |
78 | | - conf := &oauth2.Config{ |
79 | | - ClientID: foundCreds.ClientID, |
80 | | - Scopes: foundCreds.Scopes, |
81 | | - Endpoint: oauth2.Endpoint{ |
82 | | - AuthURL: foundCreds.EndpointAuthURL, |
83 | | - TokenURL: foundCreds.EndpointTokenURL, |
84 | | - }, |
85 | | - } |
86 | | - |
87 | | - // Create TokenSource |
88 | | - currentToken := *foundCreds.Token |
89 | | - tokenSource := conf.TokenSource(ctx, ¤tToken) |
90 | | - |
91 | | - // Get fresh token |
| 59 | + // Get fresh token (will refresh if needed and persist automatically) |
92 | 60 | newToken, err := tokenSource.Token() |
93 | 61 | if err != nil { |
94 | | - return fmt.Errorf("failed to refresh token for active user '%s': %w", foundUserKey, err) |
95 | | - } |
96 | | - |
97 | | - // Update keyring if refreshed |
98 | | - if newToken.AccessToken != currentToken.AccessToken { |
99 | | - updatedCreds := foundCreds |
100 | | - updatedCreds.Token = newToken |
101 | | - credsJSONBytes, err := json.Marshal(updatedCreds) |
102 | | - if err == nil { |
103 | | - err = keyring.Set(authutil.ServiceName, foundUserKey, string(credsJSONBytes)) |
104 | | - if err != nil { |
105 | | - // Print a warning instead of silently ignoring. |
106 | | - fmt.Fprintf(os.Stderr, "Warning: failed to update refreshed token in keyring for user '%s': %v\n", foundUserKey, err) |
107 | | - } |
108 | | - } // If marshalling failed, we can't save anyway, maybe log this too? (Optional) |
| 62 | + return fmt.Errorf("failed to get token: %w", err) |
109 | 63 | } |
110 | 64 |
|
111 | 65 | // --- Output based on requested format --- |
|
0 commit comments