author | Mikael Berthe <mikael@lilotux.net> |
Sat, 04 Feb 2023 13:35:58 +0100 | |
changeset 271 | c8b8b7cc8896 |
parent 265 | 05c40b36d3b2 |
permissions | -rw-r--r-- |
265
05c40b36d3b2
Bump to version 3.0.0-dev, using madon v3
Mikael Berthe <mikael@lilotux.net>
parents:
245
diff
changeset
|
1 |
// Copyright © 2017-2023 Mikael Berthe <mikael@lilotux.net> |
113 | 2 |
// |
3 |
// Licensed under the MIT license. |
|
4 |
// Please see the LICENSE file is this directory. |
|
5 |
||
6 |
package cmd |
|
7 |
||
8 |
import ( |
|
9 |
"fmt" |
|
10 |
"os" |
|
11 |
||
12 |
"github.com/pkg/errors" |
|
13 |
"github.com/spf13/cobra" |
|
14 |
//"github.com/McKael/madonctl/printer" |
|
15 |
) |
|
16 |
||
17 |
var oauth2Cmd = &cobra.Command{ |
|
18 |
Use: "oauth2", |
|
19 |
Short: "OAuth2 authentication/authorization", |
|
127
2b4d0f198a94
Update online documention to config and oauth2 commands
Mikael Berthe <mikael@lilotux.net>
parents:
113
diff
changeset
|
20 |
Example: ` madonctl oauth2 # Interactive OAuth2 login |
2b4d0f198a94
Update online documention to config and oauth2 commands
Mikael Berthe <mikael@lilotux.net>
parents:
113
diff
changeset
|
21 |
madonctl oauth2 get-url # Display OAuth2 auhtorization URL |
2b4d0f198a94
Update online documention to config and oauth2 commands
Mikael Berthe <mikael@lilotux.net>
parents:
113
diff
changeset
|
22 |
madonctl oauth2 code CODE # Enter OAuth2 code |
2b4d0f198a94
Update online documention to config and oauth2 commands
Mikael Berthe <mikael@lilotux.net>
parents:
113
diff
changeset
|
23 |
|
2b4d0f198a94
Update online documention to config and oauth2 commands
Mikael Berthe <mikael@lilotux.net>
parents:
113
diff
changeset
|
24 |
madonctl oauth2 > config.yaml # Redirect to configuration file`, |
113 | 25 |
RunE: func(cmd *cobra.Command, args []string) error { |
26 |
return oAuth2Interactive(args) |
|
27 |
}, |
|
28 |
PersistentPreRunE: func(cmd *cobra.Command, args []string) error { |
|
29 |
// Initialize application; do not log in yet |
|
30 |
return madonInit(false) |
|
31 |
}, |
|
32 |
} |
|
33 |
||
34 |
func init() { |
|
35 |
RootCmd.AddCommand(oauth2Cmd) |
|
36 |
||
37 |
// Subcommands |
|
38 |
oauth2Cmd.AddCommand(oauth2Subcommands...) |
|
39 |
} |
|
40 |
||
41 |
var oauth2Subcommands = []*cobra.Command{ |
|
42 |
&cobra.Command{ |
|
43 |
Use: "get-url", |
|
44 |
Short: "Get OAuth2 URL", |
|
45 |
RunE: func(cmd *cobra.Command, args []string) error { |
|
46 |
return oAuth2GetURL() |
|
47 |
}, |
|
48 |
}, |
|
49 |
&cobra.Command{ |
|
243
1bec7d3d6a85
Improve oauth2 code usage message
Mikael Berthe <mikael@lilotux.net>
parents:
185
diff
changeset
|
50 |
Use: "code CODE", |
113 | 51 |
Short: "Log in with OAuth2 code", |
52 |
RunE: func(cmd *cobra.Command, args []string) error { |
|
53 |
return oAuth2ExchangeCode(args) |
|
54 |
}, |
|
55 |
}, |
|
56 |
} |
|
57 |
||
58 |
func oAuth2GetURL() error { |
|
59 |
// (gClient != nil thanks to PreRun) |
|
60 |
||
61 |
url, err := gClient.LoginOAuth2("", scopes) |
|
62 |
if err != nil { |
|
63 |
return errors.Wrap(err, "OAuth2 authentication failed") |
|
64 |
} |
|
65 |
||
66 |
fmt.Printf("%s\n", url) |
|
67 |
return nil |
|
68 |
} |
|
69 |
||
70 |
func oAuth2ExchangeCode(args []string) error { |
|
71 |
// (gClient != nil thanks to PreRun) |
|
72 |
||
73 |
if len(args) != 1 { |
|
74 |
return errors.New("wrong usage: code needs 1 argument") |
|
75 |
} |
|
76 |
||
77 |
code := args[0] |
|
78 |
||
79 |
if code == "" { |
|
80 |
return errors.New("no code entered") |
|
81 |
} |
|
82 |
||
83 |
// The code has been set; proceed with token exchange |
|
84 |
_, err := gClient.LoginOAuth2(code, scopes) |
|
85 |
if err != nil { |
|
86 |
return err |
|
87 |
} |
|
88 |
||
89 |
if gClient.UserToken != nil { |
|
90 |
errPrint("Login successful.\n") |
|
245
910f00ab2799
Fix oauth2 not displaying the new token
Mikael Berthe <mikael@lilotux.net>
parents:
244
diff
changeset
|
91 |
errPrint("The new token is %s.\n", gClient.UserToken.AccessToken) |
244
a01bc98ae01a
Display config details after oauth2
Mikael Berthe <mikael@lilotux.net>
parents:
243
diff
changeset
|
92 |
configDump(true) |
113 | 93 |
} |
94 |
return nil |
|
95 |
} |
|
96 |
||
97 |
// oAuth2Interactive is the default behaviour |
|
98 |
func oAuth2Interactive(args []string) error { |
|
99 |
// (gClient != nil thanks to PreRun) |
|
100 |
||
101 |
url, err := gClient.LoginOAuth2("", scopes) |
|
102 |
if err != nil { |
|
103 |
return errors.Wrap(err, "OAuth2 authentication failed") |
|
104 |
} |
|
105 |
||
106 |
fmt.Fprintf(os.Stderr, "Visit the URL for the auth dialog:\n%s\n", url) |
|
107 |
fmt.Fprintf(os.Stderr, "Enter code: ") |
|
108 |
var code string |
|
109 |
if _, err := fmt.Scan(&code); err != nil { |
|
110 |
return err |
|
111 |
} |
|
112 |
||
113 |
if code == "" { |
|
114 |
return errors.New("no code entered") |
|
115 |
} |
|
116 |
||
117 |
// The code has been set; proceed with token exchange |
|
118 |
return oAuth2ExchangeCode([]string{code}) |
|
119 |
} |