()
| 26 | } |
| 27 | |
| 28 | func externalAuthAccessToken() *serpent.Command { |
| 29 | var extra string |
| 30 | agentAuth := &AgentAuth{} |
| 31 | cmd := &serpent.Command{ |
| 32 | Use: "access-token <provider>", |
| 33 | Short: "Print auth for an external provider", |
| 34 | Long: "Print an access-token for an external auth provider. " + |
| 35 | "The access-token will be validated and sent to stdout with exit code 0. " + |
| 36 | "If a valid access-token cannot be obtained, the URL to authenticate will be sent to stdout with exit code 1\n" + FormatExamples( |
| 37 | Example{ |
| 38 | Description: "Ensure that the user is authenticated with GitHub before cloning.", |
| 39 | Command: `#!/usr/bin/env sh |
| 40 | |
| 41 | OUTPUT=$(coder external-auth access-token github) |
| 42 | if [ $? -eq 0 ]; then |
| 43 | echo "Authenticated with GitHub" |
| 44 | else |
| 45 | echo "Please authenticate with GitHub:" |
| 46 | echo $OUTPUT |
| 47 | fi |
| 48 | `, |
| 49 | }, |
| 50 | Example{ |
| 51 | Description: "Obtain an extra property of an access token for additional metadata.", |
| 52 | Command: "coder external-auth access-token slack --extra \"authed_user.id\"", |
| 53 | }, |
| 54 | ), |
| 55 | Middleware: serpent.Chain( |
| 56 | serpent.RequireNArgs(1), |
| 57 | ), |
| 58 | Options: serpent.OptionSet{{ |
| 59 | Name: "Extra", |
| 60 | Flag: "extra", |
| 61 | Description: "Extract a field from the \"extra\" properties of the OAuth token.", |
| 62 | Value: serpent.StringOf(&extra), |
| 63 | }}, |
| 64 | |
| 65 | Handler: func(inv *serpent.Invocation) error { |
| 66 | ctx := inv.Context() |
| 67 | |
| 68 | ctx, stop := inv.SignalNotifyContext(ctx, StopSignals...) |
| 69 | defer stop() |
| 70 | |
| 71 | client, err := agentAuth.CreateClient() |
| 72 | if err != nil { |
| 73 | return xerrors.Errorf("create agent client: %w", err) |
| 74 | } |
| 75 | |
| 76 | extAuth, err := client.ExternalAuth(ctx, agentsdk.ExternalAuthRequest{ |
| 77 | ID: inv.Args[0], |
| 78 | }) |
| 79 | if err != nil { |
| 80 | return xerrors.Errorf("get external auth token: %w", err) |
| 81 | } |
| 82 | if extAuth.URL != "" { |
| 83 | _, err = inv.Stdout.Write([]byte(extAuth.URL)) |
| 84 | if err != nil { |
| 85 | return err |
no test coverage detected