()
| 26 | } |
| 27 | |
| 28 | func newDebugAuthCmd() *cobra.Command { |
| 29 | var jsonOutput bool |
| 30 | |
| 31 | cmd := &cobra.Command{ |
| 32 | Use: "auth", |
| 33 | Short: "Print Docker Desktop authentication information", |
| 34 | Args: cobra.NoArgs, |
| 35 | RunE: func(cmd *cobra.Command, _ []string) (commandErr error) { |
| 36 | ctx := cmd.Context() |
| 37 | telemetry.TrackCommand(ctx, "debug", []string{"auth"}) |
| 38 | defer func() { // do not inline this defer so that commandErr is not resolved early |
| 39 | telemetry.TrackCommandError(ctx, "debug", []string{"auth"}, commandErr) |
| 40 | }() |
| 41 | |
| 42 | w := cmd.OutOrStdout() |
| 43 | |
| 44 | token := desktop.GetToken(ctx) |
| 45 | if token == "" { |
| 46 | if jsonOutput { |
| 47 | return json.NewEncoder(w).Encode(map[string]string{ |
| 48 | "error": "no token found (is Docker Desktop running and are you logged in?)", |
| 49 | }) |
| 50 | } |
| 51 | fmt.Fprintln(w, "No token found. Is Docker Desktop running and are you logged in?") |
| 52 | return nil |
| 53 | } |
| 54 | |
| 55 | info, err := parseAuthInfo(token) |
| 56 | if err != nil { |
| 57 | return fmt.Errorf("failed to parse JWT: %w", err) |
| 58 | } |
| 59 | |
| 60 | userInfo := desktop.GetUserInfo(ctx) |
| 61 | info.Username = userInfo.Username |
| 62 | info.Email = userInfo.Email |
| 63 | |
| 64 | if jsonOutput { |
| 65 | enc := json.NewEncoder(w) |
| 66 | enc.SetIndent("", " ") |
| 67 | return enc.Encode(info) |
| 68 | } |
| 69 | |
| 70 | printAuthInfoText(w, info) |
| 71 | return nil |
| 72 | }, |
| 73 | } |
| 74 | |
| 75 | cmd.Flags().BoolVar(&jsonOutput, "json", false, "Output in JSON format") |
| 76 | |
| 77 | return cmd |
| 78 | } |
| 79 | |
| 80 | func parseAuthInfo(token string) (*authInfo, error) { |
| 81 | parsed, _, err := jwt.NewParser().ParseUnverified(token, jwt.MapClaims{}) |
no test coverage detected